xref: /netbsd-src/tests/lib/libc/regex/debug.c (revision 56bb44cae5b13a6b74792381ba1e6d930b26aa67)
1 /*	$NetBSD: debug.c,v 1.1 2011/01/08 18:10:31 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
27  */
28 
29 #include <ctype.h>
30 #include <limits.h>
31 #include <regex.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <sys/types.h>
37 
38 /* Don't sort these! */
39 #include "utils.h"
40 #include "regex2.h"
41 
42 #include "test_regex.h"
43 
44 static void s_print(struct re_guts *, FILE *);
45 static char *regchar(int);
46 
47 /*
48  * regprint - print a regexp for debugging
49  */
50 void
51 regprint(regex_t *r, FILE *d)
52 {
53 	struct re_guts *g = r->re_g;
54 	int i;
55 	int c;
56 	int last;
57 	int nincat[NC];
58 
59 	fprintf(d, "%ld states, %d categories", (long)g->nstates,
60 							g->ncategories);
61 	fprintf(d, ", first %ld last %ld", (long)g->firststate,
62 						(long)g->laststate);
63 	if (g->iflags&USEBOL)
64 		fprintf(d, ", USEBOL");
65 	if (g->iflags&USEEOL)
66 		fprintf(d, ", USEEOL");
67 	if (g->iflags&BAD)
68 		fprintf(d, ", BAD");
69 	if (g->nsub > 0)
70 		fprintf(d, ", nsub=%ld", (long)g->nsub);
71 	if (g->must != NULL)
72 		fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
73 								g->must);
74 	if (g->backrefs)
75 		fprintf(d, ", backrefs");
76 	if (g->nplus > 0)
77 		fprintf(d, ", nplus %ld", (long)g->nplus);
78 	fprintf(d, "\n");
79 	s_print(g, d);
80 	for (i = 0; i < g->ncategories; i++) {
81 		nincat[i] = 0;
82 		for (c = CHAR_MIN; c <= CHAR_MAX; c++)
83 			if (g->categories[c] == i)
84 				nincat[i]++;
85 	}
86 	fprintf(d, "cc0#%d", nincat[0]);
87 	for (i = 1; i < g->ncategories; i++)
88 		if (nincat[i] == 1) {
89 			for (c = CHAR_MIN; c <= CHAR_MAX; c++)
90 				if (g->categories[c] == i)
91 					break;
92 			fprintf(d, ", %d=%s", i, regchar(c));
93 		}
94 	fprintf(d, "\n");
95 	for (i = 1; i < g->ncategories; i++)
96 		if (nincat[i] != 1) {
97 			fprintf(d, "cc%d\t", i);
98 			last = -1;
99 			for (c = CHAR_MIN; c <= CHAR_MAX+1; c++)	/* +1 does flush */
100 				if (c <= CHAR_MAX && g->categories[c] == i) {
101 					if (last < 0) {
102 						fprintf(d, "%s", regchar(c));
103 						last = c;
104 					}
105 				} else {
106 					if (last >= 0) {
107 						if (last != c-1)
108 							fprintf(d, "-%s",
109 								regchar(c-1));
110 						last = -1;
111 					}
112 				}
113 			fprintf(d, "\n");
114 		}
115 }
116 
117 /*
118  * s_print - print the strip for debugging
119  */
120 static void
121 s_print(struct re_guts *g, FILE *d)
122 {
123 	sop *s;
124 	cset *cs;
125 	int i;
126 	int done = 0;
127 	sop opnd;
128 	int col = 0;
129 	int last;
130 	sopno offset = 2;
131 #	define	GAP()	{	if (offset % 5 == 0) { \
132 					if (col > 40) { \
133 						fprintf(d, "\n\t"); \
134 						col = 0; \
135 					} else { \
136 						fprintf(d, " "); \
137 						col++; \
138 					} \
139 				} else \
140 					col++; \
141 				offset++; \
142 			}
143 
144 	if (OP(g->strip[0]) != OEND)
145 		fprintf(d, "missing initial OEND!\n");
146 	for (s = &g->strip[1]; !done; s++) {
147 		opnd = OPND(*s);
148 		switch (OP(*s)) {
149 		case OEND:
150 			fprintf(d, "\n");
151 			done = 1;
152 			break;
153 		case OCHAR:
154 			if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL)
155 				fprintf(d, "\\%c", (char)opnd);
156 			else
157 				fprintf(d, "%s", regchar((char)opnd));
158 			break;
159 		case OBOL:
160 			fprintf(d, "^");
161 			break;
162 		case OEOL:
163 			fprintf(d, "$");
164 			break;
165 		case OBOW:
166 			fprintf(d, "\\{");
167 			break;
168 		case OEOW:
169 			fprintf(d, "\\}");
170 			break;
171 		case OANY:
172 			fprintf(d, ".");
173 			break;
174 		case OANYOF:
175 			fprintf(d, "[(%ld)", (long)opnd);
176 			cs = &g->sets[opnd];
177 			last = -1;
178 			for (i = 0; i < g->csetsize+1; i++)	/* +1 flushes */
179 				if (CHIN(cs, i) && i < g->csetsize) {
180 					if (last < 0) {
181 						fprintf(d, "%s", regchar(i));
182 						last = i;
183 					}
184 				} else {
185 					if (last >= 0) {
186 						if (last != i-1)
187 							fprintf(d, "-%s",
188 								regchar(i-1));
189 						last = -1;
190 					}
191 				}
192 			fprintf(d, "]");
193 			break;
194 		case OBACK_:
195 			fprintf(d, "(\\<%ld>", (long)opnd);
196 			break;
197 		case O_BACK:
198 			fprintf(d, "<%ld>\\)", (long)opnd);
199 			break;
200 		case OPLUS_:
201 			fprintf(d, "(+");
202 			if (OP(*(s+opnd)) != O_PLUS)
203 				fprintf(d, "<%ld>", (long)opnd);
204 			break;
205 		case O_PLUS:
206 			if (OP(*(s-opnd)) != OPLUS_)
207 				fprintf(d, "<%ld>", (long)opnd);
208 			fprintf(d, "+)");
209 			break;
210 		case OQUEST_:
211 			fprintf(d, "(?");
212 			if (OP(*(s+opnd)) != O_QUEST)
213 				fprintf(d, "<%ld>", (long)opnd);
214 			break;
215 		case O_QUEST:
216 			if (OP(*(s-opnd)) != OQUEST_)
217 				fprintf(d, "<%ld>", (long)opnd);
218 			fprintf(d, "?)");
219 			break;
220 		case OLPAREN:
221 			fprintf(d, "((<%ld>", (long)opnd);
222 			break;
223 		case ORPAREN:
224 			fprintf(d, "<%ld>))", (long)opnd);
225 			break;
226 		case OCH_:
227 			fprintf(d, "<");
228 			if (OP(*(s+opnd)) != OOR2)
229 				fprintf(d, "<%ld>", (long)opnd);
230 			break;
231 		case OOR1:
232 			if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_)
233 				fprintf(d, "<%ld>", (long)opnd);
234 			fprintf(d, "|");
235 			break;
236 		case OOR2:
237 			fprintf(d, "|");
238 			if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH)
239 				fprintf(d, "<%ld>", (long)opnd);
240 			break;
241 		case O_CH:
242 			if (OP(*(s-opnd)) != OOR1)
243 				fprintf(d, "<%ld>", (long)opnd);
244 			fprintf(d, ">");
245 			break;
246 		default:
247 			fprintf(d, "!%d(%d)!", OP(*s), opnd);
248 			break;
249 		}
250 		if (!done)
251 			GAP();
252 	}
253 }
254 
255 /*
256  * regchar - make a character printable
257  */
258 static char *			/* -> representation */
259 regchar(int ch)
260 {
261 	static char buf[10];
262 
263 	if (isprint(ch) || ch == ' ')
264 		sprintf(buf, "%c", ch);
265 	else
266 		sprintf(buf, "\\%o", ch);
267 	return(buf);
268 }
269