1 /***********************************************************************
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 *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 * regcmp implementation
40 unsigned char sub
[SUB
];
43 char buf
[ALIGN_BOUND2
];
46 __DEFINE__(char*, __loc1
, 0);
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
))
55 data
= (void*)re
->cur
;
61 regcmp(const char* pattern
, ...)
73 unsigned char paren
[128];
74 unsigned char sub
[SUB
];
77 va_start(ap
, pattern
);
78 if (!pattern
|| !*pattern
|| !(sp
= sfstropen()))
80 memset(paren
, 0, sizeof(paren
));
118 * someone explain in one sentence why
119 * a cast is needed to make this work
122 if (p
< (int)(elementsof(paren
) - 1))
126 else if (c
== ')' && p
>= 0)
128 for (i
= p
; i
> 0; i
--)
131 if (*s
== '$' && (j
= *(s
+ 1)) >= '0' && j
<= '9')
138 memset(sub
, 0, sizeof(sub
));
141 sub
[j
] = paren
[i
] + 1;
147 } while (s
= va_arg(ap
, char*));
149 if (!(s
= sfstruse(sp
)))
158 if ((n
+= INC
) > TOT
|| !(re
= newof(re
, Regex_t
, 0, n
)))
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
);
177 memcpy(re
->sub
, sub
, (nsub
+ 1) * sizeof(sub
[0]));
182 regex(const char* handle
, const char* subject
, ...)
184 register Regex_t
* re
;
189 regmatch_t match
[SUB
+ 1];
192 va_start(ap
, subject
);
193 if (!(re
= (Regex_t
*)handle
) || !subject
)
195 for (n
= 0; n
< re
->nsub
; n
++)
196 sub
[n
] = va_arg(ap
, char*);
198 if (regexec(&re
->re
, subject
, SUB
+ 1, match
, 0))
200 for (n
= 0; n
< re
->nsub
; n
++)
204 k
= match
[i
].rm_eo
- match
[i
].rm_so
;
205 strncpy(sub
[n
], subject
+ match
[i
].rm_so
, k
);
208 __loc1
= (char*)subject
+ match
[0].rm_so
;
209 return (char*)subject
+ match
[0].rm_eo
;