/*
 *  Test file for the GNU regular expression package.
 *  Edwin Hoogerbeets 18/07/89
 *
 *  This file may be copied and distributed under the GNU Public
 *  Licence. See the comment at the top of regex.c for details.
 *
 *  Adapted from Elib by Jim Mackraz, mklib by Edwin Hoogerbeets, and the
 *  GNU regular expression package by the Free Software Foundation.
 */

#include <stdio.h>
#include "regex.h"

extern char *malloc();
extern char *OpenLibrary();

/* Indexed by a character, gives the upper case equivalent of the character */

main (argc, argv)
int argc;
char **argv;
{
  char pat[80];
  struct re_pattern_buffer buf;
  int i;
  char c;
  char *fastmap;

  if ( (RegexBase =
        (struct RegexBase *) OpenLibrary("regex.library",0L)) == NULL) {
    printf("Could not open regex.library");
    exit(1);
  }

  printf("This is the RegexBase pointer: 0x%lx\n",RegexBase);

  re_initialize_buffer(&buf,__Upcase);
  fastmap = buf.fastmap;

  while ( *pat != '%' )
    {
      printf("Enter pattern, or %% to exit: ");
      gets(pat);

      if ( pat[0] == '%' )
        break;

      if (*pat)
        {
          re_compile_pattern (pat, strlen(pat), &buf, RE_SYNTAX_GREP);

          for (i = 0; i < buf.used; i++)
            printchar (buf.buffer[i]);

          putchar ('\n');

          printf ("%d allocated, %d used.\n", buf.allocated, buf.used);

          re_compile_fastmap (&buf);
          printf ("Allowed by fastmap: ");
          for (i = 0; i < (1 << BYTEWIDTH); i++)
            if (fastmap[i]) {
              printchar (i);
            }
          putchar ('\n');
        }

      printf("Now enter a string to match against, or %% to exit\n");
      gets(pat); /* Now read the string to match against */

      i = re_match(&buf, pat, strlen (pat), 0L, 0L);
      printf("Match value %d.\n", i);
    }

  re_terminate_buffer(&buf);
  CloseLibrary(RegexBase);
}


_abort()
{
  puts("Enter % to stop");
}

printchar (c)
     char c;
{
  if (c < 041 || c >= 0177)
    {
      putchar ('\\');
      putchar (((c >> 6) & 3) + '0');
      putchar (((c >> 3) & 7) + '0');
      putchar ((c & 7) + '0');
    }
  else
    putchar (c);
}

error (string)
     char *string;
{
  puts (string);
  exit (1);
}

