No empty .Rs/.Re
[netbsd-mini2440.git] / regress / lib / libc / regex / debug.c
blob27e7b5d4cd09d90f1a921b88e090c1a249331122
1 /* $NetBSD: debug.c,v 1.4 2005/02/06 06:05:19 perry Exp $ */
3 /*-
4 * Copyright (c) 1993 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
29 #include <stdio.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #include <regex.h>
37 #include "utils.h"
38 #include "regex2.h"
39 #include "debug.ih"
42 - regprint - print a regexp for debugging
43 == void regprint(regex_t *r, FILE *d);
45 void
46 regprint(r, d)
47 regex_t *r;
48 FILE *d;
50 struct re_guts *g = r->re_g;
51 int i;
52 int c;
53 int last;
54 int nincat[NC];
56 fprintf(d, "%ld states, %d categories", (long)g->nstates,
57 g->ncategories);
58 fprintf(d, ", first %ld last %ld", (long)g->firststate,
59 (long)g->laststate);
60 if (g->iflags&USEBOL)
61 fprintf(d, ", USEBOL");
62 if (g->iflags&USEEOL)
63 fprintf(d, ", USEEOL");
64 if (g->iflags&BAD)
65 fprintf(d, ", BAD");
66 if (g->nsub > 0)
67 fprintf(d, ", nsub=%ld", (long)g->nsub);
68 if (g->must != NULL)
69 fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
70 g->must);
71 if (g->backrefs)
72 fprintf(d, ", backrefs");
73 if (g->nplus > 0)
74 fprintf(d, ", nplus %ld", (long)g->nplus);
75 fprintf(d, "\n");
76 s_print(g, d);
77 for (i = 0; i < g->ncategories; i++) {
78 nincat[i] = 0;
79 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
80 if (g->categories[c] == i)
81 nincat[i]++;
83 fprintf(d, "cc0#%d", nincat[0]);
84 for (i = 1; i < g->ncategories; i++)
85 if (nincat[i] == 1) {
86 for (c = CHAR_MIN; c <= CHAR_MAX; c++)
87 if (g->categories[c] == i)
88 break;
89 fprintf(d, ", %d=%s", i, regchar(c));
91 fprintf(d, "\n");
92 for (i = 1; i < g->ncategories; i++)
93 if (nincat[i] != 1) {
94 fprintf(d, "cc%d\t", i);
95 last = -1;
96 for (c = CHAR_MIN; c <= CHAR_MAX+1; c++) /* +1 does flush */
97 if (c <= CHAR_MAX && g->categories[c] == i) {
98 if (last < 0) {
99 fprintf(d, "%s", regchar(c));
100 last = c;
102 } else {
103 if (last >= 0) {
104 if (last != c-1)
105 fprintf(d, "-%s",
106 regchar(c-1));
107 last = -1;
110 fprintf(d, "\n");
115 - s_print - print the strip for debugging
116 == static void s_print(struct re_guts *g, FILE *d);
118 static void
119 s_print(g, d)
120 struct re_guts *g;
121 FILE *d;
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++; \
139 } else \
140 col++; \
141 offset++; \
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;
184 } else {
185 if (last >= 0) {
186 if (last != i-1)
187 fprintf(d, "-%s",
188 regchar(i-1));
189 last = -1;
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;
250 if (!done)
251 GAP();
256 - regchar - make a character printable
257 == static char *regchar(int ch);
259 static char * /* -> representation */
260 regchar(ch)
261 int ch;
263 static char buf[10];
265 if (isprint(ch) || ch == ' ')
266 sprintf(buf, "%c", ch);
267 else
268 sprintf(buf, "\\%o", ch);
269 return(buf);