summaryrefslogtreecommitdiff
path: root/search.c
blob: 3a15e29b6f9d599c6dc6bb039ef8c11afec9ead4 (plain)
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
744
745
746
747
748
749
750
751
752
753
754
/* $Header: search.c,v 1.0.1.2 88/01/28 10:30:46 root Exp $
 *
 * $Log:	search.c,v $
 * Revision 1.0.1.2  88/01/28  10:30:46  root
 * patch8: uncommented free_compex for use with eval operator.
 * 
 * Revision 1.0.1.1  88/01/24  03:55:05  root
 * patch 2: made depend on perl.h.
 * 
 * Revision 1.0  87/12/18  13:05:59  root
 * Initial revision
 * 
 */

/* string search routines */
 
#include "EXTERN.h"
#include "handy.h"
#include "util.h"
#include "INTERN.h"
#include "search.h"
#include "EXTERN.h"
#include "perl.h"

#define VERBOSE
#define FLUSH
#define MEM_SIZE int

#ifndef BITSPERBYTE
#define BITSPERBYTE 8
#endif

#define BMAPSIZ (127 / BITSPERBYTE + 1)

#define	CHAR	0		/*	a normal character */
#define	ANY	1		/* .	matches anything except newline */
#define	CCL	2		/* [..]	character class */
#define	NCCL	3		/* [^..]negated character class */
#define BEG	4		/* ^	beginning of a line */
#define	END	5		/* $	end of a line */
#define	LPAR	6		/* (	begin sub-match */
#define	RPAR	7		/* )	end sub-match */
#define	REF	8		/* \N	backreference to the Nth submatch */
#define WORD	9		/* \w	matches alphanumeric character */
#define NWORD	10		/* \W	matches non-alphanumeric character */
#define WBOUND	11		/* \b	matches word boundary */
#define NWBOUND	12		/* \B	matches non-boundary  */
#define	FINIS	13		/*	the end of the pattern */
 
#define CODEMASK 15

/* Quantifiers: */

#define MINZERO 16		/* minimum is 0, not 1 */
#define MAXINF	32		/* maximum is infinity, not 1 */
 
#define ASCSIZ 0200
typedef char	TRANSTABLE[ASCSIZ];

static	TRANSTABLE trans = {
0000,0001,0002,0003,0004,0005,0006,0007,
0010,0011,0012,0013,0014,0015,0016,0017,
0020,0021,0022,0023,0024,0025,0026,0027,
0030,0031,0032,0033,0034,0035,0036,0037,
0040,0041,0042,0043,0044,0045,0046,0047,
0050,0051,0052,0053,0054,0055,0056,0057,
0060,0061,0062,0063,0064,0065,0066,0067,
0070,0071,0072,0073,0074,0075,0076,0077,
0100,0101,0102,0103,0104,0105,0106,0107,
0110,0111,0112,0113,0114,0115,0116,0117,
0120,0121,0122,0123,0124,0125,0126,0127,
0130,0131,0132,0133,0134,0135,0136,0137,
0140,0141,0142,0143,0144,0145,0146,0147,
0150,0151,0152,0153,0154,0155,0156,0157,
0160,0161,0162,0163,0164,0165,0166,0167,
0170,0171,0172,0173,0174,0175,0176,0177,
};
static bool folding = FALSE;

static int err;
#define NOERR 0
#define BEGFAIL 1
#define FATAL 2

static char *FirstCharacter;
static char *matchend;
static char *matchtill;

void
search_init()
{
#ifdef UNDEF
    register int    i;
    
    for (i = 0; i < ASCSIZ; i++)
	trans[i] = i;
#else
    ;
#endif
}

void
init_compex(compex)
register COMPEX *compex;
{
    /* the following must start off zeroed */

    compex->precomp = Nullch;
    compex->complen = 0;
    compex->subbase = Nullch;
}

void
free_compex(compex)
register COMPEX *compex;
{
    if (compex->complen) {
	safefree(compex->compbuf);
	compex->complen = 0;
    }
    if (compex->subbase) {
	safefree(compex->subbase);
	compex->subbase = Nullch;
    }
}

static char *gbr_str = Nullch;
static int gbr_siz = 0;

