home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / c / bm_src / execute.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-01-16  |  4.2 KB  |  157 lines

  1. #include <stdio.h>
  2. #include <osbind.h>
  3. #include "bm.h"
  4. #include "Extern.h"
  5.  
  6. static void loinit();
  7. static int loread();
  8.  
  9. Execute(DescVec, NPats, TextFile, Buffer, inbufsiz)
  10. register struct PattDesc *DescVec[];
  11. /* pointers to status vectors for the different
  12.     * patterns, including skip tables, position in buffer, etc. */
  13. register int NPats; /* number of patterns */
  14. register char Buffer[]; /* holds text from file */
  15. register int TextFile; /* file to search */
  16. {
  17.     int NRead, /* number of chars read from file */
  18.         NWanted, /* number of chars wanted */
  19.         NAvail, /* number of chars actually read */
  20.         BuffSize, /* number of chars in buffer */
  21.         BuffPos, /* offset of first char in Buffer in TextFile */
  22.         BuffEx, /* flag to indicate that buffer has been searched */
  23.         ResSize,
  24.         /* number of characters in the last, incomplete line */
  25.         Found, /* flag indicates whether pattern found
  26.         * completely and all matches printed */
  27.         Valid; /* was the match "valid", i.e. if -x used,
  28.         * did the whole line match? */
  29.     char *BuffEnd; /* pointer to last char of last complete line */
  30.  
  31.     /* misc working variables */
  32.     int i;
  33.  
  34.     /* initialize */
  35.         loinit();
  36.     ResSize = 0;
  37.     Found = 0;
  38.     BuffPos = 0;
  39.     for (i=0; i < NPats; i++) {
  40.         DescVec[i] -> Success = 0;
  41.         DescVec[i] -> Start = Buffer;
  42.     } /* for */
  43.     /* now do the searching */
  44.     do {
  45.         /* first, read a bufferfull and set up the variables */
  46.         NWanted = MAXBUFF - ResSize; NRead = 0;
  47.         do {
  48.             NAvail =
  49.                loread(TextFile,Buffer + ResSize + NRead, NWanted);
  50.             if (NAvail == -1) {
  51.                 fprintf(stderr,
  52.                   "bm: error reading from input file\n");
  53.                 exit(2);
  54.             } /* if */
  55.             NRead += NAvail; NWanted -= NAvail;
  56.         } while (NAvail && NWanted);
  57.         BuffEx = 0;
  58.         BuffSize = ResSize + NRead;
  59.         BuffEnd = Buffer + BuffSize - 1;
  60.         /* locate the end of the last complete line */
  61.         while (*BuffEnd != '\n' && BuffEnd >= Buffer)
  62.             --BuffEnd;
  63.         if (BuffEnd < Buffer)
  64.             BuffEnd = Buffer + BuffSize - 1;
  65.         while (!BuffEx) { /* work through one buffer full */
  66.             BuffEx = 1; /* set it provisionally, then clear
  67.             * it if we find the buffer non-empty */
  68.             for (i=0; i< NPats; i++) {
  69.                 if (!DescVec[i]->Success)
  70.                 /* if the pattern  has not been found */
  71.                     DescVec[i]-> Success =
  72.                     Search(DescVec[i]->Pattern,
  73.                     DescVec[i]->PatLen, BuffEnd,
  74.                     DescVec[i]->Skip1, DescVec[i]->Skip2,
  75.                     DescVec[i]);
  76.                 if (DescVec[i]->Success){
  77.                 /* if a match occurred */
  78.                     BuffEx = 0;
  79.                     Valid = MatchFound(DescVec[i],BuffPos,
  80.                     Buffer, BuffEnd);
  81.                     Found |= Valid;
  82.                     if ((sFlag || lFlag) && Found)
  83.                         return(0);
  84.                 } /* if */
  85.             } /* for */
  86.         } /* while */
  87.         if(NRead) {
  88.             ResSize = MoveResidue(DescVec,NPats,Buffer,
  89.             Buffer+BuffSize-1);
  90.             BuffPos += BuffSize - ResSize;
  91.         } /* if */
  92.     } while (NRead);
  93.     return(!Found);
  94. } /* Execute */
  95.  
  96. static char *lobuf, *lopos;
  97. static int lobread, loeof, lbsiz;
  98.  
  99. static void loinit()
  100. {
  101.     int newsiz;
  102.  
  103.     if (lobuf == (char *)0) {
  104.         lbsiz = Malloc(-1);
  105.         while (newsiz = lbsiz & (lbsiz-1)) {
  106.             lbsiz = newsiz;
  107.         }
  108.         lobuf = (char *)Malloc(lbsiz);
  109.         if (lobuf == (char *)-1) {
  110.             fprintf(stderr,"bm: memory allocation failed\n");
  111.             exit(1);
  112.         }
  113.     }
  114.     lopos = lobuf;
  115.     lobread = 0;
  116.     loeof = 0;
  117. }
  118.  
  119. static int loread(fd,pos,len)
  120. int fd,len;
  121. char *pos;
  122. {
  123.     int totalread = 0;
  124.     register char *from, *to;
  125.     register int toread;
  126.  
  127.     if (loeof) {
  128.         return totalread;
  129.     }
  130.     for (;;) {
  131.         if (lopos == lobuf + lobread) {
  132.             if (lobread > 0 && lobread < lbsiz) {
  133.                 loeof = 1;
  134.                 return totalread;
  135.             }
  136.             lobread = read(fd,lobuf,lbsiz);
  137.             if (lobread == 0) {
  138.                 loeof = 1;
  139.                 return totalread;
  140.             }
  141.             lopos = lobuf;
  142.         }
  143.         toread = (lobuf+lobread)-lopos;
  144.         toread = min(toread,len);
  145.         if (toread == 0) {
  146.             return totalread;
  147.         }
  148.         len -= toread;
  149.         totalread += toread;
  150.         for (from = lopos, to = pos; toread-- != 0; ) {
  151.             *to++ = *from++;
  152.         }
  153.         pos = to;
  154.         lopos = from;
  155.     }
  156. }
  157.