7 #define NGRPS 64 /* maximum number of groups */
8 #define NREPS 128 /* maximum repetitions */
9 #define NDEPT 256 /* re_rec() recursion depth limit */
11 #define MAX(a, b) ((a) < (b) ? (b) : (a))
12 #define LEN(a) (sizeof(a) / sizeof((a)[0]))
14 /* regular expressions atoms */
15 #define RA_CHR '\0' /* character literal */
16 #define RA_BEG '^' /* string start */
17 #define RA_END '$' /* string end */
18 #define RA_ANY '.' /* any character */
19 #define RA_BRK '[' /* bracket expression */
20 #define RA_WBEG '<' /* word start */
21 #define RA_WEND '>' /* word end */
23 /* regular expression node types */
24 #define RN_ATOM '\0' /* regular expression */
25 #define RN_CAT 'c' /* concatenation */
26 #define RN_ALT '|' /* alternate expressions */
27 #define RN_GRP '(' /* pattern group */
29 /* regular expression program instructions */
30 #define RI_ATOM '\0' /* regular expression */
31 #define RI_FORK 'f' /* fork the execution */
32 #define RI_JUMP 'j' /* jump to the given instruction */
33 #define RI_MARK 'm' /* mark the current position */
34 #define RI_MATCH 'q' /* the pattern is matched */
36 /* regular expression atom */
38 int ra
; /* atom type (RA_*) */
39 char *s
; /* atom argument */
42 /* regular expression instruction */
44 int ri
; /* instruction type (RI_*) */
45 struct ratom ra
; /* regular expression atom (RI_ATOM) */
46 int a1
, a2
; /* destination of RI_FORK and RI_JUMP */
47 int mark
; /* mark (RI_MARK) */
50 /* regular expression program */
52 struct rinst
*p
; /* the program */
53 int n
; /* number of instructions */
54 int grpcnt
; /* number of groups */
55 int flg
; /* regcomp() flags */
58 /* regular expression matching state */
60 int mark
[NGRPS
* 2]; /* marks for RI_MARK */
61 int pc
; /* program counter */
62 char *s
; /* the current position in the string */
63 char *o
; /* the beginning of the string */
64 int flg
; /* flags passed to regcomp() and regexec() */
65 int dep
; /* re_rec() depth */
68 /* regular expression tree; used for parsing */
70 int rn
; /* node type (RN_*) */
71 struct ratom ra
; /* regular expression atom (RN_ATOM) */
72 int mincnt
, maxcnt
; /* number of repetitions */
73 struct rnode
*c1
, *c2
; /* children */
76 static struct rnode
*rnode_make(int rn
, struct rnode
*c1
, struct rnode
*c2
)
78 struct rnode
*rnode
= malloc(sizeof(*rnode
));
79 memset(rnode
, 0, sizeof(*rnode
));
88 static void rnode_free(struct rnode
*rnode
)
91 rnode_free(rnode
->c1
);
93 rnode_free(rnode
->c2
);
98 static int uc_len(char *s
)
100 int c
= (unsigned char) s
[0];
101 if (~c
& 0x80) /* ASCII */
103 if (~c
& 0x40) /* invalid UTF-8 */
114 static int uc_dec(char *s
)
116 int c
= (unsigned char) s
[0];
120 return ((c
& 0x1f) << 6) | (s
[1] & 0x3f);
122 return ((c
& 0x0f) << 12) | ((s
[1] & 0x3f) << 6) | (s
[2] & 0x3f);
124 return ((c
& 0x07) << 18) | ((s
[1] & 0x3f) << 12) | ((s
[2] & 0x3f) << 6) | (s
[3] & 0x3f);
128 static void ratom_copy(struct ratom
*dst
, struct ratom
*src
)
133 int len
= strlen(src
->s
);
134 dst
->s
= malloc(len
+ 1);
135 memcpy(dst
->s
, src
->s
, len
+ 1);
139 static int brk_len(char *s
)
142 if (s
[n
] == '^') /* exclusion mark */
144 if (s
[n
] == ']') /* handling []a] */
146 while (s
[n
] && s
[n
] != ']') {
147 if (s
[n
] == '[' && (s
[n
+ 1] == ':' || s
[n
+ 1] == '='))
148 while (s
[n
] && s
[n
] != ']')
153 return s
[n
] == ']' ? n
+ 1 : n
;
156 static void ratom_readbrk(struct ratom
*ra
, char **pat
)
158 int len
= brk_len(*pat
);
160 ra
->s
= malloc(len
+ 1);
161 memcpy(ra
->s
, *pat
, len
);
166 static void ratom_read(struct ratom
*ra
, char **pat
)
169 switch ((unsigned char) **pat
) {
183 ratom_readbrk(ra
, pat
);
186 if ((*pat
)[1] == '<' || (*pat
)[1] == '>') {
187 ra
->ra
= (*pat
)[1] == '<' ? RA_WBEG
: RA_WEND
;
196 memcpy(ra
->s
, *pat
, len
);
202 static char *uc_beg(char *beg
, char *s
)
204 while (s
> beg
&& (((unsigned char) *s
) & 0xc0) == 0x80)
209 static int isword(char *s
)
211 int c
= (unsigned char) s
[0];
212 return isalnum(c
) || c
== '_' || c
> 127;
215 static char *brk_classes
[][2] = {
216 {":alnum:", "a-zA-Z0-9"},
217 {":alpha:", "a-zA-Z"},
221 {":print:", "\x20-\x7e"},
222 {":punct:", "][!\"#$%&'()*+,./:;<=>?@\\^_`{|}~-"},
223 {":space:", " \t\r\n\v\f"},
225 {":word:", "a-zA-Z0-9_"},
226 {":xdigit:", "a-fA-F0-9"},
229 static int brk_match(char *brk
, int c
, int flg
)
233 int not = brk
[0] == '^';
234 char *p
= not ? brk
+ 1 : brk
;
236 if (flg
& REG_ICASE
&& c
< 128 && isupper(c
))
238 while (*p
&& (p
== p0
|| *p
!= ']')) {
239 if (p
[0] == '[' && p
[1] == ':') {
240 for (i
= 0; i
< LEN(brk_classes
); i
++) {
241 char *cc
= brk_classes
[i
][0];
242 char *cp
= brk_classes
[i
][1];
243 if (!strncmp(cc
, p
+ 1, strlen(cc
)))
244 if (!brk_match(cp
, c
, flg
))
253 if (p
[0] == '-' && p
[1] && p
[1] != ']') {
258 if (flg
& REG_ICASE
&& beg
< 128 && isupper(beg
))
260 if (flg
& REG_ICASE
&& end
< 128 && isupper(end
))
262 if (c
>= beg
&& c
<= end
)
268 static int ratom_match(struct ratom
*ra
, struct rstate
*rs
)
270 if (ra
->ra
== RA_CHR
) {
271 int c1
= uc_dec(ra
->s
);
272 int c2
= uc_dec(rs
->s
);
273 if (rs
->flg
& REG_ICASE
&& c1
< 128 && isupper(c1
))
275 if (rs
->flg
& REG_ICASE
&& c2
< 128 && isupper(c2
))
279 rs
->s
+= uc_len(ra
->s
);
282 if (ra
->ra
== RA_ANY
) {
283 if (!rs
->s
[0] || (rs
->s
[0] == '\n' && !(rs
->flg
& REG_NOTEOL
)))
285 rs
->s
+= uc_len(rs
->s
);
288 if (ra
->ra
== RA_BRK
) {
289 int c
= uc_dec(rs
->s
);
290 if (!c
|| (c
== '\n' && !(rs
->flg
& REG_NOTEOL
)))
292 rs
->s
+= uc_len(rs
->s
);
293 return brk_match(ra
->s
+ 1, c
, rs
->flg
);
295 if (ra
->ra
== RA_BEG
&& !(rs
->flg
& REG_NOTBOL
))
296 return !(rs
->s
== rs
->o
|| rs
->s
[-1] == '\n');
297 if (ra
->ra
== RA_END
&& !(rs
->flg
& REG_NOTEOL
))
298 return rs
->s
[0] != '\0' && rs
->s
[0] != '\n';
299 if (ra
->ra
== RA_WBEG
)
300 return !((rs
->s
== rs
->o
|| !isword(uc_beg(rs
->o
, rs
->s
- 1))) &&
302 if (ra
->ra
== RA_WEND
)
303 return !(rs
->s
!= rs
->o
&& isword(uc_beg(rs
->o
, rs
->s
- 1)) &&
304 (!rs
->s
[0] || !isword(rs
->s
)));
308 static struct rnode
*rnode_parse(char **pat
);
310 static struct rnode
*rnode_grp(char **pat
)
313 if ((*pat
)[0] != '(')
316 rnode
= rnode_parse(pat
);
319 if ((*pat
)[0] != ')') {
324 return rnode_make(RN_GRP
, rnode
, NULL
);
327 static struct rnode
*rnode_atom(char **pat
)
332 if ((*pat
)[0] == '|' || (*pat
)[0] == ')')
334 if ((*pat
)[0] == '(') {
335 rnode
= rnode_grp(pat
);
337 rnode
= rnode_make(RN_ATOM
, NULL
, NULL
);
338 ratom_read(&rnode
->ra
, pat
);
342 if ((*pat
)[0] == '*' || (*pat
)[0] == '?') {
344 rnode
->maxcnt
= (*pat
)[0] == '*' ? -1 : 1;
347 if ((*pat
)[0] == '+') {
352 if ((*pat
)[0] == '{') {
356 while (isdigit((unsigned char) **pat
))
357 rnode
->mincnt
= rnode
->mincnt
* 10 + *(*pat
)++ - '0';
360 if ((*pat
)[0] == '}')
362 while (isdigit((unsigned char) **pat
))
363 rnode
->maxcnt
= rnode
->maxcnt
* 10 + *(*pat
)++ - '0';
365 rnode
->maxcnt
= rnode
->mincnt
;
368 if (rnode
->mincnt
> NREPS
|| rnode
->maxcnt
> NREPS
) {
376 static struct rnode
*rnode_seq(char **pat
)
378 struct rnode
*c1
= rnode_atom(pat
);
383 return c2
? rnode_make(RN_CAT
, c1
, c2
) : c1
;
386 static struct rnode
*rnode_parse(char **pat
)
388 struct rnode
*c1
= rnode_seq(pat
);
390 if ((*pat
)[0] != '|')
393 c2
= rnode_parse(pat
);
394 return c2
? rnode_make(RN_ALT
, c1
, c2
) : c1
;
397 static int rnode_count(struct rnode
*rnode
)
402 if (rnode
->rn
== RN_CAT
)
403 n
= rnode_count(rnode
->c1
) + rnode_count(rnode
->c2
);
404 if (rnode
->rn
== RN_ALT
)
405 n
= rnode_count(rnode
->c1
) + rnode_count(rnode
->c2
) + 2;
406 if (rnode
->rn
== RN_GRP
)
407 n
= rnode_count(rnode
->c1
) + 2;
408 if (rnode
->mincnt
== 0 && rnode
->maxcnt
== 0)
410 if (rnode
->mincnt
== 1 && rnode
->maxcnt
== 1)
412 if (rnode
->maxcnt
< 0) {
413 n
= (rnode
->mincnt
+ 1) * n
+ 1;
415 n
= (rnode
->mincnt
+ rnode
->maxcnt
) * n
+
416 rnode
->maxcnt
- rnode
->mincnt
;
423 static int re_insert(struct regex
*p
, int ri
)
425 p
->p
[p
->n
++].ri
= ri
;
429 static void rnode_emit(struct rnode
*n
, struct regex
*p
);
431 static void rnode_emitnorep(struct rnode
*n
, struct regex
*p
)
433 int fork
, done
, mark
;
434 if (n
->rn
== RN_ALT
) {
435 fork
= re_insert(p
, RI_FORK
);
436 p
->p
[fork
].a1
= p
->n
;
437 rnode_emit(n
->c1
, p
);
438 done
= re_insert(p
, RI_JUMP
);
439 p
->p
[fork
].a2
= p
->n
;
440 rnode_emit(n
->c2
, p
);
441 p
->p
[done
].a1
= p
->n
;
443 if (n
->rn
== RN_CAT
) {
444 rnode_emit(n
->c1
, p
);
445 rnode_emit(n
->c2
, p
);
447 if (n
->rn
== RN_GRP
) {
448 int grp
= p
->grpcnt
++ + 1;
449 mark
= re_insert(p
, RI_MARK
);
450 p
->p
[mark
].mark
= 2 * grp
;
451 rnode_emit(n
->c1
, p
);
452 mark
= re_insert(p
, RI_MARK
);
453 p
->p
[mark
].mark
= 2 * grp
+ 1;
455 if (n
->rn
== RN_ATOM
) {
456 int atom
= re_insert(p
, RI_ATOM
);
457 ratom_copy(&p
->p
[atom
].ra
, &n
->ra
);
461 static void rnode_emit(struct rnode
*n
, struct regex
*p
)
469 if (n
->mincnt
== 0 && n
->maxcnt
== 0)
471 if (n
->mincnt
== 1 && n
->maxcnt
== 1) {
472 rnode_emitnorep(n
, p
);
475 if (n
->mincnt
== 0) {
476 int fork
= re_insert(p
, RI_FORK
);
477 p
->p
[fork
].a1
= p
->n
;
478 jmpend
[jmpend_cnt
++] = fork
;
480 for (i
= 0; i
< MAX(1, n
->mincnt
); i
++) {
482 rnode_emitnorep(n
, p
);
486 fork
= re_insert(p
, RI_FORK
);
487 p
->p
[fork
].a1
= last
;
488 p
->p
[fork
].a2
= p
->n
;
490 for (i
= MAX(1, n
->mincnt
); i
< n
->maxcnt
; i
++) {
491 int fork
= re_insert(p
, RI_FORK
);
492 p
->p
[fork
].a1
= p
->n
;
493 jmpend
[jmpend_cnt
++] = fork
;
494 rnode_emitnorep(n
, p
);
496 for (i
= 0; i
< jmpend_cnt
; i
++)
497 p
->p
[jmpend
[i
]].a2
= p
->n
;
500 int regcomp(regex_t
*preg
, char *pat
, int flg
)
502 struct rnode
*rnode
= rnode_parse(&pat
);
504 int n
= rnode_count(rnode
) + 3;
508 re
= malloc(sizeof(*re
));
509 memset(re
, 0, sizeof(*re
));
510 re
->p
= malloc(n
* sizeof(re
->p
[0]));
511 memset(re
->p
, 0, n
* sizeof(re
->p
[0]));
512 mark
= re_insert(re
, RI_MARK
);
513 re
->p
[mark
].mark
= 0;
514 rnode_emit(rnode
, re
);
515 mark
= re_insert(re
, RI_MARK
);
516 re
->p
[mark
].mark
= 1;
517 mark
= re_insert(re
, RI_MATCH
);
524 void regfree(regex_t
*preg
)
526 struct regex
*re
= *preg
;
528 for (i
= 0; i
< re
->n
; i
++)
529 if (re
->p
[i
].ri
== RI_ATOM
)
535 static int re_rec(struct regex
*re
, struct rstate
*rs
)
537 struct rinst
*ri
= NULL
;
538 if (++(rs
->dep
) >= NDEPT
)
542 if (ri
->ri
== RI_ATOM
) {
543 if (ratom_match(&ri
->ra
, rs
))
548 if (ri
->ri
== RI_MARK
) {
549 if (ri
->mark
< NGRPS
)
550 rs
->mark
[ri
->mark
] = rs
->s
- rs
->o
;
554 if (ri
->ri
== RI_JUMP
) {
558 if (ri
->ri
== RI_FORK
) {
559 struct rstate base
= *rs
;
569 return ri
->ri
!= RI_MATCH
;
572 static int re_recmatch(struct regex
*re
, struct rstate
*rs
, int nsub
, regmatch_t
*psub
)
576 for (i
= 0; i
< LEN(rs
->mark
); i
++)
579 if (!re_rec(re
, rs
)) {
580 for (i
= 0; i
< nsub
; i
++) {
581 psub
[i
].rm_so
= i
* 2 < LEN(rs
->mark
) ? rs
->mark
[i
* 2] : -1;
582 psub
[i
].rm_eo
= i
* 2 < LEN(rs
->mark
) ? rs
->mark
[i
* 2 + 1] : -1;
589 int regexec(regex_t
*preg
, char *s
, int nsub
, regmatch_t psub
[], int flg
)
591 struct regex
*re
= *preg
;
593 memset(&rs
, 0, sizeof(rs
));
594 rs
.flg
= re
->flg
| flg
;
599 if (!re_recmatch(re
, &rs
, flg
& REG_NOSUB
? 0 : nsub
, psub
))
605 int regerror(int errcode
, regex_t
*preg
, char *errbuf
, int errbuf_size
)