2 * Regular expression matching for expr(1). Bugs: The upper bound of
3 * a range specified by the \{ feature cannot be zero.
5 * Copyright (C) 1989 by Kenneth Almquist. All rights reserved.
6 * This file is part of ash, which is distributed under the terms specified
7 * by the Ash General Public License. See the file named LICENSE.
15 #define RE_END 0 /* end of regular expression */
16 #define RE_LITERAL 1 /* normal character follows */
17 #define RE_DOT 2 /* "." */
18 #define RE_CCL 3 /* "[...]" */
19 #define RE_NCCL 4 /* "[^...]" */
20 #define RE_LP 5 /* "\(" */
21 #define RE_RP 6 /* "\)" */
22 #define RE_MATCHED 7 /* "\digit" */
23 #define RE_EOS 8 /* "$" matches end of string */
24 #define RE_STAR 9 /* "*" */
25 #define RE_RANGE 10 /* "\{num,num\}" */
29 char *match_begin
[10];
30 short match_length
[10];
32 static int match(char *pattern
, char *string
);
57 comp
= q
= malloc(2 * strlen(p
) + 1);
79 while (*p
!= ']' || first
== 1) {
80 if (p
[1] == '-' && p
[2] != ']') {
85 } else if (*p
== '-') {
109 i
= (type
== RE_RANGE
)? 3 : 1;
118 if (type
== RE_RANGE
) {
120 while ((unsigned)(*p
- '0') <= 9)
121 i
= 10 * i
+ (*p
++ - '0');
128 while ((unsigned)(*p
- '0') <= 9)
129 i
= 10 * i
+ (*p
++ - '0');
132 if (*p
!= '\\' || *++p
!= '}')
139 if ((c
= *p
++) == '(') {
144 *stackp
++ = paren_num
;
146 } else if (c
== ')') {
152 } else if (c
== '{') {
155 } else if ((unsigned)(c
- '1') < 9) {
156 /* should check validity here */
165 dft
: *q
++ = RE_LITERAL
;
174 number_parens
= paren_num
;
181 re_match(pattern
, string
)
187 match_begin
[0] = string
;
188 for (pp
= &match_begin
[1] ; pp
<= &match_begin
[9] ; pp
++)
190 return match(pattern
, string
);
196 match(pattern
, string
)
200 register char *p
, *q
;
202 int low
, high
, count
;
222 match_length
[0] = q
- match_begin
[0];
242 if (c
>= *++p
&& c
<= *++p
)
255 match_begin
[*p
++] = q
;
258 match_length
[*p
] = q
- match_begin
[*p
];
263 len
= match_length
[*p
++];
294 if (*curpat
== RE_MATCHED
)
295 len
= match_length
[curpat
[1]];
296 while (--count
>= low
) {
297 if (match(p
, start_count
+ count
* len
))