Category : C Source Code
Archive   : FASTGREP.ZIP
Filename : BM.C

 
Output of file : BM.C contained in archive : FASTGREP.ZIP

/*******************************************************************************
* Boyer-Moore String Matching Algorithm *
* *
* Turbo-C version for MS-DOS *
* adapted from UNIX source code *
* *
* Uses Boyer-Moore search algorithm. *
* *
* For help execute the program without parameters. *
* *
* Multiple file specifications may be made on the command line. *
* *
* This was compiled with the tiny memory model and then EXE2BIN *
* was used on the output. The small memory model could just *
* as easily been used. *
* *
* David Polansky *
* 3-21-88 *
* *
*******************************************************************************/
#include
#include
#include
#include
#include
#include
#include "bm.h"
#include "extern.h"
#define TRUE 1
#define FALSE 0

char s_path[80]; /* source path */
char s_drv[3]; /* drive */
char s_dir[66]; /* directory */
char s_file[9]; /* filename */
char s_ext[5]; /* file ext */

struct ffblk fcb; /* where our file names will be stored */

main(argc,argv)
int argc;
char **argv;
{
/* test driver for grep based on Boyer-Moore algorithm */
char BigBuff[MAXBUFF + 2];
/* We leave one extra character at the beginning and end of the buffer,
* but don't tell Execute about it. This is so when someone is
* scanning the buffer and scans past the end (or beginning)
* we are still technically in the buffer (picky, but there ARE
* machines which would complain) */
int ret = 1, /* return code from Execute */
NPats; /* number of patterns to search for */
char *FlagPtr; /* pointer to the flags */
char c;
int i; /* index of argument list */
int TextFile; /* file to search */
struct PattDesc *DescVec[MAXPATS];
int first_time_through = TRUE; /* whether to use findfirst or findnext */
char *PatternFile;
char *Pattern;

mFlag = TRUE;
if (argc < 2)
{
PutUsage();
}
for (i = 1;i < argc;i++)
{
if ((argv[i][0] != '-') || (argv[i][1] == '\0'))
{
break;
}
FlagPtr = argv[i];
while (c = *++FlagPtr)
{
switch(c)
{
case 'm': mFlag = FALSE; break;
case 'c': cFlag = 1; break;
case 'f': fFlag = 1;
/* read the patterns from a file */
PatternFile = argv[++i];
break;
case 'e': eFlag = 1;
/* get the patterns from next arg */
Pattern = argv[++i];
break;
case 'l': lFlag = 1; break;
case 'n': nFlag = 1; break;
case 's': sFlag = 1; break;
case 'x': xFlag = 1; break;
case 'h': hFlag = 1; break;
default:
fprintf(stderr,"grep: invalid option: -%c \n",c);
PutUsage();
break;
} /* switch */
} /* while */
}
/* argv[i] now points to patterns */
if (!fFlag && !eFlag)
{
if (i > (argc - 1))
{
fprintf(stderr,"grep: no pattern specified\n");
PutUsage();
}
else
{
NPats = MkDescVec(DescVec,argv[i]);
++i;
}
}
else if (fFlag)
{
NPats = GetPatFile(PatternFile,DescVec);
}
else if (eFlag)
{
NPats = MkDescVec(DescVec,Pattern);
}
/* argv[i] now points to first file */
if (i == argc)
{
/* use standard input, could be keyboard */
ret &= Execute(DescVec,NPats,0,BigBuff+1);
}
else
{
for ( ;i < argc;i++)
{
first_time_through = TRUE;
fnsplit(argv[i],s_drv,s_dir,s_file,s_ext); /* split the names */
while (TRUE) /* a BREAK will end this loop */
{
if (first_time_through)
{
if (findfirst(argv[i],&fcb,0) == EOF)
{
fprintf(stderr,"No files matching %s\n",argv[i]);
exit(2);
}
first_time_through = FALSE;
}
else
{
if (findnext(&fcb) == EOF)
break; /* no more files to convert */
}
FileName = fcb.ff_name;
strcpy(s_path,s_drv);
strcat(s_path,s_dir);
strcat(s_path,fcb.ff_name);
TextFile = _open(s_path,O_RDONLY);
ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
if (sFlag && !ret)
{
exit(0);
}
close(TextFile);
}
}
}
if (cFlag) printf("%d\n",MatchCount);
exit(ret);
} /* main */