1 /* $NetBSD: debug.c,v 1.3 2017/01/14 00:50:56 christos 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 <sys/types.h> 30 #include <ctype.h> 31 #include <limits.h> 32 #include <regex.h> 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <wchar.h> 37 #include <wctype.h> 38 39 /* Don't sort these! */ 40 #include "utils.h" 41 #include "regex2.h" 42 43 #include "test_regex.h" 44 45 static void s_print(struct re_guts *, FILE *); 46 static char *regchar(int); 47 48 /* 49 * regprint - print a regexp for debugging 50 */ 51 void 52 regprint(regex_t *r, FILE *d) 53 { 54 struct re_guts *g = r->re_g; 55 int c; 56 int last; 57 int nincat[NC]; 58 59 fprintf(d, "%ld states, %zu 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 (size_t 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 (size_t 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, ", %zu=%s", i, regchar(c)); 93 } 94 fprintf(d, "\n"); 95 for (size_t i = 1; i < g->ncategories; i++) 96 if (nincat[i] != 1) { 97 fprintf(d, "cc%zu\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 done = 0; 126 sop opnd; 127 int col = 0; 128 ssize_t last; 129 sopno offset = 2; 130 # define GAP() { if (offset % 5 == 0) { \ 131 if (col > 40) { \ 132 fprintf(d, "\n\t"); \ 133 col = 0; \ 134 } else { \ 135 fprintf(d, " "); \ 136 col++; \ 137 } \ 138 } else \ 139 col++; \ 140 offset++; \ 141 } 142 143 if (OP(g->strip[0]) != OEND) 144 fprintf(d, "missing initial OEND!\n"); 145 for (s = &g->strip[1]; !done; s++) { 146 opnd = OPND(*s); 147 switch (OP(*s)) { 148 case OEND: 149 fprintf(d, "\n"); 150 done = 1; 151 break; 152 case OCHAR: 153 if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL) 154 fprintf(d, "\\%c", (char)opnd); 155 else 156 fprintf(d, "%s", regchar((char)opnd)); 157 break; 158 case OBOL: 159 fprintf(d, "^"); 160 break; 161 case OEOL: 162 fprintf(d, "$"); 163 break; 164 case OBOW: 165 fprintf(d, "\\{"); 166 break; 167 case OEOW: 168 fprintf(d, "\\}"); 169 break; 170 case OANY: 171 fprintf(d, "."); 172 break; 173 case OANYOF: 174 fprintf(d, "[(%ld)", (long)opnd); 175 cs = &g->sets[opnd]; 176 last = -1; 177 for (size_t i = 0; i < g->csetsize+1; i++) /* +1 flushes */ 178 if (CHIN(cs, i) && i < g->csetsize) { 179 if (last < 0) { 180 fprintf(d, "%s", regchar(i)); 181 last = i; 182 } 183 } else { 184 if (last >= 0) { 185 if (last != (ssize_t)i - 1) 186 fprintf(d, "-%s", 187 regchar(i - 1)); 188 last = -1; 189 } 190 } 191 fprintf(d, "]"); 192 break; 193 case OBACK_: 194 fprintf(d, "(\\<%ld>", (long)opnd); 195 break; 196 case O_BACK: 197 fprintf(d, "<%ld>\\)", (long)opnd); 198 break; 199 case OPLUS_: 200 fprintf(d, "(+"); 201 if (OP(*(s+opnd)) != O_PLUS) 202 fprintf(d, "<%ld>", (long)opnd); 203 break; 204 case O_PLUS: 205 if (OP(*(s-opnd)) != OPLUS_) 206 fprintf(d, "<%ld>", (long)opnd); 207 fprintf(d, "+)"); 208 break; 209 case OQUEST_: 210 fprintf(d, "(?"); 211 if (OP(*(s+opnd)) != O_QUEST) 212 fprintf(d, "<%ld>", (long)opnd); 213 break; 214 case O_QUEST: 215 if (OP(*(s-opnd)) != OQUEST_) 216 fprintf(d, "<%ld>", (long)opnd); 217 fprintf(d, "?)"); 218 break; 219 case OLPAREN: 220 fprintf(d, "((<%ld>", (long)opnd); 221 break; 222 case ORPAREN: 223 fprintf(d, "<%ld>))", (long)opnd); 224 break; 225 case OCH_: 226 fprintf(d, "<"); 227 if (OP(*(s+opnd)) != OOR2) 228 fprintf(d, "<%ld>", (long)opnd); 229 break; 230 case OOR1: 231 if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_) 232 fprintf(d, "<%ld>", (long)opnd); 233 fprintf(d, "|"); 234 break; 235 case OOR2: 236 fprintf(d, "|"); 237 if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH) 238 fprintf(d, "<%ld>", (long)opnd); 239 break; 240 case O_CH: 241 if (OP(*(s-opnd)) != OOR1) 242 fprintf(d, "<%ld>", (long)opnd); 243 fprintf(d, ">"); 244 break; 245 default: 246 fprintf(d, "!%d(%d)!", OP(*s), opnd); 247 break; 248 } 249 if (!done) 250 GAP(); 251 } 252 } 253 254 /* 255 * regchar - make a character printable 256 */ 257 static char * /* -> representation */ 258 regchar(int ch) 259 { 260 static char buf[10]; 261 262 if (isprint(ch) || ch == ' ') 263 sprintf(buf, "%c", ch); 264 else 265 sprintf(buf, "\\%o", ch); 266 return(buf); 267 } 268