home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 278.lha / RegexLibrary_v1.0 / tinygrep.c < prev   
C/C++ Source or Header  |  1989-08-06  |  3KB  |  183 lines

  1. /*
  2.  *  Test file for the GNU regular expression package.
  3.  *  Edwin Hoogerbeets 18/07/89
  4.  *
  5.  *  This file may be copied and distributed under the GNU Public
  6.  *  Licence. See the comment at the top of regex.c for details.
  7.  *
  8.  *  Adapted from Elib by Jim Mackraz, mklib by Edwin Hoogerbeets, and the
  9.  *  GNU regular expression package by the Free Software Foundation.
  10.  */
  11.  
  12. #include "regex.h"
  13. #include <libraries/dos.h>
  14. #include <exec/memory.h>
  15.  
  16. #define BUFSIZ 8*1024
  17.  
  18. extern char *AllocMem();
  19. extern char *OpenLibrary();
  20.  
  21. extern void searchfile();
  22.  
  23. void puts(str)
  24. char *str;
  25. {
  26.   Write(Output(),str,strlen(str));
  27. }
  28.  
  29. char *inputbuffer;
  30. long inputcounter = -1, inputsize, eof;
  31.  
  32. main (argc, argv)
  33. int argc;
  34. char **argv;
  35. {
  36.   struct re_pattern_buffer buf;
  37.   int index;
  38.   long input;
  39.  
  40.   if ( argc < 2 || *argv[1] == '\0' ) {
  41.     puts("Usage: ");
  42.     puts(argv[0]);
  43.     puts("regular_expression filename [...]\n");
  44.     exit(1);
  45.   }
  46.  
  47.   if ( (RegexBase =
  48.         (struct RegexBase *) OpenLibrary("regex.library",0L)) == NULL) {
  49.     puts("Could not open regex.library");
  50.     exit(2);
  51.   }
  52.  
  53.   inputbuffer = AllocMem(BUFSIZ,MEMF_PUBLIC|MEMF_CLEAR);
  54.   re_initialize_buffer(&buf,__Upcase);
  55.  
  56.   re_compile_pattern(argv[1], strlen(argv[1]), &buf, RE_SYNTAX_GREP);
  57.  
  58.   if ( argc < 3 ) {
  59.     input = Input();
  60.     searchfile(input,&buf,"");
  61.   } else {
  62.     register int printnames = (argc - 2) > 1 ? 1 : 0;
  63.     char name[256];
  64.  
  65.     index = 2;
  66.  
  67.     while ( index < argc ) {
  68.       if ( !(input = Open(argv[index],MODE_OLDFILE)) ) {
  69.         puts("Could not open ");
  70.         puts(argv[index]);
  71.         puts("\n");
  72.       } else {
  73.  
  74.         if ( printnames ) {
  75.           strcpy(name,argv[index]);
  76.           strcat(name,": ");
  77.         } else {
  78.           name[0] = '\0';
  79.         }
  80.  
  81.         inputcounter = -1;
  82.         searchfile(input,&buf, name);
  83.         Close(input);
  84.       }
  85.       ++index;
  86.     }
  87.   }
  88.  
  89.   FreeMem(inputbuffer,BUFSIZ);
  90.  
  91.   re_terminate_buffer(&buf);
  92.   CloseLibrary(RegexBase);
  93. }
  94.  
  95. long newbuffer(f)
  96. long f;
  97. {
  98.   inputsize = Read(f,inputbuffer,BUFSIZ);
  99.   inputcounter = 0;
  100.  
  101.   return( inputsize );
  102. }
  103.  
  104. char
  105. getc(f)
  106. long f;
  107. {
  108.   char result = inputbuffer[inputcounter++];
  109.  
  110.   if ( inputsize == 0 ) {
  111.     return('\0');
  112.   }
  113.  
  114.   if ( inputcounter >= inputsize ) {
  115.     newbuffer(f);
  116.   }
  117.  
  118.   return(result);
  119. }
  120.  
  121. char *
  122. fgets(buf,len,f)
  123. char *buf;
  124. long len, f;
  125. {
  126.   long counter = 0;
  127.   char c;
  128.  
  129.   if ( inputcounter < 0 ) {
  130.     if ( !newbuffer(f) ) {
  131.       return(NULL);
  132.     }
  133.   }
  134.  
  135.   while ( (c = getc(f)) != '\0' && counter < len ) {
  136.     buf[counter++] = c;
  137.  
  138.     if ( c == '\n' ) {
  139.       if ( counter < len ) {
  140.         buf[counter] = '\0';
  141.       }
  142.       return(buf);
  143.     }
  144.   }
  145.  
  146.   if ( c == '\0' ) {
  147.     return(NULL);
  148.   }
  149.  
  150.   if ( counter < len ) {
  151.     buf[counter] = '\0';
  152.   }
  153.   return(buf);
  154. }
  155.  
  156. void searchfile(f,buf,name)
  157. long f;
  158. struct re_pattern_buffer *buf;
  159. char *name;
  160. {
  161.   char buffer[256];
  162.   int result;
  163. /*  struct re_registers foo; */
  164.  
  165.   while ( fgets(buffer,256L,f) != NULL ) {
  166.     if ( (result = re_search(buf, buffer, strlen(buffer), 0L,
  167.                              strlen(buffer)-2, NULL)) >= 0 ) {
  168.       puts(name);
  169.       puts(buffer);
  170.     }
  171.   }
  172. }
  173.  
  174. _abort()
  175. {
  176.   puts("DON'T TOUCH THAT KEY, SILLY!");
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.