7 |
#include <string.h> |
#include <string.h> |
8 |
#include <stdlib.h> |
#include <stdlib.h> |
9 |
#include <time.h> |
#include <time.h> |
10 |
|
#include <locale.h> |
11 |
|
|
12 |
/* Use the internal info for displaying the results of pcre_study(). */ |
/* Use the internal info for displaying the results of pcre_study(). */ |
13 |
|
|
22 |
#endif |
#endif |
23 |
#endif |
#endif |
24 |
|
|
25 |
|
#define LOOPREPEAT 20000 |
26 |
|
|
27 |
|
|
28 |
static FILE *outfile; |
static FILE *outfile; |
29 |
static int log_store = 0; |
static int log_store = 0; |
35 |
|
|
36 |
static const char *OP_names[] = { |
static const char *OP_names[] = { |
37 |
"End", "\\A", "\\B", "\\b", "\\D", "\\d", |
"End", "\\A", "\\B", "\\b", "\\D", "\\d", |
38 |
"\\S", "\\s", "\\W", "\\w", "Cut", "\\Z", "^", "$", "Any", "chars", |
"\\S", "\\s", "\\W", "\\w", "\\Z", "\\z", |
39 |
"not", |
"Opt", "^", "$", "Any", "chars", "not", |
40 |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
41 |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
42 |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", "{", |
43 |
"*", "*?", "+", "+?", "?", "??", "{", "{", |
"*", "*?", "+", "+?", "?", "??", "{", "{", |
44 |
"class", "Ref", |
"class", "Ref", |
45 |
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", "Once", |
"Alt", "Ket", "KetRmax", "KetRmin", "Assert", "Assert not", |
46 |
|
"AssertB", "AssertB not", "Reverse", "Once", "Cond", "Cref", |
47 |
"Brazero", "Braminzero", "Bra" |
"Brazero", "Braminzero", "Bra" |
48 |
}; |
}; |
49 |
|
|
50 |
|
|
51 |
static void print_internals(pcre *re) |
static void print_internals(pcre *re, FILE *outfile) |
52 |
{ |
{ |
53 |
unsigned char *code = ((real_pcre *)re)->code; |
unsigned char *code = ((real_pcre *)re)->code; |
54 |
|
|
55 |
printf("------------------------------------------------------------------\n"); |
fprintf(outfile, "------------------------------------------------------------------\n"); |
56 |
|
|
57 |
for(;;) |
for(;;) |
58 |
{ |
{ |
59 |
int c; |
int c; |
60 |
int charlength; |
int charlength; |
61 |
|
|
62 |
printf("%3d ", code - ((real_pcre *)re)->code); |
fprintf(outfile, "%3d ", (int)(code - ((real_pcre *)re)->code)); |
63 |
|
|
64 |
if (*code >= OP_BRA) |
if (*code >= OP_BRA) |
65 |
{ |
{ |
66 |
printf("%3d Bra %d", (code[1] << 8) + code[2], *code - OP_BRA); |
fprintf(outfile, "%3d Bra %d", (code[1] << 8) + code[2], *code - OP_BRA); |
67 |
code += 2; |
code += 2; |
68 |
} |
} |
69 |
|
|
70 |
else switch(*code) |
else switch(*code) |
71 |
{ |
{ |
72 |
case OP_END: |
case OP_END: |
73 |
printf(" %s\n", OP_names[*code]); |
fprintf(outfile, " %s\n", OP_names[*code]); |
74 |
printf("------------------------------------------------------------------\n"); |
fprintf(outfile, "------------------------------------------------------------------\n"); |
75 |
return; |
return; |
76 |
|
|
77 |
|
case OP_OPT: |
78 |
|
fprintf(outfile, " %.2x %s", code[1], OP_names[*code]); |
79 |
|
code++; |
80 |
|
break; |
81 |
|
|
82 |
|
case OP_COND: |
83 |
|
fprintf(outfile, "%3d Cond", (code[1] << 8) + code[2]); |
84 |
|
code += 2; |
85 |
|
break; |
86 |
|
|
87 |
|
case OP_CREF: |
88 |
|
fprintf(outfile, " %.2d %s", code[1], OP_names[*code]); |
89 |
|
code++; |
90 |
|
break; |
91 |
|
|
92 |
case OP_CHARS: |
case OP_CHARS: |
93 |
charlength = *(++code); |
charlength = *(++code); |
94 |
printf("%3d ", charlength); |
fprintf(outfile, "%3d ", charlength); |
95 |
while (charlength-- > 0) |
while (charlength-- > 0) |
96 |
if (isprint(c = *(++code))) printf("%c", c); else printf("\\x%02x", c); |
if (isprint(c = *(++code))) fprintf(outfile, "%c", c); |
97 |
|
else fprintf(outfile, "\\x%02x", c); |
98 |
break; |
break; |
99 |
|
|
100 |
case OP_KETRMAX: |
case OP_KETRMAX: |
103 |
case OP_KET: |
case OP_KET: |
104 |
case OP_ASSERT: |
case OP_ASSERT: |
105 |
case OP_ASSERT_NOT: |
case OP_ASSERT_NOT: |
106 |
|
case OP_ASSERTBACK: |
107 |
|
case OP_ASSERTBACK_NOT: |
108 |
case OP_ONCE: |
case OP_ONCE: |
109 |
printf("%3d %s", (code[1] << 8) + code[2], OP_names[*code]); |
fprintf(outfile, "%3d %s", (code[1] << 8) + code[2], OP_names[*code]); |
110 |
|
code += 2; |
111 |
|
break; |
112 |
|
|
113 |
|
case OP_REVERSE: |
114 |
|
fprintf(outfile, "%3d %s", (code[1] << 8) + code[2], OP_names[*code]); |
115 |
code += 2; |
code += 2; |
116 |
break; |
break; |
117 |
|
|
128 |
case OP_TYPEQUERY: |
case OP_TYPEQUERY: |
129 |
case OP_TYPEMINQUERY: |
case OP_TYPEMINQUERY: |
130 |
if (*code >= OP_TYPESTAR) |
if (*code >= OP_TYPESTAR) |
131 |
printf(" %s", OP_names[code[1]]); |
fprintf(outfile, " %s", OP_names[code[1]]); |
132 |
else if (isprint(c = code[1])) printf(" %c", c); |
else if (isprint(c = code[1])) fprintf(outfile, " %c", c); |
133 |
else printf(" \\x%02x", c); |
else fprintf(outfile, " \\x%02x", c); |
134 |
printf("%s", OP_names[*code++]); |
fprintf(outfile, "%s", OP_names[*code++]); |
135 |
break; |
break; |
136 |
|
|
137 |
case OP_EXACT: |
case OP_EXACT: |
138 |
case OP_UPTO: |
case OP_UPTO: |
139 |
case OP_MINUPTO: |
case OP_MINUPTO: |
140 |
if (isprint(c = code[3])) printf(" %c{", c); |
if (isprint(c = code[3])) fprintf(outfile, " %c{", c); |
141 |
else printf(" \\x%02x{", c); |
else fprintf(outfile, " \\x%02x{", c); |
142 |
if (*code != OP_EXACT) printf(","); |
if (*code != OP_EXACT) fprintf(outfile, ","); |
143 |
printf("%d}", (code[1] << 8) + code[2]); |
fprintf(outfile, "%d}", (code[1] << 8) + code[2]); |
144 |
if (*code == OP_MINUPTO) printf("?"); |
if (*code == OP_MINUPTO) fprintf(outfile, "?"); |
145 |
code += 3; |
code += 3; |
146 |
break; |
break; |
147 |
|
|
148 |
case OP_TYPEEXACT: |
case OP_TYPEEXACT: |
149 |
case OP_TYPEUPTO: |
case OP_TYPEUPTO: |
150 |
case OP_TYPEMINUPTO: |
case OP_TYPEMINUPTO: |
151 |
printf(" %s{", OP_names[code[3]]); |
fprintf(outfile, " %s{", OP_names[code[3]]); |
152 |
if (*code != OP_TYPEEXACT) printf("0,"); |
if (*code != OP_TYPEEXACT) fprintf(outfile, "0,"); |
153 |
printf("%d}", (code[1] << 8) + code[2]); |
fprintf(outfile, "%d}", (code[1] << 8) + code[2]); |
154 |
if (*code == OP_TYPEMINUPTO) printf("?"); |
if (*code == OP_TYPEMINUPTO) fprintf(outfile, "?"); |
155 |
code += 3; |
code += 3; |
156 |
break; |
break; |
157 |
|
|
158 |
case OP_NOT: |
case OP_NOT: |
159 |
if (isprint(c = *(++code))) printf(" [^%c]", c); |
if (isprint(c = *(++code))) fprintf(outfile, " [^%c]", c); |
160 |
else printf(" [^\\x%02x]", c); |
else fprintf(outfile, " [^\\x%02x]", c); |
161 |
break; |
break; |
162 |
|
|
163 |
case OP_NOTSTAR: |
case OP_NOTSTAR: |
166 |
case OP_NOTMINPLUS: |
case OP_NOTMINPLUS: |
167 |
case OP_NOTQUERY: |
case OP_NOTQUERY: |
168 |
case OP_NOTMINQUERY: |
case OP_NOTMINQUERY: |
169 |
if (isprint(c = code[1])) printf(" [^%c]", c); |
if (isprint(c = code[1])) fprintf(outfile, " [^%c]", c); |
170 |
else printf(" [^\\x%02x]", c); |
else fprintf(outfile, " [^\\x%02x]", c); |
171 |
printf("%s", OP_names[*code++]); |
fprintf(outfile, "%s", OP_names[*code++]); |
172 |
break; |
break; |
173 |
|
|
174 |
case OP_NOTEXACT: |
case OP_NOTEXACT: |
175 |
case OP_NOTUPTO: |
case OP_NOTUPTO: |
176 |
case OP_NOTMINUPTO: |
case OP_NOTMINUPTO: |
177 |
if (isprint(c = code[3])) printf(" [^%c]{", c); |
if (isprint(c = code[3])) fprintf(outfile, " [^%c]{", c); |
178 |
else printf(" [^\\x%02x]{", c); |
else fprintf(outfile, " [^\\x%02x]{", c); |
179 |
if (*code != OP_NOTEXACT) printf(","); |
if (*code != OP_NOTEXACT) fprintf(outfile, ","); |
180 |
printf("%d}", (code[1] << 8) + code[2]); |
fprintf(outfile, "%d}", (code[1] << 8) + code[2]); |
181 |
if (*code == OP_NOTMINUPTO) printf("?"); |
if (*code == OP_NOTMINUPTO) fprintf(outfile, "?"); |
182 |
code += 3; |
code += 3; |
183 |
break; |
break; |
184 |
|
|
185 |
case OP_REF: |
case OP_REF: |
186 |
printf(" \\%d", *(++code)); |
fprintf(outfile, " \\%d", *(++code)); |
187 |
code++; |
code++; |
188 |
goto CLASS_REF_REPEAT; |
goto CLASS_REF_REPEAT; |
189 |
|
|
190 |
case OP_CLASS: |
case OP_CLASS: |
191 |
{ |
{ |
192 |
int i, min, max; |
int i, min, max; |
|
|
|
193 |
code++; |
code++; |
194 |
printf(" ["); |
fprintf(outfile, " ["); |
195 |
|
|
196 |
for (i = 0; i < 256; i++) |
for (i = 0; i < 256; i++) |
197 |
{ |
{ |
200 |
int j; |
int j; |
201 |
for (j = i+1; j < 256; j++) |
for (j = i+1; j < 256; j++) |
202 |
if ((code[j/8] & (1 << (j&7))) == 0) break; |
if ((code[j/8] & (1 << (j&7))) == 0) break; |
203 |
if (i == '-' || i == ']') printf("\\"); |
if (i == '-' || i == ']') fprintf(outfile, "\\"); |
204 |
if (isprint(i)) printf("%c", i); else printf("\\x%02x", i); |
if (isprint(i)) fprintf(outfile, "%c", i); else fprintf(outfile, "\\x%02x", i); |
205 |
if (--j > i) |
if (--j > i) |
206 |
{ |
{ |
207 |
printf("-"); |
fprintf(outfile, "-"); |
208 |
if (j == '-' || j == ']') printf("\\"); |
if (j == '-' || j == ']') fprintf(outfile, "\\"); |
209 |
if (isprint(j)) printf("%c", j); else printf("\\x%02x", j); |
if (isprint(j)) fprintf(outfile, "%c", j); else fprintf(outfile, "\\x%02x", j); |
210 |
} |
} |
211 |
i = j; |
i = j; |
212 |
} |
} |
213 |
} |
} |
214 |
printf("]"); |
fprintf(outfile, "]"); |
215 |
code += 32; |
code += 32; |
216 |
|
|
217 |
CLASS_REF_REPEAT: |
CLASS_REF_REPEAT: |
224 |
case OP_CRMINPLUS: |
case OP_CRMINPLUS: |
225 |
case OP_CRQUERY: |
case OP_CRQUERY: |
226 |
case OP_CRMINQUERY: |
case OP_CRMINQUERY: |
227 |
printf("%s", OP_names[*code]); |
fprintf(outfile, "%s", OP_names[*code]); |
228 |
break; |
break; |
229 |
|
|
230 |
case OP_CRRANGE: |
case OP_CRRANGE: |
231 |
case OP_CRMINRANGE: |
case OP_CRMINRANGE: |
232 |
min = (code[1] << 8) + code[2]; |
min = (code[1] << 8) + code[2]; |
233 |
max = (code[3] << 8) + code[4]; |
max = (code[3] << 8) + code[4]; |
234 |
if (max == 0) printf("{%d,}", min); |
if (max == 0) fprintf(outfile, "{%d,}", min); |
235 |
else printf("{%d,%d}", min, max); |
else fprintf(outfile, "{%d,%d}", min, max); |
236 |
if (*code == OP_CRMINRANGE) printf("?"); |
if (*code == OP_CRMINRANGE) fprintf(outfile, "?"); |
237 |
code += 4; |
code += 4; |
238 |
break; |
break; |
239 |
|
|
246 |
/* Anything else is just a one-node item */ |
/* Anything else is just a one-node item */ |
247 |
|
|
248 |
default: |
default: |
249 |
printf(" %s", OP_names[*code]); |
fprintf(outfile, " %s", OP_names[*code]); |
250 |
break; |
break; |
251 |
} |
} |
252 |
|
|
253 |
code++; |
code++; |
254 |
printf("\n"); |
fprintf(outfile, "\n"); |
255 |
} |
} |
256 |
} |
} |
257 |
|
|
274 |
|
|
275 |
static void *new_malloc(size_t size) |
static void *new_malloc(size_t size) |
276 |
{ |
{ |
277 |
if (log_store) fprintf(outfile, "Store size request: %d\n", (int)size); |
if (log_store) |
278 |
|
fprintf(outfile, "Memory allocation request: %d (code space %d)\n", |
279 |
|
(int)size, (int)size - offsetof(real_pcre, code[0])); |
280 |
return malloc(size); |
return malloc(size); |
281 |
} |
} |
282 |
|
|
294 |
int op = 1; |
int op = 1; |
295 |
int timeit = 0; |
int timeit = 0; |
296 |
int showinfo = 0; |
int showinfo = 0; |
297 |
|
int showstore = 0; |
298 |
int posix = 0; |
int posix = 0; |
299 |
int debug = 0; |
int debug = 0; |
300 |
int done = 0; |
int done = 0; |
309 |
|
|
310 |
while (argc > 1 && argv[op][0] == '-') |
while (argc > 1 && argv[op][0] == '-') |
311 |
{ |
{ |
312 |
if (strcmp(argv[op], "-s") == 0) log_store = 1; |
if (strcmp(argv[op], "-s") == 0 || strcmp(argv[op], "-m") == 0) |
313 |
|
showstore = 1; |
314 |
else if (strcmp(argv[op], "-t") == 0) timeit = 1; |
else if (strcmp(argv[op], "-t") == 0) timeit = 1; |
315 |
else if (strcmp(argv[op], "-i") == 0) showinfo = 1; |
else if (strcmp(argv[op], "-i") == 0) showinfo = 1; |
316 |
else if (strcmp(argv[op], "-d") == 0) showinfo = debug = 1; |
else if (strcmp(argv[op], "-d") == 0) showinfo = debug = 1; |
318 |
else |
else |
319 |
{ |
{ |
320 |
printf("*** Unknown option %s\n", argv[op]); |
printf("*** Unknown option %s\n", argv[op]); |
321 |
|
printf("Usage: pcretest [-d] [-i] [-p] [-s] [-t] [<input> [<output>]]\n"); |
322 |
|
printf(" -d debug: show compiled code; implies -i\n" |
323 |
|
" -i show information about compiled pattern\n" |
324 |
|
" -p use POSIX interface\n" |
325 |
|
" -s output store information\n" |
326 |
|
" -t time compilation and execution\n"); |
327 |
return 1; |
return 1; |
328 |
} |
} |
329 |
op++; |
op++; |
356 |
|
|
357 |
pcre_malloc = new_malloc; |
pcre_malloc = new_malloc; |
358 |
|
|
359 |
/* Heading line, then prompt for first re if stdin */ |
/* Heading line, then prompt for first regex if stdin */ |
360 |
|
|
|
fprintf(outfile, "Testing Perl-Compatible Regular Expressions\n"); |
|
361 |
fprintf(outfile, "PCRE version %s\n\n", pcre_version()); |
fprintf(outfile, "PCRE version %s\n\n", pcre_version()); |
362 |
|
|
363 |
/* Main loop */ |
/* Main loop */ |
368 |
pcre_extra *extra = NULL; |
pcre_extra *extra = NULL; |
369 |
regex_t preg; |
regex_t preg; |
370 |
const char *error; |
const char *error; |
371 |
unsigned char *p, *pp; |
unsigned char *p, *pp, *ppp; |
372 |
|
unsigned const char *tables = NULL; |
373 |
int do_study = 0; |
int do_study = 0; |
374 |
int do_debug = 0; |
int do_debug = debug; |
375 |
|
int do_showinfo = showinfo; |
376 |
int do_posix = 0; |
int do_posix = 0; |
377 |
int erroroffset, len, delimiter; |
int erroroffset, len, delimiter; |
378 |
|
|
379 |
if (infile == stdin) printf(" re> "); |
if (infile == stdin) printf(" re> "); |
380 |
if (fgets((char *)buffer, sizeof(buffer), infile) == NULL) break; |
if (fgets((char *)buffer, sizeof(buffer), infile) == NULL) break; |
381 |
if (infile != stdin) fprintf(outfile, (char *)buffer); |
if (infile != stdin) fprintf(outfile, "%s", (char *)buffer); |
382 |
|
|
383 |
p = buffer; |
p = buffer; |
384 |
while (isspace(*p)) p++; |
while (isspace(*p)) p++; |
389 |
|
|
390 |
delimiter = *p++; |
delimiter = *p++; |
391 |
|
|
392 |
if (isalnum(delimiter)) |
if (isalnum(delimiter) || delimiter == '\\') |
393 |
{ |
{ |
394 |
fprintf(outfile, "** Delimiter must not be alphameric\n"); |
fprintf(outfile, "** Delimiter must not be alphameric or \\\n"); |
395 |
goto SKIP_DATA; |
goto SKIP_DATA; |
396 |
} |
} |
397 |
|
|
399 |
|
|
400 |
for(;;) |
for(;;) |
401 |
{ |
{ |
402 |
while (*pp != 0 && *pp != delimiter) pp++; |
while (*pp != 0) |
403 |
|
{ |
404 |
|
if (*pp == '\\' && pp[1] != 0) pp++; |
405 |
|
else if (*pp == delimiter) break; |
406 |
|
pp++; |
407 |
|
} |
408 |
if (*pp != 0) break; |
if (*pp != 0) break; |
409 |
|
|
410 |
len = sizeof(buffer) - (pp - buffer); |
len = sizeof(buffer) - (pp - buffer); |
421 |
done = 1; |
done = 1; |
422 |
goto CONTINUE; |
goto CONTINUE; |
423 |
} |
} |
424 |
if (infile != stdin) fprintf(outfile, (char *)pp); |
if (infile != stdin) fprintf(outfile, "%s", (char *)pp); |
425 |
} |
} |
426 |
|
|
427 |
|
/* If the first character after the delimiter is backslash, make |
428 |
|
the pattern end with backslash. This is purely to provide a way |
429 |
|
of testing for the error message when a pattern ends with backslash. */ |
430 |
|
|
431 |
|
if (pp[1] == '\\') *pp++ = '\\'; |
432 |
|
|
433 |
/* Terminate the pattern at the delimiter */ |
/* Terminate the pattern at the delimiter */ |
434 |
|
|
435 |
*pp++ = 0; |
*pp++ = 0; |
438 |
|
|
439 |
options = 0; |
options = 0; |
440 |
study_options = 0; |
study_options = 0; |
441 |
|
log_store = showstore; /* default from command line */ |
442 |
|
|
443 |
while (*pp != 0) |
while (*pp != 0) |
444 |
{ |
{ |
445 |
switch (*pp++) |
switch (*pp++) |
448 |
case 'm': options |= PCRE_MULTILINE; break; |
case 'm': options |= PCRE_MULTILINE; break; |
449 |
case 's': options |= PCRE_DOTALL; break; |
case 's': options |= PCRE_DOTALL; break; |
450 |
case 'x': options |= PCRE_EXTENDED; break; |
case 'x': options |= PCRE_EXTENDED; break; |
451 |
|
|
452 |
case 'A': options |= PCRE_ANCHORED; break; |
case 'A': options |= PCRE_ANCHORED; break; |
453 |
case 'D': do_debug = 1; break; |
case 'D': do_debug = do_showinfo = 1; break; |
454 |
case 'E': options |= PCRE_DOLLAR_ENDONLY; break; |
case 'E': options |= PCRE_DOLLAR_ENDONLY; break; |
455 |
|
case 'I': do_showinfo = 1; break; |
456 |
|
case 'M': log_store = 1; break; |
457 |
case 'P': do_posix = 1; break; |
case 'P': do_posix = 1; break; |
458 |
case 'S': do_study = 1; break; |
case 'S': do_study = 1; break; |
459 |
case 'I': study_options |= PCRE_CASELESS; break; |
case 'U': options |= PCRE_UNGREEDY; break; |
460 |
case 'X': options |= PCRE_EXTRA; break; |
case 'X': options |= PCRE_EXTRA; break; |
461 |
|
|
462 |
|
case 'L': |
463 |
|
ppp = pp; |
464 |
|
while (*ppp != '\n' && *ppp != ' ') ppp++; |
465 |
|
*ppp = 0; |
466 |
|
if (setlocale(LC_CTYPE, (const char *)pp) == NULL) |
467 |
|
{ |
468 |
|
fprintf(outfile, "** Failed to set locale \"%s\"\n", pp); |
469 |
|
goto SKIP_DATA; |
470 |
|
} |
471 |
|
tables = pcre_maketables(); |
472 |
|
pp = ppp; |
473 |
|
break; |
474 |
|
|
475 |
case '\n': case ' ': break; |
case '\n': case ' ': break; |
476 |
default: |
default: |
477 |
fprintf(outfile, "** Unknown option '%c'\n", pp[-1]); |
fprintf(outfile, "** Unknown option '%c'\n", pp[-1]); |
480 |
} |
} |
481 |
|
|
482 |
/* Handle compiling via the POSIX interface, which doesn't support the |
/* Handle compiling via the POSIX interface, which doesn't support the |
483 |
timing, showing, or debugging options. */ |
timing, showing, or debugging options, nor the ability to pass over |
484 |
|
local character tables. */ |
485 |
|
|
486 |
if (posix || do_posix) |
if (posix || do_posix) |
487 |
{ |
{ |
511 |
register int i; |
register int i; |
512 |
clock_t time_taken; |
clock_t time_taken; |
513 |
clock_t start_time = clock(); |
clock_t start_time = clock(); |
514 |
for (i = 0; i < 4000; i++) |
for (i = 0; i < LOOPREPEAT; i++) |
515 |
{ |
{ |
516 |
re = pcre_compile((char *)p, options, &error, &erroroffset); |
re = pcre_compile((char *)p, options, &error, &erroroffset, tables); |
517 |
if (re != NULL) free(re); |
if (re != NULL) free(re); |
518 |
} |
} |
519 |
time_taken = clock() - start_time; |
time_taken = clock() - start_time; |
520 |
fprintf(outfile, "Compile time %.2f milliseconds\n", |
fprintf(outfile, "Compile time %.3f milliseconds\n", |
521 |
((double)time_taken)/(4 * CLOCKS_PER_SEC)); |
((double)time_taken * 1000.0) / |
522 |
|
((double)LOOPREPEAT * (double)CLOCKS_PER_SEC)); |
523 |
} |
} |
524 |
|
|
525 |
re = pcre_compile((char *)p, options, &error, &erroroffset); |
re = pcre_compile((char *)p, options, &error, &erroroffset, tables); |
526 |
|
|
527 |
/* Compilation failed; go back for another re, skipping to blank line |
/* Compilation failed; go back for another re, skipping to blank line |
528 |
if non-interactive. */ |
if non-interactive. */ |
546 |
} |
} |
547 |
fprintf(outfile, "\n"); |
fprintf(outfile, "\n"); |
548 |
} |
} |
549 |
continue; |
goto CONTINUE; |
550 |
} |
} |
551 |
|
|
552 |
/* Compilation succeeded; print data if required */ |
/* Compilation succeeded; print data if required */ |
553 |
|
|
554 |
if (showinfo || do_debug) |
if (do_showinfo) |
555 |
{ |
{ |
556 |
int first_char, count; |
int first_char, count; |
557 |
|
|
558 |
if (debug || do_debug) print_internals(re); |
if (do_debug) print_internals(re, outfile); |
559 |
|
|
560 |
count = pcre_info(re, &options, &first_char); |
count = pcre_info(re, &options, &first_char); |
561 |
if (count < 0) fprintf(outfile, |
if (count < 0) fprintf(outfile, |
564 |
{ |
{ |
565 |
fprintf(outfile, "Identifying subpattern count = %d\n", count); |
fprintf(outfile, "Identifying subpattern count = %d\n", count); |
566 |
if (options == 0) fprintf(outfile, "No options\n"); |
if (options == 0) fprintf(outfile, "No options\n"); |
567 |
else fprintf(outfile, "Options:%s%s%s%s%s%s%s\n", |
else fprintf(outfile, "Options:%s%s%s%s%s%s%s%s\n", |
568 |
((options & PCRE_ANCHORED) != 0)? " anchored" : "", |
((options & PCRE_ANCHORED) != 0)? " anchored" : "", |
569 |
((options & PCRE_CASELESS) != 0)? " caseless" : "", |
((options & PCRE_CASELESS) != 0)? " caseless" : "", |
570 |
((options & PCRE_EXTENDED) != 0)? " extended" : "", |
((options & PCRE_EXTENDED) != 0)? " extended" : "", |
571 |
((options & PCRE_MULTILINE) != 0)? " multiline" : "", |
((options & PCRE_MULTILINE) != 0)? " multiline" : "", |
572 |
((options & PCRE_DOTALL) != 0)? " dotall" : "", |
((options & PCRE_DOTALL) != 0)? " dotall" : "", |
573 |
((options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "", |
((options & PCRE_DOLLAR_ENDONLY) != 0)? " dollar_endonly" : "", |
574 |
((options & PCRE_EXTRA) != 0)? " extra" : ""); |
((options & PCRE_EXTRA) != 0)? " extra" : "", |
575 |
|
((options & PCRE_UNGREEDY) != 0)? " ungreedy" : ""); |
576 |
if (first_char == -1) |
if (first_char == -1) |
577 |
{ |
{ |
578 |
fprintf(outfile, "First char at start or follows \\n\n"); |
fprintf(outfile, "First char at start or follows \\n\n"); |
601 |
register int i; |
register int i; |
602 |
clock_t time_taken; |
clock_t time_taken; |
603 |
clock_t start_time = clock(); |
clock_t start_time = clock(); |
604 |
for (i = 0; i < 4000; i++) |
for (i = 0; i < LOOPREPEAT; i++) |
605 |
extra = pcre_study(re, study_options, &error); |
extra = pcre_study(re, study_options, &error); |
606 |
time_taken = clock() - start_time; |
time_taken = clock() - start_time; |
607 |
if (extra != NULL) free(extra); |
if (extra != NULL) free(extra); |
608 |
fprintf(outfile, " Study time %.2f milliseconds\n", |
fprintf(outfile, " Study time %.3f milliseconds\n", |
609 |
((double)time_taken)/(4 * CLOCKS_PER_SEC)); |
((double)time_taken * 1000.0)/ |
610 |
|
((double)LOOPREPEAT * (double)CLOCKS_PER_SEC)); |
611 |
} |
} |
612 |
|
|
613 |
extra = pcre_study(re, study_options, &error); |
extra = pcre_study(re, study_options, &error); |
619 |
/* This looks at internal information. A bit kludgy to do it this |
/* This looks at internal information. A bit kludgy to do it this |
620 |
way, but it is useful for testing. */ |
way, but it is useful for testing. */ |
621 |
|
|
622 |
else if (showinfo || do_debug) |
else if (do_showinfo) |
623 |
{ |
{ |
624 |
real_pcre_extra *xx = (real_pcre_extra *)extra; |
real_pcre_extra *xx = (real_pcre_extra *)extra; |
625 |
if ((xx->options & PCRE_STUDY_MAPPED) == 0) |
if ((xx->options & PCRE_STUDY_MAPPED) == 0) |
662 |
{ |
{ |
663 |
unsigned char *q; |
unsigned char *q; |
664 |
int count, c; |
int count, c; |
665 |
int offsets[30]; |
int copystrings = 0; |
666 |
|
int getstrings = 0; |
667 |
|
int getlist = 0; |
668 |
|
int offsets[45]; |
669 |
int size_offsets = sizeof(offsets)/sizeof(int); |
int size_offsets = sizeof(offsets)/sizeof(int); |
670 |
|
|
671 |
options = 0; |
options = 0; |
676 |
done = 1; |
done = 1; |
677 |
goto CONTINUE; |
goto CONTINUE; |
678 |
} |
} |
679 |
if (infile != stdin) fprintf(outfile, (char *)buffer); |
if (infile != stdin) fprintf(outfile, "%s", (char *)buffer); |
680 |
|
|
681 |
len = (int)strlen((char *)buffer); |
len = (int)strlen((char *)buffer); |
682 |
while (len > 0 && isspace(buffer[len-1])) len--; |
while (len > 0 && isspace(buffer[len-1])) len--; |
730 |
options |= PCRE_NOTBOL; |
options |= PCRE_NOTBOL; |
731 |
continue; |
continue; |
732 |
|
|
733 |
case 'E': |
case 'C': |
734 |
options |= PCRE_DOLLAR_ENDONLY; |
while(isdigit(*p)) n = n * 10 + *p++ - '0'; |
735 |
continue; |
copystrings |= 1 << n; |
|
|
|
|
case 'I': |
|
|
options |= PCRE_CASELESS; |
|
736 |
continue; |
continue; |
737 |
|
|
738 |
case 'M': |
case 'G': |
739 |
options |= PCRE_MULTILINE; |
while(isdigit(*p)) n = n * 10 + *p++ - '0'; |
740 |
|
getstrings |= 1 << n; |
741 |
continue; |
continue; |
742 |
|
|
743 |
case 'S': |
case 'L': |
744 |
options |= PCRE_DOTALL; |
getlist = 1; |
745 |
continue; |
continue; |
746 |
|
|
747 |
case 'O': |
case 'O': |
784 |
{ |
{ |
785 |
if (pmatch[i].rm_so >= 0) |
if (pmatch[i].rm_so >= 0) |
786 |
{ |
{ |
787 |
fprintf(outfile, "%2d: ", i); |
fprintf(outfile, "%2d: ", (int)i); |
788 |
pchars(dbuffer + pmatch[i].rm_so, |
pchars(dbuffer + pmatch[i].rm_so, |
789 |
pmatch[i].rm_eo - pmatch[i].rm_so); |
pmatch[i].rm_eo - pmatch[i].rm_so); |
790 |
fprintf(outfile, "\n"); |
fprintf(outfile, "\n"); |
802 |
register int i; |
register int i; |
803 |
clock_t time_taken; |
clock_t time_taken; |
804 |
clock_t start_time = clock(); |
clock_t start_time = clock(); |
805 |
for (i = 0; i < 4000; i++) |
for (i = 0; i < LOOPREPEAT; i++) |
806 |
count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets, |
count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets, |
807 |
size_offsets); |
size_offsets); |
808 |
time_taken = clock() - start_time; |
time_taken = clock() - start_time; |
809 |
fprintf(outfile, "Execute time %.2f milliseconds\n", |
fprintf(outfile, "Execute time %.3f milliseconds\n", |
810 |
((double)time_taken)/(4 * CLOCKS_PER_SEC)); |
((double)time_taken * 1000.0)/ |
811 |
|
((double)LOOPREPEAT * (double)CLOCKS_PER_SEC)); |
812 |
} |
} |
813 |
|
|
814 |
count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets, |
count = pcre_exec(re, extra, (char *)dbuffer, len, options, offsets, |
817 |
if (count == 0) |
if (count == 0) |
818 |
{ |
{ |
819 |
fprintf(outfile, "Matched, but too many substrings\n"); |
fprintf(outfile, "Matched, but too many substrings\n"); |
820 |
count = size_offsets/2; |
count = size_offsets/3; |
821 |
} |
} |
822 |
|
|
823 |
if (count >= 0) |
if (count >= 0) |
824 |
{ |
{ |
825 |
int i; |
int i; |
826 |
count *= 2; |
for (i = 0; i < count * 2; i += 2) |
|
for (i = 0; i < count; i += 2) |
|
827 |
{ |
{ |
828 |
if (offsets[i] < 0) |
if (offsets[i] < 0) |
829 |
fprintf(outfile, "%2d: <unset>\n", i/2); |
fprintf(outfile, "%2d: <unset>\n", i/2); |
834 |
fprintf(outfile, "\n"); |
fprintf(outfile, "\n"); |
835 |
} |
} |
836 |
} |
} |
837 |
|
|
838 |
|
for (i = 0; i < 32; i++) |
839 |
|
{ |
840 |
|
if ((copystrings & (1 << i)) != 0) |
841 |
|
{ |
842 |
|
char buffer[16]; |
843 |
|
int rc = pcre_copy_substring((char *)dbuffer, offsets, count, |
844 |
|
i, buffer, sizeof(buffer)); |
845 |
|
if (rc < 0) |
846 |
|
fprintf(outfile, "copy substring %d failed %d\n", i, rc); |
847 |
|
else |
848 |
|
fprintf(outfile, "%2dC %s (%d)\n", i, buffer, rc); |
849 |
|
} |
850 |
|
} |
851 |
|
|
852 |
|
for (i = 0; i < 32; i++) |
853 |
|
{ |
854 |
|
if ((getstrings & (1 << i)) != 0) |
855 |
|
{ |
856 |
|
const char *substring; |
857 |
|
int rc = pcre_get_substring((char *)dbuffer, offsets, count, |
858 |
|
i, &substring); |
859 |
|
if (rc < 0) |
860 |
|
fprintf(outfile, "get substring %d failed %d\n", i, rc); |
861 |
|
else |
862 |
|
{ |
863 |
|
fprintf(outfile, "%2dG %s (%d)\n", i, substring, rc); |
864 |
|
free((void *)substring); |
865 |
|
} |
866 |
|
} |
867 |
|
} |
868 |
|
|
869 |
|
if (getlist) |
870 |
|
{ |
871 |
|
const char **stringlist; |
872 |
|
int rc = pcre_get_substring_list((char *)dbuffer, offsets, count, |
873 |
|
&stringlist); |
874 |
|
if (rc < 0) |
875 |
|
fprintf(outfile, "get substring list failed %d\n", rc); |
876 |
|
else |
877 |
|
{ |
878 |
|
for (i = 0; i < count; i++) |
879 |
|
fprintf(outfile, "%2dL %s\n", i, stringlist[i]); |
880 |
|
if (stringlist[i] != NULL) |
881 |
|
fprintf(outfile, "string list not terminated by NULL\n"); |
882 |
|
free((void *)stringlist); |
883 |
|
} |
884 |
|
} |
885 |
|
|
886 |
} |
} |
887 |
else |
else |
888 |
{ |
{ |
896 |
if (posix || do_posix) regfree(&preg); |
if (posix || do_posix) regfree(&preg); |
897 |
if (re != NULL) free(re); |
if (re != NULL) free(re); |
898 |
if (extra != NULL) free(extra); |
if (extra != NULL) free(extra); |
899 |
|
if (tables != NULL) |
900 |
|
{ |
901 |
|
free((void *)tables); |
902 |
|
setlocale(LC_CTYPE, "C"); |
903 |
|
} |
904 |
} |
} |
905 |
|
|
906 |
fprintf(outfile, "\n"); |
fprintf(outfile, "\n"); |