1 1.7 christos /* $NetBSD: debug.c,v 1.7 2021/02/25 22:37:36 christos Exp $ */ 2 1.1 pgoyette 3 1.1 pgoyette /*- 4 1.1 pgoyette * Copyright (c) 1993 The NetBSD Foundation, Inc. 5 1.1 pgoyette * All rights reserved. 6 1.1 pgoyette * 7 1.1 pgoyette * Redistribution and use in source and binary forms, with or without 8 1.1 pgoyette * modification, are permitted provided that the following conditions 9 1.1 pgoyette * are met: 10 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright 11 1.1 pgoyette * notice, this list of conditions and the following disclaimer. 12 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the 14 1.1 pgoyette * documentation and/or other materials provided with the distribution. 15 1.1 pgoyette * 16 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 pgoyette * POSSIBILITY OF SUCH DAMAGE. 27 1.1 pgoyette */ 28 1.1 pgoyette 29 1.3 christos #include <sys/types.h> 30 1.1 pgoyette #include <ctype.h> 31 1.1 pgoyette #include <limits.h> 32 1.1 pgoyette #include <regex.h> 33 1.1 pgoyette #include <stdio.h> 34 1.1 pgoyette #include <stdlib.h> 35 1.1 pgoyette #include <string.h> 36 1.1 pgoyette 37 1.4 christos #ifndef __linux__ 38 1.1 pgoyette /* Don't sort these! */ 39 1.1 pgoyette #include "utils.h" 40 1.1 pgoyette #include "regex2.h" 41 1.4 christos #else 42 1.4 christos #define REGEX_NODEBUG 43 1.4 christos #endif 44 1.4 christos 45 1.4 christos #ifdef REGEX_TRE 46 1.4 christos #define REGEX_NODEBUG 47 1.4 christos #endif 48 1.1 pgoyette 49 1.1 pgoyette #include "test_regex.h" 50 1.1 pgoyette 51 1.4 christos #ifndef REGEX_NODEBUG 52 1.1 pgoyette static void s_print(struct re_guts *, FILE *); 53 1.1 pgoyette static char *regchar(int); 54 1.4 christos #endif 55 1.1 pgoyette 56 1.1 pgoyette /* 57 1.1 pgoyette * regprint - print a regexp for debugging 58 1.1 pgoyette */ 59 1.1 pgoyette void 60 1.1 pgoyette regprint(regex_t *r, FILE *d) 61 1.1 pgoyette { 62 1.4 christos #ifndef REGEX_NODEBUG 63 1.1 pgoyette struct re_guts *g = r->re_g; 64 1.1 pgoyette 65 1.6 christos fprintf(d, ", first %u last %u", g->firststate, g->laststate); 66 1.1 pgoyette if (g->iflags&USEBOL) 67 1.1 pgoyette fprintf(d, ", USEBOL"); 68 1.1 pgoyette if (g->iflags&USEEOL) 69 1.1 pgoyette fprintf(d, ", USEEOL"); 70 1.1 pgoyette if (g->iflags&BAD) 71 1.1 pgoyette fprintf(d, ", BAD"); 72 1.1 pgoyette if (g->nsub > 0) 73 1.6 christos fprintf(d, ", nsub=%zu", g->nsub); 74 1.1 pgoyette if (g->must != NULL) 75 1.6 christos fprintf(d, ", must(%zu) `%*s'", g->mlen, (int)g->mlen, g->must); 76 1.1 pgoyette if (g->backrefs) 77 1.1 pgoyette fprintf(d, ", backrefs"); 78 1.1 pgoyette if (g->nplus > 0) 79 1.6 christos fprintf(d, ", nplus %u", g->nplus); 80 1.1 pgoyette fprintf(d, "\n"); 81 1.1 pgoyette s_print(g, d); 82 1.1 pgoyette fprintf(d, "\n"); 83 1.4 christos #endif 84 1.1 pgoyette } 85 1.1 pgoyette 86 1.4 christos #ifndef REGEX_NODEBUG 87 1.1 pgoyette /* 88 1.1 pgoyette * s_print - print the strip for debugging 89 1.1 pgoyette */ 90 1.1 pgoyette static void 91 1.1 pgoyette s_print(struct re_guts *g, FILE *d) 92 1.1 pgoyette { 93 1.1 pgoyette sop *s; 94 1.1 pgoyette int done = 0; 95 1.1 pgoyette sop opnd; 96 1.1 pgoyette int col = 0; 97 1.1 pgoyette sopno offset = 2; 98 1.1 pgoyette # define GAP() { if (offset % 5 == 0) { \ 99 1.1 pgoyette if (col > 40) { \ 100 1.1 pgoyette fprintf(d, "\n\t"); \ 101 1.1 pgoyette col = 0; \ 102 1.1 pgoyette } else { \ 103 1.1 pgoyette fprintf(d, " "); \ 104 1.1 pgoyette col++; \ 105 1.1 pgoyette } \ 106 1.1 pgoyette } else \ 107 1.1 pgoyette col++; \ 108 1.1 pgoyette offset++; \ 109 1.1 pgoyette } 110 1.1 pgoyette 111 1.1 pgoyette if (OP(g->strip[0]) != OEND) 112 1.1 pgoyette fprintf(d, "missing initial OEND!\n"); 113 1.1 pgoyette for (s = &g->strip[1]; !done; s++) { 114 1.1 pgoyette opnd = OPND(*s); 115 1.1 pgoyette switch (OP(*s)) { 116 1.1 pgoyette case OEND: 117 1.1 pgoyette fprintf(d, "\n"); 118 1.1 pgoyette done = 1; 119 1.1 pgoyette break; 120 1.1 pgoyette case OCHAR: 121 1.1 pgoyette if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL) 122 1.1 pgoyette fprintf(d, "\\%c", (char)opnd); 123 1.1 pgoyette else 124 1.1 pgoyette fprintf(d, "%s", regchar((char)opnd)); 125 1.1 pgoyette break; 126 1.1 pgoyette case OBOL: 127 1.1 pgoyette fprintf(d, "^"); 128 1.1 pgoyette break; 129 1.1 pgoyette case OEOL: 130 1.1 pgoyette fprintf(d, "$"); 131 1.1 pgoyette break; 132 1.1 pgoyette case OBOW: 133 1.1 pgoyette fprintf(d, "\\{"); 134 1.1 pgoyette break; 135 1.1 pgoyette case OEOW: 136 1.1 pgoyette fprintf(d, "\\}"); 137 1.1 pgoyette break; 138 1.1 pgoyette case OANY: 139 1.1 pgoyette fprintf(d, "."); 140 1.1 pgoyette break; 141 1.1 pgoyette case OANYOF: 142 1.6 christos fprintf(d, "[(%u)", opnd); 143 1.1 pgoyette fprintf(d, "]"); 144 1.1 pgoyette break; 145 1.1 pgoyette case OBACK_: 146 1.6 christos fprintf(d, "(\\<%u>", opnd); 147 1.1 pgoyette break; 148 1.1 pgoyette case O_BACK: 149 1.6 christos fprintf(d, "<%u>\\)", opnd); 150 1.1 pgoyette break; 151 1.1 pgoyette case OPLUS_: 152 1.1 pgoyette fprintf(d, "(+"); 153 1.1 pgoyette if (OP(*(s+opnd)) != O_PLUS) 154 1.6 christos fprintf(d, "<%u>", opnd); 155 1.1 pgoyette break; 156 1.1 pgoyette case O_PLUS: 157 1.1 pgoyette if (OP(*(s-opnd)) != OPLUS_) 158 1.6 christos fprintf(d, "<%u>", opnd); 159 1.1 pgoyette fprintf(d, "+)"); 160 1.1 pgoyette break; 161 1.1 pgoyette case OQUEST_: 162 1.1 pgoyette fprintf(d, "(?"); 163 1.1 pgoyette if (OP(*(s+opnd)) != O_QUEST) 164 1.6 christos fprintf(d, "<%u>", opnd); 165 1.1 pgoyette break; 166 1.1 pgoyette case O_QUEST: 167 1.1 pgoyette if (OP(*(s-opnd)) != OQUEST_) 168 1.6 christos fprintf(d, "<%u>", opnd); 169 1.1 pgoyette fprintf(d, "?)"); 170 1.1 pgoyette break; 171 1.1 pgoyette case OLPAREN: 172 1.6 christos fprintf(d, "((<%u>", opnd); 173 1.1 pgoyette break; 174 1.1 pgoyette case ORPAREN: 175 1.6 christos fprintf(d, "<%u>))", opnd); 176 1.1 pgoyette break; 177 1.1 pgoyette case OCH_: 178 1.1 pgoyette fprintf(d, "<"); 179 1.6 christos if (OP(*(s+opnd)) != OOR2) 180 1.6 christos fprintf(d, "<%u>", opnd); 181 1.1 pgoyette break; 182 1.1 pgoyette case OOR1: 183 1.6 christos if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_) 184 1.6 christos fprintf(d, "<%u>", opnd); 185 1.1 pgoyette fprintf(d, "|"); 186 1.1 pgoyette break; 187 1.1 pgoyette case OOR2: 188 1.1 pgoyette fprintf(d, "|"); 189 1.6 christos if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH) 190 1.6 christos fprintf(d, "<%u>", opnd); 191 1.1 pgoyette break; 192 1.1 pgoyette case O_CH: 193 1.6 christos if (OP(*(s-opnd)) != OOR1) 194 1.6 christos fprintf(d, "<%u>", opnd); 195 1.1 pgoyette fprintf(d, ">"); 196 1.1 pgoyette break; 197 1.1 pgoyette default: 198 1.6 christos fprintf(d, "!%u(%u)!", OP(*s), opnd); 199 1.1 pgoyette break; 200 1.1 pgoyette } 201 1.1 pgoyette if (!done) 202 1.1 pgoyette GAP(); 203 1.1 pgoyette } 204 1.1 pgoyette } 205 1.1 pgoyette 206 1.1 pgoyette /* 207 1.1 pgoyette * regchar - make a character printable 208 1.1 pgoyette */ 209 1.1 pgoyette static char * /* -> representation */ 210 1.1 pgoyette regchar(int ch) 211 1.1 pgoyette { 212 1.1 pgoyette static char buf[10]; 213 1.1 pgoyette 214 1.1 pgoyette if (isprint(ch) || ch == ' ') 215 1.1 pgoyette sprintf(buf, "%c", ch); 216 1.1 pgoyette else 217 1.1 pgoyette sprintf(buf, "\\%o", ch); 218 1.1 pgoyette return(buf); 219 1.1 pgoyette } 220 1.4 christos #endif 221