* Simple test program for regexp(3) stuff. Knows about debugging hooks.
* Copyright (c) 1986 by University of Toronto.
* Written by Henry Spencer. Not derived from licensed software.
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
* Usage: try re [string [output [-]]]
* The re is compiled and dumped, regexeced against the string, the result
* is applied to output using regsub(). The - triggers a running narrative
* from regexec(). Dumping and narrative don't happen unless DEBUG.
* If there are no arguments, stdin is assumed to be a stream of lines with
* five fields: a r.e., a string to match it against, a result code, a
* source string for regsub, and the proper result. Result codes are 'c'
* for compile failure, 'y' for match success, 'n' for match failure.
* Field separator is tab.
extern char *mkprogname();
int errreport
= 0; /* Report errors via errseen? */
char *errseen
= NULL
; /* Error message. */
int status
= 0; /* Exit status. */
progname
= mkprogname(argv
[0]);
error("regcomp failure", "");
for (i
= 1; i
< NSUBEXP
; i
++)
if (r
->startp
[i
] != NULL
&& r
->endp
[i
] != NULL
)
fprintf(stderr
, "regexp: ");
regexp badregexp
; /* Implicit init to 0. */
while (fgets(rbuf
, sizeof(rbuf
), stdin
) != NULL
) {
rbuf
[strlen(rbuf
)-1] = '\0'; /* Dispense with \n. */
for (i
= 0; i
< 5; i
++) {
complain("bad testfile format", "");
scan
= strchr(scan
, '\t');
/* And finish up with some internal testing... */
if (regcomp((char *)NULL
) != NULL
|| errseen
== NULL
)
complain("regcomp(NULL) doesn't complain", "");
if (regexec((regexp
*)NULL
, "foo") || errseen
== NULL
)
complain("regexec(NULL, ...) doesn't complain", "");
complain("regcomp(\"foo\") fails", "");
if (regexec(r
, (char *)NULL
) || errseen
== NULL
)
complain("regexec(..., NULL) doesn't complain", "");
regsub((regexp
*)NULL
, "foo", rbuf
);
complain("regsub(NULL, ..., ...) doesn't complain", "");
regsub(r
, (char *)NULL
, rbuf
);
complain("regsub(..., NULL, ...) doesn't complain", "");
regsub(r
, "foo", (char *)NULL
);
complain("regsub(..., ..., NULL) doesn't complain", "");
if (regexec(&badregexp
, "foo") || errseen
== NULL
)
complain("regexec(nonsense, ...) doesn't complain", "");
regsub(&badregexp
, "foo", rbuf
);
complain("regsub(nonsense, ..., ...) doesn't complain", "");
complain("regcomp failure in `%s'", fields
[0]);
complain("unexpected regcomp success in `%s'", fields
[0]);
if (!regexec(r
, fields
[1])) {
complain("regexec failure in `%s'", "");
complain("unexpected regexec success", "");
regsub(r
, fields
[3], dbuf
);
complain("regsub complaint", "");
if (strcmp(dbuf
, fields
[4]) != 0)
complain("regsub result `%s' wrong", dbuf
);
fprintf(stderr
, "try: %d: ", lineno
);
fprintf(stderr
, " (%s)\n", (errseen
!= NULL
) ? errseen
: "");