xref: /freebsd/lib/libc/regex/grot/debug.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
18ca5c256SDaniel C. Sobral #include <stdio.h>
28ca5c256SDaniel C. Sobral #include <string.h>
38ca5c256SDaniel C. Sobral #include <ctype.h>
48ca5c256SDaniel C. Sobral #include <limits.h>
58ca5c256SDaniel C. Sobral #include <stdlib.h>
68ca5c256SDaniel C. Sobral #include <sys/types.h>
78ca5c256SDaniel C. Sobral #include <regex.h>
8ec8b0411STim J. Robbins #include <wchar.h>
9ec8b0411STim J. Robbins #include <wctype.h>
108ca5c256SDaniel C. Sobral 
118ca5c256SDaniel C. Sobral #include "utils.h"
128ca5c256SDaniel C. Sobral #include "regex2.h"
138ca5c256SDaniel C. Sobral #include "debug.ih"
148ca5c256SDaniel C. Sobral 
158ca5c256SDaniel C. Sobral /*
168ca5c256SDaniel C. Sobral  - regprint - print a regexp for debugging
178ca5c256SDaniel C. Sobral  == void regprint(regex_t *r, FILE *d);
188ca5c256SDaniel C. Sobral  */
198ca5c256SDaniel C. Sobral void
regprint(r,d)208ca5c256SDaniel C. Sobral regprint(r, d)
218ca5c256SDaniel C. Sobral regex_t *r;
228ca5c256SDaniel C. Sobral FILE *d;
238ca5c256SDaniel C. Sobral {
248fb3f3f6SDavid E. O'Brien 	struct re_guts *g = r->re_g;
258fb3f3f6SDavid E. O'Brien 	int i;
268fb3f3f6SDavid E. O'Brien 	int c;
278fb3f3f6SDavid E. O'Brien 	int last;
288ca5c256SDaniel C. Sobral 
29ec8b0411STim J. Robbins 	fprintf(d, "%ld states", (long)g->nstates);
308ca5c256SDaniel C. Sobral 	fprintf(d, ", first %ld last %ld", (long)g->firststate,
318ca5c256SDaniel C. Sobral 						(long)g->laststate);
328ca5c256SDaniel C. Sobral 	if (g->iflags&USEBOL)
338ca5c256SDaniel C. Sobral 		fprintf(d, ", USEBOL");
348ca5c256SDaniel C. Sobral 	if (g->iflags&USEEOL)
358ca5c256SDaniel C. Sobral 		fprintf(d, ", USEEOL");
368ca5c256SDaniel C. Sobral 	if (g->iflags&BAD)
378ca5c256SDaniel C. Sobral 		fprintf(d, ", BAD");
388ca5c256SDaniel C. Sobral 	if (g->nsub > 0)
398ca5c256SDaniel C. Sobral 		fprintf(d, ", nsub=%ld", (long)g->nsub);
408ca5c256SDaniel C. Sobral 	if (g->must != NULL)
418ca5c256SDaniel C. Sobral 		fprintf(d, ", must(%ld) `%*s'", (long)g->mlen, (int)g->mlen,
428ca5c256SDaniel C. Sobral 								g->must);
438ca5c256SDaniel C. Sobral 	if (g->backrefs)
448ca5c256SDaniel C. Sobral 		fprintf(d, ", backrefs");
458ca5c256SDaniel C. Sobral 	if (g->nplus > 0)
468ca5c256SDaniel C. Sobral 		fprintf(d, ", nplus %ld", (long)g->nplus);
478ca5c256SDaniel C. Sobral 	fprintf(d, "\n");
488ca5c256SDaniel C. Sobral 	s_print(g, d);
498ca5c256SDaniel C. Sobral }
508ca5c256SDaniel C. Sobral 
518ca5c256SDaniel C. Sobral /*
528ca5c256SDaniel C. Sobral  - s_print - print the strip for debugging
538fb3f3f6SDavid E. O'Brien  == static void s_print(struct re_guts *g, FILE *d);
548ca5c256SDaniel C. Sobral  */
558ca5c256SDaniel C. Sobral static void
s_print(g,d)568ca5c256SDaniel C. Sobral s_print(g, d)
578fb3f3f6SDavid E. O'Brien struct re_guts *g;
588ca5c256SDaniel C. Sobral FILE *d;
598ca5c256SDaniel C. Sobral {
608fb3f3f6SDavid E. O'Brien 	sop *s;
618fb3f3f6SDavid E. O'Brien 	cset *cs;
628fb3f3f6SDavid E. O'Brien 	int i;
638fb3f3f6SDavid E. O'Brien 	int done = 0;
648fb3f3f6SDavid E. O'Brien 	sop opnd;
658fb3f3f6SDavid E. O'Brien 	int col = 0;
668fb3f3f6SDavid E. O'Brien 	int last;
678fb3f3f6SDavid E. O'Brien 	sopno offset = 2;
688ca5c256SDaniel C. Sobral #	define	GAP()	{	if (offset % 5 == 0) { \
698ca5c256SDaniel C. Sobral 					if (col > 40) { \
708ca5c256SDaniel C. Sobral 						fprintf(d, "\n\t"); \
718ca5c256SDaniel C. Sobral 						col = 0; \
728ca5c256SDaniel C. Sobral 					} else { \
738ca5c256SDaniel C. Sobral 						fprintf(d, " "); \
748ca5c256SDaniel C. Sobral 						col++; \
758ca5c256SDaniel C. Sobral 					} \
768ca5c256SDaniel C. Sobral 				} else \
778ca5c256SDaniel C. Sobral 					col++; \
788ca5c256SDaniel C. Sobral 				offset++; \
798ca5c256SDaniel C. Sobral 			}
808ca5c256SDaniel C. Sobral 
818ca5c256SDaniel C. Sobral 	if (OP(g->strip[0]) != OEND)
828ca5c256SDaniel C. Sobral 		fprintf(d, "missing initial OEND!\n");
838ca5c256SDaniel C. Sobral 	for (s = &g->strip[1]; !done; s++) {
848ca5c256SDaniel C. Sobral 		opnd = OPND(*s);
858ca5c256SDaniel C. Sobral 		switch (OP(*s)) {
868ca5c256SDaniel C. Sobral 		case OEND:
878ca5c256SDaniel C. Sobral 			fprintf(d, "\n");
888ca5c256SDaniel C. Sobral 			done = 1;
898ca5c256SDaniel C. Sobral 			break;
908ca5c256SDaniel C. Sobral 		case OCHAR:
918ca5c256SDaniel C. Sobral 			if (strchr("\\|()^$.[+*?{}!<> ", (char)opnd) != NULL)
928ca5c256SDaniel C. Sobral 				fprintf(d, "\\%c", (char)opnd);
938ca5c256SDaniel C. Sobral 			else
948ca5c256SDaniel C. Sobral 				fprintf(d, "%s", regchar((char)opnd));
958ca5c256SDaniel C. Sobral 			break;
968ca5c256SDaniel C. Sobral 		case OBOL:
978ca5c256SDaniel C. Sobral 			fprintf(d, "^");
988ca5c256SDaniel C. Sobral 			break;
998ca5c256SDaniel C. Sobral 		case OEOL:
1008ca5c256SDaniel C. Sobral 			fprintf(d, "$");
1018ca5c256SDaniel C. Sobral 			break;
1028ca5c256SDaniel C. Sobral 		case OBOW:
1038ca5c256SDaniel C. Sobral 			fprintf(d, "\\{");
1048ca5c256SDaniel C. Sobral 			break;
1058ca5c256SDaniel C. Sobral 		case OEOW:
1068ca5c256SDaniel C. Sobral 			fprintf(d, "\\}");
1078ca5c256SDaniel C. Sobral 			break;
1088ca5c256SDaniel C. Sobral 		case OANY:
1098ca5c256SDaniel C. Sobral 			fprintf(d, ".");
1108ca5c256SDaniel C. Sobral 			break;
1118ca5c256SDaniel C. Sobral 		case OANYOF:
1128ca5c256SDaniel C. Sobral 			fprintf(d, "[(%ld)", (long)opnd);
113ec8b0411STim J. Robbins #if 0
1148ca5c256SDaniel C. Sobral 			cs = &g->sets[opnd];
1158ca5c256SDaniel C. Sobral 			last = -1;
1168ca5c256SDaniel C. Sobral 			for (i = 0; i < g->csetsize+1; i++)	/* +1 flushes */
1178ca5c256SDaniel C. Sobral 				if (CHIN(cs, i) && i < g->csetsize) {
1188ca5c256SDaniel C. Sobral 					if (last < 0) {
1198ca5c256SDaniel C. Sobral 						fprintf(d, "%s", regchar(i));
1208ca5c256SDaniel C. Sobral 						last = i;
1218ca5c256SDaniel C. Sobral 					}
1228ca5c256SDaniel C. Sobral 				} else {
1238ca5c256SDaniel C. Sobral 					if (last >= 0) {
1248ca5c256SDaniel C. Sobral 						if (last != i-1)
1258ca5c256SDaniel C. Sobral 							fprintf(d, "-%s",
1268ca5c256SDaniel C. Sobral 								regchar(i-1));
1278ca5c256SDaniel C. Sobral 						last = -1;
1288ca5c256SDaniel C. Sobral 					}
1298ca5c256SDaniel C. Sobral 				}
130ec8b0411STim J. Robbins #endif
1318ca5c256SDaniel C. Sobral 			fprintf(d, "]");
1328ca5c256SDaniel C. Sobral 			break;
1338ca5c256SDaniel C. Sobral 		case OBACK_:
1348ca5c256SDaniel C. Sobral 			fprintf(d, "(\\<%ld>", (long)opnd);
1358ca5c256SDaniel C. Sobral 			break;
1368ca5c256SDaniel C. Sobral 		case O_BACK:
1378ca5c256SDaniel C. Sobral 			fprintf(d, "<%ld>\\)", (long)opnd);
1388ca5c256SDaniel C. Sobral 			break;
1398ca5c256SDaniel C. Sobral 		case OPLUS_:
1408ca5c256SDaniel C. Sobral 			fprintf(d, "(+");
1418ca5c256SDaniel C. Sobral 			if (OP(*(s+opnd)) != O_PLUS)
1428ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1438ca5c256SDaniel C. Sobral 			break;
1448ca5c256SDaniel C. Sobral 		case O_PLUS:
1458ca5c256SDaniel C. Sobral 			if (OP(*(s-opnd)) != OPLUS_)
1468ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1478ca5c256SDaniel C. Sobral 			fprintf(d, "+)");
1488ca5c256SDaniel C. Sobral 			break;
1498ca5c256SDaniel C. Sobral 		case OQUEST_:
1508ca5c256SDaniel C. Sobral 			fprintf(d, "(?");
1518ca5c256SDaniel C. Sobral 			if (OP(*(s+opnd)) != O_QUEST)
1528ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1538ca5c256SDaniel C. Sobral 			break;
1548ca5c256SDaniel C. Sobral 		case O_QUEST:
1558ca5c256SDaniel C. Sobral 			if (OP(*(s-opnd)) != OQUEST_)
1568ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1578ca5c256SDaniel C. Sobral 			fprintf(d, "?)");
1588ca5c256SDaniel C. Sobral 			break;
1598ca5c256SDaniel C. Sobral 		case OLPAREN:
1608ca5c256SDaniel C. Sobral 			fprintf(d, "((<%ld>", (long)opnd);
1618ca5c256SDaniel C. Sobral 			break;
1628ca5c256SDaniel C. Sobral 		case ORPAREN:
1638ca5c256SDaniel C. Sobral 			fprintf(d, "<%ld>))", (long)opnd);
1648ca5c256SDaniel C. Sobral 			break;
1658ca5c256SDaniel C. Sobral 		case OCH_:
1668ca5c256SDaniel C. Sobral 			fprintf(d, "<");
1678ca5c256SDaniel C. Sobral 			if (OP(*(s+opnd)) != OOR2)
1688ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1698ca5c256SDaniel C. Sobral 			break;
1708ca5c256SDaniel C. Sobral 		case OOR1:
1718ca5c256SDaniel C. Sobral 			if (OP(*(s-opnd)) != OOR1 && OP(*(s-opnd)) != OCH_)
1728ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1738ca5c256SDaniel C. Sobral 			fprintf(d, "|");
1748ca5c256SDaniel C. Sobral 			break;
1758ca5c256SDaniel C. Sobral 		case OOR2:
1768ca5c256SDaniel C. Sobral 			fprintf(d, "|");
1778ca5c256SDaniel C. Sobral 			if (OP(*(s+opnd)) != OOR2 && OP(*(s+opnd)) != O_CH)
1788ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1798ca5c256SDaniel C. Sobral 			break;
1808ca5c256SDaniel C. Sobral 		case O_CH:
1818ca5c256SDaniel C. Sobral 			if (OP(*(s-opnd)) != OOR1)
1828ca5c256SDaniel C. Sobral 				fprintf(d, "<%ld>", (long)opnd);
1838ca5c256SDaniel C. Sobral 			fprintf(d, ">");
1848ca5c256SDaniel C. Sobral 			break;
1858ca5c256SDaniel C. Sobral 		default:
186*b20e9c5fSEnji Cooper 			fprintf(d, "!%ld(%ld)!", OP(*s), (long)opnd);
1878ca5c256SDaniel C. Sobral 			break;
1888ca5c256SDaniel C. Sobral 		}
1898ca5c256SDaniel C. Sobral 		if (!done)
1908ca5c256SDaniel C. Sobral 			GAP();
1918ca5c256SDaniel C. Sobral 	}
1928ca5c256SDaniel C. Sobral }
1938ca5c256SDaniel C. Sobral 
1948ca5c256SDaniel C. Sobral /*
1958ca5c256SDaniel C. Sobral  - regchar - make a character printable
1968ca5c256SDaniel C. Sobral  == static char *regchar(int ch);
1978ca5c256SDaniel C. Sobral  */
1988ca5c256SDaniel C. Sobral static char *			/* -> representation */
regchar(ch)1998ca5c256SDaniel C. Sobral regchar(ch)
2008ca5c256SDaniel C. Sobral int ch;
2018ca5c256SDaniel C. Sobral {
2028ca5c256SDaniel C. Sobral 	static char buf[10];
2038ca5c256SDaniel C. Sobral 
2048ca5c256SDaniel C. Sobral 	if (isprint(ch) || ch == ' ')
2058ca5c256SDaniel C. Sobral 		sprintf(buf, "%c", ch);
2068ca5c256SDaniel C. Sobral 	else
2078ca5c256SDaniel C. Sobral 		sprintf(buf, "\\%o", ch);
2088ca5c256SDaniel C. Sobral 	return(buf);
2098ca5c256SDaniel C. Sobral }
210