home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / unix / dgrep.arc / TRY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-15  |  5.4 KB  |  231 lines

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