home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume7 / regex / grep.c next >
Encoding:
C/C++ Source or Header  |  1988-10-17  |  1014 b   |  68 lines

  1. #ifdef vms
  2. #include stdio
  3. #else
  4. #include <stdio.h>
  5. #endif
  6.  
  7. /*
  8.  * Rudimentary grep to test regex routines.
  9.  *
  10.  * DEBUG is only applicable
  11.  * to oZ version of regex. Make sure to 
  12.  * compile regex.c with -DDEBUG as well.
  13.  *
  14.  */
  15. extern char *re_comp();
  16. extern re_exec();
  17.  
  18. main(argc,argv)
  19. char *argv[];
  20. {
  21.     char str[512];
  22.     FILE *f;
  23.     register int n;
  24.     register char *p;
  25.  
  26.     if (argc < 2)
  27.         error("usage: grep pat [file]");
  28.  
  29.     if ((p = re_comp(argv[1])) != 0) {
  30.         printf("\t%s: %s\n", p, argv[1]);
  31.         exit(1);
  32.     }
  33. #ifdef DEBUG
  34.     symbolic(argv[1]);
  35. #endif
  36.     if (p = argv[2]) {
  37.         if ((f = fopen(p, "r")) == NULL) {
  38.             printf("cannot open %s\n", argv[2]);
  39.             exit(1);
  40.         }
  41.         while ((n = load(str, f)) != EOF)
  42.             if (re_exec(str))
  43.                 printf("%s\n",str);
  44.  
  45.     }
  46.     exit(0);
  47. }
  48. load (s, f)
  49. char *s;
  50. FILE *f;
  51. {
  52.     register int c;
  53.     static int lineno = 0;
  54.  
  55.     while ((c = getc(f)) != '\n' && c != EOF)
  56.         *s++ = c;
  57.     if (c == EOF)
  58.         return (EOF);
  59.     *s = (char) 0;
  60.     return (++lineno);
  61. }
  62.  
  63. error(s) char *s ; 
  64.     fprintf(stderr,"%s\n",s); 
  65.     exit(1); 
  66. }
  67.