debug.c revision 1.4 1 1.4 christos /* $NetBSD: debug.c,v 1.4 2021/02/23 14:59:09 christos Exp $ */
2 1.1 pgoyette
3 1.1 pgoyette /*-
4 1.1 pgoyette * Copyright (c) 1993 The NetBSD Foundation, Inc.
5 1.1 pgoyette * All rights reserved.
6 1.1 pgoyette *
7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
8 1.1 pgoyette * modification, are permitted provided that the following conditions
9 1.1 pgoyette * are met:
10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
11 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
14 1.1 pgoyette * documentation and/or other materials provided with the distribution.
15 1.1 pgoyette *
16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.3 christos #include <sys/types.h>
30 1.1 pgoyette #include <ctype.h>
31 1.1 pgoyette #include <limits.h>
32 1.1 pgoyette #include <regex.h>
33 1.1 pgoyette #include <stdio.h>
34 1.1 pgoyette #include <stdlib.h>
35 1.1 pgoyette #include <string.h>
36 1.3 christos #include <wchar.h>
37 1.3 christos #include <wctype.h>
38 1.1 pgoyette
39 1.4 christos #ifndef __linux__
40 1.1 pgoyette /* Don't sort these! */
41 1.1 pgoyette #include "utils.h"
42 1.1 pgoyette #include "regex2.h"
43 1.4 christos #else
44 1.4 christos #define REGEX_NODEBUG
45 1.4 christos #endif
46 1.4 christos
47 1.4 christos #ifdef REGEX_TRE
48 1.4 christos #define REGEX_NODEBUG
49 1.4 christos #endif
50 1.1 pgoyette
51 1.1 pgoyette #include "test_regex.h"
52 1.1 pgoyette
53 1.4 christos #ifndef REGEX_NODEBUG
54 1.1 pgoyette static void s_print(struct re_guts *, FILE *);
55 1.1 pgoyette static char *regchar(int);
56 1.4 christos #endif
57 1.1 pgoyette
58 1.1 pgoyette /*
59 1.1 pgoyette * regprint - print a regexp for debugging
60 1.1 pgoyette */
61 1.1 pgoyette void
62 1.1 pgoyette regprint(regex_t *r, FILE *d)
63 1.1 pgoyette {
64 1.4 christos #ifndef REGEX_NODEBUG
65 1.1 pgoyette struct re_guts *g = r->re_g;
66 1.1 pgoyette
67 1.4 christos fprintf(d, "%ld states, %zu ncsets", (long)g->nstates, g->ncsets);
68 1.1 pgoyette fprintf(d, ", first %ld last %ld", (long)g->firststate,
69 1.1 pgoyette (long)g->laststate);
70 1.1 pgoyette if (g->iflags&USEBOL)
71 1.1 pgoyette fprintf(d, ", USEBOL");
72 1.1 pgoyette if (g->iflags&USEEOL)
73 1.1 pgoyette fprintf(d, ", USEEOL");
74 1.1 pgoyette if (g->iflags&BAD)
75 1.1 pgoyette fprintf(d, ", BAD");
76 1.1 pgoyette if (g->nsub > 0)
77 1.1 pgoyette fprintf(d, ", nsub=%ld", (long)g->nsub);
78 1.1 pgoyette if (g->must != NULL)
79 1.1 pgoyette fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
80 1.1 pgoyette g->must);
81 1.1 pgoyette if (g->backrefs)
82 1.1 pgoyette fprintf(d, ", backrefs");
83 1.1 pgoyette if (g->nplus > 0)
84 1.1 pgoyette fprintf(d, ", nplus %ld", (long)g->nplus);
85 1.1 pgoyette fprintf(d, "\n");
86 1.1 pgoyette s_print(g, d);
87 1.1 pgoyette fprintf(d, "\n");
88 1.4 christos #endif
89 1.1 pgoyette }
90 1.1 pgoyette
91 1.4 christos #ifndef REGEX_NODEBUG
92 1.1 pgoyette /*
93 1.1 pgoyette * s_print - print the strip for debugging
94 1.1 pgoyette */
95 1.1 pgoyette static void
96 1.1 pgoyette s_print(struct re_guts *g, FILE *d)
97 1.1 pgoyette {
98 1.1 pgoyette sop *s;
99 1.1 pgoyette int done = 0;
100 1.1 pgoyette sop opnd;
101 1.1 pgoyette int col = 0;
102 1.1 pgoyette sopno offset = 2;
103 1.1 pgoyette # define GAP() { if (offset % 5 == 0) { \
104 1.1 pgoyette if (col > 40) { \
105 1.1 pgoyette fprintf(d, "\n\t"); \
106 1.1 pgoyette col = 0; \
107 1.1 pgoyette } else { \
108 1.1 pgoyette fprintf(d, " "); \
109 1.1 pgoyette col++; \
110 1.1 pgoyette } \
111 1.1 pgoyette } else \
112 1.1 pgoyette col++; \
113 1.1 pgoyette offset++; \
114 1.1 pgoyette }
115 1.1 pgoyette
116 1.1 pgoyette if (OP(g->strip[0]) != OEND)
117 1.1 pgoyette fprintf(d, "missing initial OEND!\n");
118 1.1 pgoyette for (s = &g->strip[1]; !done; s++) {
119 1.1 pgoyette opnd = OPND(*s);
120 1.1 pgoyette switch (OP(*s)) {
121 1.1 pgoyette case OEND:
122 1.1 pgoyette fprintf(d, "\n");
123 1.1 pgoyette done = 1;
124 1.1 pgoyette break;
125 1.1 pgoyette case OCHAR:
126 1.1 pgoyette if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL)
127 1.1 pgoyette fprintf(d, "\\%c", (char)opnd);
128 1.1 pgoyette else
129 1.1 pgoyette fprintf(d, "%s", regchar((char)opnd));
130 1.1 pgoyette break;
131 1.1 pgoyette case OBOL:
132 1.1 pgoyette fprintf(d, "^");
133 1.1 pgoyette break;
134 1.1 pgoyette case OEOL:
135 1.1 pgoyette fprintf(d, "$");
136 1.1 pgoyette break;
137 1.1 pgoyette case OBOW:
138 1.1 pgoyette fprintf(d, "\\{");
139 1.1 pgoyette break;
140 1.1 pgoyette case OEOW:
141 1.1 pgoyette fprintf(d, "\\}");
142 1.1 pgoyette break;
143 1.1 pgoyette case OANY:
144 1.1 pgoyette fprintf(d, ".");
145 1.1 pgoyette break;
146 1.1 pgoyette case OANYOF:
147 1.1 pgoyette fprintf(d, "[(%ld)", (long)opnd);
148 1.1 pgoyette fprintf(d, "]");
149 1.1 pgoyette break;
150 1.1 pgoyette case OBACK_:
151 1.1 pgoyette fprintf(d, "(\\<%ld>", (long)opnd);
152 1.1 pgoyette break;
153 1.1 pgoyette case O_BACK:
154 1.1 pgoyette fprintf(d, "<%ld>\\)", (long)opnd);
155 1.1 pgoyette break;
156 1.1 pgoyette case OPLUS_:
157 1.1 pgoyette fprintf(d, "(+");
158 1.1 pgoyette if (OP(*(s+opnd)) != O_PLUS)
159 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
160 1.1 pgoyette break;
161 1.1 pgoyette case O_PLUS:
162 1.1 pgoyette if (OP(*(s-opnd)) != OPLUS_)
163 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
164 1.1 pgoyette fprintf(d, "+)");
165 1.1 pgoyette break;
166 1.1 pgoyette case OQUEST_:
167 1.1 pgoyette fprintf(d, "(?");
168 1.1 pgoyette if (OP(*(s+opnd)) != O_QUEST)
169 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
170 1.1 pgoyette break;
171 1.1 pgoyette case O_QUEST:
172 1.1 pgoyette if (OP(*(s-opnd)) != OQUEST_)
173 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
174 1.1 pgoyette fprintf(d, "?)");
175 1.1 pgoyette break;
176 1.1 pgoyette case OLPAREN:
177 1.1 pgoyette fprintf(d, "((<%ld>", (long)opnd);
178 1.1 pgoyette break;
179 1.1 pgoyette case ORPAREN:
180 1.1 pgoyette fprintf(d, "<%ld>))", (long)opnd);
181 1.1 pgoyette break;
182 1.1 pgoyette case OCH_:
183 1.1 pgoyette fprintf(d, "<");
184 1.1 pgoyette if (OP(*(s+opnd)) != OOR2)
185 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
186 1.1 pgoyette break;
187 1.1 pgoyette case OOR1:
188 1.1 pgoyette if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_)
189 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
190 1.1 pgoyette fprintf(d, "|");
191 1.1 pgoyette break;
192 1.1 pgoyette case OOR2:
193 1.1 pgoyette fprintf(d, "|");
194 1.1 pgoyette if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH)
195 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
196 1.1 pgoyette break;
197 1.1 pgoyette case O_CH:
198 1.1 pgoyette if (OP(*(s-opnd)) != OOR1)
199 1.1 pgoyette fprintf(d, "<%ld>", (long)opnd);
200 1.1 pgoyette fprintf(d, ">");
201 1.1 pgoyette break;
202 1.1 pgoyette default:
203 1.4 christos fprintf(d, "!%ld(%ld)!", (long)OP(*s), (long)opnd);
204 1.1 pgoyette break;
205 1.1 pgoyette }
206 1.1 pgoyette if (!done)
207 1.1 pgoyette GAP();
208 1.1 pgoyette }
209 1.1 pgoyette }
210 1.1 pgoyette
211 1.1 pgoyette /*
212 1.1 pgoyette * regchar - make a character printable
213 1.1 pgoyette */
214 1.1 pgoyette static char * /* -> representation */
215 1.1 pgoyette regchar(int ch)
216 1.1 pgoyette {
217 1.1 pgoyette static char buf[10];
218 1.1 pgoyette
219 1.1 pgoyette if (isprint(ch) || ch == ' ')
220 1.1 pgoyette sprintf(buf, "%c", ch);
221 1.1 pgoyette else
222 1.1 pgoyette sprintf(buf, "\\%o", ch);
223 1.1 pgoyette return(buf);
224 1.1 pgoyette }
225 1.4 christos #endif
226