[mod.sources] Ken Yap's changes to bm

sources-request@panda.UUCP (08/22/85)

Mod.sources:  Volume 3, Issue 1
Submitted by: Peter Bain <ihnp4!watmath!wateng!pdbain>


Here is the revised bm.c for bm version 1.1, incorporating Ken Yap's changes.
If it encounters a bad file bm now prints an error message and continues
to the next file.  Several people has asked for bm to have an option to
"stop after the first match in each file" to scan the subject lines
in rn articles.  I haven't put this in out of a desire to avoid
creeping featurism, because bm isn't good at looking at a bit of one file
then going to the next, and finally because there is already a command
to do this - hgrep:

HGREP(PUBLIC)    Waterloo UNIX Programmer's Manual   HGREP(PUBLIC)

NAME
     hgrep - search header of (news) files for a pattern
			.
			.
			.
DESCRIPTION
     Hgrep is a cousin of grep(1) that limits its search to the
     -----                ----
     "header" portions of files.  A blank line marks the end of
     the header....

     The pattern is a limited regular expression of the ilk
         -------
     recognized by ed(1); hgrep uses re comp and re exec (see
                   --     -----      -------     -------
     reference below) to interpret it....

     and so on....

____________________ shear energy! ___________________________________________
: This is a shar archive.	Extract with sh, not csh.
: The rest of this file will extract:
: bm.c
echo Extracting bm.c
sed 's/^X//' > bm.c << 'e-o-f'
X#include <stdio.h>
X#include <sys/file.h>
X#include <strings.h>
X#include "bm.h"
X#include "Extern.h"
Xmain(argc,argv)
Xint argc;
Xchar *argv[];
X{
X	/* test driver for grep based on Boyer-Moore algorithm */
X	char BigBuff[MAXBUFF + 2];
X	/*
X	* We leave one extra character at the beginning and end of the buffer,
X	* but don't tell Execute about it. This is so when someone is
X	* scanning the buffer and scans past the end (or beginning)
X	* we are still technically in the buffer (picky, but there ARE
X	* machines which would complain)
X	*/
X	int ret = 1, /* return code from Execute */
X		NotFound = 0, /* non-zero if file not readable */
X		NFiles,
X		NPats; /* number of patterns to search for */
X	char i,
X		*FlagPtr,
X		**OptPtr; /* used to scan command line */
X	int TextFile /* file to search */;
X	struct PattDesc *DescVec[MAXPATS];
X
X	--argc;
X	OptPtr = argv + 1;
X	while ( argc && **OptPtr == '-') {
X		FlagPtr = *OptPtr + 1;
X		while (*FlagPtr) {
X			switch (*FlagPtr) {
X				case 'c': cFlag = 1; break;
X				case 'e': eFlag = 1;
X					/* get the patterns from next arg */
X					NPats = MkDescVec(DescVec,*++OptPtr);
X					--argc;
X					break;
X				case 'f': fFlag = 1; 
X					/* read the patterns from a file */
X					NPats = GetPatFile(*++OptPtr,DescVec);
X					--argc;
X					break;
X				case 'l': lFlag = 1; break;
X				case 'n': nFlag = 1; break;
X				case 's': sFlag = 1; break;
X				case 'x': xFlag = 1; break;
X				case 'h': hFlag = 1; break;
X				default:
X					fprintf(stderr,
X					"bm: invalid option: -%c \n",
X					*FlagPtr);
X					PutUsage();
X					exit(2);
X			} /* switch */
X			++FlagPtr;
X		} /* while */
X		++OptPtr; --argc;
X	} /* while */
X	/* OptPtr now points to patterns */
X	if (!fFlag && !eFlag) {
X		if (!argc) {
X			fprintf(stderr,"bm: no pattern specified\n");
X			PutUsage();
X			exit(2);
X		} else
X			NPats = MkDescVec(DescVec,*OptPtr);
X		++OptPtr; --argc;
X	}
X	/* OptPtr now points to first file */
X	NFiles = argc;
X	if (!NFiles)
X		ret &= Execute(DescVec,NPats,0,BigBuff+1);
X	else while (argc--) {
X		if ((NFiles > 1) || lFlag) FileName = *OptPtr;
X		if ((TextFile = open(*OptPtr,O_RDONLY,0)) < 0) {
X			fprintf(stderr,"bm: can't open %s\n",*OptPtr);
X			NotFound++;
X		} else {
X			ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
X			if (sFlag && !ret)
X				exit(0);
X			close(TextFile);
X		} /* if */
X		++OptPtr;
X	} /* while */
X	if (cFlag) printf("%d\n",MatchCount);
X	exit(NotFound ? 2 : ret);
X} /* main */
e-o-f
exit 0
-- 
   - peter bain
...!{allegra|decvax|clyde|ihnp4 }!watmath!wateng!pdbain
hard mail:	CCNG, CPH-2369A, University of Waterloo,
	Waterloo, Ont. Canada N2M 5G4
telephone:	(519) 885-1211 x2810