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

Contents of /code/trunk/pcre_printint.src

Parent Directory Parent Directory | Revision Log Revision Log


Revision 654 - (show annotations) (download) (as text)
Tue Aug 2 11:00:40 2011 UTC (2 years, 11 months ago) by ph10
File MIME type: application/x-wais-source
File size: 17555 byte(s)
Documentation and general text tidies in preparation for test release.

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_CBRAPOS:
242 case OP_SCBRA:
243 case OP_SCBRAPOS:
244 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
245 else fprintf(f, " ");
246 fprintf(f, "%s %d", OP_names[*code], GET2(code, 1+LINK_SIZE));
247 break;
248
249 case OP_BRA:
250 case OP_BRAPOS:
251 case OP_SBRA:
252 case OP_SBRAPOS:
253 case OP_KETRMAX:
254 case OP_KETRMIN:
255 case OP_KETRPOS:
256 case OP_ALT:
257 case OP_KET:
258 case OP_ASSERT:
259 case OP_ASSERT_NOT:
260 case OP_ASSERTBACK:
261 case OP_ASSERTBACK_NOT:
262 case OP_ONCE:
263 case OP_COND:
264 case OP_SCOND:
265 case OP_REVERSE:
266 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
267 else fprintf(f, " ");
268 fprintf(f, "%s", OP_names[*code]);
269 break;
270
271 case OP_CLOSE:
272 fprintf(f, " %s %d", OP_names[*code], GET2(code, 1));
273 break;
274
275 case OP_CREF:
276 case OP_NCREF:
277 fprintf(f, "%3d %s", GET2(code,1), OP_names[*code]);
278 break;
279
280 case OP_RREF:
281 c = GET2(code, 1);
282 if (c == RREF_ANY)
283 fprintf(f, " Cond recurse any");
284 else
285 fprintf(f, " Cond recurse %d", c);
286 break;
287
288 case OP_NRREF:
289 c = GET2(code, 1);
290 if (c == RREF_ANY)
291 fprintf(f, " Cond nrecurse any");
292 else
293 fprintf(f, " Cond nrecurse %d", c);
294 break;
295
296 case OP_DEF:
297 fprintf(f, " Cond def");
298 break;
299
300 case OP_STARI:
301 case OP_MINSTARI:
302 case OP_POSSTARI:
303 case OP_PLUSI:
304 case OP_MINPLUSI:
305 case OP_POSPLUSI:
306 case OP_QUERYI:
307 case OP_MINQUERYI:
308 case OP_POSQUERYI:
309 flag = "/i";
310 /* Fall through */
311 case OP_STAR:
312 case OP_MINSTAR:
313 case OP_POSSTAR:
314 case OP_PLUS:
315 case OP_MINPLUS:
316 case OP_POSPLUS:
317 case OP_QUERY:
318 case OP_MINQUERY:
319 case OP_POSQUERY:
320 case OP_TYPESTAR:
321 case OP_TYPEMINSTAR:
322 case OP_TYPEPOSSTAR:
323 case OP_TYPEPLUS:
324 case OP_TYPEMINPLUS:
325 case OP_TYPEPOSPLUS:
326 case OP_TYPEQUERY:
327 case OP_TYPEMINQUERY:
328 case OP_TYPEPOSQUERY:
329 fprintf(f, " %s ", flag);
330 if (*code >= OP_TYPESTAR)
331 {
332 fprintf(f, "%s", OP_names[code[1]]);
333 if (code[1] == OP_PROP || code[1] == OP_NOTPROP)
334 {
335 fprintf(f, " %s ", get_ucpname(code[2], code[3]));
336 extra = 2;
337 }
338 }
339 else extra = print_char(f, code+1, utf8);
340 fprintf(f, "%s", OP_names[*code]);
341 break;
342
343 case OP_EXACTI:
344 case OP_UPTOI:
345 case OP_MINUPTOI:
346 case OP_POSUPTOI:
347 flag = "/i";
348 /* Fall through */
349 case OP_EXACT:
350 case OP_UPTO:
351 case OP_MINUPTO:
352 case OP_POSUPTO:
353 fprintf(f, " %s ", flag);
354 extra = print_char(f, code+3, utf8);
355 fprintf(f, "{");
356 if (*code != OP_EXACT && *code != OP_EXACTI) fprintf(f, "0,");
357 fprintf(f, "%d}", GET2(code,1));
358 if (*code == OP_MINUPTO || *code == OP_MINUPTOI) fprintf(f, "?");
359 else if (*code == OP_POSUPTO || *code == OP_POSUPTOI) fprintf(f, "+");
360 break;
361
362 case OP_TYPEEXACT:
363 case OP_TYPEUPTO:
364 case OP_TYPEMINUPTO:
365 case OP_TYPEPOSUPTO:
366 fprintf(f, " %s", OP_names[code[3]]);
367 if (code[3] == OP_PROP || code[3] == OP_NOTPROP)
368 {
369 fprintf(f, " %s ", get_ucpname(code[4], code[5]));
370 extra = 2;
371 }
372 fprintf(f, "{");
373 if (*code != OP_TYPEEXACT) fprintf(f, "0,");
374 fprintf(f, "%d}", GET2(code,1));
375 if (*code == OP_TYPEMINUPTO) fprintf(f, "?");
376 else if (*code == OP_TYPEPOSUPTO) fprintf(f, "+");
377 break;
378
379 case OP_NOTI:
380 flag = "/i";
381 /* Fall through */
382 case OP_NOT:
383 c = code[1];
384 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
385 else fprintf(f, " %s [^\\x%02x]", flag, c);
386 break;
387
388 case OP_NOTSTARI:
389 case OP_NOTMINSTARI:
390 case OP_NOTPOSSTARI:
391 case OP_NOTPLUSI:
392 case OP_NOTMINPLUSI:
393 case OP_NOTPOSPLUSI:
394 case OP_NOTQUERYI:
395 case OP_NOTMINQUERYI:
396 case OP_NOTPOSQUERYI:
397 flag = "/i";
398 /* Fall through */
399
400 case OP_NOTSTAR:
401 case OP_NOTMINSTAR:
402 case OP_NOTPOSSTAR:
403 case OP_NOTPLUS:
404 case OP_NOTMINPLUS:
405 case OP_NOTPOSPLUS:
406 case OP_NOTQUERY:
407 case OP_NOTMINQUERY:
408 case OP_NOTPOSQUERY:
409 c = code[1];
410 if (PRINTABLE(c)) fprintf(f, " %s [^%c]", flag, c);
411 else fprintf(f, " %s [^\\x%02x]", flag, c);
412 fprintf(f, "%s", OP_names[*code]);
413 break;
414
415 case OP_NOTEXACTI:
416 case OP_NOTUPTOI:
417 case OP_NOTMINUPTOI:
418 case OP_NOTPOSUPTOI:
419 flag = "/i";
420 /* Fall through */
421
422 case OP_NOTEXACT:
423 case OP_NOTUPTO:
424 case OP_NOTMINUPTO:
425 case OP_NOTPOSUPTO:
426 c = code[3];
427 if (PRINTABLE(c)) fprintf(f, " %s [^%c]{", flag, c);
428 else fprintf(f, " %s [^\\x%02x]{", flag, c);
429 if (*code != OP_NOTEXACT && *code != OP_NOTEXACTI) fprintf(f, "0,");
430 fprintf(f, "%d}", GET2(code,1));
431 if (*code == OP_NOTMINUPTO || *code == OP_NOTMINUPTOI) fprintf(f, "?");
432 else
433 if (*code == OP_NOTPOSUPTO || *code == OP_NOTPOSUPTOI) fprintf(f, "+");
434 break;
435
436 case OP_RECURSE:
437 if (print_lengths) fprintf(f, "%3d ", GET(code, 1));
438 else fprintf(f, " ");
439 fprintf(f, "%s", OP_names[*code]);
440 break;
441
442 case OP_REFI:
443 flag = "/i";
444 /* Fall through */
445 case OP_REF:
446 fprintf(f, " %s \\%d", flag, GET2(code,1));
447 ccode = code + _pcre_OP_lengths[*code];
448 goto CLASS_REF_REPEAT;
449
450 case OP_CALLOUT:
451 fprintf(f, " %s %d %d %d", OP_names[*code], code[1], GET(code,2),
452 GET(code, 2 + LINK_SIZE));
453 break;
454
455 case OP_PROP:
456 case OP_NOTPROP:
457 fprintf(f, " %s %s", OP_names[*code], get_ucpname(code[1], code[2]));
458 break;
459
460 /* OP_XCLASS can only occur in UTF-8 mode. However, there's no harm in
461 having this code always here, and it makes it less messy without all those
462 #ifdefs. */
463
464 case OP_CLASS:
465 case OP_NCLASS:
466 case OP_XCLASS:
467 {
468 int i, min, max;
469 BOOL printmap;
470
471 fprintf(f, " [");
472
473 if (*code == OP_XCLASS)
474 {
475 extra = GET(code, 1);
476 ccode = code + LINK_SIZE + 1;
477 printmap = (*ccode & XCL_MAP) != 0;
478 if ((*ccode++ & XCL_NOT) != 0) fprintf(f, "^");
479 }
480 else
481 {
482 printmap = TRUE;
483 ccode = code + 1;
484 }
485
486 /* Print a bit map */
487
488 if (printmap)
489 {
490 for (i = 0; i < 256; i++)
491 {
492 if ((ccode[i/8] & (1 << (i&7))) != 0)
493 {
494 int j;
495 for (j = i+1; j < 256; j++)
496 if ((ccode[j/8] & (1 << (j&7))) == 0) break;
497 if (i == '-' || i == ']') fprintf(f, "\\");
498 if (PRINTABLE(i)) fprintf(f, "%c", i);
499 else fprintf(f, "\\x%02x", i);
500 if (--j > i)
501 {
502 if (j != i + 1) fprintf(f, "-");
503 if (j == '-' || j == ']') fprintf(f, "\\");
504 if (PRINTABLE(j)) fprintf(f, "%c", j);
505 else fprintf(f, "\\x%02x", j);
506 }
507 i = j;
508 }
509 }
510 ccode += 32;
511 }
512
513 /* For an XCLASS there is always some additional data */
514
515 if (*code == OP_XCLASS)
516 {
517 int ch;
518 while ((ch = *ccode++) != XCL_END)
519 {
520 if (ch == XCL_PROP)
521 {
522 int ptype = *ccode++;
523 int pvalue = *ccode++;
524 fprintf(f, "\\p{%s}", get_ucpname(ptype, pvalue));
525 }
526 else if (ch == XCL_NOTPROP)
527 {
528 int ptype = *ccode++;
529 int pvalue = *ccode++;
530 fprintf(f, "\\P{%s}", get_ucpname(ptype, pvalue));
531 }
532 else
533 {
534 ccode += 1 + print_char(f, ccode, TRUE);
535 if (ch == XCL_RANGE)
536 {
537 fprintf(f, "-");
538 ccode += 1 + print_char(f, ccode, TRUE);
539 }
540 }
541 }
542 }
543
544 /* Indicate a non-UTF8 class which was created by negation */
545
546 fprintf(f, "]%s", (*code == OP_NCLASS)? " (neg)" : "");
547
548 /* Handle repeats after a class or a back reference */
549
550 CLASS_REF_REPEAT:
551 switch(*ccode)
552 {
553 case OP_CRSTAR:
554 case OP_CRMINSTAR:
555 case OP_CRPLUS:
556 case OP_CRMINPLUS:
557 case OP_CRQUERY:
558 case OP_CRMINQUERY:
559 fprintf(f, "%s", OP_names[*ccode]);
560 extra += _pcre_OP_lengths[*ccode];
561 break;
562
563 case OP_CRRANGE:
564 case OP_CRMINRANGE:
565 min = GET2(ccode,1);
566 max = GET2(ccode,3);
567 if (max == 0) fprintf(f, "{%d,}", min);
568 else fprintf(f, "{%d,%d}", min, max);
569 if (*ccode == OP_CRMINRANGE) fprintf(f, "?");
570 extra += _pcre_OP_lengths[*ccode];
571 break;
572
573 /* Do nothing if it's not a repeat; this code stops picky compilers
574 warning about the lack of a default code path. */
575
576 default:
577 break;
578 }
579 }
580 break;
581
582 case OP_MARK:
583 case OP_PRUNE_ARG:
584 case OP_SKIP_ARG:
585 fprintf(f, " %s %s", OP_names[*code], code + 2);
586 extra += code[1];
587 break;
588
589 case OP_THEN:
590 if (print_lengths)
591 fprintf(f, " %s %d", OP_names[*code], GET(code, 1));
592 else
593 fprintf(f, " %s", OP_names[*code]);
594 break;
595
596 case OP_THEN_ARG:
597 if (print_lengths)
598 fprintf(f, " %s %d %s", OP_names[*code], GET(code, 1),
599 code + 2 + LINK_SIZE);
600 else
601 fprintf(f, " %s %s", OP_names[*code], code + 2 + LINK_SIZE);
602 extra += code[1+LINK_SIZE];
603 break;
604
605 case OP_CIRCM:
606 case OP_DOLLM:
607 flag = "/m";
608 /* Fall through */
609
610 /* Anything else is just an item with no data, but possibly a flag. */
611
612 default:
613 fprintf(f, " %s %s", flag, OP_names[*code]);
614 break;
615 }
616
617 code += _pcre_OP_lengths[*code] + extra;
618 fprintf(f, "\n");
619 }
620 }
621
622 /* 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