C:/Users/Dennis/src/lang/Life_start/Life/life-1.02/source/regexp/try.c

Go to the documentation of this file.
00001 /*
00002  * Simple test program for regexp(3) stuff.  Knows about debugging hooks.
00003  *
00004  *      Copyright (c) 1986 by University of Toronto.
00005  *      Written by Henry Spencer.  Not derived from licensed software.
00006  *
00007  *      Permission is granted to anyone to use this software for any
00008  *      purpose on any computer system, and to redistribute it freely,
00009  *      subject to the following restrictions:
00010  *
00011  *      1. The author is not responsible for the consequences of use of
00012  *              this software, no matter how awful, even if they arise
00013  *              from defects in it.
00014  *
00015  *      2. The origin of this software must not be misrepresented, either
00016  *              by explicit claim or by omission.
00017  *
00018  *      3. Altered versions must be plainly marked as such, and must not
00019  *              be misrepresented as being the original software.
00020  *
00021  * Usage: try re [string [output [-]]]
00022  * The re is compiled and dumped, regexeced against the string, the result
00023  * is applied to output using regsub().  The - triggers a running narrative
00024  * from regexec().  Dumping and narrative don't happen unless DEBUG.
00025  *
00026  * If there are no arguments, stdin is assumed to be a stream of lines with
00027  * five fields:  a r.e., a string to match it against, a result code, a
00028  * source string for regsub, and the proper result.  Result codes are 'c'
00029  * for compile failure, 'y' for match success, 'n' for match failure.
00030  * Field separator is tab.
00031  */
00032 #include <stdio.h>
00033 #include <string.h>
00034 #include "regexp.h"
00035 
00036 #ifdef ERRAVAIL
00037 char *progname;
00038 extern char *mkprogname();
00039 #endif
00040 
00041 #ifdef DEBUG
00042 extern int regnarrate;
00043 #endif
00044 
00045 char buf[BUFSIZ];
00046 
00047 int errreport = 0;              /* Report errors via errseen? */
00048 char *errseen = NULL;           /* Error message. */
00049 int status = 0;                 /* Exit status. */
00050 
00051 /* ARGSUSED */
00052 main(argc, argv)
00053 int argc;
00054 char *argv[];
00055 {
00056         regexp *r;
00057         int i;
00058 
00059 #ifdef ERRAVAIL
00060         progname = mkprogname(argv[0]);
00061 #endif
00062 
00063         if (argc == 1) {
00064                 multiple();
00065                 exit(status);
00066         }
00067 
00068         r = regcomp(argv[1]);
00069         if (r == NULL)
00070                 error("regcomp failure", "");
00071 #ifdef DEBUG
00072         regdump(r);
00073         if (argc > 4)
00074                 regnarrate++;
00075 #endif
00076         if (argc > 2) {
00077                 i = regexec(r, argv[2]);
00078                 printf("%d", i);
00079                 for (i = 1; i < NSUBEXP; i++)
00080                         if (r->startp[i] != NULL && r->endp[i] != NULL)
00081                                 printf(" \\%d", i);
00082                 printf("\n");
00083         }
00084         if (argc > 3) {
00085                 regsub(r, argv[3], buf);
00086                 printf("%s\n", buf);
00087         }
00088         exit(status);
00089 }
00090 
00091 void
00092 regerror(s)
00093 char *s;
00094 {
00095         if (errreport)
00096                 errseen = s;
00097         else
00098                 error(s, "");
00099 }
00100 
00101 #ifndef ERRAVAIL
00102 error(s1, s2)
00103 char *s1;
00104 char *s2;
00105 {
00106         fprintf(stderr, "regexp: ");
00107         fprintf(stderr, s1, s2);
00108         fprintf(stderr, "\n");
00109         exit(1);
00110 }
00111 #endif
00112 
00113 int lineno;
00114 
00115 regexp badregexp;               /* Implicit init to 0. */
00116 
00117 multiple()
00118 {
00119         char rbuf[BUFSIZ];
00120         char *field[5];
00121         char *scan;
00122         int i;
00123         regexp *r;
00124         extern char *strchr();
00125 
00126         errreport = 1;
00127         lineno = 0;
00128         while (fgets(rbuf, sizeof(rbuf), stdin) != NULL) {
00129                 rbuf[strlen(rbuf)-1] = '\0';    /* Dispense with \n. */
00130                 lineno++;
00131                 scan = rbuf;
00132                 for (i = 0; i < 5; i++) {
00133                         field[i] = scan;
00134                         if (field[i] == NULL) {
00135                                 complain("bad testfile format", "");
00136                                 exit(1);
00137                         }
00138                         scan = strchr(scan, '\t');
00139                         if (scan != NULL)
00140                                 *scan++ = '\0';
00141                 }
00142                 try(field);
00143         }
00144 
00145         /* And finish up with some internal testing... */
00146         lineno = 9990;
00147         errseen = NULL;
00148         if (regcomp((char *)NULL) != NULL || errseen == NULL)
00149                 complain("regcomp(NULL) doesn't complain", "");
00150         lineno = 9991;
00151         errseen = NULL;
00152         if (regexec((regexp *)NULL, "foo") || errseen == NULL)
00153                 complain("regexec(NULL, ...) doesn't complain", "");
00154         lineno = 9992;
00155         r = regcomp("foo");
00156         if (r == NULL) {
00157                 complain("regcomp(\"foo\") fails", "");
00158                 return;
00159         }
00160         lineno = 9993;
00161         errseen = NULL;
00162         if (regexec(r, (char *)NULL) || errseen == NULL)
00163                 complain("regexec(..., NULL) doesn't complain", "");
00164         lineno = 9994;
00165         errseen = NULL;
00166         regsub((regexp *)NULL, "foo", rbuf);
00167         if (errseen == NULL)
00168                 complain("regsub(NULL, ..., ...) doesn't complain", "");
00169         lineno = 9995;
00170         errseen = NULL;
00171         regsub(r, (char *)NULL, rbuf);
00172         if (errseen == NULL)
00173                 complain("regsub(..., NULL, ...) doesn't complain", "");
00174         lineno = 9996;
00175         errseen = NULL;
00176         regsub(r, "foo", (char *)NULL);
00177         if (errseen == NULL)
00178                 complain("regsub(..., ..., NULL) doesn't complain", "");
00179         lineno = 9997;
00180         errseen = NULL;
00181         if (regexec(&badregexp, "foo") || errseen == NULL)
00182                 complain("regexec(nonsense, ...) doesn't complain", "");
00183         lineno = 9998;
00184         errseen = NULL;
00185         regsub(&badregexp, "foo", rbuf);
00186         if (errseen == NULL)
00187                 complain("regsub(nonsense, ..., ...) doesn't complain", "");
00188 }
00189 
00190 try(fields)
00191 char **fields;
00192 {
00193         regexp *r;
00194         char dbuf[BUFSIZ];
00195 
00196         errseen = NULL;
00197         r = regcomp(fields[0]);
00198         if (r == NULL) {
00199                 if (*fields[2] != 'c')
00200                         complain("regcomp failure in `%s'", fields[0]);
00201                 return;
00202         }
00203         if (*fields[2] == 'c') {
00204                 complain("unexpected regcomp success in `%s'", fields[0]);
00205                 free((char *)r);
00206                 return;
00207         }
00208         if (!regexec(r, fields[1])) {
00209                 if (*fields[2] != 'n')
00210                         complain("regexec failure in `%s'", "");
00211                 free((char *)r);
00212                 return;
00213         }
00214         if (*fields[2] == 'n') {
00215                 complain("unexpected regexec success", "");
00216                 free((char *)r);
00217                 return;
00218         }
00219         errseen = NULL;
00220         regsub(r, fields[3], dbuf);
00221         if (errseen != NULL) {
00222                 complain("regsub complaint", "");
00223                 free((char *)r);
00224                 return;
00225         }
00226         if (strcmp(dbuf, fields[4]) != 0)
00227                 complain("regsub result `%s' wrong", dbuf);
00228         free((char *)r);
00229 }
00230 
00231 complain(s1, s2)
00232 char *s1;
00233 char *s2;
00234 {
00235         fprintf(stderr, "try: %d: ", lineno);
00236         fprintf(stderr, s1, s2);
00237         fprintf(stderr, " (%s)\n", (errseen != NULL) ? errseen : "");
00238         status = 1;
00239 }

Generated on Sat Jan 26 08:48:07 2008 for WildLife by  doxygen 1.5.4