/[pcre]/code/trunk/pcre_printint.src
ViewVC logotype

Contents of /code/trunk/pcre_printint.src

Parent Directory Parent Directory | Revision Log Revision Log


Revision 602 - (show annotations) (download) (as text)
Wed May 25 08:29:03 2011 UTC (3 years, 4 months ago) by ph10
File MIME type: application/x-wais-source
File size: 17476 byte(s)
Remove OP_OPT by handling /i and /m entirely at compile time. Fixes bug with 
patterns like /(?i:([^b]))(?1)/, where the /i option was mishandled.

1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /* PCRE is a library of functions to support regular expressions whose syntax
6 and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
9 Copyright (c) 1997-2010 University of Cambridge
10
11 -----------------------------------------------------------------------------
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 * Redistributions of source code must retain the above copyright notice,
16 this list of conditions and the following disclaimer.
17
18 * Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
22 * Neither the name of the University of Cambridge nor the names of its
23 contributors may be used to endorse or promote products derived from
24 this software without specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37 -----------------------------------------------------------------------------
38 */
39
40
41 /* This module contains a PCRE private debugging function for printing out the
42 internal form of a compiled regular expression, along with some supporting
43 local functions. This source file is used in two places:
44
45 (1) It is #included by pcre_compile.c when it is compiled in debugging mode
46 (PCRE_DEBUG defined in pcre_internal.h). It is not included in production
47 compiles.
48
49 (2) It is always #included by pcretest.c, which can be asked to print out a
50 compiled regex for debugging purposes. */
51
52
53 /* Macro that decides whether a character should be output as a literal or in
54 hexadecimal. We don't use isprint() because that can vary from system to system
55 (even without the use of locales) and we want the output always to be the same,
56 for testing purposes. This macro is used in pcretest as well as in this file. */
57
58 #ifdef EBCDIC
59 #define PRINTABLE(c) ((c) >= 64 && (c) < 255)
60 #else
61 #define PRINTABLE(c) ((c) >= 32 && (c) < 127)
62 #endif
63
64 /* The table of operator names. */
65
66 static const char *OP_names[] = { OP_NAME_LIST };
67
68
69
70 /*************************************************
71 * Print single- or multi-byte character *
72 *************************************************/
73
74 static int
75 print_char(FILE *f, uschar *ptr, BOOL utf8)
76 {
77 int c = *ptr;
78
79 #ifndef SUPPORT_UTF8
80 utf8 = utf8; /* Avoid compiler warning */
81 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
82 return 0;
83
84 #else
85 if (!utf8 || (c & 0xc0) != 0xc0)
86 {
87 if (PRINTABLE(c)) fprintf(f, "%c", c); else fprintf(f, "\\x%02x", c);
88 return 0;
89 }
90 else
91 {
92 int i;
93 int a = _pcre_utf8_table4[c & 0x3f]; /* Number of additional bytes */
94 int s = 6*a;
95 c = (c & _pcre_utf8_table3[a]) << s;
96 for (i = 1; i <= a; i++)
97 {
98 /* This is a check for malformed UTF-8; it should only occur if the sanity
99 check has been turned off. Rather than swallow random bytes, just stop if
100 we hit a bad one. Print it with \X instead of \x as an indication. */
101
102 if ((ptr[i] & 0xc0) != 0x80)
103 {
104 fprintf(f, "\\X{%x}", c);
105 return i - 1;
106 }
107
108 /* The byte is OK */
109
110 s -= 6;
111 c |= (ptr[i] & 0x3f) << s;
112 }
113 if (c < 128) fprintf(f, "\\x%02x", c); else fprintf(f, "\\x{%x}", c);
114 return a;
115 }
116 #endif
117 }
118
119
120
121 /*************************************************
122 * Find Unicode property name *
123 *************************************************/
124
125 static const char *
126 get_ucpname(int ptype, int pvalue)
127 {
128 #ifdef SUPPORT_UCP
129 int i;
130 for (i = _pcre_utt_size - 1; i >= 0; i--)
131 {
132 if (ptype == _pcre_utt[i].type && pvalue == _pcre_utt[i].value) break;
133 }
134 return (i >= 0)? _pcre_utt_names + _pcre_utt[i].name_offset : "??";
135 #else
136 /* It gets harder and harder to shut off unwanted compiler warnings. */
137 ptype = ptype * pvalue;
138 return (ptype == pvalue)? "??" : "??";
139 #endif
140 }
141
142
143
144 /*************************************************
145 * Print compiled regex *
146 *************************************************/
147
148 /* Make this function work for a regex with integers either byte order.
149 However, we assume that what we are passed is a compiled regex. The
150 print_lengths flag controls whether offsets and lengths of items are printed.
151 They can be turned off from pcretest so that automatic tests on bytecode can be
152 written that do not depend on the value of LINK_SIZE. */
153
154 static void
155 pcre_printint(pcre *external_re, FILE *f, BOOL print_lengths)
156 {
157 real_pcre *re = (real_pcre *)external_re;
158 uschar *codestart, *code;
159 BOOL utf8;
160
161 unsigned int options = re->options;
162 int offset = re->name_table_offset;
163 int count = re->name_count;
164 int size = re->name_entry_size;
165
166 if (re->magic_number != MAGIC_NUMBER)
167 {
168 offset = ((offset << 8) & 0xff00) | ((offset >> 8) & 0xff);
169 count = ((count << 8) & 0xff00) | ((count >> 8) & 0xff);
170 size = ((size << 8) & 0xff00) | ((size >> 8) & 0xff);
171 options = ((options << 24) & 0xff000000) |
172 ((options << 8) & 0x00ff0000) |
173 ((options >> 8) & 0x0000ff00) |
174 ((options >> 24) & 0x000000ff);
175 }
176
177 code = codestart = (uschar *)re + offset + count * size;
178 utf8 = (options & PCRE_UTF8) != 0;
179
180 for(;;)
181 {
182 uschar *ccode;
183 const char *flag = " ";
184 int c;
185 int extra = 0;
186
187 if (print_lengths)
188 fprintf(f, "%3d ", (int)(code - codestart));
189 else
190 fprintf(f, " ");
191
192 switch(*code)
193 {
194 /* ========================================================================== */
195 /* These cases are never obeyed. This is a fudge that causes a compile-
196 time error if the vectors OP_names or _pcre_OP_lengths, which are indexed
197 by opcode, are not the correct length. It seems to be the only way to do
198 such a check at compile time, as the sizeof() operator does not work in
199 the C preprocessor. We do this while compiling pcretest, because that
200 #includes pcre_tables.c, which holds _pcre_OP_lengths. We can't do this
201 when building pcre_compile.c with PCRE_DEBUG set, because it doesn't then
202 know the size of _pcre_OP_lengths. */
203
204 #ifdef COMPILING_PCRETEST
205 case OP_TABLE_LENGTH:
206 case OP_TABLE_LENGTH +
207 ((sizeof(OP_names)/sizeof(const char *) == OP_TABLE_LENGTH) &&
208 (sizeof(_pcre_OP_lengths) == OP_TABLE_LENGTH)):
209 break;
210 #endif
211 /* ========================================================================== */
212
213 case OP_END:
214 fprintf(f, " %s\n", OP_names[*code]);
215 fprintf(f, "------------------------------------------------------------------\n");
216 return;
217
218 case OP_CHAR:
219 fprintf(f, " ");
220 do
221 {
222 code++;
223 code += 1 + print_char(f, code, utf8);
224 }
225 while (*code == OP_CHAR);
226 fprintf(f, "\n");
227 continue;
228
229 case OP_CHARI:
230 fprintf(f, " /i ");
231 do
232 {
233 code++;
234 code += 1 + print_char(f, code, utf8);
235 }
236 while (*code == OP_CHARI);
237 fprintf(f, "\n");
238 continue;
239
240 case OP_CBRA:
241 case OP_SCBRA:
242 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
243 else fprintf(f, " ");
244 fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));
245 break;
246
247 case OP_BRA:
248 case OP_SBRA:
249 case OP_KETRMAX:
250 case OP_KETRMIN:
251 case OP_ALT:
252 case OP_KET:
253 case OP_ASSERT:
254 case OP_ASSERT_NOT:
255 case OP_ASSERTBACK:
256 case OP_ASSERTBACK_NOT:
257 case OP_ONCE:
258 case OP_COND:
259 case OP_SCOND:
260 case OP_REVERSE:
261 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
262 else fprintf(f, " ");
263 fprintf(f, "%s", OP_names[*code]);
264 break;
265
266 case OP_CLOSE:
267 fprintf(f, " %s %d", OP_names[*code], GET2(code, 1));
268 break;
269
270 case OP_CREF:
271 case OP_NCREF:
272 fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);
273 break;
274
275 case OP_RREF:
276 c = GET2(code, 1);
277 if (c == RREF_ANY)
278 fprintf(f, " Cond recurse any");
279 else
280 fprintf(f, " Cond recurse %d", c);
281 break;
282
283 case OP_NRREF:
284 c = GET2(code, 1);
285 if (c == RREF_ANY)
286 fprintf(f, " Cond nrecurse any");
287 else
288 fprintf(f, " Cond nrecurse %d", c);
289 break;
290
291 case OP_DEF:
292 fprintf(f, " Cond def");
293 break;
294
295 case OP_STARI:
296 case OP_MINSTARI:
297 case OP_POSSTARI:
298 case OP_PLUSI:
299 case OP_MINPLUSI:
300 case OP_POSPLUSI:
301 case OP_QUERYI:
302 case OP_MINQUERYI:
303 case OP_POSQUERYI:
304 flag = "/i";
305 /* Fall through */
306 case OP_STAR:
307 case OP_MINSTAR:
308 case OP_POSSTAR:
309 case OP_PLUS:
310 case OP_MINPLUS:
311 case OP_POSPLUS:
312 case OP_QUERY:
313 case OP_MINQUERY:
314 case OP_POSQUERY:
315 case OP_TYPESTAR:
316 case OP_TYPEMINSTAR:
317 case OP_TYPEPOSSTAR:
318 case OP_TYPEPLUS:
319 case OP_TYPEMINPLUS:
320 case OP_TYPEPOSPLUS:
321 case OP_TYPEQUERY:
322 case OP_TYPEMINQUERY:
323 case OP_TYPEPOSQUERY:
324 fprintf(f, " %s ", flag);
325 if (*code >= OP_TYPESTAR)
326 {
327 fprintf(f, "%s", OP_names[code[1]]);
328 if (code[1] == OP_PROP || code[1] == OP_NOTPROP)
329 {
330 fprintf(f, " %s ", get_ucpname(code[2], code[3]));
331 extra = 2;
332 }
333 }
334 else extra = print_char(f, code+1, utf8);
335 fprintf(f, "%s", OP_names[*code]);
336 break;
337
338 case OP_EXACTI:
339 case OP_UPTOI:
340 case OP_MINUPTOI:
341 case OP_POSUPTOI:
342 flag = "/i";
343 /* Fall through */
344 case OP_EXACT:
345 case OP_UPTO:
346 case OP_MINUPTO:
347 case OP_POSUPTO:
348 fprintf(f, " %s ", flag);
349 extra = print_char(f, code+3, utf8);
350 fprintf(f, "{");
351 if (*code != OP_EXACT && *code != OP_EXACTI) fprintf(f, "0,");
352 fprintf(f, "%d}", GET2(code,1));
353 if (*code == OP_MINUPTO || *code == OP_MINUPTOI) fprintf(f, "?");
354 else if (*code == OP_POSUPTO || *code == OP_POSUPTOI) fprintf(f, "+");
355 break;
356
357 case OP_TYPEEXACT:
358 case OP_TYPEUPTO:
359 case OP_TYPEMINUPTO:
360 case OP_TYPEPOSUPTO:
361 fprintf(f, " %s", OP_names[code[3]]);
362 if (code[3] == OP_PROP || code[3] == OP_NOTPROP)
363 {
364 fprintf(f, " %s ", get_ucpname(code[4], code[5]));
365 extra = 2;
366 }
367 fprintf(f, "{");
368 if (*code != OP_TYPEEXACT) fprintf(f, "0,");
369 fprintf(f, "%d}", GET2(code,1));
370 if (*code == OP_TYPEMINUPTO) fprintf(f, "?");
371 else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
372 break;
373
374 case OP_NOTI:
375 flag = "/i";
376 /* Fall through */
377 case OP_NOT:
378 c = code[1];
379 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
380 else fprintf(f, " %s [^\\x%02x]", flag, c);
381 break;
382
383 case OP_NOTSTARI:
384 case OP_NOTMINSTARI:
385 case OP_NOTPOSSTARI:
386 case OP_NOTPLUSI:
387 case OP_NOTMINPLUSI:
388 case OP_NOTPOSPLUSI:
389 case OP_NOTQUERYI:
390 case OP_NOTMINQUERYI:
391 case OP_NOTPOSQUERYI:
392 flag = "/i";
393 /* Fall through */
394
395 case OP_NOTSTAR:
396 case OP_NOTMINSTAR:
397 case OP_NOTPOSSTAR:
398 case OP_NOTPLUS:
399 case OP_NOTMINPLUS:
400 case OP_NOTPOSPLUS:
401 case OP_NOTQUERY:
402 case OP_NOTMINQUERY:
403 case OP_NOTPOSQUERY:
404 c = code[1];
405 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
406 else fprintf(f, " %s [^\\x%02x]", flag, c);
407 fprintf(f, "%s", OP_names[*code]);
408 break;
409
410 case OP_NOTEXACTI:
411 case OP_NOTUPTOI:
412 case OP_NOTMINUPTOI:
413 case OP_NOTPOSUPTOI:
414 flag = "/i";
415 /* Fall through */
416
417 case OP_NOTEXACT:
418 case OP_NOTUPTO:
419 case OP_NOTMINUPTO:
420 case OP_NOTPOSUPTO:
421 c = code[3];
422 if (PRINTABLE(c)) fprintf(f, " %s [^%c]{", flag, c);
423 else fprintf(f, " %s [^\\x%02x]{", flag, c);
424 if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,");
425 fprintf(f, "%d}", GET2(code,1));
426 if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?");
427 else
428 if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+");
429 break;
430
431 case OP_RECURSE:
432 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
433 else fprintf(f, " ");
434 fprintf(f, "%s", OP_names[*code]);
435 break;
436
437 case OP_REFI:
438 flag = "/i";
439 /* Fall through */
440 case OP_REF:
441 fprintf(f, " %s \\%d", flag, GET2(code,1));
442 ccode = code + _pcre_OP_lengths[*code];
443 goto CLASS_REF_REPEAT;
444
445 case OP_CALLOUT:
446 fprintf(f, " %s %d %d %d", OP_names[*code], code[1], GET(code,2),
447 GET(code, 2 + LINK_SIZE));
448 break;
449
450 case OP_PROP:
451 case OP_NOTPROP:
452 fprintf(f, " %s %s", OP_names[*code], get_ucpname(code[1], code[2]));
453 break;
454
455 /* OP_XCLASS can only occur in UTF-8 mode. However, there's no harm in
456 having this code always here, and it makes it less messy without all those
457 #ifdefs. */
458
459 case OP_CLASS:
460 case OP_NCLASS:
461 case OP_XCLASS:
462 {
463 int i, min, max;
464 BOOL printmap;
465
466 fprintf(f, " [");
467
468 if (*code == OP_XCLASS)
469 {
470 extra = GET(code, 1);
471 ccode = code + LINK_SIZE + 1;
472 printmap = (*ccode & XCL_MAP) != 0;
473 if ((*ccode++ & XCL_NOT) != 0) fprintf(f, "^");
474 }
475 else
476 {
477 printmap = TRUE;
478 ccode = code + 1;
479 }
480
481 /* Print a bit map */
482
483 if (printmap)
484 {
485 for (i = 0; i < 256; i++)
486 {
487 if ((ccode[i/8] & (1 << (i&7))) != 0)
488 {
489 int j;
490 for (j = i+1; j < 256; j++)
491 if ((ccode[j/8] & (1 << (j&7))) == 0) break;
492 if (i == '-' || i == ']') fprintf(f, "\\");
493 if (PRINTABLE(i)) fprintf(f, "%c", i);
494 else fprintf(f, "\\x%02x", i);
495 if (--j > i)
496 {
497 if (j != i + 1) fprintf(f, "-");
498 if (j == '-' || j == ']') fprintf(f, "\\");
499 if (PRINTABLE(j)) fprintf(f, "%c", j);
500 else fprintf(f, "\\x%02x", j);
501 }
502 i = j;
503 }
504 }
505 ccode += 32;
506 }
507
508 /* For an XCLASS there is always some additional data */
509
510 if (*code == OP_XCLASS)
511 {
512 int ch;
513 while ((ch = *ccode++) != XCL_END)
514 {
515 if (ch == XCL_PROP)
516 {
517 int ptype = *ccode++;
518 int pvalue = *ccode++;
519 fprintf(f, "\\p{%s}", get_ucpname(ptype, pvalue));
520 }
521 else if (ch == XCL_NOTPROP)
522 {
523 int ptype = *ccode++;
524 int pvalue = *ccode++;
525 fprintf(f, "\\P{%s}", get_ucpname(ptype, pvalue));
526 }
527 else
528 {
529 ccode += 1 + print_char(f, ccode, TRUE);
530 if (ch == XCL_RANGE)
531 {
532 fprintf(f, "-");
533 ccode += 1 + print_char(f, ccode, TRUE);
534 }
535 }
536 }
537 }
538
539 /* Indicate a non-UTF8 class which was created by negation */
540
541 fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : "");
542
543 /* Handle repeats after a class or a back reference */
544
545 CLASS_REF_REPEAT:
546 switch(*ccode)
547 {
548 case OP_CRSTAR:
549 case OP_CRMINSTAR:
550 case OP_CRPLUS:
551 case OP_CRMINPLUS:
552 case OP_CRQUERY:
553 case OP_CRMINQUERY:
554 fprintf(f, "%s", OP_names[*ccode]);
555 extra += _pcre_OP_lengths[*ccode];
556 break;
557
558 case OP_CRRANGE:
559 case OP_CRMINRANGE:
560 min = GET2(ccode,1);
561 max = GET2(ccode,3);
562 if (max == 0) fprintf(f, "{%d,}", min);
563 else fprintf(f, "{%d,%d}", min, max);
564 if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
565 extra += _pcre_OP_lengths[*ccode];
566 break;
567
568 /* Do nothing if it's not a repeat; this code stops picky compilers
569 warning about the lack of a default code path. */
570
571 default:
572 break;
573 }
574 }
575 break;
576
577 case OP_MARK:
578 case OP_PRUNE_ARG:
579 case OP_SKIP_ARG:
580 fprintf(f, " %s %s", OP_names[*code], code + 2);
581 extra += code[1];
582 break;
583
584 case OP_THEN:
585 if (print_lengths)
586 fprintf(f, " %s %d", OP_names[*code], GET(code, 1));
587 else
588 fprintf(f, " %s", OP_names[*code]);
589 break;
590
591 case OP_THEN_ARG:
592 if (print_lengths)
593 fprintf(f, " %s %d %s", OP_names[*code], GET(code, 1),
594 code + 2 + LINK_SIZE);
595 else
596 fprintf(f, " %s %s", OP_names[*code], code + 2 + LINK_SIZE);
597 extra += code[1+LINK_SIZE];
598 break;
599
600 case OP_CIRCM:
601 case OP_DOLLM:
602 flag = "/m";
603 /* Fall through */
604
605 /* Anything else is just an item with no data, but possibly a flag. */
606
607 default:
608 fprintf(f, " %s %s", flag, OP_names[*code]);
609 break;
610 }
611
612 code += _pcre_OP_lengths[*code] + extra;
613 fprintf(f, "\n");
614 }
615 }
616
617 /* End of pcre_printint.src */

Properties

Name Value
svn:eol-style native
svn:keywords "Author Date Id Revision Url"

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12