-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmgsubs
744 lines (685 loc) · 17.1 KB
/
bmgsubs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
Subject: bmgsubs - Boyer-Moore-Gosper fast string search subroutines
Newsgroups: mod.sources
Approved: [email protected]
Mod.sources: Volume 5, Issue 14
Submitted by: Jeff Mogul <talcott!su-navajo.arpa:mogul>
: Run this shell script with "sh" not "csh"
PATH=:/bin:/usr/bin:/usr/ucb
export PATH
all=FALSE
if [ $1x = -ax ]; then
all=TRUE
fi
/bin/echo 'Extracting README'
sed 's/^X//' <<'//go.sysin dd *' >README
Here are routines to perform fast string searches using the
Boyer-Moore-Gosper algorithm; they can be used in any Unix program (and
should be portable to non-Unix systems). You can search either a file
or a buffer in memory.
The code is mostly due to James A. Woods ([email protected])
although I have modified it heavily, so all bugs are my fault. The
original code is from his sped-up version of egrep, recently posted on
mod.sources and available via anonymous FTP from ames-aurora.arpa as
pub/egrep.one and pub/egrep.two. That code handles regular
expressions; mine does not.
The files included here are
README This file
Makefile Can be modified for 4.xBSD or Sys V compilation
bmgsubs.3l Manual page
bmgsubs.c The search code
bmgtest.c Test program (example of file searching)
bmgtest2.c Another test program, showing buffer searching
These have only been tested on 4.2BSD Vax systems.
-Jeff Mogul
decwrl!glacier!navajo!mogul
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
/bin/chmod 664 README
/bin/echo -n ' '; /bin/ls -ld README
fi
/bin/echo 'Extracting Makefile'
sed 's/^X//' <<'//go.sysin dd *' >Makefile
# Makefile for bmgsubs (Boyer-Moore-Gosper subroutines)
#
#
# Define BSD if you're compiling this for a 4.xBSD system
# (BSD has bcopy, has index() instead of strchr().)
#
ENV = -DBSD
CFLAGS = -O
bmgsubs.o: bmgsubs.c
bmgtest: bmgsubs.o bmgtest.o
cc -o bmgtest bmgsubs.o bmgtest.o
bmgtest2: bmgsubs.o bmgtest2.o
cc -o bmgtest2 bmgsubs.o bmgtest2.o
clean:
rm -f bmgtest bmgtest2 *.o *.BAK *.CKP
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
/bin/chmod 664 Makefile
/bin/echo -n ' '; /bin/ls -ld Makefile
fi
/bin/echo 'Extracting bmgsubs.3l'
sed 's/^X//' <<'//go.sysin dd *' >bmgsubs.3l
X.TH BMGSUBS 3L "16 May 1986"
X.SH NAME
(bmgsubs) bmg_setup, bmg_search, bmg_fsearch \- Boyer-Moore-Gosper string search routines
X.SH SYNOPSIS
bmg_setup(search_string, case_fold_flag)
X.br
char *search_string;
X.br
int case_fold_flag;
X.sp
bmg_fsearch(file_des, action_func)
X.br
int file_des;
X.br
int (*action_func)();
X.sp
bmg_search(buffer_base, buffer_length, action_func)
X.br
char *buffer_base;
X.br
int buffer_length;
X.br
int (*action_func)();
X.SH DESCRIPTION
These routines perform fast searches for strings, using the
Boyer-Moore-Gosper algorithm. No meta-characters
(such as `*' or `.')
are interpreted, and the search string cannot contain newlines.
X.PP
X.I Bmg_setup
must be called as the first step in performing a search. The
X.I search_string
parameter is the string to be searched for.
X.I Case_fold_flag
should be false (zero) if characters should match exactly, and
true (non-zero) if case should be ignored when checking for
matches.
X.PP
Once a search string has been specified using
X.IR bmg_setup ,
one or more searches for that string may be performed.
X.PP
X.I Bmg_fsearch
searches a file, open for reading on file descriptor
X.I file_des
(this is
X.I not
a
X.I stdio
file.)
For each line that contains the search string,
X.I bmg_fsearch
will call the
X.I action_func
function specified by the caller as
action_func(matching_line, byte_offset).
The
X.I matching_line
parameter is a (char *) pointer to a temporary copy of the line;
X.I byte_offset
is the offset from the beginning of the file to the first occurence
of the search string in that line.
X.I Action_func
should return true (non-zero) if the search should continue, or
false (zero) if the search should terminate at this point.
X.PP
X.I Bmg_search
is like
X.IR bmg_fsearch ,
except that instead of searching a file, it searches the buffer
pointed to by
X.IR buffer_base ;
X.I buffer_length
specifies the number of bytes in the buffer.
The
X.I byte_offset
parameter to
X.I action_func
gives the offset from the beginning of the buffer.
X.PP
If the user merely wants the matching lines printed on the
standard output, the
X.I action_func
parameter
to
X.I bmg_fsearch
or
X.I bmg_search
can be NULL.
X.SH AUTHOR
Jeffrey Mogul (Stanford University), based on code written
by James A. Woods (NASA Ames)
X.SH BUGS
Might be nice to have a version of this that handles regular expressions.
X.PP
There are large, but finite, limits on the length of both pattern
strings and text lines. When these limits are exceeded, all bets are off.
X.PP
The string pointer passed to
X.I action_func
points to a temporary copy of the matching line, and must be
copied elsewhere before
X.I action_func
returns.
X.PP
X.I Bmg_search
does not
X.I permanently
modify the buffer in any way, but during its execution (and therefore
when
X.I action_func
is called), the last byte of the buffer may be temporarily changed.
X.PP
The Boyer-Moore algorithm cannot find lines that do not contain
a given pattern (like "grep -v") or count lines ("grep -n").
Although it is fast even for short search strings, it gets faster as
the search string length increases.
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
/bin/chmod 664 bmgsubs.3l
/bin/echo -n ' '; /bin/ls -ld bmgsubs.3l
fi
/bin/echo 'Extracting bmgsubs.c'
sed 's/^X//' <<'//go.sysin dd *' >bmgsubs.c
X/*
* bmgsubs.c
*
* Subroutines for fast string searching; no regular expressions
*
* Entries:
*
* bmg_setup(pattern_string, case_fold_flag)
* char *pattern_string; -- the string you are searching for
* int case_fold_flag; -- true if you want search to ignore case
*
* bmg_fsearch(file_des, action_func)
* int file_des; -- file descriptor to read on
* int (*action_func)(); -- called on each hit
*
* bmg_search(buffer, buflen, action_func)
* char *buffer; -- buffer to search
* int buflen; -- bytes in buffer
* int (*action_func)(); -- called on each hit
*
* Usage:
* Start by calling bmg_setup() to specify a pattern. Then open
* the file you want to search and pass the file descriptor (NOT a
* stdio file pointer) to bmg_fsearch(). For each line that contains
* the pattern, bmg_fsearch() will call action_func(string, position)
* with string being a static copy of the matching line and position
* being the disntance in bytes from the beginning of the search to the
* matching substring.
* action_func() should return a non-zero integer unless the
* search should be terminated, in which case it should return 0.
* bmg_search() works the same as bmg_fsearch(), but searches
* a buffer rather than a file.
*
* Adapted from:
*
Boyer/Moore/Gosper-assisted 'egrep' search, with delta0 table as in
original paper (CACM, October, 1977). No delta1 or delta2. According to
experiment (Horspool, Soft. Prac. Exp., 1982), delta2 is of minimal
practical value. However, to improve for worst case input, integrating
the improved Galil strategies (Apostolico/Giancarlo, Siam. J. Comput.,
February 1986) deserves consideration.
James A. Woods Copyright (c) 1986
NASA Ames Research Center
*
* 29 April 1986 Jeff Mogul Stanford
* Adapted as a set of subroutines for use by a program. No
* regular-expression support.
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#ifdef BSD
#define strchr index
#endif
#ifndef BUFSIZE
#define BUFSIZE 8192
#endif BUFSIZE
#ifndef PATSIZE
#define PATSIZE 1000
#endif PATSIZE
#define LARGE BUFSIZE + PATSIZE
#define NL '\n'
#define EOS '\0'
struct bmg_data {
int bmg_delta0[256]; /* ascii only */
char bmg_cmap[256]; /* (un)folded characters */
char bmg_str[BUFSIZE + 2];
char bmg_linetemp[BUFSIZE];
char bmg_pattern[PATSIZE];
int bmg_patlen;
int bmg_nsuccess;
} bmdata;
#define delta0 bmdata.bmg_delta0
#define cmap bmdata.bmg_cmap
#define str bmdata.bmg_str
#define linetemp bmdata.bmg_linetemp
#define pattern bmdata.bmg_pattern
#define patlen bmdata.bmg_patlen
#define nsuccess bmdata.bmg_nsuccess
char *strcpy(), *strncpy(), *malloc();
bmg_search(buffer, buflen, action)
char *buffer;
int buflen;
int (*action)();
{
register char *k,
*strend,
*s;
register int j;
char *t;
char *strbegin;
char *gotamatch();
int byte_offset = 0;
char last_byte = buffer[buflen - 1];
nsuccess = 0;
/* make sure that buffer ends with NL */
if (last_byte != NL)
buffer[buflen - 1] = NL;
/*
* We must process the buffer in chunks of BUFSIZE to make
* "LARGE" hack work right.
*/
for (strbegin = buffer;
strbegin < &buffer[buflen]; strbegin += BUFSIZE) {
strend = strbegin + BUFSIZE;
if (strend > &buffer[buflen]) /* stay inside buffer */
strend = &buffer[buflen];
/* find end of last full line in this chunk of the buffer */
s = strbegin;
while ((strend[-1]) != NL && (strend > s))
strend--;
if (strend <= strbegin) /* line longer than BUFSIZE, punt */
continue;
k = strbegin + patlen - 1;
for (;;) {
/*
for a large class of patterns, upwards of 80% of
match time is spent on the next line.
we beat existing microcode (vax 'matchc') this way.
*/
while ((k += delta0[*(unsigned char *) k]) < strend)
;
if (k < (strbegin + LARGE))
break;
k -= LARGE;
j = patlen - 1;
s = k - 1;
while (cmap[*s--] == pattern[--j]);
/*
* delta-less shortcut for literati, but
* short shrift for genetic engineers.
*/
if (j >= 0)
k++;
else { /* submatch */
t = k;
s = k - patlen + 1;
do
;
while (*s != NL && --s >= strbegin);
k = s + 1; /* at line start */
if ((k = gotamatch( k,
(t - buffer),
action)) == NULL)
return;
if (k >= strend)
break;
}
}
}
if (buffer[buflen - 1] != last_byte)
buffer[buflen - 1] = last_byte;
return(nsuccess);
}
bmg_fsearch(fd, action)
int fd;
int (*action)();
{
register char *k,
*strend,
*s;
register int j;
char *t;
char *gotamatch();
int nleftover,
count,
file_offset = 0;
nleftover = 0;
nsuccess = 0;
while ((count = read(fd, str + nleftover, BUFSIZE - nleftover)) > 0) {
count += nleftover;
if (count != BUFSIZE && fd != 0) {
str[count++] = NL; /* insurance for broken last line */
}
str[count] = 0;
for (j = count - 1; str[j] != NL && j >= 0;)
j--;
if (j < 0) { /* break up long line */
str[count] = NL;
str[++count] = EOS;
strend = str + count;
linetemp[0] = EOS;
nleftover = 0;
}
else { /* save partial line */
strend = str + j;
nleftover = count - j - 1;
strncpy(linetemp, str + j + 1, nleftover);
}
k = str + patlen - 1;
for (;;) {
/*
for a large class of patterns, upwards of 80% of
match time is spent on the next line.
we beat existing microcode (vax 'matchc') this way.
*/
while ((k += delta0[*(unsigned char *) k]) < strend)
;
if (k < (str + LARGE))
break;
k -= LARGE;
j = patlen - 1;
s = k - 1;
while (cmap[*s--] == pattern[--j]);
/*
* delta-less shortcut for literati, but
* short shrift for genetic engineers.
*/
if (j >= 0)
k++;
else { /* submatch */
t = k;
s = k - patlen + 1;
do
;
while (*s != NL && --s >= str);
k = s + 1; /* at line start */
if ((k = gotamatch( k,
(file_offset + (t - str)),
action)) == NULL)
return;
if (k >= strend)
break;
}
}
file_offset += ( 1 + (strend - str));
strncpy(str, linetemp, nleftover);
}
return(nsuccess);
}
bmg_setup(pat, folded) /* compute "boyer-moore" delta table */
char *pat; /* ... HAKMEM lives ... */
int folded;
{
register int j;
/*
for multibyte characters (e.g. kanji), there are ways.
extrapolating 256 to 64K may be unwise, in favor of more
dynamics within boyermoore() itself.
*/
patlen = strlen(pat);
if (folded) { /* fold case while saving pattern */
for (j = 0; j < patlen; j++) {
pattern[j] = (isupper((int) pat[j])
? (char) tolower((int) pat[j]) : pat[j]);
}
}
else
bcopy(pat, pattern, patlen);
for (j = 0; j < 256; j++) {
delta0[j] = patlen;
cmap[j] = (char) j; /* could be done at compile time */
}
for (j = 0; j < patlen - 1; j++)
delta0[pattern[j]] = patlen - j - 1;
delta0[pattern[patlen - 1]] = LARGE;
if (folded) {
for (j = 0; j < patlen - 1; j++)
if (islower((int) pattern[j]))
delta0[toupper((int) pattern[j])] = patlen - j - 1;
if (islower((int) pattern[patlen - 1]))
delta0[toupper((int) pattern[patlen - 1])] = LARGE;
for (j = 'A'; j <= 'Z'; j++)
cmap[j] = (char) tolower((int) j);
}
}
static char *gotamatch(s, pos, action)
register char *s;
register int pos;
int (*action)();
{
static char result[BUFSIZE];
register char *r = result;
nsuccess++;
do
*r++ = *s;
while (*s++ != NL);
*r = 0;
if (action == NULL) {
printf("%s", result);
return(s);
}
else
if (action(result, pos))
return(s);
else
return(NULL);
}
#ifndef BSD
bcopy(from, to, len)
register char *from;
register char *to;
register int len;
{
while (len-- > 0) {
*to++ = *from++;
}
}
#endif BSD
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
/bin/chmod 664 bmgsubs.c
/bin/echo -n ' '; /bin/ls -ld bmgsubs.c
fi
/bin/echo 'Extracting bmgtest.c'
sed 's/^X//' <<'//go.sysin dd *' >bmgtest.c
X/*
* bmgtest.c
*
* Test driver/example program for Boyer-Moore-Gosper search routines
*
* Works like a stupid grep; takes only a few flags (-i, -n)
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#ifdef BSD
#define strchr index
#endif
int iflag = 0,
nflag = 0;
X/*
* Called by bmg_fsearch for each line that matches the pattern
*/
printit(string, pos)
char *string; /* pointer to temporary copy of line */
int pos; /* byte offset from beginning of file */
{
if (nflag)
printf("%d: %s", pos, string);
else
printf("%s", string);
return(1); /* tells bmg_fsearch to continue the search */
}
main(argc, argv)
int argc;
char **argv;
{
int nsuccess,
fd;
char *pattern;
while ((argc > 1) && (argv[1][0] == '-')) {
switch (argv[1][1]) {
case 'i':
iflag++;
break;
case 'n':
nflag++;
break;
default:
oops("usage: bmgtest [-i] [-n] pattern [file ...]");
}
argc--;
argv++;
}
argv++;
if (argc <= 1)
oops("bmgtest: pattern argument missing");
pattern = *argv;
argc--;
argv++;
bmg_setup(pattern, iflag);
if (argc <= 1) { /* process stdin */
fd = 0;
bmg_fsearch(fd, printit);
}
else {
while (--argc > 0) {
if ((fd = open(*argv, 0)) <= 0) {
fprintf(stderr, "bmgtest: can't open %s\n", *argv);
nsuccess = 2;
argv++;
continue;
}
bmg_fsearch(fd, printit);
close(fd);
argv++;
}
}
exit((nsuccess == 2) ? 2 : (nsuccess == 0));
}
oops(message)
char *message;
{
fprintf(stderr, "%s\n", message);
exit(2);
}
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
/bin/chmod 664 bmgtest.c
/bin/echo -n ' '; /bin/ls -ld bmgtest.c
fi
/bin/echo 'Extracting bmgtest2.c'
sed 's/^X//' <<'//go.sysin dd *' >bmgtest2.c
X/*
* bmgtest2.c
*
* Test driver/example program for Boyer-Moore-Gosper search routines
* does buffer search instead of file search.
*
* Works like a very stupid grep; takes only a few flags (-i, -n)
*/
#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
char *malloc();
#ifdef BSD
#define strchr index
#endif
int iflag = 0,
nflag = 0;
X/*
* Called by bmg_search for each line that matches the pattern
*/
printit(string, pos)
char *string; /* pointer to temporary copy of line */
int pos; /* byte offset from beginning of file */
{
if (nflag)
printf("%d: %s", pos, string);
else
printf("%s", string);
return(1); /* tells bmg_fsearch to continue the search */
}
main(argc, argv)
int argc;
char **argv;
{
int nsuccess,
fd;
char *pattern;
struct stat statbuf;
char *buffer;
while ((argc > 1) && (argv[1][0] == '-')) {
switch (argv[1][1]) {
case 'i':
iflag++;
break;
case 'n':
nflag++;
break;
default:
oops("usage: bmgtest [-i] [-n] pattern [file ...]");
}
argc--;
argv++;
}
argv++;
if (argc <= 1)
oops("bmgtest: pattern argument missing");
pattern = *argv;
argc--;
argv++;
bmg_setup(pattern, iflag);
if (argc <= 1) { /* process stdin */
fd = 0;
bmg_fsearch(fd, printit);
}
else {
while (--argc > 0) {
if ((fd = open(*argv, 0)) <= 0) {
fprintf(stderr, "bmgtest: can't open %s\n", *argv);
nsuccess = 2;
argv++;
continue;
}
fstat(fd, &statbuf);
buffer = malloc(statbuf.st_size);
if (buffer == 0)
oops("malloc failure");
read(fd, buffer, statbuf.st_size);
bmg_search(buffer, statbuf.st_size, printit);
close(fd);
free(buffer);
argv++;
}
}
exit((nsuccess == 2) ? 2 : (nsuccess == 0));
}
oops(message)
char *message;
{
fprintf(stderr, "%s\n", message);
exit(2);
}
//go.sysin dd *
made=TRUE
if [ $made = TRUE ]; then
/bin/chmod 664 bmgtest2.c
/bin/echo -n ' '; /bin/ls -ld bmgtest2.c
fi