home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume2 / bm2 < prev    next >
Internet Message Format  |  1986-11-30  |  7KB

  1. From: Peter Bain <ihnp4!watmath!wateng!pdbain>
  2. Subject: bm (update to 1.1)
  3. Newsgroups: mod.sources
  4. Approved: john@genrad.UUCP
  5.  
  6. Mod.sources:  Volume 2, Issue 19
  7. Submitted by: Peter Bain <ihnp4!watmath!wateng!pdbain>
  8.  
  9.  
  10. Jeff Mogul (mogul@Carmel) pointed out a bug and kindly provided
  11. the fix.  The last line in the buffer was getting thrown away or mangled, due
  12. to an error in arguments to MoveResidue. This occurred only in long files.
  13. The correction to Execute.c is:
  14. *** 78,84
  15.               } /* for */
  16.           } /* while */
  17.           if(NRead) {
  18. !             ResSize = MoveResidue(DescVec,NPats,Buffer,BuffEnd);
  19.               BuffPos += BuffSize - ResSize;
  20.           } /* if */
  21.       } while (NRead);
  22.  
  23. --- 78,85 -----
  24.               } /* for */
  25.           } /* while */
  26.           if(NRead) {
  27. !             ResSize = MoveResidue(DescVec,NPats,Buffer,
  28. !                         Buffer + BuffSize - 1);
  29.               BuffPos += BuffSize - ResSize;
  30.           } /* if */
  31.       } while (NRead);
  32.  
  33. The correct code for Execute.c is
  34. ----------------------- go into scissor mode here ------------------
  35. : This is a shar archive.    Extract with sh, not csh.
  36. : The rest of this file will extract:
  37. : Execute.c
  38. echo Extracting Execute.c
  39. sed 's/^X//' > Execute.c << 'e-o-f'
  40. X#include <stdio.h>
  41. X#include "bm.h"
  42. X#include "Extern.h"
  43. XExecute(DescVec, NPats, TextFile, Buffer)
  44. Xstruct PattDesc *DescVec[]; /* pointers to status vectors for the different
  45. X    * patterns, including skip tables, position in buffer, etc. */
  46. Xint NPats; /* number of patterns */
  47. Xchar Buffer[]; /* holds text from file */
  48. Xint TextFile; /* file to search */
  49. X{
  50. X    int NRead, /* number of chars read from file */
  51. X        NWanted, /* number of chars wanted */
  52. X        NAvail, /* number of chars actually read */
  53. X        BuffSize, /* number of chars in buffer */
  54. X        BuffPos, /* offset of first char in Buffer in TextFile */
  55. X        BuffEx, /* flag to indicate that buffer has been searched */
  56. X        ResSize,
  57. X        /* number of characters in the last, incomplete line */
  58. X        Found, /* flag indicates whether pattern found
  59. X        * completely and all matches printed */
  60. X        Valid; /* was the match "valid", i.e. if -x used,
  61. X        * did the whole line match? */
  62. X    char *BuffEnd; /* pointer to last char of last complete line */
  63. X
  64. X    /* misc working variables */
  65. X    int i;
  66. X
  67. X    /* initialize */
  68. X    ResSize = 0;
  69. X    Found = 0;
  70. X    BuffPos = 0;
  71. X    for (i=0; i < NPats; i++) {
  72. X        DescVec[i] -> Success = 0;
  73. X        DescVec[i] -> Start = Buffer;
  74. X    } /* for */
  75. X    /* now do the searching */
  76. X    do {
  77. X        /* first, read a bufferfull and set up the variables */
  78. X        NWanted = MAXBUFF - ResSize; NRead = 0;
  79. X        do {
  80. X            NAvail =
  81. X               read(TextFile,Buffer + ResSize + NRead, NWanted);
  82. X            if (NAvail == -1) {
  83. X                fprintf(stderr,
  84. X                  "bm: error reading from input file\n");
  85. X                exit(2);
  86. X            } /* if */
  87. X            NRead += NAvail; NWanted -= NAvail;
  88. X        } while (NAvail && NWanted);
  89. X        BuffEx = 0;
  90. X        BuffSize = ResSize + NRead;
  91. X        BuffEnd = Buffer + BuffSize - 1;
  92. X        /* locate the end of the last complete line */
  93. X        while (*BuffEnd != '\n' && BuffEnd >= Buffer)
  94. X            --BuffEnd;
  95. X        if (BuffEnd < Buffer)
  96. X            BuffEnd = Buffer + BuffSize - 1;
  97. X        while (!BuffEx) { /* work through one buffer full */
  98. X            BuffEx = 1; /* set it provisionally, then clear
  99. X            * it if we find the buffer non-empty */
  100. X            for (i=0; i< NPats; i++) {
  101. X                if (!DescVec[i]->Success)
  102. X                /* if the pattern  has not been found */
  103. X                    DescVec[i]-> Success =
  104. X                    Search(DescVec[i]->Pattern,
  105. X                    DescVec[i]->PatLen, Buffer, BuffEnd,
  106. X                    DescVec[i]->Skip1, DescVec[i]->Skip2,
  107. X                    DescVec[i]);
  108. X                if (DescVec[i]->Success){
  109. X                /* if a match occurred */
  110. X                    BuffEx = 0;
  111. X                    Valid = MatchFound(DescVec[i],BuffPos,
  112. X                    Buffer, BuffEnd);
  113. X                    Found |= Valid;
  114. X                    if ((sFlag || lFlag) && Found)
  115. X                        return(0);
  116. X                } /* if */
  117. X            } /* for */
  118. X        } /* while */
  119. X        if(NRead) {
  120. X            ResSize = MoveResidue(DescVec,NPats,Buffer,
  121. X                Buffer + BuffSize -1);
  122. X            BuffPos += BuffSize - ResSize;
  123. X        } /* if */
  124. X    } while (NRead);
  125. X    return(!Found);
  126. X} /* Execute */
  127. e-o-f
  128. exit 0
  129.  
  130.  
  131. From: seismo!mcvax!ken (Ken Yap)
  132. Subject: bm bug fix
  133. Newsgroups: mod.sources
  134. Approved: john@genrad.UUCP
  135.  
  136. Mod.sources:  Volume 2, Issue 36
  137. Submitted by: seismo!mcvax!ken (Ken Yap)
  138.  
  139. Problem:
  140.     Bm quits when encountering an unreadable file (no permission,
  141.     file does not exist) with an exit status of 2. The grep
  142.     family prints an error message and continues searching.
  143.  
  144. Repeat-by:
  145.     # in some directory with a C main program
  146.     echo main > xxx
  147.     chmod 200 xxx
  148.     bm main xxx *.c
  149.     # compare with
  150.     grep main xxx *.c
  151.  
  152. Fix:
  153.     Apply the patch below.
  154.  
  155. *** bm.c.old    Tue Aug  6 10:34:57 1985
  156. --- bm.c    Tue Aug  6 10:41:22 1985
  157. ***************
  158. *** 17,22
  159.       * machines which would complain)
  160.       */
  161.       int ret = 1, /* return code from Execute */
  162.           NFiles,
  163.           NPats; /* number of patterns to search for */
  164.       char i,
  165.  
  166. --- 17,23 -----
  167.       * machines which would complain)
  168.       */
  169.       int ret = 1, /* return code from Execute */
  170. +         NotFound = 0,        /* non-zero if file not readable */
  171.           NFiles,
  172.           NPats; /* number of patterns to search for */
  173.       char i,
  174. ***************
  175. *** 76,82
  176.           if ((NFiles > 1) || lFlag) FileName = *OptPtr;
  177.           if ((TextFile = open(*OptPtr,O_RDONLY,0)) < 0) {
  178.               fprintf(stderr,"bm: can't open %s\n",*OptPtr);
  179. !             exit(2);
  180.           } /* if */
  181.           ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
  182.           if (sFlag && !ret)
  183.  
  184. --- 77,89 -----
  185.           if ((NFiles > 1) || lFlag) FileName = *OptPtr;
  186.           if ((TextFile = open(*OptPtr,O_RDONLY,0)) < 0) {
  187.               fprintf(stderr,"bm: can't open %s\n",*OptPtr);
  188. !             NotFound++;
  189. !         }
  190. !         else {
  191. !             ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
  192. !             if (sFlag && !ret)
  193. !                 exit(0);
  194. !             close(TextFile);
  195.           } /* if */
  196.           ++OptPtr;
  197.       } /* while */
  198. ***************
  199. *** 78,86
  200.               fprintf(stderr,"bm: can't open %s\n",*OptPtr);
  201.               exit(2);
  202.           } /* if */
  203. -         ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
  204. -         if (sFlag && !ret)
  205. -             exit(0);
  206.           ++OptPtr;
  207.           close(TextFile);
  208.       } /* while */
  209.  
  210. --- 85,90 -----
  211.                   exit(0);
  212.               close(TextFile);
  213.           } /* if */
  214.           ++OptPtr;
  215.       } /* while */
  216.       if (cFlag) printf("%d\n",MatchCount);
  217. ***************
  218. *** 82,88
  219.           if (sFlag && !ret)
  220.               exit(0);
  221.           ++OptPtr;
  222. -         close(TextFile);
  223.       } /* while */
  224.       if (cFlag) printf("%d\n",MatchCount);
  225.       exit(ret);
  226.  
  227. --- 86,91 -----
  228.               close(TextFile);
  229.           } /* if */
  230.           ++OptPtr;
  231.       } /* while */
  232.       if (cFlag) printf("%d\n",MatchCount);
  233.       exit(NotFound ? 2 : ret);
  234. ***************
  235. *** 85,89
  236.           close(TextFile);
  237.       } /* while */
  238.       if (cFlag) printf("%d\n",MatchCount);
  239. !     exit(ret);
  240.   } /* main */
  241.  
  242. --- 88,92 -----
  243.           ++OptPtr;
  244.       } /* while */
  245.       if (cFlag) printf("%d\n",MatchCount);
  246. !     exit(NotFound ? 2 : ret);
  247.   } /* main */
  248.  
  249.  
  250.