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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name Lucent Technologies or any of
its entities not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission.
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
#define DEBUG
#include <stdio.h>
#include <u.h>
#include <libc.h>
#include <ctype.h>
#include <bio.h>
#include <regexp.h>
#include "awk.h"
#include "y.tab.h"
/* This file provides the interface between the main body of
* awk and the pattern matching package. It preprocesses
* patterns prior to compilation to provide awk-like semantics
* to character sequences not supported by the pattern package.
* The following conversions are performed:
*
* "()" -> "[]"
* "[-" -> "[\-"
* "[^-" -> "[^\-"
* "-]" -> "\-]"
* "[]" -> "[]*"
* "\xdddd" -> "\z" where 'z' is the UTF sequence
* for the hex value
* "\ddd" -> "\o" where 'o' is a char octal value
* "\b" -> "\B" where 'B' is backspace
* "\t" -> "\T" where 'T' is tab
* "\f" -> "\F" where 'F' is form feed
* "\n" -> "\N" where 'N' is newline
* "\r" -> "\r" where 'C' is cr
*/
#define MAXRE 512
static char re[MAXRE]; /* copy buffer */
char *patbeg;
int patlen; /* number of chars in pattern */
#define NPATS 20 /* number of slots in pattern cache */
static struct pat_list /* dynamic pattern cache */
{
char *re;
int use;
Reprog *program;
} pattern[NPATS];
static int npats; /* cache fill level */
/* Compile a pattern */
void
*compre(char *pat)
{
int i, j, inclass;
char c, *p, *s;
Reprog *program;
if (!compile_time) { /* search cache for dynamic pattern */
for (i = 0; i < npats; i++)
if (!strcmp(pat, pattern[i].re)) {
pattern[i].use++;
return((void *) pattern[i].program);
}
}
/* Preprocess Pattern for compilation */
p = re;
s = pat;
inclass = 0;
while (c = *s++) {
if (c == '\\') {
quoted(&s, &p, re+MAXRE);
continue;
}
else if (!inclass && c == '(' && *s == ')') {
if (p < re+MAXRE-2) { /* '()' -> '[]*' */
*p++ = '[';
*p++ = ']';
c = '*';
s++;
}
else overflow();
}
else if (c == '['){ /* '[-' -> '[\-' */
inclass = 1;
if (*s == '-') {
if (p < re+MAXRE-2) {
*p++ = '[';
*p++ = '\\';
c = *s++;
}
else overflow();
} /* '[^-' -> '[^\-'*/
else if (*s == '^' && s[1] == '-'){
if (p < re+MAXRE-3) {
*p++ = '[';
*p++ = *s++;
*p++ = '\\';
c = *s++;
}
else overflow();
}
else if (*s == '['){ /* skip '[[' */
if (p < re+MAXRE-1)
*p++ = c;
else overflow();
c = *s++;
}
else if (*s == '^' && s[1] == '[') { /* skip '[^['*/
if (p < re+MAXRE-2) {
*p++ = c;
*p++ = *s++;
c = *s++;
}
else overflow();
}
else if (*s == ']') { /* '[]' -> '[]*' */
if (p < re+MAXRE-2) {
*p++ = c;
*p++ = *s++;
c = '*';
inclass = 0;
}
else overflow();
}
}
else if (c == '-' && *s == ']') { /* '-]' -> '\-]' */
if (p < re+MAXRE-1)
*p++ = '\\';
else overflow();
}
else if (c == ']')
inclass = 0;
if (p < re+MAXRE-1)
*p++ = c;
else overflow();
}
*p = 0;
program = regcomp(re); /* compile pattern */
if (!compile_time) {
if (npats < NPATS) /* Room in cache */
i = npats++;
else { /* Throw out least used */
int use = pattern[0].use;
i = 0;
for (j = 1; j < NPATS; j++) {
if (pattern[j].use < use) {
use = pattern[j].use;
i = j;
}
}
xfree(pattern[i].program);
xfree(pattern[i].re);
}
pattern[i].re = tostring(pat);
pattern[i].program = program;
pattern[i].use = 1;
}
return((void *) program);
}
/* T/F match indication - matched string not exported */
int
match(void *p, char *s, char *start)
{
return regexec((Reprog *) p, (char *) s, 0, 0);
}
/* match and delimit the matched string */
int
pmatch(void *p, char *s, char *start)
{
Resub m;
m.s.sp = start;
m.e.ep = 0;
if (regexec((Reprog *) p, (char *) s, &m, 1)) {
patbeg = m.s.sp;
patlen = m.e.ep-m.s.sp;
return 1;
}
patlen = -1;
patbeg = start;
return 0;
}
/* perform a non-empty match */
int
nematch(void *p, char *s, char *start)
{
if (pmatch(p, s, start) == 1 && patlen > 0)
return 1;
patlen = -1;
patbeg = start;
return 0;
}
/* in the parsing of regular expressions, metacharacters like . have */
/* to be seen literally; \056 is not a metacharacter. */
int
hexstr(char **pp) /* find and eval hex string at pp, return new p */
{
char c;
int n = 0;
int i;
for (i = 0, c = (*pp)[i]; i < 4 && isxdigit(c); i++, c = (*pp)[i]) {
if (isdigit(c))
n = 16 * n + c - '0';
else if ('a' <= c && c <= 'f')
n = 16 * n + c - 'a' + 10;
else if ('A' <= c && c <= 'F')
n = 16 * n + c - 'A' + 10;
}
*pp += i;
return n;
}
/* look for awk-specific escape sequences */
#define isoctdigit(c) ((c) >= '0' && (c) <= '7') /* multiple use of arg */
void
quoted(char **s, char **to, char *end) /* handle escaped sequence */
{
char *p = *s;
char *t = *to;
wchar_t c;
switch(c = *p++) {
case 't':
c = '\t';
break;
case 'n':
c = '\n';
break;
case 'f':
c = '\f';
break;
case 'r':
c = '\r';
break;
case 'b':
c = '\b';
break;
default:
if (t < end-1) /* all else must be escaped */
*t++ = '\\';
if (c == 'x') { /* hexadecimal goo follows */
c = hexstr(&p);
if (t < end-MB_CUR_MAX)
t += wctomb(t, c);
else overflow();
*to = t;
*s = p;
return;
} else if (isoctdigit(c)) { /* \d \dd \ddd */
c -= '0';
if (isoctdigit(*p)) {
c = 8 * c + *p++ - '0';
if (isoctdigit(*p))
c = 8 * c + *p++ - '0';
}
}
break;
}
if (t < end-1)
*t++ = c;
*s = p;
*to = t;
}
/* count rune positions */
int
countposn(char *s, int n)
{
int i, j;
char *end;
for (i = 0, end = s+n; *s && s < end; i++){
j = mblen(s, n);
if(j <= 0)
j = 1;
s += j;
}
return(i);
}
/* pattern package error handler */
void
regerror(char *s)
{
FATAL("%s", s);
}
void
overflow(void)
{
FATAL("%s", "regular expression too big");
}
|