summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/perl/regcomp.c
blob: 8337e9cfd0d51edc80d83deed8809cc9afd53a9a (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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
/* NOTE: this is derived from Henry Spencer's regexp code, and should not
 * confused with the original package (see point 3 below).  Thanks, Henry!
 */

/* Additional note: this code is very heavily munged from Henry's version
 * in places.  In some spots I've traded clarity for efficiency, so don't
 * blame Henry for some of the lack of readability.
 */

/* $RCSfile: regcomp.c,v $$Revision: 1.1.1.1 $$Date: 1993/08/23 21:29:39 $
 *
 * $Log: regcomp.c,v $
 * Revision 1.1.1.1  1993/08/23  21:29:39  nate
 * PERL!
 *
 * Revision 4.0.1.5  92/06/08  15:23:36  lwall
 * patch20: Perl now distinguishes overlapped copies from non-overlapped
 * patch20: /^stuff/ wrongly assumed an implicit $* == 1
 * patch20: /x{0}/ was wrongly interpreted as /x{0,}/
 * patch20: added \W, \S and \D inside /[...]/
 * 
 * Revision 4.0.1.4  91/11/05  22:55:14  lwall
 * patch11: Erratum
 * 
 * Revision 4.0.1.3  91/11/05  18:22:28  lwall
 * patch11: minimum match length calculation in regexp is now cumulative
 * patch11: initial .* in pattern had dependency on value of $*
 * patch11: certain patterns made use of garbage pointers from uncleared memory
 * patch11: prepared for ctype implementations that don't define isascii()
 * 
 * Revision 4.0.1.2  91/06/07  11:48:24  lwall
 * patch4: new copyright notice
 * patch4: /(x+) \1/ incorrectly optimized to not match "xxx xx"
 * patch4: // wouldn't use previous pattern if it started with a null character
 * 
 * Revision 4.0.1.1  91/04/12  09:04:45  lwall
 * patch1: random cleanup in cpp namespace
 * 
 * Revision 4.0  91/03/20  01:39:01  lwall
 * 4.0 baseline.
 * 
 */
/*SUPPRESS 112*/
/*
 * regcomp and regexec -- regsub and regerror are not used in perl
 *
 *	Copyright (c) 1986 by University of Toronto.
 *	Written by Henry Spencer.  Not derived from licensed software.
 *
 *	Permission is granted to anyone to use this software for any
 *	purpose on any computer system, and to redistribute it freely,
 *	subject to the following restrictions:
 *
 *	1. The author is not responsible for the consequences of use of
 *		this software, no matter how awful, even if they arise
 *		from defects in it.
 *
 *	2. The origin of this software must not be misrepresented, either
 *		by explicit claim or by omission.
 *
 *	3. Altered versions must be plainly marked as such, and must not
 *		be misrepresented as being the original software.
 *
 *
 ****    Alterations to Henry's code are...
 ****
 ****    Copyright (c) 1991, Larry Wall
 ****
 ****    You may distribute under the terms of either the GNU General Public
 ****    License or the Artistic License, as specified in the README file.

 *
 * Beware that some of this code is subtly aware of the way operator
 * precedence is structured in regular expressions.  Serious changes in
 * regular-expression syntax might require a total rethink.
 */
#include "EXTERN.h"
#include "perl.h"
#include "INTERN.h"
#include "regcomp.h"

#ifdef MSDOS
# if defined(BUGGY_MSC6)
 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
 # pragma optimize("a",off)
 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
 # pragma optimize("w",on )
# endif /* BUGGY_MSC6 */
#endif /* MSDOS */

#ifndef STATIC
#define	STATIC	static
#endif

#define	ISMULT1(c)	((c) == '*' || (c) == '+' || (c) == '?')
#define	ISMULT2(s)	((*s) == '*' || (*s) == '+' || (*s) == '?' || \
	((*s) == '{' && regcurly(s)))
#ifdef atarist
#define	PERL_META	"^$.[()|?+*\\"
#else
#define	META	"^$.[()|?+*\\"
#endif

#ifdef SPSTART
#undef SPSTART		/* dratted cpp namespace... */
#endif
/*
 * Flags to be passed up and down.
 */
#define	HASWIDTH	01	/* Known never to match null string. */
#define	SIMPLE		02	/* Simple enough to be STAR/PLUS operand. */
#define	SPSTART		04	/* Starts with * or +. */
#define	WORST		0	/* Worst case. */

/*
 * Global work variables for regcomp().
 */
static char *regprecomp;		/* uncompiled string. */
static char *regparse;		/* Input-scan pointer. */
static char *regxend;		/* End of input for compile */
static int regnpar;		/* () count. */
static char *regcode;		/* Code-emit pointer; &regdummy = don't. */
static long regsize;		/* Code size. */
static int regfold;
static int regsawbracket;	/* Did we do {d,d} trick? */
static int regsawback;		/* Did we see \1, ...? */

/*
 * Forward declarations for regcomp()'s friends.
 */
