~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to regex/debug.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

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