2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: try.c /main/3 1996/06/19 17:13:58 drk $ */
25 * Simple test program for regexp(3) stuff. Knows about debugging hooks.
27 * Copyright (c) 1986 by University of Toronto.
28 * Written by Henry Spencer. Not derived from licensed software.
30 * Permission is granted to anyone to use this software for any
31 * purpose on any computer system, and to redistribute it freely,
32 * subject to the following restrictions:
34 * 1. The author is not responsible for the consequences of use of
35 * this software, no matter how awful, even if they arise
38 * 2. The origin of this software must not be misrepresented, either
39 * by explicit claim or by omission.
41 * 3. Altered versions must be plainly marked as such, and must not
42 * be misrepresented as being the original software.
44 * Usage: try re [string [output [-]]]
45 * The re is compiled and dumped, regexeced against the string, the result
46 * is applied to output using regsub(). The - triggers a running narrative
47 * from regexec(). Dumping and narrative don't happen unless DEBUG.
49 * If there are no arguments, stdin is assumed to be a stream of lines with
50 * five fields: a r.e., a string to match it against, a result code, a
51 * source string for regsub, and the proper result. Result codes are 'c'
52 * for compile failure, 'y' for match success, 'n' for match failure.
53 * Field separator is tab.
56 #include <tptregexp.h>
60 extern char *mkprogname();
64 extern int regnarrate;
69 int errreport = 0; /* Report errors via errseen? */
70 char *errseen = NULL; /* Error message. */
71 int status = 0; /* Exit status. */
82 progname = mkprogname(argv[0]);
90 r = tpt_regcomp(argv[1]);
92 error("tpt_regcomp failure", "");
99 i = tpt_regexec(r, argv[2]);
101 for (i = 1; i < NSUBEXP; i++)
102 if (r->startp[i] != NULL && r->endp[i] != NULL)
107 tpt_regsub(r, argv[3], buf);
128 fprintf(stderr, "regexp: ");
129 fprintf(stderr, s1, s2);
130 fprintf(stderr, "\n");
137 regexp badregexp; /* Implicit init to 0. */
146 extern char *strchr();
150 while (fgets(rbuf, sizeof(rbuf), stdin) != NULL) {
151 rbuf[strlen(rbuf)-1] = '\0'; /* Dispense with \n. */
154 for (i = 0; i < 5; i++) {
156 if (field[i] == NULL) {
157 complain("bad testfile format", "");
160 scan = strchr(scan, '\t');
167 /* And finish up with some internal testing... */
170 if (tpt_regcomp((char *)NULL) != NULL || errseen == NULL)
171 complain("tpt_regcomp(NULL) doesn't complain", "");
174 if (tpt_regexec((regexp *)NULL, "foo") || errseen == NULL)
175 complain("tpt_regexec(NULL, ...) doesn't complain", "");
177 r = tpt_regcomp("foo");
179 complain("tpt_regcomp(\"foo\") fails", "");
184 if (tpt_regexec(r, (char *)NULL) || errseen == NULL)
185 complain("tpt_regexec(..., NULL) doesn't complain", "");
188 tpt_regsub((regexp *)NULL, "foo", rbuf);
190 complain("tpt_regsub(NULL, ..., ...) doesn't complain", "");
193 tpt_regsub(r, (char *)NULL, rbuf);
195 complain("tpt_regsub(..., NULL, ...) doesn't complain", "");
198 tpt_regsub(r, "foo", (char *)NULL);
200 complain("tpt_regsub(..., ..., NULL) doesn't complain", "");
203 if (tpt_regexec(&badregexp, "foo") || errseen == NULL)
204 complain("tpt_regexec(nonsense, ...) doesn't complain", "");
207 tpt_regsub(&badregexp, "foo", rbuf);
209 complain("tpt_regsub(nonsense, ..., ...) doesn't complain", "");
219 r = tpt_regcomp(fields[0]);
221 if (*fields[2] != 'c')
222 complain("tpt_regcomp failure in `%s'", fields[0]);
225 if (*fields[2] == 'c') {
226 complain("unexpected tpt_regcomp success in `%s'", fields[0]);
230 if (!tpt_regexec(r, fields[1])) {
231 if (*fields[2] != 'n')
232 complain("tpt_regexec failure in `%s'", "");
236 if (*fields[2] == 'n') {
237 complain("unexpected tpt_regexec success", "");
242 tpt_regsub(r, fields[3], dbuf);
243 if (errseen != NULL) {
244 complain("tpt_regsub complaint", "");
248 if (strcmp(dbuf, fields[4]) != 0)
249 complain("tpt_regsub result `%s' wrong", dbuf);
257 fprintf(stderr, "try: %d: ", lineno);
258 fprintf(stderr, s1, s2);
259 fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");