home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d179 / regexp.lha / Regexp / try.c < prev   
C/C++ Source or Header  |  1989-02-25  |  6KB  |  237 lines

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