-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfmtre.c
223 lines (218 loc) · 4.3 KB
/
fmtre.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* Copyright (c) 2020-2023 Contributors to ksh 93u+m *
* and is licensed under the *
* Eclipse Public License, Version 2.0 *
* *
* A copy of the License is available at *
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html *
* (with md5 checksum 84283fa8859daf213bdda5a9f8d1be1d) *
* *
* Glenn Fowler <[email protected]> *
* David Korn <[email protected]> *
* Phong Vo <[email protected]> *
* Martijn Dekker <[email protected]> *
* Johnothan King <[email protected]> *
* *
***********************************************************************/
/*
* Glenn Fowler
* AT&T Research
*
* return RE expression given strmatch() pattern
* 0 returned for invalid RE
*/
#include <ast.h>
typedef struct Stack_s
{
char* beg;
short len;
short min;
} Stack_t;
char*
fmtre(const char* as)
{
char* s = (char*)as;
int c;
char* t;
Stack_t* p;
char* x;
int n;
int end;
char* buf;
Stack_t stack[32];
end = 1;
c = 2 * strlen(s) + 1;
t = buf = fmtbuf(c);
p = stack;
if (*s != '*' || *(s + 1) == '(' || *(s + 1) == '-' && *(s + 2) == '(')
*t++ = '^';
else
s++;
for (;;)
{
switch (c = *s++)
{
case 0:
break;
case '\\':
if (!(c = *s++) || c == '{' || c == '}')
return NULL;
*t++ = '\\';
if ((*t++ = c) == '(' && *s == '|')
{
*t++ = *s++;
goto logical;
}
continue;
case '[':
*t++ = c;
n = 0;
if ((c = *s++) == '!')
{
*t++ = '^';
c = *s++;
}
else if (c == '^')
{
if ((c = *s++) == ']')
{
*(t - 1) = '\\';
*t++ = '^';
continue;
}
n = '^';
}
for (;;)
{
if (!(*t++ = c))
return NULL;
if ((c = *s++) == ']')
{
if (n)
*t++ = n;
*t++ = c;
break;
}
}
continue;
case '{':
for (x = s; *x && *x != '}'; x++);
if (*x++ && (*x == '(' || *x == '-' && *(x + 1) == '('))
{
if (p >= &stack[elementsof(stack)])
return NULL;
p->beg = s - 1;
s = x;
p->len = s - p->beg;
if (p->min = *s == '-')
s++;
p++;
*t++ = *s++;
}
else
*t++ = c;
continue;
case '*':
if (!*s)
{
end = 0;
break;
}
/* FALLTHROUGH */
case '?':
case '+':
case '@':
case '!':
case '~':
if (*s == '(' || c != '~' && *s == '-' && *(s + 1) == '(')
{
if (p >= &stack[elementsof(stack)])
return NULL;
p->beg = s - 1;
if (c == '~')
{
if (*(s + 1) == 'E' && *(s + 2) == ')')
{
for (s += 3; *t = *s; t++, s++);
continue;
}
p->len = 0;
p->min = 0;
*t++ = *s++;
*t++ = '?';
}
else
{
p->len = c != '@';
if (p->min = *s == '-')
s++;
*t++ = *s++;
}
p++;
}
else
{
switch (c)
{
case '*':
*t++ = '.';
break;
case '?':
c = '.';
break;
case '+':
case '!':
*t++ = '\\';
break;
}
*t++ = c;
}
continue;
case '(':
if (p >= &stack[elementsof(stack)])
return NULL;
p->beg = s - 1;
p->len = 0;
p->min = 0;
p++;
*t++ = c;
continue;
case ')':
if (p == stack)
return NULL;
*t++ = c;
p--;
for (c = 0; c < p->len; c++)
*t++ = p->beg[c];
if (p->min)
*t++ = '?';
continue;
case '^':
case '.':
case '$':
*t++ = '\\';
*t++ = c;
continue;
case '|':
if (t == buf || *(t - 1) == '(')
return NULL;
logical:
if (!*s || *s == ')')
return NULL;
/* FALLTHROUGH */
default:
*t++ = c;
continue;
}
break;
}
if (p != stack)
return NULL;
if (end)
*t++ = '$';
*t = 0;
return buf;
}