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

  1. #include <stdio.h>
  2. #include "bm.h"
  3. #include "Extern.h"
  4. Execute(DescVec, NPats, TextFile, Buffer)
  5. struct PattDesc *DescVec[]; /* pointers to status vectors for the different
  6.     * patterns, including skip tables, position in buffer, etc. */
  7. int NPats; /* number of patterns */
  8. char Buffer[]; /* holds text from file */
  9. int TextFile; /* file to search */
  10. {
  11.     long BuffPos; /* offset of first char in Buffer in TextFile */
  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.         BuffEx, /* flag to indicate that buffer has been searched */
  17.         ResSize,
  18.         /* number of characters in the last, incomplete line */
  19.         Found, /* flag indicates whether pattern found
  20.         * completely and all matches printed */
  21.         Valid; /* was the match "valid", i.e. if -x used,
  22.         * did the whole line match? */
  23.     char *BuffEnd; /* pointer to last char of last complete line */
  24.  
  25.     /*
  26.      * In order to optimize I/O for some machines which impose a severe
  27.      * penalty for I/O on an odd address, we play a nasty game.  First, we
  28.      * assume that the Buffer which is passed to us has an even address.
  29.      * Then whenever we move a buffer residual back to the beginning of
  30.      * the Buffer for the next read cycle, we actually move it to Buffer +
  31.      * Odd(Residual) where Odd() is 1 if Residual is odd, zero otherwise.
  32.      * This causes the the next read to go down on an even address.  We
  33.      * keep track of the beginning of data in the Buffer with BuffStart.
  34.      */
  35.     char *BuffStart;
  36.  
  37.     /* misc working variables */
  38. #ifdef notdef
  39.     int i;
  40. #else !notdef
  41.     register struct PattDesc    *Desc;
  42.     struct PattDesc            **Vec, **LastPatt = DescVec+NPats;
  43. #endif notdef
  44.  
  45.     /* initialize */
  46.     ResSize = 0;
  47.     Found = 0;
  48.     BuffPos = 0;
  49.     BuffStart = Buffer;
  50. #ifdef notdef
  51.     for (i=0; i < NPats; i++) {
  52.         DescVec[i] -> Success = 0;
  53.         DescVec[i] -> Start = Buffer;
  54.     } /* for */
  55. #else !notdef
  56.     for (Vec=DescVec; Vec < LastPatt; Vec++) {
  57.         Desc = *Vec;
  58.         Desc->Success = 0;
  59.         Desc->Start = Buffer;
  60.     }
  61. #endif notdef
  62.     /* now do the searching */
  63.     do {
  64.         /* first, read a bufferfull and set up the variables */
  65.         /*
  66.          * Some systems *even* get upset when you ask for an odd read
  67.          * length - ARGH!
  68.          */
  69.         NWanted = (int)((unsigned)(MAXBUFF - ResSize) & ~1);
  70.         NRead = 0;
  71.         do {
  72.             /*
  73.              * BuffStart+ResSize+BRead is even first time through
  74.              * this loop - afterwards, no guaranties, but for
  75.              * files this loop never goes more than once ...
  76.              * Can't do any better.
  77.              */
  78.             NAvail =
  79.                read(TextFile,BuffStart + ResSize + NRead, NWanted);
  80.             if (NAvail == -1) {
  81.                 fprintf(stderr,
  82.                   "bm: error reading from input file\n");
  83.                 exit(2);
  84.             } /* if */
  85.             NRead += NAvail; NWanted -= NAvail;
  86.         } while (NAvail && NWanted);
  87.         BuffEx = 0;
  88.         BuffSize = ResSize + NRead;
  89.         BuffEnd = BuffStart + BuffSize - 1;
  90.         /* locate the end of the last complete line */
  91.         while (*BuffEnd != '\n' && BuffEnd >= BuffStart)
  92.             --BuffEnd;
  93.         if (BuffEnd < BuffStart)
  94.             BuffEnd = BuffStart + BuffSize - 1;
  95.         while (!BuffEx) { /* work through one buffer full */
  96.             BuffEx = 1; /* set it provisionally, then clear
  97.             * it if we find the buffer non-empty */
  98. #ifdef notdef
  99.             for (i=0; i< NPats; i++) {
  100.                 if (!DescVec[i]->Success)
  101.                 /* if the pattern  has not been found */
  102.                     DescVec[i]-> Success =
  103.                     Search(DescVec[i]->Pattern,
  104.                     DescVec[i]->PatLen, BuffStart, BuffEnd,
  105.                     DescVec[i]->Skip1, DescVec[i]->Skip2,
  106.                     DescVec[i]);
  107.                 if (DescVec[i]->Success){
  108.                 /* if a match occurred */
  109.                     BuffEx = 0;
  110.                     Valid = MatchFound(DescVec[i],BuffPos,
  111.                     BuffStart, BuffEnd);
  112.                     Found |= Valid;
  113.                     if ((sFlag || lFlag) && Found)
  114.                         return(0);
  115.                 } /* if */
  116.             } /* for */
  117. #else !notdef
  118.             for (Vec=DescVec; Vec < LastPatt; Vec++) {
  119.                 Desc = *Vec;
  120.                 if (!Desc->Success)
  121.                 /* if the pattern  has not been found */
  122.                     Desc-> Success =
  123.                     Search(Desc->Pattern,
  124.                     Desc->PatLen, BuffStart, BuffEnd,
  125.                     Desc->Skip1, Desc->Skip2,
  126.                     Desc);
  127.                 if (Desc->Success){
  128.                 /* if a match occurred */
  129.                     BuffEx = 0;
  130.                     Valid = MatchFound(Desc,BuffPos,
  131.                     BuffStart, BuffEnd);
  132.                     Found |= Valid;
  133.                     if ((sFlag || lFlag) && Found)
  134.                         return(0);
  135.                 } /* if */
  136.             } /* for */
  137. #endif notdef
  138.         } /* while */
  139.         if(NRead) {
  140.             ResSize = MoveResidue(DescVec,NPats,Buffer,&BuffStart,BuffEnd);
  141.             BuffPos += BuffSize - ResSize;
  142.         } /* if */
  143.     } while (NRead);
  144.     return(!Found);
  145. } /* Execute */
  146.