home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume5 / bm.odd / bm.c next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  2.6 KB  |  96 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>        /* XXXX ADDED FOR BSD2.9 */
  3. #include <sys/file.h>
  4. #include <strings.h>
  5. #include "bm.h"
  6. #include "Extern.h"
  7. main(argc,argv)
  8. int argc;
  9. char *argv[];
  10. {
  11.     /* test driver for grep based on Boyer-Moore algorithm */
  12.     char BigBuff[MAXBUFF + 3];
  13.     /*
  14.     * We leave one extra character at the beginning and end of the buffer,
  15.     * but don't tell Execute about it. This is so when someone is
  16.     * scanning the buffer and scans past the end (or beginning)
  17.     * we are still technically in the buffer (picky, but there ARE
  18.     * machines which would complain).  We then leave an *additional*
  19.     * free byte at the begining so we can pass an even address to Execute
  20.     * (on some machines, odd address I/O can *COMPLETELY* destroy any
  21.     * speed benefits of bm).
  22.     */
  23.     int ret = 1, /* return code from Execute */
  24.         NotFound = 0, /* non-zero if file not readable */
  25.         NFiles,
  26.         NPats; /* number of patterns to search for */
  27.     char i,
  28.         *FlagPtr,
  29.         **OptPtr; /* used to scan command line */
  30.     int TextFile /* file to search */;
  31.     struct PattDesc *DescVec[MAXPATS];
  32.  
  33.     --argc;
  34.     OptPtr = argv + 1;
  35.     while ( argc && **OptPtr == '-') {
  36.         FlagPtr = *OptPtr + 1;
  37.         while (*FlagPtr) {
  38.             switch (*FlagPtr) {
  39.                 case 'c': cFlag = 1; break;
  40.                 case 'e': eFlag = 1;
  41.                     /* get the patterns from next arg */
  42.                     NPats = MkDescVec(DescVec,*++OptPtr);
  43.                     --argc;
  44.                     break;
  45.                 case 'f': fFlag = 1; 
  46.                     /* read the patterns from a file */
  47.                     NPats = GetPatFile(*++OptPtr,DescVec);
  48.                     --argc;
  49.                     break;
  50.                 case 'l': lFlag = 1; break;
  51.                 case 'n': nFlag = 1; break;
  52.                 case 's': sFlag = 1; break;
  53.                 case 'x': xFlag = 1; break;
  54.                 case 'h': hFlag = 1; break;
  55.                 default:
  56.                     fprintf(stderr,
  57.                     "bm: invalid option: -%c \n",
  58.                     *FlagPtr);
  59.                     PutUsage();
  60.                     exit(2);
  61.             } /* switch */
  62.             ++FlagPtr;
  63.         } /* while */
  64.         ++OptPtr; --argc;
  65.     } /* while */
  66.     /* OptPtr now points to patterns */
  67.     if (!fFlag && !eFlag) {
  68.         if (!argc) {
  69.             fprintf(stderr,"bm: no pattern specified\n");
  70.             PutUsage();
  71.             exit(2);
  72.         } else
  73.             NPats = MkDescVec(DescVec,*OptPtr);
  74.         ++OptPtr; --argc;
  75.     }
  76.     /* OptPtr now points to first file */
  77.     NFiles = argc;
  78.     if (!NFiles)
  79.         ret &= Execute(DescVec,NPats,0,BigBuff+2);
  80.     else while (argc--) {
  81.         if ((NFiles > 1) || lFlag) FileName = *OptPtr;
  82.         if ((TextFile = open(*OptPtr,O_RDONLY,0)) < 0) {
  83.             fprintf(stderr,"bm: can't open %s\n",*OptPtr);
  84.             NotFound++;
  85.         } else {
  86.             ret &= Execute(DescVec,NPats,TextFile,BigBuff+2);
  87.             if (sFlag && !ret)
  88.                 exit(0);
  89.             close(TextFile);
  90.         } /* if */
  91.         ++OptPtr;
  92.     } /* while */
  93.     if (cFlag) printf("%d\n",MatchCount);
  94.     exit(NotFound ? 2 : ret);
  95. } /* main */
  96.