/* * 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 * from defects in it. * * 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. */ #include #include /* build test strings in ++jrb */ char *test_input[] = { "abc abc y & abc", "abc xbc n - -", "abc axc n - -", "abc abx n - -", "abc xabcy y & abc", "abc ababc y & abc", "ab*c abc y & abc", "ab*bc abc y & abc", "ab*bc abbc y & abbc", "ab*bc abbbbc y & abbbbc", "ab+bc abbc y & abbc", "ab+bc abc n - -", "ab+bc abq n - -", "ab+bc abbbbc y & abbbbc", "ab?bc abbc y & abbc", "ab?bc abc y & abc", "ab?bc abbbbc n - -", "ab?c abc y & abc", "^abc$ abc y & abc", "^abc$ abcc n - -", "^abc abcc y & abc", "^abc$ aabc n - -", "abc$ aabc y & abc", "^ abc y & ", "$ abc y & ", "a.c abc y & abc", "a.c axc y & axc", "a.*c axyzc y & axyzc", "a.*c axyzd n - -", "a[bc]d abc n - -", "a[bc]d abd y & abd", "a[b-d]e abd n - -", "a[b-d]e ace y & ace", "a[b-d] aac y & ac", "a[-b] a- y & a-", "a[b-] a- y & a-", "a[b-a] - c - -", "a[]b - c - -", "a[ - c - -", "a] a] y & a]", "a[]]b a]b y & a]b", "a[^bc]d aed y & aed", "a[^bc]d abd n - -", "a[^-b]c adc y & adc", "a[^-b]c a-c n - -", "a[^]b]c a]c n - -", "a[^]b]c adc y & adc", "ab|cd abc y & ab", "ab|cd abcd y & ab", "()ef def y &-\\1 ef-", "()* - c - -", "*a - c - -", "^* - c - -", "$* - c - -", "(*)b - c - -", "$b b n - -", "a\\ - c - -", "a\\(b a(b y &-\\1 a(b-", "a\\(*b ab y & ab", "a\\(*b a((b y & a((b", "a\\\\b a\\b y & a\\b", "abc) - c - -", "(abc - c - -", "((a)) abc y &-\\1-\\2 a-a-a", "(a)b(c) abc y &-\\1-\\2 abc-a-c", "a+b+c aabbabc y & abc", "a** - c - -", "a*? - c - -", "(a*)* - c - -", "(a*)+ - c - -", "(a|)* - c - -", "(a*|b)* - c - -", "(a+|b)* ab y &-\\1 ab-b", "(a+|b)+ ab y &-\\1 ab-b", "(a+|b)? ab y &-\\1 a-a", "[^ab]* cde y & cde", "(^)* - c - -", "(ab|)* - c - -", ")( - c - -", " abc y & ", "abc n - -", "a* y & ", "([abc])*d abbbcd y &-\\1 abbbcd-c", "([abc])*bcd abcd y &-\\1 abcd-a", "a|b|c|d|e e y & e", "(a|b|c|d|e)f ef y &-\\1 ef-e", "((a*|b))* - c - -", "abcd*efg abcdefg y & abcdefg", "ab* xabyabbbz y & ab", "ab* xayabbbz y & a", "(ab|cd)e abcde y &-\\1 cde-cd", "[abhgefdc]ij hij y & hij", "^(ab|cd)e abcde n x\\1y xy", "(abc|)ef abcdef y &-\\1 ef-", "(a|b)c*d abcd y &-\\1 bcd-b", "(ab|ab*)bc abc y &-\\1 abc-a", "a([bc]*)c* abc y &-\\1 abc-bc", "a([bc]*)(c*d) abcd y &-\\1-\\2 abcd-bc-d", "a([bc]+)(c*d) abcd y &-\\1-\\2 abcd-bc-d", "a([bc]*)(c+d) abcd y &-\\1-\\2 abcd-b-cd", "a[bcd]*dcdcde adcdcde y & adcdcde", "a[bcd]+dcdcde adcdcde n - -", "(ab|a)b*c abc y &-\\1 abc-ab", "((a)(b)c)(d) abcd y \\1-\\2-\\3-\\4 abc-a-b-d", "[a-zA-Z_][a-zA-Z0-9_]* alpha y & alpha", "^a(bc+|b[eh])g|.h$ abh y &-\\1 bh-", "(bc+d$|ef*g.|h?i(j|k)) effgz y &-\\1-\\2 effgz-effgz-", "(bc+d$|ef*g.|h?i(j|k)) ij y &-\\1-\\2 ij-ij-j", "(bc+d$|ef*g.|h?i(j|k)) effg n - -", "(bc+d$|ef*g.|h?i(j|k)) bcdd n - -", "(bc+d$|ef*g.|h?i(j|k)) reffgz y &-\\1-\\2 effgz-effgz-", "((((((((((a)))))))))) - c - -", "(((((((((a))))))))) a y & a", "multiple words of text uh-uh n - -", "multiple words multiple words, yeah y & multiple words", "(.*)c(.*) abcde y &-\\1-\\2 abcde-ab-de", "\\((.*), (.*)\\) (a, b) y (\\2, \\1) (b, a)", "abcd abcd y &-\\&-\\\\& abcd-&-\\abcd", "a(bc)d abcd y \\1-\\\\1-\\\\\\1 bc-\\1-\\bc", "[ -~]* abc y & abc", "[ -~ -~]* abc y & abc", "[ -~ -~ -~]* abc y & abc", "[ -~ -~ -~ -~]* abc y & abc", "[ -~ -~ -~ -~ -~]* abc y & abc", "[ -~ -~ -~ -~ -~ -~]* abc y & abc", "[ -~ -~ -~ -~ -~ -~ -~]* abc y & abc", (char *)NULL }; #ifdef ERRAVAIL char *progname; extern char *mkprogname(); #endif #ifdef DEBUG extern int regnarrate; #endif char buf[BUFSIZ]; int errreport = 0; /* Report errors via errseen? */ char *errseen = NULL; /* Error message. */ int status = 0; /* Exit status. */ /* ARGSUSED */ int main(argc, argv) int argc; char *argv[]; { regexp *r; int i; #ifdef ERRAVAIL progname = mkprogname(argv[0]); #endif if (argc == 1) { multiple(); return(status); } r = regcomp(argv[1]); if (r == NULL) error("regcomp failure", ""); #ifdef DEBUG regdump(r); if (argc > 4) regnarrate++; #endif if (argc > 2) { i = regexec(r, argv[2], 1); printf("%d", i); for (i = 1; i < NSUBEXP; i++) if (r->startp[i] != NULL && r->endp[i] != NULL) printf(" \\%d", i); printf("\n"); } if (argc > 3) { regsub(r, argv[3], buf); printf("%s\n", buf); } return(status); } void regerror(s) char *s; { if (errreport) errseen = s; else error(s, ""); } #ifndef ERRAVAIL error(s1, s2) char *s1; char *s2; { fprintf(stderr, "regexp: "); fprintf(stderr, s1, s2); fprintf(stderr, "\n"); exit(1); } #endif int lineno; regexp badregexp; /* Implicit init to 0. */ multiple() { char *rbuf; char *field[5]; char *scan; int i, j; regexp *r; extern char *strchr(); errreport = 1; lineno = 0; for(rbuf = test_input[0], j = 0; rbuf; rbuf = test_input[++j]) { lineno++; scan = rbuf; for (i = 0; i < 5; i++) { field[i] = scan; if (field[i] == NULL) { complain("bad testfile format", ""); exit(1); } scan = strchr(scan, '\t'); if (scan != NULL) *scan++ = '\0'; } try(field); } /* And finish up with some internal testing... */ lineno = 9990; errseen = NULL; if (regcomp((char *)NULL) != NULL || errseen == NULL) complain("regcomp(NULL) doesn't complain", ""); lineno = 9991; errseen = NULL; if (regexec((regexp *)NULL, "foo", 1) || errseen == NULL) complain("regexec(NULL, ...) doesn't complain", ""); lineno = 9992; r = regcomp("foo"); if (r == NULL) { complain("regcomp(\"foo\") fails", ""); return; } lineno = 9993; errseen = NULL; if (regexec(r, (char *)NULL, 1) || errseen == NULL) complain("regexec(..., NULL) doesn't complain", ""); lineno = 9994; errseen = NULL; regsub((regexp *)NULL, "foo", rbuf); if (errseen == NULL) complain("regsub(NULL, ..., ...) doesn't complain", ""); lineno = 9995; errseen = NULL; regsub(r, (char *)NULL, rbuf); if (errseen == NULL) complain("regsub(..., NULL, ...) doesn't complain", ""); lineno = 9996; errseen = NULL; regsub(r, "foo", (char *)NULL); if (errseen == NULL) complain("regsub(..., ..., NULL) doesn't complain", ""); lineno = 9997; errseen = NULL; if (regexec(&badregexp, "foo", 1) || errseen == NULL) complain("regexec(nonsense, ...) doesn't complain", ""); lineno = 9998; errseen = NULL; regsub(&badregexp, "foo", rbuf); if (errseen == NULL) complain("regsub(nonsense, ..., ...) doesn't complain", ""); } try(fields) char **fields; { regexp *r; char dbuf[BUFSIZ]; errseen = NULL; r = regcomp(fields[0]); if (r == NULL) { if (*fields[2] != 'c') complain("regcomp failure in `%s'", fields[0]); return; } if (*fields[2] == 'c') { complain("unexpected regcomp success in `%s'", fields[0]); free((char *)r); return; } if (!regexec(r, fields[1], 1)) { if (*fields[2] != 'n') complain("regexec failure in `%s'", ""); free((char *)r); return; } if (*fields[2] == 'n') { complain("unexpected regexec success", ""); free((char *)r); return; } errseen = NULL; regsub(r, fields[3], dbuf); if (errseen != NULL) { complain("regsub complaint", ""); free((char *)r); return; } if (strcmp(dbuf, fields[4]) != 0) complain("regsub result `%s' wrong", dbuf); free((char *)r); } complain(s1, s2) char *s1; char *s2; { fprintf(stderr, "try: %d: ", lineno); fprintf(stderr, s1, s2); fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : ""); status = 1; }