char *
getparen(compex,n)
register COMPEX *compex;
int n;
{
    int length = compex->subend[n] - compex->subbeg[n];

    if (!n &&
	(!compex->numsubs || n > compex->numsubs || !compex->subend[n] || length<0))
	return "";
    growstr(&gbr_str, &gbr_siz, length+1);
    safecpy(gbr_str, compex->subbeg[n], length+1);
    return gbr_str;
}

void
case_fold(which)
int which;
{
    register int i;

    if (which != folding) {
	if (which) {
	    for (i = 'A'; i <= 'Z'; i++)
		trans[i] = tolower(i);
	}
	else {
	    for (i = 'A'; i <= 'Z'; i++)
		trans[i] = i;
	}
	folding = which;
    }
}

/* Compile the regular expression into internal form */

char *
compile(compex, sp, regex, fold)
register COMPEX *compex;
register char   *sp;
int regex;
int fold;
{
    register int c;
    register char  *cp;
    char   *lastcp;
    char    paren[MAXSUB],
	   *parenp;
    char **alt = compex->alternatives;
    char *retmes = "Badly formed search string";
 
    case_fold(compex->do_folding = fold);
    if (compex->precomp)
	safefree(compex->precomp);
    compex->precomp = savestr(sp);
    if (!compex->complen) {
	compex->compbuf = safemalloc(84);
	compex->complen = 80;
    }
    cp = compex->compbuf;		/* point at compiled buffer */
    *alt++ = cp;			/* first alternative starts here */
    parenp = paren;			/* first paren goes here */
    if (*sp == 0) {			/* nothing to compile? */
#ifdef NOTDEF
	if (*cp == 0)			/* nothing there yet? */
	    return "Null search string";
#endif
	if (*cp)
	    return Nullch;			/* just keep old expression */
    }
    compex->numsubs = 0;			/* no parens yet */
    lastcp = 0;
    for (;;) {
	if (cp - compex->compbuf >= compex->complen) {
	    char *ocompbuf = compex->compbuf;

	    grow_comp(compex);
	    if (ocompbuf != compex->compbuf) {	/* adjust pointers? */
		char **tmpalt;

		cp = compex->compbuf + (cp - ocompbuf);
		if (lastcp)
		    lastcp = compex->compbuf + (lastcp - ocompbuf);
		for (tmpalt = compex->alternatives; tmpalt < alt; tmpalt++)
		    if (*tmpalt)
			*tmpalt = compex->compbuf + (*tmpalt - ocompbuf);
	    }
	}
	c = *sp++;			/* get next char of pattern */
	if (c == 0) {			/* end of pattern? */
	    if (parenp != paren) {	/* balanced parentheses? */
#ifdef VERBOSE
		retmes = "Missing right parenthesis";
#endif
		goto badcomp;
	    }
	    *cp++ = FINIS;		/* append a stopper */
	    *alt++ = 0;			/* terminate alternative list */
	    /*
	    compex->complen = cp - compex->compbuf + 1;
	    compex->compbuf = saferealloc(compex->compbuf,compex->complen+4); */
	    return Nullch;		/* return success */
	}
	if (c != '*' && c != '?' && c != '+')
	    lastcp = cp;
	if (!regex) {			/* just a normal search string? */
	    *cp++ = CHAR;		/* everything is a normal char */
	    *cp++ = trans[c];
	}
	else				/* it is a regular expression */
	    switch (c) {
 
		default:
		  normal_char:
		    *cp++ = CHAR;
		    *cp++ = trans[c];
		    continue;

		case '.':
		    *cp++ = ANY;
		    continue;
 
		case '[': {		/* character class */
		    register int i;
		    
		    if (cp - compex->compbuf >= compex->complen - BMAPSIZ) {
			char *ocompbuf = compex->compbuf;

			grow_comp(compex);	/* reserve bitmap */
			if (ocompbuf != compex->compbuf) {/* adjust pointers? */
			    char **tmpalt;

			    cp = compex->compbuf + (cp - ocompbuf);
			    if (lastcp)
				lastcp = compex->compbuf + (lastcp - ocompbuf);
			    for (tmpalt = compex->alternatives; tmpalt < alt;
			      tmpalt++)
				if (*tmpalt)
				    *tmpalt =
					compex->compbuf + (*tmpalt - ocompbuf);
			}
		    }
		    for (i = BMAPSIZ; i; --i)
			cp[i] = 0;
		    
		    if ((c = *sp++) == '^') {
			c = *sp++;
			*cp++ = NCCL;	/* negated */
		    }
		    else
			*cp++ = CCL;	/* normal */
		    
		    i = 0;		/* remember oldchar */
		    do {
			if (c == '\0') {
#ifdef VERBOSE
			    retmes = "Missing ]";
#endif
			    goto badcomp;
			}
			if (c == '\\' && *sp) {
			    switch (*sp) {
			    default:
				c = *sp++;
				break;
			    case '0': case '1': case '2': case '3':
			    case '4': case '5': case '6': case '7':
				c = *sp++ - '0';
				if (index("01234567",*sp)) {
				    c <<= 3;
				    c += *sp++ - '0';
				}
				if (index("01234567",*sp)) {
				    c <<= 3;
				    c += *sp++ - '0';
				}
				break;
			    case 'b':
				c = '\b';
				sp++;
				break;
			    case 'n':
				c = '\n';
				sp++;
				break;
			    case 'r':
				c = '\r';
				sp++;
				break;
			    case 'f':
				c = '\f';
				sp++;
				break;
			    case 't':
				c = '\t';
				sp++;
				break;
			    }
			}
			if (*sp == '-' && *(++sp))
			    i = *sp++;
			else
			    i = c;
			while (c <= i) {
			    cp[c / BITSPERBYTE] |= 1 << (c % BITSPERBYTE);
			    if (fold && isalpha(c))
				cp[(c ^ 32) / BITSPERBYTE] |=
				    1 << ((c ^ 32) % BITSPERBYTE);
					/* set the other bit too */
			    c++;
			}
		    } while ((c = *sp++) != ']');
		    if (cp[-1] == NCCL)
			cp[0] |= 1;
		    cp += BMAPSIZ;
		    continue;
		}
 
		case '^':
		    if (cp != compex->compbuf && cp[-1] != FINIS)
			goto normal_char;
		    *cp++ = BEG;
		    continue;
 
		case '$':
		    if (isdigit(*sp)) {
			*cp++ = REF;
			*cp++ = *sp - '0';
			break;
		    }
		    if (*sp && *sp != '|')
			goto normal_char;
		    *cp++ = END;
		    continue;
 
		case '*': case '?': case '+':
		    if (lastcp == 0 ||
			(*lastcp & (MINZERO|MAXINF)) ||
			*lastcp == LPAR ||
			*lastcp == RPAR ||
			*lastcp == BEG ||
			*lastcp == END ||
			*lastcp == WBOUND ||
			*lastcp == NWBOUND )
			goto normal_char;
		    if (c != '+')
			*lastcp |= MINZERO;
		    if (c != '?')
			*lastcp |= MAXINF;
		    continue;
 
		case '(':
		    if (compex->numsubs >= MAXSUB) {
#ifdef VERBOSE
			retmes = "Too many parens";
#endif
			goto badcomp;
		    }
		    *parenp++ = ++compex->numsubs;
		    *cp++ = LPAR;
		    *cp++ = compex->numsubs;
		    break;
		case ')':
		    if (parenp <= paren) {
#ifdef VERBOSE
			retmes = "Unmatched right paren";
#endif
			goto badcomp;
		    }
		    *cp++ = RPAR;
		    *cp++ = *--parenp;
		    break;
		case '|':
		    if (parenp>paren) {
#ifdef VERBOSE
			retmes = "No | in subpattern";	/* Sigh! */
#endif
			goto badcomp;
		    }
		    *cp++ = FINIS;
		    if (alt - compex->alternatives >= MAXALT) {
#ifdef VERBOSE
			retmes = "Too many alternatives";
#endif
			goto badcomp;
		    }
		    *alt++ = cp;
		    break;
		case '\\':		/* backslashed thingie */
		    switch (c = *sp++) {
		    case '0': case '1': case '2': case '3': case '4':
		    case '5': case '6': case '7': case '8': case '9':
			*cp++ = REF;
			*cp++ = c - '0';
			break;
		    case 'w':
			*cp++ = WORD;
			break;
		    case 'W':
			*cp++ = NWORD;
			break;
		    case 'b':
			*cp++ = WBOUND;
			break;
		    case 'B':
			*cp++ = NWBOUND;
			break;
		    default:
			*cp++ = CHAR;
			if (c == '\0')
			    goto badcomp;
			switch (c) {
			case 'n':
			    c = '\n';
			    break;
			case 'r':
			    c = '\r';
			    break;
			case 'f':
			    c = '\f';
			    break;
			case 't':
			    c = '\t';
			    break;
			}
			*cp++ = c;
			break;
		    }
		    break;
	    }
    }
badcomp:
    compex->compbuf[0] = 0;
    compex->numsubs = 0;
    return retmes;
}

