1 /*
2 * Simple test program for regexp(3) stuff. Knows about debugging hooks.
3 *
4 * Copyright (c) 1986 by University of Toronto.
5 * Written by Henry Spencer. Not derived from licensed software.
6 *
7 * Permission is granted to anyone to use this software for any
8 * purpose on any computer system, and to redistribute it freely,
9 * subject to the following restrictions:
10 *
11 * 1. The author is not responsible for the consequences of use of
12 * this software, no matter how awful, even if they arise
13 * from defects in it.
14 *
15 * 2. The origin of this software must not be misrepresented, either
16 * by explicit claim or by omission.
17 *
18 * 3. Altered versions must be plainly marked as such, and must not
19 * be misrepresented as being the original software.
20 *
21 * Usage: try re [string [output [-]]]
22 * The re is compiled and dumped, regexeced against the string, the result
23 * is applied to output using regsub(). The - triggers a running narrative
24 * from regexec(). Dumping and narrative don't happen unless DEBUG.
25 *
26 * If there are no arguments, stdin is assumed to be a stream of lines with
27 * five fields: a r.e., a string to match it against, a result code, a
28 * source string for regsub, and the proper result. Result codes are 'c'
29 * for compile failure, 'y' for match success, 'n' for match failure.
30 * Field separator is tab.
31 */
32 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <stdio.h>
34 #include <tptregexp.h>
35
36 #ifdef ERRAVAIL
37 char *progname;
38 extern char *mkprogname();
39 #endif
40
41 #ifdef DEBUG
42 extern int regnarrate;
43 #endif
44
45 char buf[BUFSIZ];
46
47 int errreport = 0; /* Report errors via errseen? */
48 char *errseen = NULL; /* Error message. */
49 int status = 0; /* Exit status. */
50
51 /* ARGSUSED */
main(argc,argv)52 main(argc, argv)
53 int argc;
54 char *argv[];
55 {
56 regexp *r;
57 int i;
58
59 #ifdef ERRAVAIL
60 progname = mkprogname(argv[0]);
61 #endif
62
63 if (argc == 1) {
64 multiple();
65 exit(status);
66 }
67
68 r = tpt_regcomp(argv[1]);
69 if (r == NULL)
70 error("tpt_regcomp failure", "");
71 #ifdef DEBUG
72 regdump(r);
73 if (argc > 4)
74 regnarrate++;
75 #endif
76 if (argc > 2) {
77 i = tpt_regexec(r, argv[2]);
78 printf("%d", i);
79 for (i = 1; i < NSUBEXP; i++)
80 if (r->startp[i] != NULL && r->endp[i] != NULL)
81 printf(" \\%d", i);
82 printf("\n");
83 }
84 if (argc > 3) {
85 tpt_regsub(r, argv[3], buf);
86 printf("%s\n", buf);
87 }
88 exit(status);
89 }
90
91 void
tpt_regerror(s)92 tpt_regerror(s)
93 char *s;
94 {
95 if (errreport)
96 errseen = s;
97 else
98 error(s, "");
99 }
100
101 #ifndef ERRAVAIL
error(s1,s2)102 error(s1, s2)
103 char *s1;
104 char *s2;
105 {
106 fprintf(stderr, "regexp: ");
107 fprintf(stderr, s1, s2);
108 fprintf(stderr, "\n");
109 exit(1);
110 }
111 #endif
112
113 int lineno;
114
115 regexp badregexp; /* Implicit init to 0. */
116
multiple()117 multiple()
118 {
119 char rbuf[BUFSIZ];
120 char *field[5];
121 char *scan;
122 int i;
123 regexp *r;
124 extern char *strchr();
125
126 errreport = 1;
127 lineno = 0;
128 while (fgets(rbuf, sizeof(rbuf), stdin) != NULL) {
129 rbuf[strlen(rbuf)-1] = '\0'; /* Dispense with \n. */
130 lineno++;
131 scan = rbuf;
132 for (i = 0; i < 5; i++) {
133 field[i] = scan;
134 if (field[i] == NULL) {
135 complain("bad testfile format", "");
136 exit(1);
137 }
138 scan = strchr(scan, '\t');
139 if (scan != NULL)
140 *scan++ = '\0';
141 }
142 try(field);
143 }
144
145 /* And finish up with some internal testing... */
146 lineno = 9990;
147 errseen = NULL;
148 if (tpt_regcomp((char *)NULL) != NULL || errseen == NULL)
149 complain("tpt_regcomp(NULL) doesn't complain", "");
150 lineno = 9991;
151 errseen = NULL;
152 if (tpt_regexec((regexp *)NULL, "foo") || errseen == NULL)
153 complain("tpt_regexec(NULL, ...) doesn't complain", "");
154 lineno = 9992;
155 r = tpt_regcomp("foo");
156 if (r == NULL) {
157 complain("tpt_regcomp(\"foo\") fails", "");
158 return;
159 }
160 lineno = 9993;
161 errseen = NULL;
162 if (tpt_regexec(r, (char *)NULL) || errseen == NULL)
163 complain("tpt_regexec(..., NULL) doesn't complain", "");
164 lineno = 9994;
165 errseen = NULL;
166 tpt_regsub((regexp *)NULL, "foo", rbuf);
167 if (errseen == NULL)
168 complain("tpt_regsub(NULL, ..., ...) doesn't complain", "");
169 lineno = 9995;
170 errseen = NULL;
171 tpt_regsub(r, (char *)NULL, rbuf);
172 if (errseen == NULL)
173 complain("tpt_regsub(..., NULL, ...) doesn't complain", "");
174 lineno = 9996;
175 errseen = NULL;
176 tpt_regsub(r, "foo", (char *)NULL);
177 if (errseen == NULL)
178 complain("tpt_regsub(..., ..., NULL) doesn't complain", "");
179 lineno = 9997;
180 errseen = NULL;
181 if (tpt_regexec(&badregexp, "foo") || errseen == NULL)
182 complain("tpt_regexec(nonsense, ...) doesn't complain", "");
183 lineno = 9998;
184 errseen = NULL;
185 tpt_regsub(&badregexp, "foo", rbuf);
186 if (errseen == NULL)
187 complain("tpt_regsub(nonsense, ..., ...) doesn't complain", "");
188 }
189
try(fields)190 try(fields)
191 char **fields;
192 {
193 regexp *r;
194 char dbuf[BUFSIZ];
195
196 errseen = NULL;
197 r = tpt_regcomp(fields[0]);
198 if (r == NULL) {
199 if (*fields[2] != 'c')
200 complain("tpt_regcomp failure in `%s'", fields[0]);
201 return;
202 }
203 if (*fields[2] == 'c') {
204 complain("unexpected tpt_regcomp success in `%s'", fields[0]);
205 free((char *)r);
206 return;
207 }
208 if (!tpt_regexec(r, fields[1])) {
209 if (*fields[2] != 'n')
210 complain("tpt_regexec failure in `%s'", "");
211 free((char *)r);
212 return;
213 }
214 if (*fields[2] == 'n') {
215 complain("unexpected tpt_regexec success", "");
216 free((char *)r);
217 return;
218 }
219 errseen = NULL;
220 tpt_regsub(r, fields[3], dbuf);
221 if (errseen != NULL) {
222 complain("tpt_regsub complaint", "");
223 free((char *)r);
224 return;
225 }
226 if (strcmp(dbuf, fields[4]) != 0)
227 complain("tpt_regsub result `%s' wrong", dbuf);
228 free((char *)r);
229 }
230
complain(s1,s2)231 complain(s1, s2)
232 char *s1;
233 char *s2;
234 {
235 fprintf(stderr, "try: %d: ", lineno);
236 fprintf(stderr, s1, s2);
237 fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
238 status = 1;
239 }
240