This repository has been archived by the owner on Apr 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
regexpr.c
1749 lines (1588 loc) · 39.9 KB
/
regexpr.c
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
/*
regexpr.c
Author: Tatu Ylonen <[email protected]>
Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
Permission to use, copy, modify, distribute, and sell this software
and its documentation is hereby granted without fee, provided that the
above copyright notice appears in all source code copies, the name of
Tatu Ylonen is not used to advertise products containing this software
or a derivation thereof, and all modified versions are clearly marked
as such.
This software is provided "as is" without express or implied warranty.
Created: Thu Sep 26 17:14:05 1991 ylo
Last modified: Sun Mar 29 16:47:31 1992 ylo
This code draws many ideas from the regular expression packages by
Henry Spencer of the University of Toronto and Richard Stallman of the
Free Software Foundation.
Emacs-specific code and syntax table code is almost directly borrowed
from GNU regexp.
$Header: /u/src/lib/tools/RCS/regexpr.c,v 1.4 92/03/29 16:52:04 ylo Exp $
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "regexpr.h"
/*
char *malloc();
void free();
char *realloc();
*/
#define MACRO_BEGIN do {
#define MACRO_END } while (0)
enum regexp_compiled_ops /* opcodes for compiled regexp */
{
Cend, /* end of pattern reached */
Cbol, /* beginning of line */
Ceol, /* end of line */
Cset, /* character set. Followed by 32 bytes of set. */
Cexact, /* followed by a byte to match */
Canychar, /* matches any character except newline */
Cstart_memory, /* set register start addr (followed by reg number) */
Cend_memory, /* set register end addr (followed by reg number) */
Cmatch_memory, /* match a duplicate of reg contents (regnum follows)*/
Cjump, /* followed by two bytes (lsb,msb) of displacement. */
Cstar_jump, /* will change to jump/update_failure_jump at runtime */
Cfailure_jump, /* jump to addr on failure */
Cupdate_failure_jump, /* update topmost failure point and jump */
Cdummy_failure_jump, /* push a dummy failure point and jump */
Cbegbuf, /* match at beginning of buffer */
Cendbuf, /* match at end of buffer */
Cwordbeg, /* match at beginning of word */
Cwordend, /* match at end of word */
Cwordbound, /* match if at word boundary */
Cnotwordbound, /* match if not at word boundary */
#ifdef emacs
Cemacs_at_dot, /* emacs only: matches at dot */
#endif /* emacs */
Csyntaxspec, /* matches syntax code (1 byte follows) */
Cnotsyntaxspec /* matches if syntax code does not match (1 byte foll)*/
};
enum regexp_syntax_op /* syntax codes for plain and quoted characters */
{
Rend, /* special code for end of regexp */
Rnormal, /* normal character */
Ranychar, /* any character except newline */
Rquote, /* the quote character */
Rbol, /* match beginning of line */
Reol, /* match end of line */
Roptional, /* match preceding expression optionally */
Rstar, /* match preceding expr zero or more times */
Rplus, /* match preceding expr one or more times */
Ror, /* match either of alternatives */
Ropenpar, /* opening parenthesis */
Rclosepar, /* closing parenthesis */
Rmemory, /* match memory register */
Rextended_memory, /* \vnn to match registers 10-99 */
Ropenset, /* open set. Internal syntax hard-coded below. */
/* the following are gnu extensions to "normal" regexp syntax */
Rbegbuf, /* beginning of buffer */
Rendbuf, /* end of buffer */
Rwordchar, /* word character */
Rnotwordchar, /* not word character */
Rwordbeg, /* beginning of word */
Rwordend, /* end of word */
Rwordbound, /* word bound */
Rnotwordbound, /* not word bound */
#ifdef emacs
Remacs_at_dot, /* emacs: at dot */
Remacs_syntaxspec, /* syntaxspec */
Remacs_notsyntaxspec, /* notsyntaxspec */
#endif /* emacs */
Rnum_ops
};
static int re_compile_initialized = 0;
static int regexp_syntax = 0;
static unsigned char regexp_plain_ops[256];
static unsigned char regexp_quoted_ops[256];
static unsigned char regexp_precedences[Rnum_ops];
static int regexp_context_indep_ops;
static int regexp_ansi_sequences;
#define NUM_LEVELS 5 /* number of precedence levels in use */
#define MAX_NESTING 100 /* max nesting level of operators */
#ifdef emacs
/* This code is for emacs compatibility only. */
#include "config.h"
#include "lisp.h"
#include "buffer.h"
#include "syntax.h"
/* emacs defines NULL in some strange way? */
#undef NULL
#define NULL 0
#else /* emacs */
#define SYNTAX(ch) re_syntax_table[(unsigned char)(ch)]
#define Sword 1
/* 94/01/19 (cjk) made re_syntax_table unsigned */
#ifdef SYNTAX_TABLE
unsigned char *re_syntax_table;
#else
static unsigned char re_syntax_table[256];
#endif /* SYNTAX_TABLE */
#endif /* emacs */
/* 94/01/19 (cjk) forward declaration for memset added */
/* CFS - MacOS X Server continues to not declare memset */
/* SRL - Doesn't seem to hurt if this is left undeclared...
#if defined(NeXT) || defined(__APPLE__)
void *memset(void *, unsigned char, unsigned int);
#endif
*/
static void re_compile_initialize()
{
int a;
#if !defined(emacs) && !defined(SYNTAX_TABLE)
static int syntax_table_inited = 0;
if (!syntax_table_inited)
{
syntax_table_inited = 1;
memset(re_syntax_table, 0, 256);
for (a = 'a'; a <= 'z'; a++) re_syntax_table[a] = Sword;
for (a = 'A'; a <= 'Z'; a++) re_syntax_table[a] = Sword;
for (a = '0'; a <= '9'; a++) re_syntax_table[a] = Sword;
}
#endif /* !emacs && !SYNTAX_TABLE */
re_compile_initialized = 1;
for (a = 0; a < 256; a++)
{
regexp_plain_ops[a] = Rnormal;
regexp_quoted_ops[a] = Rnormal;
}
for (a = '0'; a <= '9'; a++)
regexp_quoted_ops[a] = Rmemory;
regexp_plain_ops['\134'] = Rquote;
if (regexp_syntax & RE_NO_BK_PARENS)
{
regexp_plain_ops['('] = Ropenpar;
regexp_plain_ops[')'] = Rclosepar;
}
else
{
regexp_quoted_ops['('] = Ropenpar;
regexp_quoted_ops[')'] = Rclosepar;
}
if (regexp_syntax & RE_NO_BK_VBAR)
regexp_plain_ops['\174'] = Ror;
else
regexp_quoted_ops['\174'] = Ror;
regexp_plain_ops['*'] = Rstar;
if (regexp_syntax & RE_BK_PLUS_QM)
{
regexp_quoted_ops['+'] = Rplus;
regexp_quoted_ops['?'] = Roptional;
}
else
{
regexp_plain_ops['+'] = Rplus;
regexp_plain_ops['?'] = Roptional;
}
if (regexp_syntax & RE_NEWLINE_OR)
regexp_plain_ops['\n'] = Ror;
regexp_plain_ops['\133'] = Ropenset;
regexp_plain_ops['\136'] = Rbol;
regexp_plain_ops['$'] = Reol;
regexp_plain_ops['.'] = Ranychar;
if (!(regexp_syntax & RE_NO_GNU_EXTENSIONS))
{
#ifdef emacs
regexp_quoted_ops['='] = Remacs_at_dot;
regexp_quoted_ops['s'] = Remacs_syntaxspec;
regexp_quoted_ops['S'] = Remacs_notsyntaxspec;
#endif /* emacs */
regexp_quoted_ops['w'] = Rwordchar;
regexp_quoted_ops['W'] = Rnotwordchar;
regexp_quoted_ops['<'] = Rwordbeg;
regexp_quoted_ops['>'] = Rwordend;
regexp_quoted_ops['b'] = Rwordbound;
regexp_quoted_ops['B'] = Rnotwordbound;
regexp_quoted_ops['`'] = Rbegbuf;
regexp_quoted_ops['\''] = Rendbuf;
}
if (regexp_syntax & RE_ANSI_HEX)
regexp_quoted_ops['v'] = Rextended_memory;
for (a = 0; a < Rnum_ops; a++)
regexp_precedences[a] = 4;
if (regexp_syntax & RE_TIGHT_VBAR)
{
regexp_precedences[Ror] = 3;
regexp_precedences[Rbol] = 2;
regexp_precedences[Reol] = 2;
}
else
{
regexp_precedences[Ror] = 2;
regexp_precedences[Rbol] = 3;
regexp_precedences[Reol] = 3;
}
regexp_precedences[Rclosepar] = 1;
regexp_precedences[Rend] = 0;
regexp_context_indep_ops = (regexp_syntax & RE_CONTEXT_INDEP_OPS) != 0;
regexp_ansi_sequences = (regexp_syntax & RE_ANSI_HEX) != 0;
}
int re_set_syntax(syntax)
int syntax;
{
int ret;
ret = regexp_syntax;
regexp_syntax = syntax;
re_compile_initialize();
return ret;
}
static int hex_char_to_decimal(ch)
int ch;
{
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
return 16;
}
char *re_compile_pattern(regex, size, bufp)
char *regex;
int size;
regexp_t bufp;
{
int a, pos, op, current_level, level, opcode;
int pattern_offset=0, alloc; /* 94/01/19 (cjk) pattern_offset initialized */
int starts[NUM_LEVELS * MAX_NESTING], starts_base;
int future_jumps[MAX_NESTING], num_jumps;
unsigned char ch=0; /* 94/01/19 (cjk) ch initialized */
char *pattern, *translate;
int next_register, paren_depth, num_open_registers, open_registers[RE_NREGS];
int beginning_context;
#define NEXTCHAR(var) \
MACRO_BEGIN \
if (pos >= size) \
goto ends_prematurely; \
(var) = regex[pos]; \
pos++; \
MACRO_END
#define ALLOC(amount) \
MACRO_BEGIN \
if (pattern_offset+(amount) > alloc) \
{ \
alloc += 256 + (amount); \
pattern = realloc(pattern, alloc); \
if (!pattern) \
goto out_of_memory; \
} \
MACRO_END
#define STORE(ch) pattern[pattern_offset++] = (ch)
#define CURRENT_LEVEL_START (starts[starts_base + current_level])
#define SET_LEVEL_START starts[starts_base + current_level] = pattern_offset
#define PUSH_LEVEL_STARTS if (starts_base < (MAX_NESTING-1)*NUM_LEVELS) \
starts_base += NUM_LEVELS; \
else \
goto too_complex
#define POP_LEVEL_STARTS starts_base -= NUM_LEVELS
#define PUT_ADDR(offset,addr) \
MACRO_BEGIN \
int disp = (addr) - (offset) - 2; \
pattern[(offset)] = disp & 0xff; \
pattern[(offset)+1] = (disp>>8) & 0xff; \
MACRO_END
#define INSERT_JUMP(pos,type,addr) \
MACRO_BEGIN \
int a, p = (pos), t = (type), ad = (addr); \
for (a = pattern_offset - 1; a >= p; a--) \
pattern[a + 3] = pattern[a]; \
pattern[p] = t; \
PUT_ADDR(p+1,ad); \
pattern_offset += 3; \
MACRO_END
#define SETBIT(buf,offset,bit) (buf)[(offset)+(bit)/8] |= (1<<((bit) & 7))
#define SET_FIELDS \
MACRO_BEGIN \
bufp->allocated = alloc; \
bufp->buffer = pattern; \
bufp->used = pattern_offset;\
MACRO_END
#define GETHEX(var) \
MACRO_BEGIN \
char gethex_ch, gethex_value; \
NEXTCHAR(gethex_ch); \
gethex_value = hex_char_to_decimal(gethex_ch); \
if (gethex_value == 16) \
goto hex_error; \
NEXTCHAR(gethex_ch); \
gethex_ch = hex_char_to_decimal(gethex_ch); \
if (gethex_ch == 16) \
goto hex_error; \
(var) = gethex_value * 16 + gethex_ch; \
MACRO_END
#define ANSI_TRANSLATE(ch) \
MACRO_BEGIN \
switch (ch) \
{ \
case 'a': \
case 'A': \
ch = 7; /* audible bell */ \
break; \
case 'b': \
case 'B': \
ch = 8; /* backspace */ \
break; \
case 'f': \
case 'F': \
ch = 12; /* form feed */ \
break; \
case 'n': \
case 'N': \
ch = 10; /* line feed */ \
break; \
case 'r': \
case 'R': \
ch = 13; /* carriage return */ \
break; \
case 't': \
case 'T': \
ch = 9; /* tab */ \
break; \
case 'v': \
case 'V': \
ch = 11; /* vertical tab */ \
break; \
case 'x': /* hex code */\
case 'X': \
GETHEX(ch); \
break; \
default: \
/* other characters passed through */ \
if (translate) \
ch = translate[(unsigned char)ch]; \
break; \
} \
MACRO_END
if (!re_compile_initialized)
re_compile_initialize();
bufp->used = 0;
bufp->fastmap_accurate = 0;
bufp->uses_registers = 0;
translate = bufp->translate;
pattern = bufp->buffer;
alloc = bufp->allocated;
if (alloc == 0 || pattern == NULL)
{
alloc = 256;
pattern = malloc(alloc);
if (!pattern)
goto out_of_memory;
}
pattern_offset = 0;
starts_base = 0;
num_jumps = 0;
current_level = 0;
SET_LEVEL_START;
num_open_registers = 0;
next_register = 1;
paren_depth = 0;
beginning_context = 1;
op = -1;
/* we use Rend dummy to ensure that pending jumps are updated (due to
low priority of Rend) before exiting the loop. */
pos = 0;
while (op != Rend)
{
if (pos >= size)
op = Rend;
else
{
NEXTCHAR(ch);
if (translate)
ch = translate[(unsigned char)ch];
op = regexp_plain_ops[(unsigned char)ch];
if (op == Rquote)
{
NEXTCHAR(ch);
op = regexp_quoted_ops[(unsigned char)ch];
if (op == Rnormal && regexp_ansi_sequences)
ANSI_TRANSLATE(ch);
}
}
level = regexp_precedences[op];
/* printf("ch='%c' op=%d level=%d current_level=%d curlevstart=%d\n",
ch, op, level, current_level, CURRENT_LEVEL_START); */
if (level > current_level)
{
for (current_level++; current_level < level; current_level++)
SET_LEVEL_START;
SET_LEVEL_START;
}
else
if (level < current_level)
{
current_level = level;
for (;num_jumps > 0 &&
future_jumps[num_jumps-1] >= CURRENT_LEVEL_START;
num_jumps--)
PUT_ADDR(future_jumps[num_jumps-1], pattern_offset);
}
switch (op)
{
case Rend:
break;
case Rnormal:
normal_char:
opcode = Cexact;
store_opcode_and_arg: /* opcode & ch must be set */
SET_LEVEL_START;
ALLOC(2);
STORE(opcode);
STORE(ch);
break;
case Ranychar:
opcode = Canychar;
store_opcode:
SET_LEVEL_START;
ALLOC(1);
STORE(opcode);
break;
case Rquote:
abort();
/*NOTREACHED*/
case Rbol:
if (!beginning_context)
{
if (regexp_context_indep_ops)
goto op_error;
else
goto normal_char;
}
opcode = Cbol;
goto store_opcode;
case Reol:
if (!(
(pos >= size)
||
((regexp_syntax & RE_NO_BK_VBAR) ? (regex[pos] == '\174') : (pos+1 < size && regex[pos] == '\134' && regex[pos+1] == '\174'))
||
((regexp_syntax & RE_NO_BK_PARENS) ? (regex[pos] == ')') : (pos+1 < size && regex[pos] == '\134' && regex[pos+1] == ')'))
))
{
if (regexp_context_indep_ops)
goto op_error;
else
goto normal_char;
}
opcode = Ceol;
goto store_opcode;
break;
case Roptional:
if (beginning_context)
{
if (regexp_context_indep_ops)
goto op_error;
else
goto normal_char;
}
if (CURRENT_LEVEL_START == pattern_offset)
break; /* ignore empty patterns for ? */
ALLOC(3);
INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump, pattern_offset + 3);
break;
case Rstar:
case Rplus:
if (beginning_context)
{
if (regexp_context_indep_ops)
goto op_error;
else
goto normal_char;
}
if (CURRENT_LEVEL_START == pattern_offset)
break; /* ignore empty patterns for + and * */
ALLOC(9);
INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump, pattern_offset + 6);
INSERT_JUMP(pattern_offset, Cstar_jump, CURRENT_LEVEL_START);
if (op == Rplus) /* jump over initial failure_jump */
INSERT_JUMP(CURRENT_LEVEL_START, Cdummy_failure_jump, CURRENT_LEVEL_START + 6);
break;
case Ror:
ALLOC(6);
INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump, pattern_offset + 6);
if (num_jumps >= MAX_NESTING) goto too_complex;
STORE(Cjump);
future_jumps[num_jumps++] = pattern_offset;
STORE(0);
STORE(0);
SET_LEVEL_START;
break;
case Ropenpar:
SET_LEVEL_START;
if (next_register < RE_NREGS)
{
bufp->uses_registers = 1;
ALLOC(2);
STORE(Cstart_memory);
STORE(next_register);
open_registers[num_open_registers++] = next_register;
next_register++;
}
paren_depth++;
PUSH_LEVEL_STARTS;
current_level = 0;
SET_LEVEL_START;
break;
case Rclosepar:
if (paren_depth <= 0)
goto parenthesis_error;
POP_LEVEL_STARTS;
current_level = regexp_precedences[Ropenpar];
paren_depth--;
if (paren_depth < num_open_registers)
{
bufp->uses_registers = 1;
ALLOC(2);
STORE(Cend_memory);
num_open_registers--;
STORE(open_registers[num_open_registers]);
}
break;
case Rmemory:
if (ch == '0')
goto bad_match_register;
assert(ch >= '0' && ch <= '9');
bufp->uses_registers = 1;
opcode = Cmatch_memory;
ch -= '0';
goto store_opcode_and_arg;
case Rextended_memory:
NEXTCHAR(ch);
if (ch < '0' || ch > '9')
goto bad_match_register;
NEXTCHAR(a);
if (a < '0' || a > '9')
goto bad_match_register;
ch = 10 * (a - '0') + ch - '0';
if (ch <= 0 || ch >= RE_NREGS)
goto bad_match_register;
bufp->uses_registers = 1;
opcode = Cmatch_memory;
goto store_opcode_and_arg;
case Ropenset:
{
int complement,prev,offset,range,firstchar;
SET_LEVEL_START;
ALLOC(1+256/8);
STORE(Cset);
offset = pattern_offset;
for (a = 0; a < 256/8; a++)
STORE(0);
NEXTCHAR(ch);
if (translate)
ch = translate[(unsigned char)ch];
if (ch == '\136')
{
complement = 1;
NEXTCHAR(ch);
if (translate)
ch = translate[(unsigned char)ch];
}
else
complement = 0;
prev = -1;
range = 0;
firstchar = 1;
while (ch != '\135' || firstchar)
{
firstchar = 0;
if (regexp_ansi_sequences && ch == '\134')
{
NEXTCHAR(ch);
ANSI_TRANSLATE(ch);
}
if (range)
{
for (a = prev; a <= ch; a++)
SETBIT(pattern, offset, a);
prev = -1;
range = 0;
}
else
if (prev != -1 && ch == '-')
range = 1;
else
{
SETBIT(pattern, offset, ch);
prev = ch;
}
NEXTCHAR(ch);
if (translate)
ch = translate[(unsigned char)ch];
}
if (range)
SETBIT(pattern, offset, '-');
if (complement)
{
for (a = 0; a < 256/8; a++)
pattern[offset+a] ^= 0xff;
}
break;
}
case Rbegbuf:
opcode = Cbegbuf;
goto store_opcode;
case Rendbuf:
opcode = Cendbuf;
goto store_opcode;
case Rwordchar:
opcode = Csyntaxspec;
ch = Sword;
goto store_opcode_and_arg;
case Rnotwordchar:
opcode = Cnotsyntaxspec;
ch = Sword;
goto store_opcode_and_arg;
case Rwordbeg:
opcode = Cwordbeg;
goto store_opcode;
case Rwordend:
opcode = Cwordend;
goto store_opcode;
case Rwordbound:
opcode = Cwordbound;
goto store_opcode;
case Rnotwordbound:
opcode = Cnotwordbound;
goto store_opcode;
#ifdef emacs
case Remacs_at_dot:
opcode = Cemacs_at_dot;
goto store_opcode;
case Remacs_syntaxspec:
NEXTCHAR(ch);
if (translate) ch = translate[(unsigned char)ch];
opcode = Csyntaxspec;
ch = syntax_spec_code[(unsigned char)ch];
goto store_opcode_and_arg;
case Remacs_notsyntaxspec:
NEXTCHAR(ch);
if (translate) ch = translate[(unsigned char)ch];
opcode = Cnotsyntaxspec;
ch = syntax_spec_code[(unsigned char)ch];
goto store_opcode_and_arg;
#endif /* emacs */
default:
abort();
}
beginning_context = (op == Ropenpar || op == Ror);
}
if (starts_base != 0)
goto parenthesis_error;
assert(num_jumps == 0);
ALLOC(1);
STORE(Cend);
SET_FIELDS;
return NULL;
op_error:
SET_FIELDS;
return "Badly placed special character";
bad_match_register:
SET_FIELDS;
return "Bad match register number";
hex_error:
SET_FIELDS;
return "Bad hexadecimal number";
parenthesis_error:
SET_FIELDS;
return "Badly placed parenthesis";
out_of_memory:
SET_FIELDS;
return "Out of memory";
ends_prematurely:
SET_FIELDS;
return "Regular expression ends prematurely";
too_complex:
SET_FIELDS;
return "Regular expression too complex";
}
#undef CHARAT
#undef NEXTCHAR
#undef GETHEX
#undef ALLOC
#undef STORE
#undef CURRENT_LEVEL_START
#undef SET_LEVEL_START
#undef PUSH_LEVEL_STARTS
#undef POP_LEVEL_STARTS
#undef PUT_ADDR
#undef INSERT_JUMP
#undef SETBIT
#undef SET_FIELDS
static void re_compile_fastmap_aux(code, pos, visited, can_be_null, fastmap)
char *code, *visited, *can_be_null, *fastmap;
int pos;
{
int a, b, syntaxcode;
if (visited[pos])
return; /* we have already been here */
visited[pos] = 1;
for (;;)
switch (code[pos++])
{
case Cend:
*can_be_null = 1;
return;
case Cbol:
case Cbegbuf:
case Cendbuf:
case Cwordbeg:
case Cwordend:
case Cwordbound:
case Cnotwordbound:
#ifdef emacs
case Cemacs_at_dot:
#endif /* emacs */
break;
case Csyntaxspec:
syntaxcode = code[pos++];
for (a = 0; a < 256; a++)
if (SYNTAX(a) == syntaxcode)
fastmap[a] = 1;
return;
case Cnotsyntaxspec:
syntaxcode = code[pos++];
for (a = 0; a < 256; a++)
if (SYNTAX(a) != syntaxcode)
fastmap[a] = 1;
return;
case Ceol:
fastmap['\n'] = 1;
if (*can_be_null == 0)
*can_be_null = 2; /* can match null, but only at end of buffer*/
return;
case Cset:
for (a = 0; a < 256/8; a++)
if (code[pos + a] != 0)
for (b = 0; b < 8; b++)
if (code[pos + a] & (1 << b))
fastmap[(a << 3) + b] = 1;
pos += 256/8;
return;
case Cexact:
fastmap[(unsigned char)code[pos]] = 1;
return;
case Canychar:
for (a = 0; a < 256; a++)
if (a != '\n')
fastmap[a] = 1;
return;
case Cstart_memory:
case Cend_memory:
pos++;
break;
case Cmatch_memory:
for (a = 0; a < 256; a++)
fastmap[a] = 1;
*can_be_null = 1;
return;
case Cjump:
case Cdummy_failure_jump:
case Cupdate_failure_jump:
case Cstar_jump:
a = (unsigned char)code[pos++];
a |= (unsigned char)code[pos++] << 8;
pos += (int)(short)a;
if (visited[pos])
{
/* argh... the regexp contains empty loops. This is not
good, as this may cause a failure stack overflow when
matching. Oh well. */
/* this path leads nowhere; pursue other paths. */
return;
}
visited[pos] = 1;
break;
case Cfailure_jump:
a = (unsigned char)code[pos++];
a |= (unsigned char)code[pos++] << 8;
a = pos + (int)(short)a;
re_compile_fastmap_aux(code, a, visited, can_be_null, fastmap);
break;
default:
abort(); /* probably some opcode is missing from this switch */
/*NOTREACHED*/
}
}
static int re_do_compile_fastmap(buffer, used, pos, can_be_null, fastmap)
char *buffer, *fastmap, *can_be_null;
int used, pos;
{
char small_visited[512], *visited;
if (used <= sizeof(small_visited))
visited = small_visited;
else
{
visited = malloc(used);
if (!visited)
return 0;
}
*can_be_null = 0;
memset(fastmap, 0, 256);
memset(visited, 0, used);
re_compile_fastmap_aux(buffer, pos, visited, can_be_null, fastmap);
if (visited != small_visited)
free(visited);
return 1;
}
void re_compile_fastmap(bufp)
regexp_t bufp;
{
if (!bufp->fastmap || bufp->fastmap_accurate)
return;
assert(bufp->used > 0);
if (!re_do_compile_fastmap(bufp->buffer, bufp->used, 0, &bufp->can_be_null, bufp->fastmap))
return;
if (bufp->buffer[0] == Cbol)
bufp->anchor = 1; /* begline */
else
if (bufp->buffer[0] == Cbegbuf)
bufp->anchor = 2; /* begbuf */
else
bufp->anchor = 0; /* none */
bufp->fastmap_accurate = 1;
}
#define INITIAL_FAILURES 128 /* initial # failure points to allocate */
#define MAX_FAILURES 4100 /* max # of failure points before failing */
int re_match_pattern_2(bufp, string1, size1, string2, size2, pos, regs, mstop)
regexp_t bufp;
char *string1, *string2;
int size1, size2, pos, mstop;
regexp_registers_t regs;
{
struct failure_point { char *text, *partend, *code; }
*failure_stack_start, *failure_sp, *failure_stack_end,
initial_failure_stack[INITIAL_FAILURES];
char *code, *translate, *text, *textend, *partend, *part_2_end;
char *regstart_text[RE_NREGS], *regstart_partend[RE_NREGS];
char *regend_text[RE_NREGS], *regend_partend[RE_NREGS];
int a, b, ch, reg, regch, match_end;
char *regtext, *regpartend, *regtextend;
#define PREFETCH \
MACRO_BEGIN \
if (text == partend) \
{ \
if (text == textend) \
goto fail; \
text = string2; \
partend = part_2_end; \
} \
MACRO_END
#define NEXTCHAR(var) \
MACRO_BEGIN \
PREFETCH; \
(var) = (unsigned char)*text++; \
if (translate) \
(var) = (unsigned char)translate[(var)]; \
MACRO_END
assert(pos >= 0 && size1 >= 0 && size2 >= 0 && mstop >= 0);
assert(mstop <= size1 + size2);
assert(pos <= mstop);
if (pos <= size1)