void
grow_comp(compex)
register COMPEX *compex;
{
    compex->complen += 80;
    compex->compbuf = saferealloc(compex->compbuf, (MEM_SIZE)compex->complen + 4);
}

char *
execute(compex, addr, beginning, minend)
register COMPEX *compex;
char *addr;
bool beginning;
int minend;
{
    register char *p1 = addr;
    register char *trt = trans;
    register int c;
    register int scr;
    register int c2;
 
    if (addr == Nullch)
	return Nullch;
    if (compex->numsubs) {			/* any submatches? */
	for (c = 0; c <= compex->numsubs; c++)
	    compex->subbeg[c] = compex->subend[c] = Nullch;
    }
    case_fold(compex->do_folding);	/* make sure table is correct */
    if (beginning)
	FirstCharacter = p1;		/* for ^ tests */
    else {
	if (multiline || compex->alternatives[1] || compex->compbuf[0] != BEG)
	    FirstCharacter = Nullch;
	else
	    return Nullch;		/* can't match */
    }
    matchend = Nullch;
    matchtill = addr + minend;
    err = 0;
    if (compex->compbuf[0] == CHAR && !compex->alternatives[1]) {
	if (compex->do_folding) {
	    c = compex->compbuf[1];	/* fast check for first character */
	    do {
		if (trt[*p1] == c && try(compex, p1, compex->compbuf))
		    goto got_it;
	    } while (*p1++ && !err);
	}
	else {
	    c = compex->compbuf[1];	/* faster check for first character */
	    if (compex->compbuf[2] == CHAR)
		c2 = compex->compbuf[3];
	    else
		c2 = 0;
	    do {
	      false_alarm:
		while (scr = *p1++, scr && scr != c) ;
		if (!scr)
		    break;
		if (c2 && *p1 != c2)	/* and maybe even second character */
		    goto false_alarm;
		if (try(compex, p1, compex->compbuf+2)) {
		    p1--;
		    goto got_it;
		}
	    } while (!err);
	}
	return Nullch;
    }
    else {			/* normal algorithm */
	do {
	    register char **alt = compex->alternatives;
	    while (*alt) {
		if (try(compex, p1, *alt++))
		    goto got_it;
	    }
	} while (*p1++ && err < FATAL);
	return Nullch;
    }

got_it:
    if (compex->numsubs) {			/* any parens? */
	trt = savestr(addr);		/* in case addr is not static */
	if (compex->subbase)
	    safefree(compex->subbase);	/* (may be freeing addr!) */
	compex->subbase = trt;
	scr = compex->subbase - addr;
	p1 += scr;
	matchend += scr;
	for (c = 0; c <= compex->numsubs; c++) {
	    if (compex->subend[c]) {
		compex->subbeg[c] += scr;
		compex->subend[c] += scr;
	    }
	}
    }
    compex->subend[0] = matchend;
    compex->subbeg[0] = p1;
    return p1;
}
 