STATIC int regcurly();
STATIC char *reg();
STATIC char *regbranch();
STATIC char *regpiece();
STATIC char *regatom();
STATIC char *regclass();
STATIC char *regnode();
STATIC char *reganode();
STATIC void regc();
STATIC void reginsert();
STATIC void regtail();
STATIC void regoptail();

/*
 - regcomp - compile a regular expression into internal code
 *
 * We can't allocate space until we know how big the compiled form will be,
 * but we can't compile it (and thus know how big it is) until we've got a
 * place to put the code.  So we cheat:  we compile it twice, once with code
 * generation turned off and size counting turned on, and once "for real".
 * This also means that we don't allocate space until we are sure that the
 * thing really will compile successfully, and we never have to move the
 * code and thus invalidate pointers into it.  (Note that it has to be in
 * one piece because free() must be able to free it all.) [NB: not true in perl]
 *
 * Beware that the optimization-preparation code in here knows about some
 * of the structure of the compiled regexp.  [I'll say.]
 */
regexp *
regcomp(exp,xend,fold)
char *exp;
char *xend;
int fold;
{
	register regexp *r;
	register char *scan;
	register STR *longish;
	STR *longest;
	register int len;
	register char *first;
	int flags;
	int backish;
	int backest;
	int curback;
	int minlen;
	int sawplus = 0;
	int sawopen = 0;

	if (exp == NULL)
		fatal("NULL regexp argument");

	/* First pass: determine size, legality. */
	regfold = fold;
	regparse = exp;
	regxend = xend;
	regprecomp = nsavestr(exp,xend-exp);
	regsawbracket = 0;
	regsawback = 0;
	regnpar = 1;
	regsize = 0L;
	regcode = &regdummy;
	regc((char)MAGIC);
	if (reg(0, &flags) == NULL) {
		Safefree(regprecomp);
		regprecomp = Nullch;
		return(NULL);
	}

	/* Small enough for pointer-storage convention? */
	if (regsize >= 32767L)		/* Probably could be 65535L. */
		FAIL("regexp too big");

	/* Allocate space. */
	Newc(1001, r, sizeof(regexp) + (unsigned)regsize, char, regexp);
	if (r == NULL)
		FAIL("regexp out of space");

	/* Second pass: emit code. */
	if (regsawbracket)
	    Copy(regprecomp,exp,xend-exp,char);
	r->prelen = xend-exp;
	r->precomp = regprecomp;
	r->subbeg = r->subbase = NULL;
	regparse = exp;
	regnpar = 1;
	regcode = r->program;
	regc((char)MAGIC);
	if (reg(0, &flags) == NULL)
		return(NULL);

	/* Dig out information for optimizations. */
	r->regstart = Nullstr;	/* Worst-case defaults. */
	r->reganch = 0;
	r->regmust = Nullstr;
	r->regback = -1;
	r->regstclass = Nullch;
	scan = r->program+1;			/* First BRANCH. */
	if (OP(regnext(scan)) == END) {/* Only one top-level choice. */
		scan = NEXTOPER(scan);

		first = scan;
		while ((OP(first) == OPEN && (sawopen = 1)) ||
		    (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
		    (OP(first) == PLUS) ||
		    (OP(first) == CURLY && ARG1(first) > 0) ) {
			if (OP(first) == PLUS)
			    sawplus = 1;
			else
			    first += regarglen[OP(first)];
			first = NEXTOPER(first);
		}

		/* Starting-point info. */
	    again:
		if (OP(first) == EXACTLY) {
			r->regstart =
			    str_make(OPERAND(first)+1,*OPERAND(first));
			if (r->regstart->str_cur > !(sawstudy|fold))
				fbmcompile(r->regstart,fold);
		}
		else if ((exp = index(simple,OP(first))) && exp > simple)
			r->regstclass = first;
		else if (OP(first) == BOUND || OP(first) == NBOUND)
			r->regstclass = first;
		else if (OP(first) == BOL) {
			r->reganch = ROPT_ANCH;
			first = NEXTOPER(first);
		    	goto again;
		}
		else if ((OP(first) == STAR && OP(NEXTOPER(first)) == ANY) &&
			 !(r->reganch & ROPT_ANCH) ) {
			/* turn .* into ^.* with an implied $*=1 */
			r->reganch = ROPT_ANCH | ROPT_IMPLICIT;
			first = NEXTOPER(first);
		    	goto again;
		}
		if (sawplus && (!sawopen || !regsawback))
		    r->reganch |= ROPT_SKIP;	/* x+ must match 1st of run */

#ifdef DEBUGGING
		if (debug & 512)
		    fprintf(stderr,"first %d next %d offset %d\n",
		      OP(first), OP(NEXTOPER(first)), first - scan);
#endif
		/*
		 * If there's something expensive in the r.e., find the
		 * longest literal string that must appear and make it the
		 * regmust.  Resolve ties in favor of later strings, since
		 * the regstart check works with the beginning of the r.e.
		 * and avoiding duplication strengthens checking.  Not a
		 * strong reason, but sufficient in the absence of others.
		 * [Now we resolve ties in favor of the earlier string if
		 * it happens that curback has been invalidated, since the
		 * earlier string may buy us something the later one won't.]
		 */
		longish = str_make("",0);
		longest = str_make("",0);
		len = 0;
		minlen = 0;
		curback = 0;
		backish = 0;
		backest = 0;
		while (OP(scan) != END) {
			if (OP(scan) == BRANCH) {
			    if (OP(regnext(scan)) == BRANCH) {
				curback = -30000;
				while (OP(scan) == BRANCH)
				    scan = regnext(scan);
			    }
			    else	/* single branch is ok */
				scan = NEXTOPER(scan);
			}
			if (OP(scan) == EXACTLY) {
			    char *t;

			    first = scan;
			    while (OP(t = regnext(scan)) == CLOSE)
				scan = t;
			    minlen += *OPERAND(first);
			    if (curback - backish == len) {
				str_ncat(longish, OPERAND(first)+1,
				    *OPERAND(first));
				len += *OPERAND(first);
				curback += *OPERAND(first);
				first = regnext(scan);
			    }
			    else if (*OPERAND(first) >= len + (curback >= 0)) {
				len = *OPERAND(first);
				str_nset(longish, OPERAND(first)+1,len);
				backish = curback;
				curback += len;
				first = regnext(scan);
			    }
			    else
				curback += *OPERAND(first);
			}
			else if (index(varies,OP(scan))) {
			    curback = -30000;
			    len = 0;
			    if (longish->str_cur > longest->str_cur) {
				str_sset(longest,longish);
				backest = backish;
			    }
			    str_nset(longish,"",0);
			    if (OP(scan) == PLUS &&
			      index(simple,OP(NEXTOPER(scan))))
				minlen++;
			    else if (OP(scan) == CURLY &&
			      index(simple,OP(NEXTOPER(scan)+4)))
				minlen += ARG1(scan);
			}
			else if (index(simple,OP(scan))) {
			    curback++;
			    minlen++;
			    len = 0;
			    if (longish->str_cur > longest->str_cur) {
				str_sset(longest,longish);
				backest = backish;
			    }
			    str_nset(longish,"",0);
			}
			scan = regnext(scan);
		}

		/* Prefer earlier on tie, unless we can tail match latter */

		if (longish->str_cur + (OP(first) == EOL) > longest->str_cur) {
		    str_sset(longest,longish);
		    backest = backish;
		}
		else
		    str_nset(longish,"",0);
		if (longest->str_cur
		    &&
		    (!r->regstart
		     ||
		     !fbminstr((unsigned char*) r->regstart->str_ptr,
			  (unsigned char *) r->regstart->str_ptr
			    + r->regstart->str_cur,
			  longest)
		    )
		   )
		{
			r->regmust = longest;
			if (backest < 0)
				backest = -1;
			r->regback = backest;
			if (longest->str_cur
			  > !(sawstudy || fold || OP(first) == EOL) )
				fbmcompile(r->regmust,fold);
			r->regmust->str_u.str_useful = 100;
			if (OP(first) == EOL && longish->str_cur)
			    r->regmust->str_pok |= SP_TAIL;
		}
		else {
			str_free(longest);
			longest = Nullstr;
		}
		str_free(longish);
	}

	r->do_folding = fold;
	r->nparens = regnpar - 1;
	r->minlen = minlen;
	Newz(1002, r->startp, regnpar, char*);
	Newz(1002, r->endp, regnpar, char*);
#ifdef DEBUGGING
	if (debug & 512)
		regdump(r);
#endif
	return(r);
}

/*
 - reg - regular expression, i.e. main body or parenthesized thing
 *
 * Caller must absorb opening parenthesis.
 *
 * Combining parenthesis handling with the base level of regular expression
 * is a trifle forced, but the need to tie the tails of the branches to what
 * follows makes it hard to avoid.
 */
static char *
reg(paren, flagp)
int paren;			/* Parenthesized? */
int *flagp;
{
	register char *ret;
	register char *br;
	register char *ender;
	register int parno;
	int flags;

	*flagp = HASWIDTH;	/* Tentatively. */

	/* Make an OPEN node, if parenthesized. */
	if (paren) {
		parno = regnpar;
		regnpar++;
		ret = reganode(OPEN, parno);
	} else
		ret = NULL;

	/* Pick up the branches, linking them together. */
	br = regbranch(&flags);
	if (br == NULL)
		return(NULL);
	if (ret != NULL)
		regtail(ret, br);	/* OPEN -> first. */
	else
		ret = br;
	if (!(flags&HASWIDTH))
		*flagp &= ~HASWIDTH;
	*flagp |= flags&SPSTART;
	while (*regparse == '|') {
		regparse++;
		br = regbranch(&flags);
		if (br == NULL)
			return(NULL);
		regtail(ret, br);	/* BRANCH -> BRANCH. */
		if (!(flags&HASWIDTH))
			*flagp &= ~HASWIDTH;
		*flagp |= flags&SPSTART;
	}

	/* Make a closing node, and hook it on the end. */
	if (paren)
	    ender = reganode(CLOSE, parno);
	else
	    ender = regnode(END);
	regtail(ret, ender);

	/* Hook the tails of the branches to the closing node. */
	for (br = ret; br != NULL; br = regnext(br))
		regoptail(br, ender);

	/* Check for proper termination. */
	if (paren && *regparse++ != ')') {
		FAIL("unmatched () in regexp");
	} else if (!paren && regparse < regxend) {
		if (*regparse == ')') {
			FAIL("unmatched () in regexp");
		} else
			FAIL("junk on end of regexp");	/* "Can't happen". */
		/* NOTREACHED */
	}

	return(ret);
}

/*
 - regbranch - one alternative of an | operator
 *
 * Implements the concatenation operator.
 */
static char *
regbranch(flagp)
int *flagp;
{
	register char *ret;
	register char *chain;
	register char *latest;
	int flags;

	*flagp = WORST;		/* Tentatively. */

	ret = regnode(BRANCH);
	chain = NULL;
	while (regparse < regxend && *regparse != '|' && *regparse != ')') {
		latest = regpiece(&flags);
		if (latest == NULL)
			return(NULL);
		*flagp |= flags&HASWIDTH;
		if (chain == NULL)	/* First piece. */
			*flagp |= flags&SPSTART;
		else
			regtail(chain, latest);
		chain = latest;
	}
	if (chain == NULL)	/* Loop ran zero times. */
		(void) regnode(NOTHING);

	return(ret);
}

/*
 - regpiece - something followed by possible [*+?]
 *
 * Note that the branching code sequences used for ? and the general cases
 * of * and + are somewhat optimized:  they use the same NOTHING node as
 * both the endmarker for their branch list and the body of the last branch.
 * It might seem that this node could be dispensed with entirely, but the
 * endmarker role is not redundant.
 */
static char *
regpiece(flagp)
int *flagp;
{
	register char *ret;
	register char op;
	register char *next;
	int flags;
	char *origparse = regparse;
	int orignpar = regnpar;
	char *max;
	int iter;
	char ch;

	ret = regatom(&flags);
	if (ret == NULL)
		return(NULL);

	op = *regparse;

	/* Here's a total kludge: if after the atom there's a {\d+,?\d*}
	 * then we decrement the first number by one and reset our
	 * parsing back to the beginning of the same atom.  If the first number
	 * is down to 0, decrement the second number instead and fake up
	 * a ? after it.  Given the way this compiler doesn't keep track
	 * of offsets on the first pass, this is the only way to replicate
	 * a piece of code.  Sigh.
	 */
	if (op == '{' && regcurly(regparse)) {
	    next = regparse + 1;
	    max = Nullch;
	    while (isDIGIT(*next) || *next == ',') {
		if (*next == ',') {
		    if (max)
			break;
		    else
			max = next;
		}
		next++;
	    }
	    if (*next == '}') {		/* got one */
		if (!max)
		    max = next;
		regparse++;
		iter = atoi(regparse);
		if (flags&SIMPLE) {	/* we can do it right after all */
		    int tmp;

		    reginsert(CURLY, ret);
		    if (iter > 0)
			*flagp = (WORST|HASWIDTH);
		    if (*max == ',')
			max++;
		    else
			max = regparse;
		    tmp = atoi(max);
		    if (!tmp && *max != '0')
			tmp = 32767;		/* meaning "infinity" */
		    if (tmp && tmp < iter)
			fatal("Can't do {n,m} with n > m");
		    if (regcode != &regdummy) {
#ifdef REGALIGN
			*(unsigned short *)(ret+3) = iter;
			*(unsigned short *)(ret+5) = tmp;
#else
			ret[3] = iter >> 8; ret[4] = iter & 0377;
			ret[5] = tmp  >> 8; ret[6] = tmp  & 0377;
#endif
		    }
		    regparse = next;
		    goto nest_check;
		}
		regsawbracket++;	/* remember we clobbered exp */
		if (iter > 0) {
		    ch = *max;
		    sprintf(regparse,"%.*d", max-regparse, iter - 1);
		    *max = ch;
		    if (*max == ',' && max[1] != '}') {
			if (atoi(max+1) <= 0)
			    fatal("Can't do {n,m} with n > m");
			ch = *next;
			sprintf(max+1,"%.*d", next-(max+1), atoi(max+1) - 1);
			*next = ch;
		    }
		    if (iter != 1 || *max == ',') {
			regparse = origparse;	/* back up input pointer */
			regnpar = orignpar;	/* don't make more parens */
		    }
		    else {
			regparse = next;
			goto nest_check;
		    }
		    *flagp = flags;
		    return ret;
		}
		if (*max == ',') {
		    max++;
		    iter = atoi(max);
		    if (max == next) {		/* any number more? */
			regparse = next;
			op = '*';		/* fake up one with a star */
		    }
		    else if (iter > 0) {
			op = '?';		/* fake up optional atom */
			ch = *next;
			sprintf(max,"%.*d", next-max, iter - 1);
			*next = ch;
			if (iter == 1)
			    regparse = next;
			else {
			    regparse = origparse - 1; /* offset ++ below */
			    regnpar = orignpar;
			}
		    }
		    else
			fatal("Can't do {n,0}");
		}
		else
		    fatal("Can't do {0}");
	    }
	}

	if (!ISMULT1(op)) {
		*flagp = flags;
		return(ret);
	}

	if (!(flags&HASWIDTH) && op != '?')
		FAIL("regexp *+ operand could be empty");
	*flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);

	if (op == '*' && (flags&SIMPLE))
		reginsert(STAR, ret);
	else if (op == '*') {
		/* Emit x* as (x&|), where & means "self". */
		reginsert(BRANCH, ret);			/* Either x */
		regoptail(ret, regnode(BACK));		/* and loop */
		regoptail(ret, ret);			/* back */
		regtail(ret, regnode(BRANCH));		/* or */
		regtail(ret, regnode(NOTHING));		/* null. */
	} else if (op == '+' && (flags&SIMPLE))
		reginsert(PLUS, ret);
	else if (op == '+') {
		/* Emit x+ as x(&|), where & means "self". */
		next = regnode(BRANCH);			/* Either */
		regtail(ret, next);
		regtail(regnode(BACK), ret);		/* loop back */
		regtail(next, regnode(BRANCH));		/* or */
		regtail(ret, regnode(NOTHING));		/* null. */
	} else if (op == '?') {
		/* Emit x? as (x|) */
		reginsert(BRANCH, ret);			/* Either x */
		regtail(ret, regnode(BRANCH));		/* or */
		next = regnode(NOTHING);		/* null. */
		regtail(ret, next);
		regoptail(ret, next);
	}
      nest_check:
	regparse++;
	if (ISMULT2(regparse))
		FAIL("nested *?+ in regexp");

	return(ret);
}

