1 /* $NetBSD: debug.c,v 1.2 2011/10/10 04:32:41 christos Exp $ */
4 * Copyright (c) 1993 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/types.h>
42 /* Don't sort these! */
46 #include "test_regex.h"
49 static void s_print(struct re_guts *, FILE *);
50 static char *regchar(int);
55 * regprint - print a regexp for debugging
58 regprint(regex_t *r, FILE *d)
60 struct re_guts *g = r->re_g;
65 fprintf(d, "%ld states, %zu categories", (long)g->nstates,
67 fprintf(d, ", first %ld last %ld", (long)g->firststate,
70 fprintf(d, ", USEBOL");
72 fprintf(d, ", USEEOL");
76 fprintf(d, ", nsub=%ld", (long)g->nsub);
78 fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
81 fprintf(d, ", backrefs");
83 fprintf(d, ", nplus %ld", (long)g->nplus);
86 for (size_t i = 0; i < g->ncategories; i++) {
88 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
89 if (g->categories[c] == i)
92 fprintf(d, "cc0#%d", nincat[0]);
93 for (size_t i = 1; i < g->ncategories; i++)
95 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
96 if (g->categories[c] == i)
98 fprintf(d, ", %zu=%s", i, regchar(c));
101 for (size_t i = 1; i < g->ncategories; i++)
102 if (nincat[i] != 1) {
103 fprintf(d, "cc%zu\t", i);
105 for (c = CHAR_MIN; c <= CHAR_MAX+1; c++) /* +1 does flush */
106 if (c <= CHAR_MAX && g->categories[c] == i) {
108 fprintf(d, "%s", regchar(c));
124 * s_print - print the strip for debugging
127 s_print(struct re_guts *g, FILE *d)
136 # define GAP() { if (offset % 5 == 0) { \
138 fprintf(d, "\n\t"); \
149 if (OP(g->strip[0]) != OEND)
150 fprintf(d, "missing initial OEND!\n");
151 for (s = &g->strip[1]; !done; s++) {
159 if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL)
160 fprintf(d, "\\%c", (char)opnd);
162 fprintf(d, "%s", regchar((char)opnd));
180 fprintf(d, "[(%ld)", (long)opnd);
183 for (size_t i = 0; i < g->csetsize+1; i++) /* +1 flushes */
184 if (CHIN(cs, i) && i < g->csetsize) {
186 fprintf(d, "%s", regchar(i));
191 if (last != (ssize_t)i - 1)
200 fprintf(d, "(\\<%ld>", (long)opnd);
203 fprintf(d, "<%ld>\\)", (long)opnd);
207 if (OP(*(s+opnd)) != O_PLUS)
208 fprintf(d, "<%ld>", (long)opnd);
211 if (OP(*(s-opnd)) != OPLUS_)
212 fprintf(d, "<%ld>", (long)opnd);
217 if (OP(*(s+opnd)) != O_QUEST)
218 fprintf(d, "<%ld>", (long)opnd);
221 if (OP(*(s-opnd)) != OQUEST_)
222 fprintf(d, "<%ld>", (long)opnd);
226 fprintf(d, "((<%ld>", (long)opnd);
229 fprintf(d, "<%ld>))", (long)opnd);
233 if (OP(*(s+opnd)) != OOR2)
234 fprintf(d, "<%ld>", (long)opnd);
237 if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_)
238 fprintf(d, "<%ld>", (long)opnd);
243 if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH)
244 fprintf(d, "<%ld>", (long)opnd);
247 if (OP(*(s-opnd)) != OOR1)
248 fprintf(d, "<%ld>", (long)opnd);
252 fprintf(d, "!%d(%d)!", OP(*s), opnd);
261 * regchar - make a character printable
263 static char * /* -> representation */
268 if (isprint(ch) || ch == ' ')
269 sprintf(buf, "%c", ch);
271 sprintf(buf, "\\%o", ch);
276 regprint(regex_t *r, FILE *d)