home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / regexps.zip / try.c < prev   
C/C++ Source or Header  |  1996-09-05  |  5KB  |  221 lines

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