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