Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
srohatgi01
GitHub Repository: srohatgi01/cups
Path: blob/master/vcnet/regex/debug.c
1090 views
1
#include <stdio.h>
2
#include <string.h>
3
#include <ctype.h>
4
#include <limits.h>
5
#include <stdlib.h>
6
#include <sys/types.h>
7
8
#include "regex.h"
9
#include "regex2.h"
10
11
static void s_print(struct re_guts *g, FILE *d);
12
static char *regchar(int ch);
13
14
/*
15
- regprint - print a regexp for debugging
16
*/
17
void
18
regprint(r, d)
19
regex_t *r;
20
FILE *d;
21
{
22
struct re_guts *g = r->re_g;
23
int i;
24
int c;
25
int last;
26
int nincat[NC];
27
28
fprintf(d, "%ld states, %d categories", (long)g->nstates,
29
g->ncategories);
30
fprintf(d, ", first %ld last %ld", (long)g->firststate,
31
(long)g->laststate);
32
if (g->iflags&USEBOL)
33
fprintf(d, ", USEBOL");
34
if (g->iflags&USEEOL)
35
fprintf(d, ", USEEOL");
36
if (g->iflags&BAD)
37
fprintf(d, ", BAD");
38
if (g->nsub > 0)
39
fprintf(d, ", nsub=%ld", (long)g->nsub);
40
if (g->must != NULL)
41
fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
42
g->must);
43
if (g->backrefs)
44
fprintf(d, ", backrefs");
45
if (g->nplus > 0)
46
fprintf(d, ", nplus %ld", (long)g->nplus);
47
fprintf(d, "\n");
48
s_print(g, d);
49
for (i = 0; i < g->ncategories; i++) {
50
nincat[i] = 0;
51
for (c = CHAR_MIN; c <= CHAR_MAX; c++)
52
if (g->categories[c] == i)
53
nincat[i]++;
54
}
55
fprintf(d, "cc0#%d", nincat[0]);
56
for (i = 1; i < g->ncategories; i++)
57
if (nincat[i] == 1) {
58
for (c = CHAR_MIN; c <= CHAR_MAX; c++)
59
if (g->categories[c] == i)
60
break;
61
fprintf(d, ", %d=%s", i, regchar(c));
62
}
63
fprintf(d, "\n");
64
for (i = 1; i < g->ncategories; i++)
65
if (nincat[i] != 1) {
66
fprintf(d, "cc%d\t", i);
67
last = -1;
68
for (c = CHAR_MIN; c <= CHAR_MAX+1; c++) /* +1 does flush */
69
if (c <= CHAR_MAX && g->categories[c] == i) {
70
if (last < 0) {
71
fprintf(d, "%s", regchar(c));
72
last = c;
73
}
74
} else {
75
if (last >= 0) {
76
if (last != c-1)
77
fprintf(d, "-%s",
78
regchar(c-1));
79
last = -1;
80
}
81
}
82
fprintf(d, "\n");
83
}
84
}
85
86
/*
87
- s_print - print the strip for debugging
88
*/
89
static void
90
s_print(g, d)
91
struct re_guts *g;
92
FILE *d;
93
{
94
sop *s;
95
cset *cs;
96
int i;
97
int done = 0;
98
sop opnd;
99
int col = 0;
100
int last;
101
sopno offset = 2;
102
# define GAP() { if (offset % 5 == 0) { \
103
if (col > 40) { \
104
fprintf(d, "\n\t"); \
105
col = 0; \
106
} else { \
107
fprintf(d, " "); \
108
col++; \
109
} \
110
} else \
111
col++; \
112
offset++; \
113
}
114
115
if (OP(g->strip[0]) != OEND)
116
fprintf(d, "missing initial OEND!\n");
117
for (s = &g->strip[1]; !done; s++) {
118
opnd = OPND(*s);
119
switch (OP(*s)) {
120
case OEND:
121
fprintf(d, "\n");
122
done = 1;
123
break;
124
case OCHAR:
125
if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL)
126
fprintf(d, "\\%c", (char)opnd);
127
else
128
fprintf(d, "%s", regchar((char)opnd));
129
break;
130
case OBOL:
131
fprintf(d, "^");
132
break;
133
case OEOL:
134
fprintf(d, "$");
135
break;
136
case OBOW:
137
fprintf(d, "\\{");
138
break;
139
case OEOW:
140
fprintf(d, "\\}");
141
break;
142
case OANY:
143
fprintf(d, ".");
144
break;
145
case OANYOF:
146
fprintf(d, "[(%ld)", (long)opnd);
147
cs = &g->sets[opnd];
148
last = -1;
149
for (i = 0; i < g->csetsize+1; i++) /* +1 flushes */
150
if (CHIN(cs, i) && i < g->csetsize) {
151
if (last < 0) {
152
fprintf(d, "%s", regchar(i));
153
last = i;
154
}
155
} else {
156
if (last >= 0) {
157
if (last != i-1)
158
fprintf(d, "-%s",
159
regchar(i-1));
160
last = -1;
161
}
162
}
163
fprintf(d, "]");
164
break;
165
case OBACK_:
166
fprintf(d, "(\\<%ld>", (long)opnd);
167
break;
168
case O_BACK:
169
fprintf(d, "<%ld>\\)", (long)opnd);
170
break;
171
case OPLUS_:
172
fprintf(d, "(+");
173
if (OP(*(s+opnd)) != O_PLUS)
174
fprintf(d, "<%ld>", (long)opnd);
175
break;
176
case O_PLUS:
177
if (OP(*(s-opnd)) != OPLUS_)
178
fprintf(d, "<%ld>", (long)opnd);
179
fprintf(d, "+)");
180
break;
181
case OQUEST_:
182
fprintf(d, "(?");
183
if (OP(*(s+opnd)) != O_QUEST)
184
fprintf(d, "<%ld>", (long)opnd);
185
break;
186
case O_QUEST:
187
if (OP(*(s-opnd)) != OQUEST_)
188
fprintf(d, "<%ld>", (long)opnd);
189
fprintf(d, "?)");
190
break;
191
case OLPAREN:
192
fprintf(d, "((<%ld>", (long)opnd);
193
break;
194
case ORPAREN:
195
fprintf(d, "<%ld>))", (long)opnd);
196
break;
197
case OCH_:
198
fprintf(d, "<");
199
if (OP(*(s+opnd)) != OOR2)
200
fprintf(d, "<%ld>", (long)opnd);
201
break;
202
case OOR1:
203
if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_)
204
fprintf(d, "<%ld>", (long)opnd);
205
fprintf(d, "|");
206
break;
207
case OOR2:
208
fprintf(d, "|");
209
if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH)
210
fprintf(d, "<%ld>", (long)opnd);
211
break;
212
case O_CH:
213
if (OP(*(s-opnd)) != OOR1)
214
fprintf(d, "<%ld>", (long)opnd);
215
fprintf(d, ">");
216
break;
217
default:
218
fprintf(d, "!%ld(%ld)!", OP(*s), opnd);
219
break;
220
}
221
if (!done)
222
GAP();
223
}
224
}
225
226
/*
227
- regchar - make a character printable
228
*/
229
static char * /* -> representation */
230
regchar(ch)
231
int ch;
232
{
233
static char buf[10];
234
235
if (isprint(ch) || ch == ' ')
236
sprintf(buf, "%c", ch);
237
else
238
sprintf(buf, "\\%o", ch);
239
return(buf);
240
}
241
242