bool
try(compex, sp, cp)
COMPEX *compex;
register char *cp;
register char *sp;
{
    register char *basesp;
    register char *trt = trans;
    register int i;
    register int backlen;
    register int code;
 
    while (*sp || (*cp & MAXINF) || *cp == BEG || *cp == RPAR ||
	*cp == WBOUND || *cp == NWBOUND) {
	switch ((code = *cp++) & CODEMASK) {
 
	    case CHAR:
		basesp = sp;
		i = *cp++;
		if (code & MAXINF)
		    while (*sp && trt[*sp] == i) sp++;
		else
		    if (*sp && trt[*sp] == i) sp++;
		backlen = 1;
		goto backoff;
 
	  backoff:
		while (sp > basesp) {
		    if (try(compex, sp, cp))
			goto right;
		    sp -= backlen;
		}
		if (code & MINZERO)
		    continue;
		goto wrong;
 
	    case ANY:
		basesp = sp;
		if (code & MAXINF)
		    while (*sp && *sp != '\n') sp++;
		else
		    if (*sp && *sp != '\n') sp++;
		backlen = 1;
		goto backoff;

	    case CCL:
		basesp = sp;
		if (code & MAXINF)
		    while (*sp && cclass(cp, *sp, 1)) sp++;
		else
		    if (*sp && cclass(cp, *sp, 1)) sp++;
		cp += BMAPSIZ;
		backlen = 1;
		goto backoff;
 
	    case NCCL:
		basesp = sp;
		if (code & MAXINF)
		    while (*sp && cclass(cp, *sp, 0)) sp++;
		else
		    if (*sp && cclass(cp, *sp, 0)) sp++;
		cp += BMAPSIZ;
		backlen = 1;
		goto backoff;
 
	    case END:
		if (!*sp || *sp == '\n') {
		    matchtill--;
		    continue;
		}
		goto wrong;
 
	    case BEG:
		if (sp == FirstCharacter || (
		    *sp && sp[-1] == '\n') ) {
		    matchtill--;
		    continue;
		}
		if (!multiline)		/* no point in advancing more */
		    err = BEGFAIL;
		goto wrong;
 
	    case WORD:
		basesp = sp;
		if (code & MAXINF)
		    while (*sp && isalnum(*sp)) sp++;
		else
		    if (*sp && isalnum(*sp)) sp++;
		backlen = 1;
		goto backoff;
 
	    case NWORD:
		basesp = sp;
		if (code & MAXINF)
		    while (*sp && !isalnum(*sp)) sp++;
		else
		    if (*sp && !isalnum(*sp)) sp++;
		backlen = 1;
		goto backoff;
 
	    case WBOUND:
		if ((sp == FirstCharacter || !isalnum(sp[-1])) !=
			(!*sp || !isalnum(*sp)) )
		    continue;
		goto wrong;
 
	    case NWBOUND:
		if ((sp == FirstCharacter || !isalnum(sp[-1])) ==
			(!*sp || !isalnum(*sp)))
		    continue;
		goto wrong;
 
	    case FINIS:
		goto right;
 
	    case LPAR:
		compex->subbeg[*cp++] = sp;
		continue;
 
	    case RPAR:
		i = *cp++;
		compex->subend[i] = sp;
		compex->lastparen = i;
		continue;
 
	    case REF:
		if (compex->subend[i = *cp++] == 0) {
		    fputs("Bad subpattern reference\n",stdout) FLUSH;
		    err = FATAL;
		    goto wrong;
		}
		basesp = sp;
		backlen = compex->subend[i] - compex->subbeg[i];
		if (code & MAXINF)
		    while (*sp && subpat(compex, i, sp)) sp += backlen;
		else
		    if (*sp && subpat(compex, i, sp)) sp += backlen;
		goto backoff;
 
	    default:
		fputs("Botched pattern compilation\n",stdout) FLUSH;
		err = FATAL;
		return -1;
	}
    }
    if (*cp == FINIS || *cp == END) {
right:
	if (matchend == Nullch || sp > matchend)
	    matchend = sp;
	return matchend >= matchtill;
    }
wrong:
    matchend = Nullch;
    return FALSE;
}
 
bool
subpat(compex, i, sp)
register COMPEX *compex;
register int i;
register char *sp;
{
    register char *bp;
 
    bp = compex->subbeg[i];
    while (*sp && *bp == *sp) {
	bp++;
	sp++;
	if (bp >= compex->subend[i])
	    return TRUE;
    }
    return FALSE;
}

bool
cclass(set, c, af)
register char  *set;
register int c;
{
    c &= 0177;
#if BITSPERBYTE == 8
    if (set[c >> 3] & 1 << (c & 7))
#else
    if (set[c / BITSPERBYTE] & 1 << (c % BITSPERBYTE))
#endif
	return af;
    return !af;
}