home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume4 / match1.2 / Execute.c next >
C/C++ Source or Header  |  1986-11-30  |  3KB  |  90 lines

  1. #include <stdio.h>
  2. #include "match.h"
  3. #include "Extern.h"
  4. Execute(DescVec, NPats, TextFile, Buffer)
  5. register struct PattDesc *DescVec[];
  6. /* pointers to status vectors for the different
  7.     * patterns, including skip tables, position in buffer, etc. */
  8. register int NPats; /* number of patterns */
  9. register char Buffer[]; /* holds text from file */
  10. register int TextFile; /* file to search */
  11. {
  12.     int NRead, /* number of chars read from file */
  13.         NWanted, /* number of chars wanted */
  14.         NAvail, /* number of chars actually read */
  15.         BuffSize, /* number of chars in buffer */
  16.         BuffPos, /* offset of first char in Buffer in TextFile */
  17.         BuffEx, /* flag to indicate that buffer has been searched */
  18.         ResSize,
  19.         /* number of characters in the last, incomplete line */
  20.         Found, /* flag indicates whether pattern found
  21.         * completely and all matches printed */
  22.         Valid; /* was the match "valid", i.e. if -x used,
  23.         * did the whole line match? */
  24.     register char *BuffEnd;
  25.     /* pointer to last char of last complete line */
  26.  
  27.     /* misc working variables */
  28.     register int i;
  29.  
  30.     /* initialize */
  31.     ResSize = 0;
  32.     Found = 0;
  33.     BuffPos = 0;
  34.     for (i=0; i < NPats; i++) {
  35.         DescVec[i] -> Success = 0;
  36.         DescVec[i] -> Start = Buffer;
  37.     } /* for */
  38.     /* now do the searching */
  39.     do {
  40.         /* first, read a bufferfull and set up the variables */
  41.         NWanted = MAXBUFF - ResSize; NRead = 0;
  42.         do {
  43.             NAvail =
  44.                read(TextFile,Buffer + ResSize + NRead, NWanted);
  45.             if (NAvail == -1) {
  46.                 fprintf(stderr,
  47.                   "bm: error reading from input file\n");
  48.                 exit(2);
  49.             } /* if */
  50.             NRead += NAvail; NWanted -= NAvail;
  51.         } while (NAvail && NWanted);
  52.         BuffEx = 0;
  53.         BuffSize = ResSize + NRead;
  54.         BuffEnd = Buffer + BuffSize - 1;
  55.         /* locate the end of the last complete line */
  56.         while (*BuffEnd != '\n' && BuffEnd >= Buffer)
  57.             --BuffEnd;
  58.         if (BuffEnd < Buffer)
  59.             BuffEnd = Buffer + BuffSize - 1;
  60.         while (!BuffEx) { /* work through one buffer full */
  61.             BuffEx = 1; /* set it provisionally, then clear
  62.             * it if we find the buffer non-empty */
  63.             for (i=0; i< NPats; i++) {
  64.                 if (!DescVec[i]->Success)
  65.                 /* if the pattern  has not been found */
  66.                     DescVec[i]-> Success =
  67.                     Search(DescVec[i]->Pattern,
  68.                     DescVec[i]->PatLen, DescVec[i]->Start,
  69.                     BuffEnd - DescVec[i]->Start + 1,
  70.                     DescVec[i]);
  71.                 if (DescVec[i]->Success){
  72.                 /* if a match occurred */
  73.                     BuffEx = 0;
  74.                     Valid = MatchFound(DescVec[i],BuffPos,
  75.                     Buffer, BuffEnd);
  76.                     Found |= Valid;
  77.                     if ((sFlag || lFlag) && Found)
  78.                         return(0);
  79.                 } /* if */
  80.             } /* for */
  81.         } /* while */
  82.         if(NRead) {
  83.             ResSize = MoveResidue(DescVec,NPats,Buffer,
  84.                 Buffer + BuffSize -1);
  85.             BuffPos += BuffSize - ResSize;
  86.         } /* if */
  87.     } while (NRead);
  88.     return(!Found);
  89. } /* Execute */
  90.