-
Notifications
You must be signed in to change notification settings - Fork 0
/
regexp.h
1210 lines (1108 loc) · 24.9 KB
/
regexp.h
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
/*
* Simple Regular Expression functions. Derived from Unix 7th Edition,
* /usr/src/cmd/expr.y
*
* Modified by Gunnar Ritter, Freiburg i. Br., Germany, February 2002.
*
* Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* Redistributions of source code and documentation must retain the
* above copyright notice, this list of conditions and the following
* disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed or owned by Caldera
* International, Inc.
* Neither the name of Caldera International, Inc. nor the names of
* other contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
* LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if __GNUC__ >= 3 && __GNUC_MINOR__ >= 4
#define REGEXP_H_USED __attribute__ ((used))
#elif defined __GNUC__
#define REGEXP_H_USED __attribute__ ((unused))
#else
#define REGEXP_H_USED
#endif
static const char regexp_h_sccsid[] REGEXP_H_USED =
"@(#)regexp.sl 1.54 (gritter) 2/19/05";
#if !defined (REGEXP_H_USED_FROM_VI) && !defined (__dietlibc__)
#define REGEXP_H_WCHARS
#endif
#define CBRA 2
#define CCHR 4
#define CDOT 8
#define CCL 12
/* CLNUM 14 used in sed */
/* CEND 16 used in sed */
#define CDOL 20
#define CCEOF 22
#define CKET 24
#define CBACK 36
#define CNCL 40
#define CBRC 44
#define CLET 48
#define CCH1 52
#define CCH2 56
#define CCH3 60
#define STAR 01
#define RNGE 03
#define REGEXP_H_LEAST 0100
#ifdef REGEXP_H_WCHARS
#define CMB 0200
#else /* !REGEXP_H_WCHARS */
#define CMB 0
#endif /* !REGEXP_H_WCHARS */
#define NBRA 9
#define PLACE(c) ep[c >> 3] |= bittab[c & 07]
#define ISTHERE(c) (ep[c >> 3] & bittab[c & 07])
#ifdef REGEXP_H_WCHARS
#define REGEXP_H_IS_THERE(ep, c) ((ep)[c >> 3] & bittab[c & 07])
#endif
#include <ctype.h>
#include <string.h>
#include <limits.h>
#ifdef REGEXP_H_WCHARS
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
#endif /* REGEXP_H_WCHARS */
#define regexp_h_uletter(c) (isalpha(c) || (c) == '_')
#ifdef REGEXP_H_WCHARS
#define regexp_h_wuletter(c) (iswalpha(c) || (c) == L'_')
/*
* Used to allocate memory for the multibyte star algorithm.
*/
#ifndef regexp_h_malloc
#define regexp_h_malloc(n) malloc(n)
#endif
#ifndef regexp_h_free
#define regexp_h_free(p) free(p)
#endif
/*
* Can be predefined to 'inline' to inline some multibyte functions;
* may improve performance for files that contain many multibyte
* sequences.
*/
#ifndef regexp_h_inline
#define regexp_h_inline
#endif
/*
* Mask to determine whether the first byte of a sequence possibly
* starts a multibyte character. Set to 0377 to force mbtowc() for
* any byte sequence (except 0).
*/
#ifndef REGEXP_H_MASK
#define REGEXP_H_MASK 0200
#endif
#endif /* REGEXP_H_WCHARS */
/*
* For regexpr.h.
*/
#ifndef regexp_h_static
#define regexp_h_static
#endif
#ifndef REGEXP_H_STEP_INIT
#define REGEXP_H_STEP_INIT
#endif
#ifndef REGEXP_H_ADVANCE_INIT
#define REGEXP_H_ADVANCE_INIT
#endif
char *braslist[NBRA];
char *braelist[NBRA];
int nbra;
char *loc1, *loc2, *locs;
int sed;
int nodelim;
regexp_h_static int circf;
regexp_h_static int low;
regexp_h_static int size;
regexp_h_static unsigned char bittab[] = {
1,
2,
4,
8,
16,
32,
64,
128
};
static int regexp_h_advance(register const char *lp,
register const char *ep);
static void regexp_h_getrnge(register const char *str, int least);
static const char *regexp_h_bol; /* beginning of input line (for \<) */
#ifdef REGEXP_H_WCHARS
static int regexp_h_wchars;
static int regexp_h_mbcurmax;
static const char *regexp_h_firstwc; /* location of first
multibyte character
on input line */
#define regexp_h_getwc(c) { \
if (regexp_h_wchars) { \
char mbbuf[MB_LEN_MAX + 1], *mbptr; \
wchar_t wcbuf; \
int mb, len; \
mbptr = mbbuf; \
do { \
mb = GETC(); \
*mbptr++ = mb; \
*mbptr = '\0'; \
} while ((len = mbtowc(&wcbuf, mbbuf, regexp_h_mbcurmax)) < 0 \
&& mb != eof && mbptr < mbbuf + MB_LEN_MAX); \
if (len == -1) \
ERROR(67); \
c = wcbuf; \
} else { \
c = GETC(); \
} \
}
#define regexp_h_store(wc, mb, me) { \
int len; \
if (wc == WEOF) \
ERROR(67); \
if ((len = me - mb) <= regexp_h_mbcurmax) { \
char mt[MB_LEN_MAX]; \
if (wctomb(mt, wc) >= len) \
ERROR(50); \
} \
switch (len = wctomb(mb, wc)) { \
case -1: \
ERROR(67); \
case 0: \
mb++; \
break; \
default: \
mb += len; \
} \
}
static regexp_h_inline wint_t
regexp_h_fetchwc(const char **mb, int islp)
{
wchar_t wc;
int len;
if ((len = mbtowc(&wc, *mb, regexp_h_mbcurmax)) < 0) {
(*mb)++;
return WEOF;
}
if (islp && regexp_h_firstwc == NULL)
regexp_h_firstwc = *mb;
/*if (len == 0) {
(*mb)++;
return L'\0';
} handled in singlebyte code */
*mb += len;
return wc;
}
#define regexp_h_fetch(mb, islp) ((*(mb) & REGEXP_H_MASK) == 0 ? \
(*(mb)++&0377): \
regexp_h_fetchwc(&(mb), islp))
static regexp_h_inline wint_t
regexp_h_showwc(const char *mb)
{
wchar_t wc;
if (mbtowc(&wc, mb, regexp_h_mbcurmax) < 0)
return WEOF;
return wc;
}
#define regexp_h_show(mb) ((*(mb) & REGEXP_H_MASK) == 0 ? (*(mb)&0377): \
regexp_h_showwc(mb))
/*
* Return the character immediately preceding mb. Since no byte is
* required to be the first byte of a character, the longest multibyte
* character ending at &[mb-1] is searched.
*/
static regexp_h_inline wint_t
regexp_h_previous(const char *mb)
{
const char *p = mb;
wchar_t wc, lastwc = WEOF;
int len, max = 0;
if (regexp_h_firstwc == NULL || mb <= regexp_h_firstwc)
return (mb > regexp_h_bol ? (mb[-1] & 0377) : WEOF);
while (p-- > regexp_h_bol) {
mbtowc(NULL, NULL, 0);
if ((len = mbtowc(&wc, p, mb - p)) >= 0) {
if (len < max || len < mb - p)
break;
max = len;
lastwc = wc;
} else if (len < 0 && max > 0)
break;
}
return lastwc;
}
#define regexp_h_cclass(set, c, af) \
((c) == 0 || (c) == WEOF ? 0 : ( \
((c) > 0177) ? \
regexp_h_cclass_wc(set, c, af) : ( \
REGEXP_H_IS_THERE((set)+1, (c)) ? (af) : !(af) \
) \
) \
)
static regexp_h_inline int
regexp_h_cclass_wc(const char *set, register wint_t c, int af)
{
register wint_t wc, wl = WEOF;
const char *end;
end = &set[18] + set[0] - 1;
set += 17;
while (set < end) {
wc = regexp_h_fetch(set, 0);
#ifdef REGEXP_H_VI_BACKSLASH
if (wc == '\\' && set < end &&
(*set == ']' || *set == '-' ||
*set == '^' || *set == '\\')) {
wc = regexp_h_fetch(set, 0);
} else
#endif /* REGEXP_H_VI_BACKSLASH */
if (wc == '-' && wl != WEOF && set < end) {
wc = regexp_h_fetch(set, 0);
#ifdef REGEXP_H_VI_BACKSLASH
if (wc == '\\' && set < end &&
(*set == ']' || *set == '-' ||
*set == '^' || *set == '\\')) {
wc = regexp_h_fetch(set, 0);
}
#endif /* REGEXP_H_VI_BACKSLASH */
if (c > wl && c < wc)
return af;
}
if (c == wc)
return af;
wl = wc;
}
return !af;
}
#else /* !REGEXP_H_WCHARS */
#define regexp_h_wchars 0
#define regexp_h_getwc(c) { c = GETC(); }
#endif /* !REGEXP_H_WCHARS */
regexp_h_static char *
compile(char *instring, char *ep, const char *endbuf, int seof)
{
INIT /* Dependent declarations and initializations */
register int c;
register int eof = seof;
char *lastep = instring;
int cclcnt;
char bracket[NBRA], *bracketp;
int closed;
char neg;
int lc;
int i, cflg;
#ifdef REGEXP_H_WCHARS
char *eq;
regexp_h_mbcurmax = MB_CUR_MAX;
regexp_h_wchars = regexp_h_mbcurmax > 1 ? CMB : 0;
#endif
lastep = 0;
bracketp = bracket;
if((c = GETC()) == eof || c == '\n') {
if (c == '\n') {
UNGETC(c);
nodelim = 1;
}
if(*ep == 0 && !sed)
ERROR(41);
if (bracketp > bracket)
ERROR(42);
RETURN(ep);
}
circf = closed = nbra = 0;
if (c == '^')
circf++;
else
UNGETC(c);
for (;;) {
if (ep >= endbuf)
ERROR(50);
regexp_h_getwc(c);
if(c != '*' && ((c != '\\') || (PEEKC() != '{')))
lastep = ep;
if (c == eof) {
*ep++ = CCEOF;
if (bracketp > bracket)
ERROR(42);
RETURN(ep);
}
switch (c) {
case '.':
*ep++ = CDOT|regexp_h_wchars;
continue;
case '\n':
if (sed == 0) {
UNGETC(c);
*ep++ = CCEOF;
nodelim = 1;
RETURN(ep);
}
ERROR(36);
case '*':
if (lastep==0 || *lastep==CBRA || *lastep==CKET ||
*lastep==(CBRC|regexp_h_wchars) ||
*lastep==(CLET|regexp_h_wchars))
goto defchar;
*lastep |= STAR;
continue;
case '$':
if(PEEKC() != eof)
goto defchar;
*ep++ = CDOL;
continue;
case '[':
#ifdef REGEXP_H_WCHARS
if (regexp_h_wchars == 0) {
#endif
if(&ep[33] >= endbuf)
ERROR(50);
*ep++ = CCL;
lc = 0;
for(i = 0; i < 32; i++)
ep[i] = 0;
neg = 0;
if((c = GETC()) == '^') {
neg = 1;
c = GETC();
}
do {
c &= 0377;
if(c == '\0' || c == '\n')
ERROR(49);
#ifdef REGEXP_H_VI_BACKSLASH
if(c == '\\' && ((c = PEEKC()) == ']' ||
c == '-' || c == '^' ||
c == '\\')) {
c = GETC();
c &= 0377;
} else
#endif /* REGEXP_H_VI_BACKSLASH */
if(c == '-' && lc != 0) {
if ((c = GETC()) == ']') {
PLACE('-');
break;
}
#ifdef REGEXP_H_VI_BACKSLASH
if(c == '\\' &&
((c = PEEKC()) == ']' ||
c == '-' ||
c == '^' ||
c == '\\'))
c = GETC();
#endif /* REGEXP_H_VI_BACKSLASH */
c &= 0377;
while(lc < c) {
PLACE(lc);
lc++;
}
}
lc = c;
PLACE(c);
} while((c = GETC()) != ']');
if(neg) {
for(cclcnt = 0; cclcnt < 32; cclcnt++)
ep[cclcnt] ^= 0377;
ep[0] &= 0376;
}
ep += 32;
#ifdef REGEXP_H_WCHARS
} else {
if (&ep[18] >= endbuf)
ERROR(50);
*ep++ = CCL|CMB;
*ep++ = 0;
lc = 0;
for (i = 0; i < 16; i++)
ep[i] = 0;
eq = &ep[16];
regexp_h_getwc(c);
if (c == L'^') {
regexp_h_getwc(c);
ep[-2] = CNCL|CMB;
}
do {
if (c == '\0' || c == '\n')
ERROR(49);
#ifdef REGEXP_H_VI_BACKSLASH
if(c == '\\' && ((c = PEEKC()) == ']' ||
c == '-' || c == '^' ||
c == '\\')) {
regexp_h_store(c, eq, endbuf);
regexp_h_getwc(c);
} else
#endif /* REGEXP_H_VI_BACKSLASH */
if (c == '-' && lc != 0 && lc <= 0177) {
regexp_h_store(c, eq, endbuf);
regexp_h_getwc(c);
if (c == ']') {
PLACE('-');
break;
}
#ifdef REGEXP_H_VI_BACKSLASH
if(c == '\\' &&
((c = PEEKC()) == ']' ||
c == '-' ||
c == '^' ||
c == '\\')) {
regexp_h_store(c, eq,
endbuf);
regexp_h_getwc(c);
}
#endif /* REGEXP_H_VI_BACKSLASH */
while (lc < (c & 0177)) {
PLACE(lc);
lc++;
}
}
lc = c;
if (c <= 0177)
PLACE(c);
regexp_h_store(c, eq, endbuf);
regexp_h_getwc(c);
} while (c != L']');
if ((i = eq - &ep[16]) > 255)
ERROR(50);
lastep[1] = i;
ep = eq;
}
#endif /* REGEXP_H_WCHARS */
continue;
case '\\':
regexp_h_getwc(c);
switch(c) {
case '(':
if(nbra >= NBRA)
ERROR(43);
*bracketp++ = nbra;
*ep++ = CBRA;
*ep++ = nbra++;
continue;
case ')':
if(bracketp <= bracket)
ERROR(42);
*ep++ = CKET;
*ep++ = *--bracketp;
closed++;
continue;
case '<':
*ep++ = CBRC|regexp_h_wchars;
continue;
case '>':
*ep++ = CLET|regexp_h_wchars;
continue;
case '{':
if(lastep == (char *) (0))
goto defchar;
*lastep |= RNGE;
cflg = 0;
nlim:
c = GETC();
i = 0;
do {
if ('0' <= c && c <= '9')
i = 10 * i + c - '0';
else
ERROR(16);
} while(((c = GETC()) != '\\') && (c != ','));
if (i > 255)
ERROR(11);
*ep++ = i;
if (c == ',') {
if(cflg++)
ERROR(44);
if((c = GETC()) == '\\') {
*ep++ = (char)255;
*lastep |= REGEXP_H_LEAST;
} else {
UNGETC(c);
goto nlim; /* get 2'nd number */
}
}
if(GETC() != '}')
ERROR(45);
if(!cflg) /* one number */
*ep++ = i;
else if((ep[-1] & 0377) < (ep[-2] & 0377))
ERROR(46);
continue;
case '\n':
ERROR(36);
case 'n':
c = '\n';
goto defchar;
default:
if(c >= '1' && c <= '9') {
if((c -= '1') >= closed)
ERROR(25);
*ep++ = CBACK;
*ep++ = c;
continue;
}
}
/* Drop through to default to use \ to turn off special chars */
defchar:
default:
lastep = ep;
#ifdef REGEXP_H_WCHARS
if (regexp_h_wchars == 0) {
#endif
*ep++ = CCHR;
*ep++ = c;
#ifdef REGEXP_H_WCHARS
} else {
char mbbuf[MB_LEN_MAX];
switch (wctomb(mbbuf, c)) {
case 1: *ep++ = CCH1;
break;
case 2: *ep++ = CCH2;
break;
case 3: *ep++ = CCH3;
break;
default:
*ep++ = CCHR|CMB;
}
regexp_h_store(c, ep, endbuf);
}
#endif /* REGEXP_H_WCHARS */
}
}
}
int
step(const char *p1, const char *p2)
{
register int c;
#ifdef REGEXP_H_WCHARS
register int d;
#endif /* REGEXP_H_WCHARS */
REGEXP_H_STEP_INIT /* get circf */
regexp_h_bol = p1;
#ifdef REGEXP_H_WCHARS
regexp_h_firstwc = NULL;
#endif /* REGEXP_H_WCHARS */
if (circf) {
loc1 = (char *)p1;
return(regexp_h_advance(p1, p2));
}
/* fast check for first character */
if (*p2==CCHR) {
c = p2[1] & 0377;
do {
if ((*p1 & 0377) != c)
continue;
if (regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
} while (*p1++);
return(0);
}
#ifdef REGEXP_H_WCHARS
else if (*p2==CCH1) {
do {
if (p1[0] == p2[1] && regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
c = regexp_h_fetch(p1, 1);
} while (c);
return(0);
} else if (*p2==CCH2) {
do {
if (p1[0] == p2[1] && p1[1] == p2[2] &&
regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
c = regexp_h_fetch(p1, 1);
} while (c);
return(0);
} else if (*p2==CCH3) {
do {
if (p1[0] == p2[1] && p1[1] == p2[2] && p1[2] == p2[3]&&
regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
c = regexp_h_fetch(p1, 1);
} while (c);
return(0);
} else if ((*p2&0377)==(CCHR|CMB)) {
d = regexp_h_fetch(p2, 0);
do {
c = regexp_h_fetch(p1, 1);
if (c == d && regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
} while(c);
return(0);
}
/* regular algorithm */
if (regexp_h_wchars)
do {
if (regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
c = regexp_h_fetch(p1, 1);
} while (c);
else
#endif /* REGEXP_H_WCHARS */
do {
if (regexp_h_advance(p1, p2)) {
loc1 = (char *)p1;
return(1);
}
} while (*p1++);
return(0);
}
#ifdef REGEXP_H_WCHARS
/*
* It is painfully slow to read character-wise backwards in a
* multibyte string (see regexp_h_previous() above). For the star
* algorithm, we therefore keep track of every character as it is
* read in forward direction.
*
* Don't use alloca() for stack blocks since there is no measurable
* speedup and huge amounts of memory are used up for long input
* lines.
*/
#ifndef REGEXP_H_STAKBLOK
#define REGEXP_H_STAKBLOK 1000
#endif
struct regexp_h_stack {
struct regexp_h_stack *s_nxt;
struct regexp_h_stack *s_prv;
const char *s_ptr[REGEXP_H_STAKBLOK];
};
#define regexp_h_push(sb, sp, sc, lp) (regexp_h_wchars ? \
regexp_h_pushwc(sb, sp, sc, lp) : (void)0)
static regexp_h_inline void
regexp_h_pushwc(struct regexp_h_stack **sb,
struct regexp_h_stack **sp,
const char ***sc, const char *lp)
{
if (regexp_h_firstwc == NULL || lp < regexp_h_firstwc)
return;
if (*sb == NULL) {
if ((*sb = regexp_h_malloc(sizeof **sb)) == NULL)
return;
(*sb)->s_nxt = (*sb)->s_prv = NULL;
*sp = *sb;
*sc = &(*sb)->s_ptr[0];
} else if (*sc >= &(*sp)->s_ptr[REGEXP_H_STAKBLOK]) {
if ((*sp)->s_nxt == NULL) {
struct regexp_h_stack *bq;
if ((bq = regexp_h_malloc(sizeof *bq)) == NULL)
return;
bq->s_nxt = NULL;
bq->s_prv = *sp;
(*sp)->s_nxt = bq;
*sp = bq;
} else
*sp = (*sp)->s_nxt;
*sc = &(*sp)->s_ptr[0];
}
*(*sc)++ = lp;
}
static regexp_h_inline const char *
regexp_h_pop(struct regexp_h_stack **sp, const char ***sc,
const char *lp)
{
if (regexp_h_firstwc == NULL || lp <= regexp_h_firstwc)
return &lp[-1];
if (*sp == NULL)
return regexp_h_firstwc;
if (*sc == &(*sp)->s_ptr[0]) {
if ((*sp)->s_prv == NULL) {
regexp_h_free(*sp);
*sp = NULL;
return regexp_h_firstwc;
}
*sp = (*sp)->s_prv;
regexp_h_free((*sp)->s_nxt);
(*sp)->s_nxt = NULL ;
*sc = &(*sp)->s_ptr[REGEXP_H_STAKBLOK];
}
return *(--(*sc));
}
static void
regexp_h_zerostak(struct regexp_h_stack **sb, struct regexp_h_stack **sp)
{
for (*sp = *sb; *sp && (*sp)->s_nxt; *sp = (*sp)->s_nxt)
if ((*sp)->s_prv)
regexp_h_free((*sp)->s_prv);
if (*sp) {
if ((*sp)->s_prv)
regexp_h_free((*sp)->s_prv);
regexp_h_free(*sp);
}
*sp = *sb = NULL;
}
#else /* !REGEXP_H_WCHARS */
#define regexp_h_push(sb, sp, sc, lp)
#endif /* !REGEXP_H_WCHARS */
static int
regexp_h_advance(const char *lp, const char *ep)
{
register const char *curlp;
int c, least;
#ifdef REGEXP_H_WCHARS
int d;
struct regexp_h_stack *sb = NULL, *sp = NULL;
const char **sc;
#endif /* REGEXP_H_WCHARS */
char *bbeg;
int ct;
for (;;) switch (least = *ep++ & 0377, least & ~REGEXP_H_LEAST) {
case CCHR:
#ifdef REGEXP_H_WCHARS
case CCH1:
#endif
if (*ep++ == *lp++)
continue;
return(0);
#ifdef REGEXP_H_WCHARS
case CCHR|CMB:
if (regexp_h_fetch(ep, 0) == regexp_h_fetch(lp, 1))
continue;
return(0);
case CCH2:
if (ep[0] == lp[0] && ep[1] == lp[1]) {
ep += 2, lp += 2;
continue;
}
return(0);
case CCH3:
if (ep[0] == lp[0] && ep[1] == lp[1] && ep[2] == lp[2]) {
ep += 3, lp += 3;
continue;
}
return(0);
#endif /* REGEXP_H_WCHARS */
case CDOT:
if (*lp++)
continue;
return(0);
#ifdef REGEXP_H_WCHARS
case CDOT|CMB:
if ((c = regexp_h_fetch(lp, 1)) != L'\0' && c != WEOF)
continue;
return(0);
#endif /* REGEXP_H_WCHARS */
case CDOL:
if (*lp==0)
continue;
return(0);
case CCEOF:
loc2 = (char *)lp;
return(1);
case CCL:
c = *lp++ & 0377;
if(ISTHERE(c)) {
ep += 32;
continue;
}
return(0);
#ifdef REGEXP_H_WCHARS
case CCL|CMB:
case CNCL|CMB:
c = regexp_h_fetch(lp, 1);
if (regexp_h_cclass(ep, c, (ep[-1] & 0377) == (CCL|CMB))) {
ep += (*ep & 0377) + 17;
continue;
}
return 0;
#endif /* REGEXP_H_WCHARS */
case CBRA:
braslist[*ep++ & 0377] = (char *)lp;
continue;
case CKET:
braelist[*ep++ & 0377] = (char *)lp;
continue;
case CBRC:
if (lp == regexp_h_bol && locs == NULL)
continue;
if ((isdigit(lp[0] & 0377) || regexp_h_uletter(lp[0] & 0377))
&& !regexp_h_uletter(lp[-1] & 0377)
&& !isdigit(lp[-1] & 0377))
continue;
return(0);
#ifdef REGEXP_H_WCHARS
case CBRC|CMB:
c = regexp_h_show(lp);
d = regexp_h_previous(lp);
if ((iswdigit(c) || regexp_h_wuletter(c))
&& !regexp_h_wuletter(d)
&& !iswdigit(d))
continue;
return(0);
#endif /* REGEXP_H_WCHARS */
case CLET:
if (!regexp_h_uletter(lp[0] & 0377) && !isdigit(lp[0] & 0377))
continue;
return(0);
#ifdef REGEXP_H_WCHARS
case CLET|CMB:
c = regexp_h_show(lp);
if (!regexp_h_wuletter(c) && !iswdigit(c))
continue;
return(0);
#endif /* REGEXP_H_WCHARS */
case CCHR|RNGE:
c = *ep++;
regexp_h_getrnge(ep, least);
while(low--)
if(*lp++ != c)
return(0);
curlp = lp;
while(size--) {
regexp_h_push(&sb, &sp, &sc, lp);
if(*lp++ != c)
break;
}
if(size < 0) {
regexp_h_push(&sb, &sp, &sc, lp);
lp++;
}
ep += 2;
goto star;
#ifdef REGEXP_H_WCHARS
case CCHR|RNGE|CMB:
case CCH1|RNGE:
case CCH2|RNGE:
case CCH3|RNGE:
c = regexp_h_fetch(ep, 0);
regexp_h_getrnge(ep, least);
while (low--)
if (regexp_h_fetch(lp, 1) != c)
return 0;
curlp = lp;
while (size--) {
regexp_h_push(&sb, &sp, &sc, lp);
if (regexp_h_fetch(lp, 1) != c)
break;
}
if(size < 0) {
regexp_h_push(&sb, &sp, &sc, lp);
regexp_h_fetch(lp, 1);
}
ep += 2;
goto star;
#endif /* REGEXP_H_WCHARS */
case CDOT|RNGE:
regexp_h_getrnge(ep, least);
while(low--)
if(*lp++ == '\0')
return(0);
curlp = lp;