-
Notifications
You must be signed in to change notification settings - Fork 0
/
rexel.c
2786 lines (2647 loc) · 69.8 KB
/
rexel.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
/*
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
* rexel - A Regular EXpression Evaluation Library
* because everyone needs their own regex library
*
* This library supports matching without backtracking by providing
* a single character at a time. When a match is found, the
* length of that match is reported.
*
* Compiled form of a regex is an array of 16 bit unsigned numbers called
* rexels, or Regular EXpression ELements.
* This involves some cheating as wctype_t (unsigned long int) values
* are stored in 16 bits.
* This array is comprised of a "regexp" section follow by some a "set"
* section.
* The first entry in the regex section is the size of that section (including
* the length). Adding this size to the start gives the start of the
* "set" section.
*
* The "set" section contains some "sets" each of which contains 1 or
* more subsections followed by a "zero". Each subsection starts with
* its size. The first section can have size zero, others cannot (as
* another zero marks the end of the "set").
* The first subsection of a set is a list of "character classes".
* An internal mapping is created from used character classes (like
* "digit" and "lower" etc) to small numbers. If a set should match a
* given character class, the small number is stored in this subsection
* If a set should *not* match, then the small number is added with the
* msb set.
*
* Subsequent subsections contain a general character-set, each
* subsection for a single unicode plane. The top six bits of the first
* entry is the plane number, the remaining bits are the size. After
* this are "size" 16bit chars in sorted order. The values in even
* slots are in the set, values in odd slots are not. Value not in any
* slot are treated like the largest value less than it which does have
* a slot. So iff a search for "largest entry nor larger than" finds an
* even slot, then the target is in the set.
*
* The rexels in the "regexp" section come in 4 groups.
* 0: 15 bit unicode number. Other unicode numbers require the plane
* to be given first, described below.
* 100: address of a "regex" subarray. The match forks at this point,
* both the next entry and the addressed entry are considered.
* This limits total size to 8192 entries.
*
* 101: address of a char set. This 13 bit address is an offset from the
* start of the "set" section.
*
* 1100: start of a capture group. 4096 possible groups.
* 1101: end of a capture group.
*
* 1110: must match the most recent instance of the capture group.
*
* 1111 0000 000: A UNICODE plane number - 0..16 The next 16 bit
* number is combined with the plane number to produce a code point
* to match. The first half of plane zero does not need this
* 32bit format - those value can be given in 16 bites.
*
* The last several values have special meanings.
* 0xffe0 - match any char
* 0xffe1 - match any character except any EOL character
* 0xffe2 - match no char - dead end.
* 0xffe3 - report success.
* 0xffe4 - match at start of line
* 0xffe5 - match at end of line
* 0xffe6 - match at start of word
* 0xffe7 - match at end of word
* 0xffe8 - match a word break (start or end)
* 0xffe9 - match any point that isn't a word break.
* 0xffea - match start of document
* 0xffeb - match end of document
* 0xffec - match the location indicated by RXL_POINT
*
* 0xfffb - match any quote: " ' ` - lax searching
* 0xfffc - match 1 or more spaces/tabs/newlines - lax searching.
* 0xfffd - match - or _ - lax searching
* 0xfffe - Subsequent chars (0x..) are matched ignoring case
* 0xffff - Subsequent chars are match case-correct.
*
* When matching, two pairs of extra arrays are allocated and used.
* One pair is 'before', one pair is 'after'. They swap on each char.
* One contains a threaded linkage among all points in regex subarray
* which are currently matched. A 'zero' marks the end of the chain.
* The other records the length of the longest match at that point.
* So when a char is matched, the length+1 of the 'before' moves
* to the 'after' position.
*
* A match is *before* processing the index command.
*
* "man 7 regex" described POSIX regular expression and notes some area
* where implementations differ, using (!). The terminology describes
* a Regular Expression (RE) as:
* RE -> branch ( '|' branch ) * # 1 or more branches
* # separated by '|'
* branch -> piece ( piece ) * # 1 or more pieces,
* # concatenated.
* piece -> atom ( '*' | '+' | '?' | bound )? # an atom, possibly
* # followed by repeater
* bound -> '{' N ( ',' ( N )? )? '}' # 1 or 2 numbers in braces.
* atom -> '(' RE ')' | C | '.' | \?? # dot or char or
* # RE in parentheses.
*
* Responding to each implementation difference:
* - There must be at least one branch in an RE, and all must be non-empty.
* - A branch needs at least one piece.
* - This implementation (currently) only allows a *single* '*','+','?'
* after an atom.
* - Integers in a bound must be less than 256.
* - The empty string atom '()' is not permitted
* - '\C', where 'C' is a special character: one of ^.[$()|*+?{\ removes any
* special meaning from that character. This does not apply inside []
* as those characters have no special meaning, or a different meaning there.
* - '\C', where 'C' is not in that list is an error except for those used for
* some special character classes. Those classes which are not
* "everything except" are permitted equally inside character sets ([]).
* The classes are:
* \d a digit
* \p a punctuation character
* \s a spacing character
* \w a word (alphabetic character)
* \D \P \S \W are negation of above. So non-digit, non-punctuation etc.
* \A an upper case character
* \a a lower case character
*
* - A '{' followed by a non-digit is just a '{'
* - Two ranges may *not* share an endpoint.
* - equivalence classes and collating elements are not implemented.
* - No particular limit on the length of an RE is imposed (yet)
*
*
* Other extensions:
* ?: - a group that will never be captured
* ?| - don't capture, and within each branch any caputuring uses
* same numbering
* ?0 - all remainder is literal
* ?nnn:- a group of precisely nnn literal match chars
* ?isLn-isLn: - flags before any optional '-' are set. Flags after
* are cleared.
* i - ignore case
* L - lax matching for space, hyphen, and quote
* s - single line, '.' matches newline
* n - no capture in subgroups either
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <wchar.h>
#include <ctype.h>
#include <wctype.h>
#include <memory.h>
#ifdef DEBUG
#include <getopt.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#endif
#include "safe.h"
#include "misc.h"
#include "rexel.h"
#ifdef DEBUG
#include <stdio.h>
#endif
struct match_state {
unsigned short *rxl safe;
unsigned short * safe link[2];
unsigned short * safe leng[2];
unsigned long * safe ignorecase;
unsigned short active;
bool anchored;
int match;
int len;
int start, total;
/* Backtracking state */
bool backtrack;
/* 'buf' is an allocated buffer of 'buf_size'.
* There is text up to buf_count which we have been asked
* to match against. We are currently considering the
* wchar at buf_pos to see if it matches. If buf_pos == buf_count,
* rxl_advance_bt() will return so that another char can be provided.
*/
wint_t * safe buf;
unsigned short buf_size;
unsigned short buf_pos, buf_count;
/* pos in an index in 'rxl' that buf[buf_pos] must be matched against. */
unsigned short pos;
/* 'record' is an allocated buffer of record_size of
* which record_count entries record details of the current match
* of buf..buf_pos from above.
* We only record fork points that were taken on the path through the
* pattern which achieved the current match. During matching, we may
* need to rewind to these, and follow forward from that fork.
* During capture extraction, we need to follow those to find
* how the match applied.
*/
struct record {
unsigned short pos;
unsigned short len;
} * safe record;
unsigned short record_count;
unsigned short record_size;
#ifdef DEBUG
bool trace;
#endif
};
#define wctype_cells (sizeof(wctype_t) / sizeof(unsigned short))
/* ignorecase is a bit set */
#define BITS_PER_LONG (sizeof(unsigned long) * 8)
static inline int BITSET_SIZE(int bits)
{
return (bits + BITS_PER_LONG - 1) / BITS_PER_LONG;
}
static inline bool test_bit(int bit, const unsigned long *set safe)
{
return !!(set[bit / BITS_PER_LONG] & (1 << (bit % BITS_PER_LONG)));
}
static inline void set_bit(int bit, unsigned long *set safe)
{
set[bit / BITS_PER_LONG] |= (1 << (bit % BITS_PER_LONG));
}
static inline void clear_bit(int bit, unsigned long *set safe)
{
set[bit / BITS_PER_LONG] &= ~(1 << (bit % BITS_PER_LONG));
}
#define NO_LINK 0xFFFF
#define LOOP_CHECK 0xFFFE
/* RExel Commands */
#define REC_ANY 0xFFE0
#define REC_ANY_NONL 0xFFE1
#define REC_NONE 0xFFE2
#define REC_MATCH 0xFFE3
#define REC_SOL 0xFFE4
#define REC_EOL 0xFFE5
#define REC_SOW 0xFFE6
#define REC_EOW 0xFFE7
#define REC_WBRK 0xFFE8
#define REC_NOWBRK 0xFFE9
#define REC_SOD 0xFFEa /* Start of Document */
#define REC_EOD 0xFFEb /* End of Document */
#define REC_POINT 0xFFEc
#define REC_LAXQUOT 0xFFFb
#define REC_LAXSPC 0xFFFc
#define REC_LAXDASH 0xFFFd
#define REC_IGNCASE 0xFFFe
#define REC_USECASE 0xFFFf
#define REC_FORK 0x8000
#define REC_FORKLAST 0x8000
#define REC_FORKFIRST 0x9000
#define REC_SET 0xa000
#define REC_CAPTURE 0xc000
#define REC_CAPTURED 0xc800
/* 0xd??? unused */
#define REC_BACKREF 0xe000
#define REC_PLANE 0xf000
#define REC_ISCHAR(x) (!((x) & 0x8000))
#define REC_ISSPEC(x) ((x) >= REC_ANY)
#define REC_ISFORK(x) (((x) & 0xe000) == REC_FORK)
#define REC_ISSET(x) (((x) & 0xe000) == REC_SET)
#define REC_ISCAPTURE(x) (((x) & 0xf000) == REC_CAPTURE)
#define REC_ISBACKREF(x) (((x) & 0xf000) == REC_BACKREF)
#define REC_BACKREF_MAKE(x) (REC_BACKREF | ((x) & 0x7ff))
#define REC_ADDR(x) ((x) & 0x0fff)
#define REC_ISFIRST(x) (!!((x) & (REC_FORKFIRST ^ REC_FORKLAST)))
#define REC_CAPNUM(x) ((x) & 0x07ff)
#define REC_CAPEND(x) ((x) & 0x0800)
#define REC_ISPLANE(x) (((x) & 0xFFE0) == 0xF000)
#define REC_PLANE_OF(x) ((x) & 0x1F)
#define REC_PLANE_MAKE(x, y) ((REC_PLANE_OF(x)<<16) | y)
static inline bool rec_noop(unsigned short cmd)
{
/* These commands don't affect matching directly, they
* are extracted and used outside the core matcher.
*/
return cmd == REC_IGNCASE || cmd == REC_USECASE ||
REC_ISCAPTURE(cmd);
}
static inline bool rec_zerowidth(unsigned short cmd)
{
return ((cmd >= REC_SOL && cmd <= REC_NOWBRK) ||
rec_noop(cmd));
}
/* First entry contains start of maps, and flags */
#define RXL_PATNLEN(rxl) ((rxl)[0] & 0x3fff)
#define RXL_SETSTART(rxl) ((rxl) + RXL_PATNLEN(rxl))
static enum rxl_found rxl_advance_bt(struct match_state *st safe, wint_t ch);
static void bt_link(struct match_state *st safe, int pos, int len);
static int get_capture(struct match_state *s safe, int n, int which,
char *buf, int *startp);
/*
* The match_state contains several partial matches that lead to "here".
* rxl_advance() examines each of these to determine if they will still match
* after consuming either a character or a position-type flag (SOL, EOL, etc).
* It calls do_link for each case that is still a possible match.
* 'pos' is the position in the regexp that matches the new point in the target.
* 'dest' is the place in the new threaded list to record this match. i.e.
* the slot that it currently the end of the list.
* 'len' is the length of the match of to this (new) point in the target.
* If there is already a match to this point in the pattern, we just update
* the length and don't relink anything.
*
*/
static void do_link(struct match_state *st safe, int pos, int *destp, int len)
{
unsigned short cmd = st->rxl[pos];
while (rec_noop(cmd)) {
pos += 1;
cmd = st->rxl[pos];
}
if (cmd == REC_MATCH) {
if (st->match < len)
st->match = len;
/* Don't accept another start point */
st->anchored = True;
}
if (st->backtrack) {
bt_link(st, pos, len);
return;
}
if (!destp)
return;
if (cmd == REC_NOWBRK) {
/* NOWBRK is special because it matches a character
* without consuming it. We link them at the start
* of the list so they can be found quickly.
*/
if (st->link[st->active][pos] == NO_LINK) {
st->leng[st->active][pos] = len;
if (st->link[st->active][0] == 0)
*destp = pos;
st->link[st->active][pos] =
st->link[st->active][0];
st->link[st->active][0] = pos;
} else if (st->leng[st->active][pos] < len)
st->leng[st->active][pos] = len;
} else if (!REC_ISFORK(cmd)) {
/* not a FORK, so just link it in. */
if (st->link[st->active][pos] == NO_LINK) {
st->leng[st->active][pos] = len;
st->link[st->active][*destp] = pos;
st->link[st->active][pos] = 0;
*destp = pos;
} else if (st->leng[st->active][pos] < len)
st->leng[st->active][pos] = len;
} else if (st->link[st->active][pos] == NO_LINK ||
st->leng[st->active][pos] < len) {
st->link[st->active][pos] = LOOP_CHECK;
st->leng[st->active][pos] = len;
do_link(st, REC_ADDR(cmd), destp, len);
do_link(st, pos+1, destp, len);
}
}
static int set_match(struct match_state *st safe, unsigned short addr,
wchar_t ch, bool ic)
{
unsigned short *set safe = RXL_SETSTART(st->rxl) + addr;
wchar_t uch = ch, lch = ch;
unsigned short len;
bool invert = False;
if (ic) {
/* As Unicode has 3 cases, can we be sure that everything
* has a 'lower' to map to? Surely everything has at least
* and upper or a lower...
*/
uch = towupper(ch);
lch = towlower(ch);
}
/* First there is an 'invert' flag and possibly some char classes */
len = *set++;
invert = !!(len & 0x8000);
if (len) {
len &= 0x7fff;
for ( ; len; set += wctype_cells) {
wctype_t class;
len -= wctype_cells;
memcpy(&class, set, sizeof(wctype_t));
if (iswctype(uch, class) ||
(uch != lch && iswctype(lch, class)))
return !invert;
}
}
/* now there might be some sets. Each set starts with a size with
* top 5 bytes indicating top bytes of unicode planes, and bottom
* 11 bytes size of table
*/
while ((len = *set++) != 0) {
int high = (len & 0xF800) << 5;
/* Both upper and lower case have been placed in set,
* so only need to search for one of them
*/
unsigned short target;
int lo, hi;
len &= 0x7ff;
if ((uch & 0x1f0000) == high)
target = uch & 0xffff;
else if ((lch & 0x1f0000) == high)
target = lch & 0xffff;
else {
set += len;
continue;
}
/* Binary search to find first entry that is greater
* than target.
*/
lo = 0; /* Invar: No entry before lo is greater than target */
hi = len; /* Invar: Every entry at or after hi is greater
* than target */
#ifdef DEBUG
/* Sanity check - array must be sorted */
for (lo = 1; lo < len; lo++)
if (set[lo-1] >= set[lo]) {
printf("Set %d, subset %d not ordered at %u\n",
addr, set - RXL_SETSTART(st->rxl) - addr,
lo);
exit(1);
}
lo = 0;
#endif
while (lo < hi) {
int mid = (lo + hi) / 2;
if (set[mid] > target)
hi = mid;
else
lo = mid + 1;
}
/* set[lo] == set[hi] = first entry greater than target.
* If 'lo' is even, there was no match. If 'lo' is odd,
* there was.
*/
if (lo & 1)
return !invert;
set += len;
}
return invert;
}
/*
* Advance the match state to process 'ch' or a flag.
* flag indicates start/end of word/line.
* Returns -2 if there is no possibility of a match including this ch/flag
* Returns -1 if part of the pattern has matched, and more input is needed.
* Returns >=0 if a match has been found. The return value is the number
* of characters (not flags) in the match.
* When a >=0 return is given, it might still be useful to keep calling
* rxl_advance if a maximal match is wanted.
* If the match must be anchor to the first character, this must have been
* advised to rxl_prepare. The caller should keep calling until no chars are
* left, or -2 is returned (no match), or >=0 is returned. In the latter case
* the call might keep calling to see if a longer match is possible. Until -2
* is seen, a longer match is still possible.
*/
static int advance_one(struct match_state *st safe, int i,
wint_t ch, int flag,
int len, int *eolp)
{
unsigned int cmd = st->rxl[i];
wint_t lch = ch, uch = ch;
bool ic = test_bit(i, st->ignorecase);
int advance = 0;
int inc = 1;
if (ic) {
uch = toupper(ch);
lch = tolower(ch);
}
if (REC_ISSPEC(cmd)) {
switch(cmd) {
case REC_ANY:
advance = 1;
if (flag)
advance = 0;
break;
case REC_ANY_NONL:
advance = 1;
if (ch == '\n' || ch == '\r' || ch == '\f')
advance = -1;
if (flag)
advance = 0;
break;
case REC_MATCH:
/* cannot match more chars here */
if (flag)
advance = 0;
else
advance = -1;
break;
case REC_NONE:
advance = -1;
break;
case REC_SOL:
if (flag & RXL_SOL)
advance = 1;
else if (!flag)
advance = -1;
else
advance = 0;
break;
case REC_EOL:
if (flag & RXL_EOL)
advance = 1;
else if (!flag)
advance = -1;
else
advance = 0;
break;
case REC_SOD:
if (flag & RXL_SOD)
advance = 1;
else if (!flag)
advance = -1;
else
advance = 0;
break;
case REC_EOD:
if (flag & RXL_EOD)
advance = 1;
else if (!flag)
advance = -1;
else
advance = 0;
break;
case REC_POINT:
if (flag & RXL_POINT)
advance = 1;
else if (!flag)
advance = -1;
else
advance = 0;
break;
case REC_SOW:
if (flag & RXL_SOW)
advance = 1;
else if (flag & RXL_NOWBRK || !flag)
advance = -1;
else
advance = 0;
break;
case REC_EOW:
if (flag & RXL_EOW)
advance = 1;
else if (flag & RXL_NOWBRK || !flag)
advance = -1;
else
advance = 0;
break;
case REC_WBRK:
if (flag & (RXL_SOW | RXL_EOW))
advance = 1;
else if (flag & RXL_NOWBRK || !flag)
advance = -1;
else
advance = 0;
break;
case REC_NOWBRK:
if (flag & (RXL_SOW | RXL_EOW))
advance = -1;
else if (flag & RXL_NOWBRK)
advance = 1;
else
advance = 0;
break;
case REC_LAXSPC:
if (strchr(" \t\r\n\f", ch) != NULL)
advance = 1;
else
advance = -1;
if (flag)
advance = 0;
break;
case REC_LAXQUOT:
if (strchr("'`\"", ch) != NULL)
advance = 1;
else
advance = -1;
if (flag)
advance = 0;
break;
case REC_LAXDASH:
if (strchr("-_.", ch) != NULL)
advance = 1;
else
advance = -1;
if (flag)
advance = 0;
break;
}
} else if (flag) {
/* expecting a char, so ignore position info */
advance = 0;
} else if (REC_ISCHAR(cmd)) {
if (cmd == ch ||
(ic && (cmd == uch || cmd == lch)))
advance = 1;
else
advance = -1;
} else if (REC_ISPLANE(cmd) && i+1 < RXL_PATNLEN(st->rxl)) {
cmd = REC_PLANE_MAKE(cmd, st->rxl[i+1]);
inc = 2;
if (cmd == ch ||
(ic && (cmd == uch || cmd == lch)))
advance = 1;
else
advance = -1;
} else if (REC_ISSET(cmd)) {
if (set_match(st, REC_ADDR(cmd), ch, ic))
advance = 1;
else
advance = -1;
} else if (REC_ISBACKREF(cmd)) {
/* Backref not supported */
advance = -2;
} else
/* Nothing else is possible here */
/* abort(); */
advance = -2;
if (advance < 0)
/* no match on this path */
;
else if (advance == 0)
/* Nothing conclusive here */
do_link(st, i, eolp, len);
else
/* Need to advance and link the new address in. However
* if there is a fork, we might need to link multiple
* addresses in. Best use recursion.
*/
do_link(st, i+inc, eolp, len + (ch != 0));
return advance;
}
enum rxl_found rxl_advance(struct match_state *st safe, wint_t ch)
{
int active;
int next;
int eol;
unsigned short i;
wint_t flag = ch & ~(0x1fffff);
enum rxl_found ret = RXL_NOMATCH;
if (st->backtrack)
return rxl_advance_bt(st, ch);
ch &= 0x1fffff;
if (flag && ((flag & (flag-1)) || ch)) {
int f;
enum rxl_found r;
/* Need to handle flags separately */
for (f = RXL_SOD ; f && f <= RXL_LAST; f <<= 1) {
if (!(flag & f))
continue;
r = rxl_advance(st, f);
if (r > ret)
ret = r;
}
flag = 0;
if (!ch)
return ret;
}
if (flag == RXL_ANCHOR) {
st->anchored = True;
return ret;
}
active = st->active;
next = 1-active;
if (flag == RXL_NOWBRK &&
(st->link[active][0] == NO_LINK ||
st->rxl[st->link[active][0]] != REC_NOWBRK))
/* Reporting not-a-word-boundary, but no-one cares,
* so just return.
*/
return st->match >= 0 ? RXL_MATCH_FLAG :
st->len >= 0 ? RXL_CONTINUE : RXL_NOMATCH;
if (!flag)
st->total += 1;
if (!st->anchored) {
/* We haven't found a match yet and search is not anchored,
* so possibly start a search here.
*/
eol = 0;
while (st->link[active][eol])
eol = st->link[active][eol];
/* Found the end of the list. */
do_link(st, 1, &eol, 0);
}
st->match = -1;
eol = 0;
st->active = next;
#ifdef DEBUG
if (st->trace) {
/* Trace shows current length at each state. FORK points
* are not state points
* At each point we print the Char or Set number, then the
* length of a match to there - on the next line.
* Allow 4 chars per column
*/
char t[5];
unsigned short cnt;
int len = RXL_PATNLEN(st->rxl);
for (i = 1; i < len; i++)
if (!REC_ISFORK(st->rxl[i])) {
unsigned short cmd = st->rxl[i];
if (REC_ISCHAR(cmd)) {
if (cmd > ' ' && cmd < 0x7f)
printf("'%c' ", cmd);
else
printf("x%3x", cmd);
} else if (REC_ISSET(cmd)) {
printf("S%-3d", REC_ADDR(cmd));
} else if (REC_ISBACKREF(cmd)) {
printf("B%-3d", REC_CAPNUM(cmd));
} else if (REC_ISCAPTURE(cmd)) {
if (REC_CAPEND(cmd))
printf("%3d)", REC_CAPNUM(cmd));
else
printf("(%-3d", REC_CAPNUM(cmd));
} else switch(cmd) {
case REC_ANY: printf(" . "); break;
case REC_ANY_NONL: printf(" .? "); break;
case REC_NONE:printf("## "); break;
case REC_SOL: printf(" ^ "); break;
case REC_EOL: printf(" $ "); break;
case REC_SOW: printf("\\< "); break;
case REC_EOW: printf("\\> "); break;
case REC_SOD: printf("\\` "); break;
case REC_EOD: printf("\\' "); break;
case REC_POINT: printf("\\= "); break;
case REC_WBRK: printf("\\b "); break;
case REC_NOWBRK: printf("\\B "); break;
case REC_MATCH:printf("!!! "); break;
case REC_LAXSPC: printf("x20!"); break;
case REC_LAXQUOT: printf("'! "); break;
case REC_LAXDASH: printf("-! "); break;
case REC_IGNCASE: printf("?i:"); break;
case REC_USECASE: printf("?c:"); break;
default: printf("!%04x", cmd);
}
}
printf("\n");
for (i = 1 ; i < len; i++)
if (!REC_ISFORK(st->rxl[i])) {
if (st->link[active][i] == NO_LINK)
printf("-- ");
else
printf("%2d ", st->leng[active][i]);
}
if (flag) {
printf("Flag:");
if (flag & RXL_SOD) printf(" SOD");
if (flag & RXL_SOL) printf(" SOL");
if (flag & RXL_EOL) printf(" EOL");
if (flag & RXL_EOD) printf(" EOD");
if (flag & RXL_SOW) printf(" SOW");
if (flag & RXL_EOW) printf(" EOW");
if (flag & RXL_NOWBRK) printf(" NOWBRK");
if (flag & RXL_POINT) printf(" POINT");
} else
printf("Match %s(%x)",
ch < ' ' ? "?" : put_utf8(t, ch) , ch);
/* Now check the linkage is correct. The chain should lead
* to 0 without seeing any 'NO_LINK' or any ISFORK, and
* the number of NO_LINK plus number on chain should make len
*/
cnt = 0;
i = 0;
do {
if (st->link[active][i] == NO_LINK)
abort();
if (i && REC_ISFORK(st->rxl[i]))
abort();
cnt += 1;
i = st->link[active][i];
if (i >= len)
abort();
} while (i);
for (i = 0; i < len; i++)
if (st->link[active][i] == NO_LINK ||
st->link[active][i] == LOOP_CHECK)
cnt += 1;
if (cnt != len)
abort();
}
#endif /* DEBUG */
/* Firstly, clear out next lists */
/* This works because NO_LINK is 0xffff */
memset(st->link[next], 0xff, RXL_PATNLEN(st->rxl) * 2);
memset(st->leng[next], 0, RXL_PATNLEN(st->rxl) * 2);
st->link[next][0] = 0;
/* Now advance each current match */
for (i = st->link[active][0]; i; i = st->link[active][i]) {
int len = st->leng[active][i];
advance_one(st, i, ch, flag, len, &eol);
}
st->link[next][eol] = 0;
if (st->match > st->len) {
st->len = st->match;
st->start = st->total - st->len;
}
#ifdef DEBUG
if (st->trace) {
if (st->match >= 0 || eol != 0)
printf(" ... -> %d\n", st->match);
else
printf(" ... -> NOMATCH\n");
}
#endif
/* 'ret' is the result of an flags, or RXL_NOMATCH. */
if (ret >= RXL_MATCH && !flag && st->match >= 0)
return RXL_MATCH;
if (ret >= RXL_MATCH_FLAG || (st->match >= 0 && flag))
return RXL_MATCH_FLAG;
if (ret >= RXL_MATCH || st->match >= 0)
return RXL_MATCH;
if (eol == 0 && st->match < 0 && st->anchored) {
/* No chance of finding (another) match now */
return RXL_DONE;
}
if (st->len >= 0)
return RXL_CONTINUE;
return RXL_NOMATCH;
}
int rxl_info(struct match_state *st safe, int *lenp safe, int *totalp,
int *startp, int *since_startp)
{
*lenp = st->len;
if (totalp)
*totalp = st->total;
if (startp) {
if (st->len < 0)
*startp = -1;
else
*startp = st->start;
}
if (since_startp) {
if (st->len < 0)
*since_startp = -1;
else
*since_startp = st->total - st->start;
}
/* Return 'true' if there might be something here and
* we cannot safely skip forward
*/
return st->anchored ||
(st->link[st->active] && st->link[st->active][0] != 0);
}
#define RESIZE(buf) \
do { \
if ((buf##_size) <= (buf##_count+1)) { \
if ((buf##_size) < 8) \
(buf##_size) = 8; \
(buf##_size) *= 2; \
(buf) = realloc(buf, (buf##_size) * sizeof((buf)[0]));\
} \
} while(0);
static void bt_link(struct match_state *st safe, int pos, int len)
{
unsigned short cmd = st->rxl[pos];
if (!REC_ISFORK(cmd)) {
st->pos = pos;
return;
}
RESIZE(st->record);
st->record[st->record_count].pos = pos;
st->record[st->record_count].len = len;
st->record_count += 1;
if (REC_ISFIRST(cmd))
/* priority fork - follow the fork */
do_link(st, REC_ADDR(cmd), NULL, len);
else
/* just continue for now, fork later */
do_link(st, pos + 1, NULL, len);
}
static enum rxl_found rxl_advance_bt(struct match_state *st safe, wint_t ch)
{
/* This is a back-tracking version of rxl_advance(). If the new
* ch/flag matches, we store it and return. If it doesn't, we
* backtrack and retry another path until we run out of all
* paths, or find a path where more input is needed.
*/
if (st->anchored && st->record_count == 0 && st->pos == 0)
return RXL_DONE;
if (st->len >= 0)
return RXL_DONE;
RESIZE(st->buf);
st->buf[st->buf_count++] = ch;
st->total += 1;
st->match = -1;
do {
wint_t flags;
int f = 1;;
ch = st->buf[st->buf_pos++];
flags = ch & ~0x1FFFFF;
ch &= 0x1FFFFF;
for (f = RXL_SOD ; f && f <= RXL_EOD*2; f <<= 1) {
int i = st->pos;
int r;
#ifdef DEBUG
if (!st->trace)
;
else if (f == RXL_EOD*2) {
char t[5];
printf("%d: Match %s(%x)", i,
ch < ' ' ? "?" : put_utf8(t, ch) , ch);
} else if (f & flags){
printf("%d: Flag:", i);
if (f & RXL_SOD) printf(" SOD");
if (f & RXL_EOD) printf(" EOD");
if (f & RXL_SOL) printf(" SOL");
if (f & RXL_EOL) printf(" EOL");
if (f & RXL_SOW) printf(" SOW");
if (f & RXL_EOW) printf(" EOW");
if (f & RXL_NOWBRK) printf(" NOWBRK");
if (f & RXL_POINT) printf(" POINT");
}
#endif
if (f == RXL_EOD*2 && ch)
r = advance_one(st, i, ch, 0,
st->buf_pos - 1, NULL);
else if (f == RXL_EOD*2)
r = -1;
else if (f & flags)
r = advance_one(st, i, 0, f,
st->buf_pos - 1, NULL);
else
continue;
if (r == -2) {
/* REC_BACKREF cannot be handled generically
* by advance_one. We need to find the
* referenced string and match - or ask for more.
*/
int prevpos;
int start;
int len = get_capture(st, REC_CAPNUM(st->rxl[i]), 0,
NULL, &start);
st->buf_pos -= 1;
prevpos = st->buf_pos;
while (len > 0 && st->buf_pos < st->buf_count) {
ch = st->buf[st->buf_pos++] & 0x1FFFFF;
if ((st->buf[start] & 0x1FFFFF) == ch) {
start += 1;
len -= 1;
} else {
/* Failure to match */
len = -1;
}
}
if (len > 0) {