/*
 - regatom - the lowest level
 *
 * Optimization:  gobbles an entire sequence of ordinary characters so that
 * it can turn them into a single node, which is smaller to store and
 * faster to run.  Backslashed characters are exceptions, each becoming a
 * separate node; the code is simpler that way and it's not worth fixing.
 *
 * [Yes, it is worth fixing, some scripts can run twice the speed.]
 */
static char *
regatom(flagp)
int *flagp;
{
	register char *ret;
	int flags;

	*flagp = WORST;		/* Tentatively. */

	switch (*regparse++) {
	case '^':
		ret = regnode(BOL);
		break;
	case '$':
		ret = regnode(EOL);
		break;
	case '.':
		ret = regnode(ANY);
		*flagp |= HASWIDTH|SIMPLE;
		break;
	case '[':
		ret = regclass();
		*flagp |= HASWIDTH|SIMPLE;
		break;
	case '(':
		ret = reg(1, &flags);
		if (ret == NULL)
			return(NULL);
		*flagp |= flags&(HASWIDTH|SPSTART);
		break;
	case '|':
	case ')':
		FAIL("internal urp in regexp");	/* Supposed to be caught earlier. */
		break;
	case '?':
	case '+':
	case '*':
		FAIL("?+* follows nothing in regexp");
		break;
	case '\\':
		switch (*regparse) {
		case 'w':
			ret = regnode(ALNUM);
			*flagp |= HASWIDTH|SIMPLE;
			regparse++;
			break;
		case 'W':
			ret = regnode(NALNUM);
			*flagp |= HASWIDTH|SIMPLE;
			regparse++;
			break;
		case 'b':
			ret = regnode(BOUND);
			*flagp |= SIMPLE;
			regparse++;
			break;
		case 'B':
			ret = regnode(NBOUND);
			*flagp |= SIMPLE;
			regparse++;
			break;
		case 's':
			ret = regnode(SPACE);
			*flagp |= HASWIDTH|SIMPLE;
			regparse++;
			break;
		case 'S':
			ret = regnode(NSPACE);
			*flagp |= HASWIDTH|SIMPLE;
			regparse++;
			break;
		case 'd':
			ret = regnode(DIGIT);
			*flagp |= HASWIDTH|SIMPLE;
			regparse++;
			break;
		case 'D':
			ret = regnode(NDIGIT);
			*flagp |= HASWIDTH|SIMPLE;
			regparse++;
			break;
		case 'n':
		case 'r':
		case 't':
		case 'f':
		case 'e':
		case 'a':
		case 'x':
		case 'c':
		case '0':
			goto defchar;
		case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			{
			    int num = atoi(regparse);

			    if (num > 9 && num >= regnpar)
				goto defchar;
			    else {
				regsawback = 1;
				ret = reganode(REF, num);
				while (isDIGIT(*regparse))
				    regparse++;
				*flagp |= SIMPLE;
			    }
			}
			break;
		case '\0':
			if (regparse >= regxend)
			    FAIL("trailing \\ in regexp");
			/* FALL THROUGH */
		default:
			goto defchar;
		}
		break;
	default: {
			register int len;
			register char ender;
			register char *p;
			char *oldp;
			int numlen;

		    defchar:
			ret = regnode(EXACTLY);
			regc(0);		/* save spot for len */
			for (len=0, p=regparse-1;
			  len < 127 && p < regxend;
			  len++)
			{
			    oldp = p;
			    switch (*p) {
			    case '^':
			    case '$':
			    case '.':
			    case '[':
			    case '(':
			    case ')':
			    case '|':
				goto loopdone;
			    case '\\':
				switch (*++p) {
				case 'w':
				case 'W':
				case 'b':
				case 'B':
				case 's':
				case 'S':
				case 'd':
				case 'D':
				    --p;
				    goto loopdone;
				case 'n':
					ender = '\n';
					p++;
					break;
				case 'r':
					ender = '\r';
					p++;
					break;
				case 't':
					ender = '\t';
					p++;
					break;
				case 'f':
					ender = '\f';
					p++;
					break;
				case 'e':
					ender = '\033';
					p++;
					break;
				case 'a':
					ender = '\007';
					p++;
					break;
				case 'x':
				    ender = scanhex(++p, 2, &numlen);
				    p += numlen;
				    break;
				case 'c':
				    p++;
				    ender = *p++;
				    if (isLOWER(ender))
					ender = toupper(ender);
				    ender ^= 64;
				    break;
				case '0': case '1': case '2': case '3':case '4':
				case '5': case '6': case '7': case '8':case '9':
				    if (*p == '0' ||
				      (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
					ender = scanoct(p, 3, &numlen);
					p += numlen;
				    }
				    else {
					--p;
					goto loopdone;
				    }
				    break;
				case '\0':
				    if (p >= regxend)
					FAIL("trailing \\ in regexp");
				    /* FALL THROUGH */
				default:
				    ender = *p++;
				    break;
				}
				break;
			    default:
				ender = *p++;
				break;
			    }
			    if (regfold && isUPPER(ender))
				    ender = tolower(ender);
			    if (ISMULT2(p)) { /* Back off on ?+*. */
				if (len)
				    p = oldp;
				else {
				    len++;
				    regc(ender);
				}
				break;
			    }
			    regc(ender);
			}
		    loopdone:
			regparse = p;
			if (len <= 0)
				FAIL("internal disaster in regexp");
			*flagp |= HASWIDTH;
			if (len == 1)
				*flagp |= SIMPLE;
			if (regcode != &regdummy)
			    *OPERAND(ret) = len;
			regc('\0');
		}
		break;
	}

	return(ret);
}

static void
regset(bits,def,c)
char *bits;
int def;
register int c;
{
	if (regcode == &regdummy)
	    return;
	c &= 255;
	if (def)
		bits[c >> 3] &= ~(1 << (c & 7));
	else
		bits[c >> 3] |=  (1 << (c & 7));
}

static char *
regclass()
{
	register char *bits;
	register int class;
	register int lastclass;
	register int range = 0;
	register char *ret;
	register int def;
	int numlen;

	ret = regnode(ANYOF);
	if (*regparse == '^') {	/* Complement of range. */
		regparse++;
		def = 0;
	} else {
		def = 255;
	}
	bits = regcode;
	for (class = 0; class < 32; class++)
	    regc(def);
	if (*regparse == ']' || *regparse == '-')
		goto skipcond;		/* allow 1st char to be ] or - */
	while (regparse < regxend && *regparse != ']') {
	      skipcond:
		class = UCHARAT(regparse++);
		if (class == '\\') {
			class = UCHARAT(regparse++);
			switch (class) {
			case 'w':
				for (class = 0; class < 256; class++)
				    if (isALNUM(class))
					regset(bits,def,class);
				lastclass = 1234;
				continue;
			case 'W':
				for (class = 0; class < 256; class++)
				    if (!isALNUM(class))
					regset(bits,def,class);
				lastclass = 1234;
				continue;
			case 's':
				for (class = 0; class < 256; class++)
				    if (isSPACE(class))
					regset(bits,def,class);
				lastclass = 1234;
				continue;
			case 'S':
				for (class = 0; class < 256; class++)
				    if (!isSPACE(class))
					regset(bits,def,class);
				lastclass = 1234;
				continue;
			case 'd':
				for (class = '0'; class <= '9'; class++)
					regset(bits,def,class);
				lastclass = 1234;
				continue;
			case 'D':
				for (class = 0; class < '0'; class++)
					regset(bits,def,class);
				for (class = '9' + 1; class < 256; class++)
					regset(bits,def,class);
				lastclass = 1234;
				continue;
			case 'n':
				class = '\n';
				break;
			case 'r':
				class = '\r';
				break;
			case 't':
				class = '\t';
				break;
			case 'f':
				class = '\f';
				break;
			case 'b':
				class = '\b';
				break;
			case 'e':
				class = '\033';
				break;
			case 'a':
				class = '\007';
				break;
			case 'x':
				class = scanhex(regparse, 2, &numlen);
				regparse += numlen;
				break;
			case 'c':
				class = *regparse++;
				if (isLOWER(class))
				    class = toupper(class);
				class ^= 64;
				break;
			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9':
				class = scanoct(--regparse, 3, &numlen);
				regparse += numlen;
				break;
			}
		}
		if (range) {
			if (lastclass > class)
				FAIL("invalid [] range in regexp");
			range = 0;
		}
		else {
			lastclass = class;
			if (*regparse == '-' && regparse+1 < regxend &&
			    regparse[1] != ']') {
				regparse++;
				range = 1;
				continue;	/* do it next time */
			}
		}
		for ( ; lastclass <= class; lastclass++) {
			regset(bits,def,lastclass);
			if (regfold && isUPPER(lastclass))
				regset(bits,def,tolower(lastclass));
		}
		lastclass = class;
	}
	if (*regparse != ']')
		FAIL("unmatched [] in regexp");
	regparse++;
	return ret;
}

/*
 - regnode - emit a node
 */
static char *			/* Location. */
regnode(op)
char op;
{
	register char *ret;
	register char *ptr;

	ret = regcode;
	if (ret == &regdummy) {
#ifdef REGALIGN
		if (!(regsize & 1))
			regsize++;
#endif
		regsize += 3;
		return(ret);
	}

#ifdef REGALIGN
#ifndef lint
	if (!((long)ret & 1))
	    *ret++ = 127;
#endif
#endif
	ptr = ret;
	*ptr++ = op;
	*ptr++ = '\0';		/* Null "next" pointer. */
	*ptr++ = '\0';
	regcode = ptr;

	return(ret);
}

/*
 - reganode - emit a node with an argument
 */
static char *			/* Location. */
reganode(op, arg)
char op;
unsigned short arg;
{
	register char *ret;
	register char *ptr;

	ret = regcode;
	if (ret == &regdummy) {
#ifdef REGALIGN
		if (!(regsize & 1))
			regsize++;
#endif
		regsize += 5;
		return(ret);
	}

#ifdef REGALIGN
#ifndef lint
	if (!((long)ret & 1))
	    *ret++ = 127;
#endif
#endif
	ptr = ret;
	*ptr++ = op;
	*ptr++ = '\0';		/* Null "next" pointer. */
	*ptr++ = '\0';
#ifdef REGALIGN
	*(unsigned short *)(ret+3) = arg;
#else
	ret[3] = arg >> 8; ret[4] = arg & 0377;
#endif
	ptr += 2;
	regcode = ptr;

	return(ret);
}

/*
 - regc - emit (if appropriate) a byte of code
 */
static void
regc(b)
char b;
{
	if (regcode != &regdummy)
		*regcode++ = b;
	else
		regsize++;
}

/*
 - reginsert - insert an operator in front of already-emitted operand
 *
 * Means relocating the operand.
 */
static void
reginsert(op, opnd)
char op;
char *opnd;
{
	register char *src;
	register char *dst;
	register char *place;
	register offset = (op == CURLY ? 4 : 0);

	if (regcode == &regdummy) {
#ifdef REGALIGN
		regsize += 4 + offset;
#else
		regsize += 3 + offset;
#endif
		return;
	}

	src = regcode;
#ifdef REGALIGN
	regcode += 4 + offset;
#else
	regcode += 3 + offset;
#endif
	dst = regcode;
	while (src > opnd)
		*--dst = *--src;

	place = opnd;		/* Op node, where operand used to be. */
	*place++ = op;
	*place++ = '\0';
	*place++ = '\0';
	while (offset-- > 0)
	    *place++ = '\0';
#ifdef REGALIGN
	*place++ = '\177';
#endif
}

/*
 - regtail - set the next-pointer at the end of a node chain
 */
static void
regtail(p, val)
char *p;
char *val;
{
	register char *scan;
	register char *temp;
	register int offset;

	if (p == &regdummy)
		return;

	/* Find last node. */
	scan = p;
	for (;;) {
		temp = regnext(scan);
		if (temp == NULL)
			break;
		scan = temp;
	}

#ifdef REGALIGN
	offset = val - scan;
#ifndef lint
	*(short*)(scan+1) = offset;
#else
	offset = offset;
#endif
#else
	if (OP(scan) == BACK)
		offset = scan - val;
	else
		offset = val - scan;
	*(scan+1) = (offset>>8)&0377;
	*(scan+2) = offset&0377;
#endif
}

/*
 - regoptail - regtail on operand of first argument; nop if operandless
 */
static void
regoptail(p, val)
char *p;
char *val;
{
	/* "Operandless" and "op != BRANCH" are synonymous in practice. */
	if (p == NULL || p == &regdummy || OP(p) != BRANCH)
		return;
	regtail(NEXTOPER(p), val);
}

/*
 - regcurly - a little FSA that accepts {\d+,?\d*}
 */
STATIC int
regcurly(s)
register char *s;
{
    if (*s++ != '{')
	return FALSE;
    if (!isDIGIT(*s))
	return FALSE;
    while (isDIGIT(*s))
	s++;
    if (*s == ',')
	s++;
    while (isDIGIT(*s))
	s++;
    if (*s != '}')
	return FALSE;
    return TRUE;
}

#ifdef DEBUGGING

/*
 - regdump - dump a regexp onto stderr in vaguely comprehensible form
 */
void
regdump(r)
regexp *r;
{
	register char *s;
	register char op = EXACTLY;	/* Arbitrary non-END op. */
	register char *next;


	s = r->program + 1;
	while (op != END) {	/* While that wasn't END last time... */
#ifdef REGALIGN
		if (!((long)s & 1))
			s++;
#endif
		op = OP(s);
		fprintf(stderr,"%2d%s", s-r->program, regprop(s));	/* Where, what. */
		next = regnext(s);
		s += regarglen[op];
		if (next == NULL)		/* Next ptr. */
			fprintf(stderr,"(0)");
		else 
			fprintf(stderr,"(%d)", (s-r->program)+(next-s));
		s += 3;
		if (op == ANYOF) {
			s += 32;
		}
		if (op == EXACTLY) {
			/* Literal string, where present. */
			s++;
			while (*s != '\0') {
				(void)putchar(*s);
				s++;
			}
			s++;
		}
		(void)putchar('\n');
	}

	/* Header fields of interest. */
	if (r->regstart)
		fprintf(stderr,"start `%s' ", r->regstart->str_ptr);
	if (r->regstclass)
		fprintf(stderr,"stclass `%s' ", regprop(r->regstclass));
	if (r->reganch & ROPT_ANCH)
		fprintf(stderr,"anchored ");
	if (r->reganch & ROPT_SKIP)
		fprintf(stderr,"plus ");
	if (r->reganch & ROPT_IMPLICIT)
		fprintf(stderr,"implicit ");
	if (r->regmust != NULL)
		fprintf(stderr,"must have \"%s\" back %d ", r->regmust->str_ptr,
		  r->regback);
	fprintf(stderr, "minlen %d ", r->minlen);
	fprintf(stderr,"\n");
}

/*
 - regprop - printable representation of opcode
 */
char *
regprop(op)
char *op;
{
	register char *p;

	(void) strcpy(buf, ":");

	switch (OP(op)) {
	case BOL:
		p = "BOL";
		break;
	case EOL:
		p = "EOL";
		break;
	case ANY:
		p = "ANY";
		break;
	case ANYOF:
		p = "ANYOF";
		break;
	case BRANCH:
		p = "BRANCH";
		break;
	case EXACTLY:
		p = "EXACTLY";
		break;
	case NOTHING:
		p = "NOTHING";
		break;
	case BACK:
		p = "BACK";
		break;
	case END:
		p = "END";
		break;
	case ALNUM:
		p = "ALNUM";
		break;
	case NALNUM:
		p = "NALNUM";
		break;
	case BOUND:
		p = "BOUND";
		break;
	case NBOUND:
		p = "NBOUND";
		break;
	case SPACE:
		p = "SPACE";
		break;
	case NSPACE:
		p = "NSPACE";
		break;
	case DIGIT:
		p = "DIGIT";
		break;
	case NDIGIT:
		p = "NDIGIT";
		break;
	case CURLY:
		(void)sprintf(buf+strlen(buf), "CURLY {%d,%d}",
		    ARG1(op),ARG2(op));
		p = NULL;
		break;
	case REF:
		(void)sprintf(buf+strlen(buf), "REF%d", ARG1(op));
		p = NULL;
		break;
	case OPEN:
		(void)sprintf(buf+strlen(buf), "OPEN%d", ARG1(op));
		p = NULL;
		break;
	case CLOSE:
		(void)sprintf(buf+strlen(buf), "CLOSE%d", ARG1(op));
		p = NULL;
		break;
	case STAR:
		p = "STAR";
		break;
	case PLUS:
		p = "PLUS";
		break;
	default:
		FAIL("corrupted regexp opcode");
	}
	if (p != NULL)
		(void) strcat(buf, p);
	return(buf);
}
#endif /* DEBUGGING */

void
regfree(r)
struct regexp *r;
{
	if (r->precomp) {
		Safefree(r->precomp);
		r->precomp = Nullch;
	}
	if (r->subbase) {
		Safefree(r->subbase);
		r->subbase = Nullch;
	}
	if (r->regmust) {
		str_free(r->regmust);
		r->regmust = Nullstr;
	}
	if (r->regstart) {
		str_free(r->regstart);
		r->regstart = Nullstr;
	}
	Safefree(r->startp);
	Safefree(r->endp);
	Safefree(r);
}
OpenPOWER on IntegriCloud