xref: /original-bsd/lib/libcompat/regexp/timer.c (revision 0cbf4461)
1*0cbf4461Sbostic /*
2*0cbf4461Sbostic  * Simple timing program for regcomp().
3*0cbf4461Sbostic  *
4*0cbf4461Sbostic  *	Copyright (c) 1986 by University of Toronto.
5*0cbf4461Sbostic  *	Written by Henry Spencer.  Not derived from licensed software.
6*0cbf4461Sbostic  *
7*0cbf4461Sbostic  *	Permission is granted to anyone to use this software for any
8*0cbf4461Sbostic  *	purpose on any computer system, and to redistribute it freely,
9*0cbf4461Sbostic  *	subject to the following restrictions:
10*0cbf4461Sbostic  *
11*0cbf4461Sbostic  *	1. The author is not responsible for the consequences of use of
12*0cbf4461Sbostic  *		this software, no matter how awful, even if they arise
13*0cbf4461Sbostic  *		from defects in it.
14*0cbf4461Sbostic  *
15*0cbf4461Sbostic  *	2. The origin of this software must not be misrepresented, either
16*0cbf4461Sbostic  *		by explicit claim or by omission.
17*0cbf4461Sbostic  *
18*0cbf4461Sbostic  *	3. Altered versions must be plainly marked as such, and must not
19*0cbf4461Sbostic  *		be misrepresented as being the original software.
20*0cbf4461Sbostic  *
21*0cbf4461Sbostic  * Usage: timer ncomp nexec nsub
22*0cbf4461Sbostic  *	or
23*0cbf4461Sbostic  *	timer ncomp nexec nsub regexp string [ answer [ sub ] ]
24*0cbf4461Sbostic  *
25*0cbf4461Sbostic  * The second form is for timing repetitions of a single test case.
26*0cbf4461Sbostic  * The first form's test data is a compiled-in copy of the "tests" file.
27*0cbf4461Sbostic  * Ncomp, nexec, nsub are how many times to do each regcomp, regexec,
28*0cbf4461Sbostic  * and regsub.  The way to time an operation individually is to do something
29*0cbf4461Sbostic  * like "timer 1 50 1".
30*0cbf4461Sbostic  */
31*0cbf4461Sbostic #include <stdio.h>
32*0cbf4461Sbostic 
33*0cbf4461Sbostic struct try {
34*0cbf4461Sbostic 	char *re, *str, *ans, *src, *dst;
35*0cbf4461Sbostic } tests[] = {
36*0cbf4461Sbostic #include "timer.t.h"
37*0cbf4461Sbostic { NULL, NULL, NULL, NULL, NULL }
38*0cbf4461Sbostic };
39*0cbf4461Sbostic 
40*0cbf4461Sbostic #include <regexp.h>
41*0cbf4461Sbostic 
42*0cbf4461Sbostic int errreport = 0;		/* Report errors via errseen? */
43*0cbf4461Sbostic char *errseen = NULL;		/* Error message. */
44*0cbf4461Sbostic 
45*0cbf4461Sbostic char *progname;
46*0cbf4461Sbostic 
47*0cbf4461Sbostic /* ARGSUSED */
main(argc,argv)48*0cbf4461Sbostic main(argc, argv)
49*0cbf4461Sbostic int argc;
50*0cbf4461Sbostic char *argv[];
51*0cbf4461Sbostic {
52*0cbf4461Sbostic 	int ncomp, nexec, nsub;
53*0cbf4461Sbostic 	struct try one;
54*0cbf4461Sbostic 	char dummy[512];
55*0cbf4461Sbostic 
56*0cbf4461Sbostic 	if (argc < 4) {
57*0cbf4461Sbostic 		ncomp = 1;
58*0cbf4461Sbostic 		nexec = 1;
59*0cbf4461Sbostic 		nsub = 1;
60*0cbf4461Sbostic 	} else {
61*0cbf4461Sbostic 		ncomp = atoi(argv[1]);
62*0cbf4461Sbostic 		nexec = atoi(argv[2]);
63*0cbf4461Sbostic 		nsub = atoi(argv[3]);
64*0cbf4461Sbostic 	}
65*0cbf4461Sbostic 
66*0cbf4461Sbostic 	progname = argv[0];
67*0cbf4461Sbostic 	if (argc > 5) {
68*0cbf4461Sbostic 		one.re = argv[4];
69*0cbf4461Sbostic 		one.str = argv[5];
70*0cbf4461Sbostic 		if (argc > 6)
71*0cbf4461Sbostic 			one.ans = argv[6];
72*0cbf4461Sbostic 		else
73*0cbf4461Sbostic 			one.ans = "y";
74*0cbf4461Sbostic 		if (argc > 7) {
75*0cbf4461Sbostic 			one.src = argv[7];
76*0cbf4461Sbostic 			one.dst = "xxx";
77*0cbf4461Sbostic 		} else {
78*0cbf4461Sbostic 			one.src = "x";
79*0cbf4461Sbostic 			one.dst = "x";
80*0cbf4461Sbostic 		}
81*0cbf4461Sbostic 		errreport = 1;
82*0cbf4461Sbostic 		try(one, ncomp, nexec, nsub);
83*0cbf4461Sbostic 	} else
84*0cbf4461Sbostic 		multiple(ncomp, nexec, nsub);
85*0cbf4461Sbostic 	exit(0);
86*0cbf4461Sbostic }
87*0cbf4461Sbostic 
88*0cbf4461Sbostic void
regerror(s)89*0cbf4461Sbostic regerror(s)
90*0cbf4461Sbostic char *s;
91*0cbf4461Sbostic {
92*0cbf4461Sbostic 	if (errreport)
93*0cbf4461Sbostic 		errseen = s;
94*0cbf4461Sbostic 	else
95*0cbf4461Sbostic 		error(s, "");
96*0cbf4461Sbostic }
97*0cbf4461Sbostic 
98*0cbf4461Sbostic #ifndef ERRAVAIL
error(s1,s2)99*0cbf4461Sbostic error(s1, s2)
100*0cbf4461Sbostic char *s1;
101*0cbf4461Sbostic char *s2;
102*0cbf4461Sbostic {
103*0cbf4461Sbostic 	fprintf(stderr, "regexp: ");
104*0cbf4461Sbostic 	fprintf(stderr, s1, s2);
105*0cbf4461Sbostic 	fprintf(stderr, "\n");
106*0cbf4461Sbostic 	exit(1);
107*0cbf4461Sbostic }
108*0cbf4461Sbostic #endif
109*0cbf4461Sbostic 
110*0cbf4461Sbostic int lineno = 0;
111*0cbf4461Sbostic 
multiple(ncomp,nexec,nsub)112*0cbf4461Sbostic multiple(ncomp, nexec, nsub)
113*0cbf4461Sbostic int ncomp, nexec, nsub;
114*0cbf4461Sbostic {
115*0cbf4461Sbostic 	register int i;
116*0cbf4461Sbostic 	extern char *strchr();
117*0cbf4461Sbostic 
118*0cbf4461Sbostic 	errreport = 1;
119*0cbf4461Sbostic 	for (i = 0; tests[i].re != NULL; i++) {
120*0cbf4461Sbostic 		lineno++;
121*0cbf4461Sbostic 		try(tests[i], ncomp, nexec, nsub);
122*0cbf4461Sbostic 	}
123*0cbf4461Sbostic }
124*0cbf4461Sbostic 
125*0cbf4461Sbostic try(fields, ncomp, nexec, nsub)
126*0cbf4461Sbostic struct try fields;
127*0cbf4461Sbostic int ncomp, nexec, nsub;
128*0cbf4461Sbostic {
129*0cbf4461Sbostic 	regexp *r;
130*0cbf4461Sbostic 	char dbuf[BUFSIZ];
131*0cbf4461Sbostic 	register int i;
132*0cbf4461Sbostic 
133*0cbf4461Sbostic 	errseen = NULL;
134*0cbf4461Sbostic 	r = regcomp(fields.re);
135*0cbf4461Sbostic 	if (r == NULL) {
136*0cbf4461Sbostic 		if (*fields.ans != 'c')
137*0cbf4461Sbostic 			complain("regcomp failure in `%s'", fields.re);
138*0cbf4461Sbostic 		return;
139*0cbf4461Sbostic 	}
140*0cbf4461Sbostic 	if (*fields.ans == 'c') {
141*0cbf4461Sbostic 		complain("unexpected regcomp success in `%s'", fields.re);
142*0cbf4461Sbostic 		free((char *)r);
143*0cbf4461Sbostic 		return;
144*0cbf4461Sbostic 	}
145*0cbf4461Sbostic 	for (i = ncomp-1; i > 0; i--) {
146*0cbf4461Sbostic 		free((char *)r);
147*0cbf4461Sbostic 		r = regcomp(fields.re);
148*0cbf4461Sbostic 	}
149*0cbf4461Sbostic 	if (!regexec(r, fields.str)) {
150*0cbf4461Sbostic 		if (*fields.ans != 'n')
151*0cbf4461Sbostic 			complain("regexec failure in `%s'", "");
152*0cbf4461Sbostic 		free((char *)r);
153*0cbf4461Sbostic 		return;
154*0cbf4461Sbostic 	}
155*0cbf4461Sbostic 	if (*fields.ans == 'n') {
156*0cbf4461Sbostic 		complain("unexpected regexec success", "");
157*0cbf4461Sbostic 		free((char *)r);
158*0cbf4461Sbostic 		return;
159*0cbf4461Sbostic 	}
160*0cbf4461Sbostic 	for (i = nexec-1; i > 0; i--)
161*0cbf4461Sbostic 		(void) regexec(r, fields.str);
162*0cbf4461Sbostic 	errseen = NULL;
163*0cbf4461Sbostic 	for (i = nsub; i > 0; i--)
164*0cbf4461Sbostic 		regsub(r, fields.src, dbuf);
165*0cbf4461Sbostic 	if (errseen != NULL) {
166*0cbf4461Sbostic 		complain("regsub complaint", "");
167*0cbf4461Sbostic 		free((char *)r);
168*0cbf4461Sbostic 		return;
169*0cbf4461Sbostic 	}
170*0cbf4461Sbostic 	if (strcmp(dbuf, fields.dst) != 0)
171*0cbf4461Sbostic 		complain("regsub result `%s' wrong", dbuf);
172*0cbf4461Sbostic 	free((char *)r);
173*0cbf4461Sbostic }
174*0cbf4461Sbostic 
complain(s1,s2)175*0cbf4461Sbostic complain(s1, s2)
176*0cbf4461Sbostic char *s1;
177*0cbf4461Sbostic char *s2;
178*0cbf4461Sbostic {
179*0cbf4461Sbostic 	fprintf(stderr, "try: %d: ", lineno);
180*0cbf4461Sbostic 	fprintf(stderr, s1, s2);
181*0cbf4461Sbostic 	fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
182*0cbf4461Sbostic }
183