sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libast / common / comp / regcmp.c
blobc048f5d86ea5a8dc53cdb2af15e4b5cb64e7ed25
1 /***********************************************************************
2 * *
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
8 * *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12 * *
13 * Information and Software Systems Research *
14 * AT&T Research *
15 * Florham Park NJ *
16 * *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
20 * *
21 ***********************************************************************/
22 #pragma prototyped
24 * regcmp implementation
27 #include <ast.h>
28 #include <libgen.h>
29 #include <regex.h>
30 #include <align.h>
32 #define INC (2*1024)
33 #define TOT (16*1024)
34 #define SUB 10
36 typedef struct
38 char* cur;
39 regex_t re;
40 unsigned char sub[SUB];
41 int nsub;
42 size_t size;
43 char buf[ALIGN_BOUND2];
44 } Regex_t;
46 __DEFINE__(char*, __loc1, 0);
48 static void*
49 block(void* handle, void* data, size_t size)
51 register Regex_t* re = (Regex_t*)handle;
53 if (data || (size = roundof(size, ALIGN_BOUND2)) > (re->buf + re->size - re->cur))
54 return 0;
55 data = (void*)re->cur;
56 re->cur += size;
57 return data;
60 char*
61 regcmp(const char* pattern, ...)
63 register char* s;
64 register Regex_t* re;
65 register size_t n;
66 register int c;
67 register int p;
68 int b;
69 int i;
70 int j;
71 int nsub;
72 register Sfio_t* sp;
73 unsigned char paren[128];
74 unsigned char sub[SUB];
75 va_list ap;
77 va_start(ap, pattern);
78 if (!pattern || !*pattern || !(sp = sfstropen()))
79 return 0;
80 memset(paren, 0, sizeof(paren));
81 n = 0;
82 p = -1;
83 b = 0;
84 nsub = 0;
85 s = (char*)pattern;
88 while (c = *s++)
90 if (c == '\\')
92 sfputc(sp, c);
93 if (!(c = *s++))
94 break;
96 else if (b)
98 if (c == ']')
99 b = 0;
101 else if (c == '[')
103 b = 1;
104 if (*s == '^')
106 sfputc(sp, c);
107 c = *s++;
109 if (*s == ']')
111 sfputc(sp, c);
112 c = *s++;
115 else if (c == '(')
118 * someone explain in one sentence why
119 * a cast is needed to make this work
122 if (p < (int)(elementsof(paren) - 1))
123 p++;
124 paren[p] = ++n;
126 else if (c == ')' && p >= 0)
128 for (i = p; i > 0; i--)
129 if (paren[i])
130 break;
131 if (*s == '$' && (j = *(s + 1)) >= '0' && j <= '9')
133 s += 2;
134 j -= '0';
135 if (nsub <= j)
137 if (!nsub)
138 memset(sub, 0, sizeof(sub));
139 nsub = j + 1;
141 sub[j] = paren[i] + 1;
143 paren[i] = 0;
145 sfputc(sp, c);
147 } while (s = va_arg(ap, char*));
148 va_end(ap);
149 if (!(s = sfstruse(sp)))
151 sfstrclose(sp);
152 return 0;
154 re = 0;
155 n = 0;
158 if ((n += INC) > TOT || !(re = newof(re, Regex_t, 0, n)))
160 free(re);
161 sfstrclose(sp);
162 return 0;
164 re->cur = re->buf;
165 re->size = n + ALIGN_BOUND2 - sizeof(Regex_t);
166 regalloc(re, block, REG_NOFREE);
167 c = regcomp(&re->re, s, REG_EXTENDED|REG_LENIENT|REG_NULL);
168 regalloc(NiL, NiL, 0);
169 } while (c == REG_ESPACE);
170 sfstrclose(sp);
171 if (c)
173 free(re);
174 return 0;
176 if (re->nsub = nsub)
177 memcpy(re->sub, sub, (nsub + 1) * sizeof(sub[0]));
178 return (char*)re;
181 char*
182 regex(const char* handle, const char* subject, ...)
184 register Regex_t* re;
185 register int n;
186 register int i;
187 register int k;
188 char* sub[SUB + 1];
189 regmatch_t match[SUB + 1];
190 va_list ap;
192 va_start(ap, subject);
193 if (!(re = (Regex_t*)handle) || !subject)
194 return 0;
195 for (n = 0; n < re->nsub; n++)
196 sub[n] = va_arg(ap, char*);
197 va_end(ap);
198 if (regexec(&re->re, subject, SUB + 1, match, 0))
199 return 0;
200 for (n = 0; n < re->nsub; n++)
201 if (i = re->sub[n])
203 i--;
204 k = match[i].rm_eo - match[i].rm_so;
205 strncpy(sub[n], subject + match[i].rm_so, k);
206 *(sub[n] + k) = 0;
208 __loc1 = (char*)subject + match[0].rm_so;
209 return (char*)subject + match[0].rm_eo;