home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #1 / MONSTER.ISO / prog / gen / regex011.taz / regex011 / regex-0.11 / test / iregex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-08  |  2.8 KB  |  157 lines

  1. /* Main program for interactive testing.  For maximum output, compile
  2.    this and regex.c with -DDEBUG.  */
  3.  
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include "regex.h"
  7.  
  8. #define BYTEWIDTH 8
  9.  
  10. extern void printchar ();
  11. extern char upcase[];
  12.  
  13. static void scanstring ();
  14. static void print_regs ();
  15.  
  16. int
  17. main (argc, argv)
  18.      int argc;
  19.      char **argv;
  20. {
  21.   int i;
  22.   struct re_pattern_buffer buf;
  23.   char fastmap[(1 << BYTEWIDTH)];
  24.  
  25.   /* Allow a command argument to specify the style of syntax.  You can
  26.      use the `syntax' program to decode integer syntax values.  */
  27.   if (argc > 1)
  28.     re_set_syntax (atoi (argv[1]));
  29.  
  30.   buf.allocated = 0;
  31.   buf.buffer = NULL;
  32.   buf.fastmap = fastmap;
  33.   buf.translate = upcase;
  34.  
  35.   for (;;)
  36.     {
  37.       char c;
  38.       char pat[500] = "", str[500];
  39.       struct re_registers regs;
  40.  
  41.       printf ("Pattern (%s) = ", pat);
  42.       gets (pat);
  43.  
  44.       if (feof (stdin))
  45.         {
  46.           putchar ('\n');
  47.           exit (0);
  48.     }
  49.  
  50.       if (*pat)
  51.     {
  52.           re_compile_pattern (pat, strlen (pat), &buf);
  53.       re_compile_fastmap (&buf);
  54. #ifdef DEBUG
  55.       print_compiled_pattern (&buf);
  56. #endif
  57.     }
  58.  
  59.       printf ("String = ");
  60.       gets (str);    /* Now read the string to match against */
  61.       scanstring (str);
  62.  
  63.       i = re_match (&buf, str, strlen (str), 0, ®s);
  64.       printf ("Match value  %d.\t", i);
  65.       print_regs (regs);
  66.       
  67.       i = re_search (&buf, str, strlen (str), 0, strlen (str), ®s);
  68.       printf ("Search value %d.\t", i);
  69.       print_regs (regs);
  70.     }
  71.  
  72.   /* We never get here, but what the heck.  */
  73.   return 0;
  74. }
  75.  
  76. void
  77. scanstring (s)
  78.      char *s;
  79. {
  80.   char *write = s;
  81.  
  82.   while (*s != '\0')
  83.     {
  84.       if (*s == '\\')
  85.     {
  86.       s++;
  87.  
  88.       switch (*s)
  89.         {
  90.         case '\0':
  91.           break;
  92.  
  93.         case '0': case '1': case '2': case '3': case '4':
  94.         case '5': case '6': case '7': case '8': case '9':
  95.           *write = *s++ - '0';
  96.  
  97.           if ('0' <= *s && *s <= '9')
  98.         {
  99.           *write = (*write << 3) + (*s++ - '0');
  100.           if ('0' <= *s && *s <= '9')
  101.             *write = (*write << 3) + (*s++ - '0');
  102.         }
  103.           write++;
  104.           break;
  105.  
  106.         case 'n':
  107.           *write++ = '\n';
  108.           s++;
  109.           break;
  110.  
  111.         case 't':
  112.           *write++ = '\t';
  113.           s++;
  114.           break;
  115.  
  116.         default:
  117.           *write++ = *s++;
  118.           break;
  119.         }
  120.     }
  121.       else
  122.     *write++ = *s++;
  123.     }
  124.  
  125.   *write++ = '\0';
  126. }
  127.  
  128. /* Print REGS in human-readable form.  */
  129.  
  130. void
  131. print_regs (regs)
  132.      struct re_registers regs;
  133. {
  134.   int i, end;
  135.  
  136.   printf ("Registers: ");
  137.   
  138.   if (regs.num_regs == 0 || regs.start[0] == -1)
  139.     {
  140.       printf ("(none)");
  141.     }
  142.   else
  143.     {
  144.       /* Find the last register pair that matched.  */
  145.       for (end = regs.num_regs - 1; end >= 0; end--)
  146.         if (regs.start[end] != -1)
  147.           break;
  148.  
  149.       printf ("[%d ", regs.start[0]);
  150.       for (i = 1; i <= end; i++)
  151.         printf ("(%d %d) ", regs.start[i], regs.end[i]);
  152.       printf ("%d]", regs.end[0]);
  153.     }
  154.   
  155.   putchar ('\n');
  156. }
  157.