forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmregex.c
1427 lines (1197 loc) · 39.1 KB
/
mregex.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
/*------------------------------------------------------------------
* Regular Expression Wrapper and Cache
* Written 1998, 2002, 2004 by Lars Duening.
* Share and Enjoy!
*------------------------------------------------------------------
* This module serves as wrapper around optional alternative
* regular expression implementations. It wraps the unique regexp
* structures into a ref-counted structure, and lets the API hand
* out glorified pointers to this internal structure.
*
* Beware! rx_exec() stores result data in the regexp structure, so the
* same pattern must not be used in two concurrent rx_compile/rx_exec pairs.
*
#ifdef RXCACHE_TABLE
* Additionally, the regular expressions are held in a cache.
* Usage of the cache can reduce the setup time
* for regexps by factor 4; the actual regexp matching showed up
* in experiments as being fast enough to make a cache for the
* results worthless.
*
* Compiled expressions are stored together with their generator
* strings in a hash table, hashed over the generator string content.
*
* The table sizes are specified in config.h as follows:
* RXCACHE_TABLE: size of the expression hash table
#endif
*
* TODO: Using shared strings as cache-indices can speed things up,
* TODO:: especially when knowing where to find the hashvalue from
* TODO:: the strtable.
* TODO: Use in-table chaining to improve the use of the table space
* TODO:: and reduce the number of collisions.
* TODO: Separate the results out from HS-Regexp, so that the compiled
* TODO:: program and the results can be held separate.
*------------------------------------------------------------------
*/
/*--------------------------------------------------------------------*/
#include "driver.h"
#include <stdio.h>
#include <string.h>
#include "mregex.h"
#include "comm.h" /* add_message() */
#include "gcollect.h"
#include "hash.h"
#include "interpret.h"
#include "main.h"
#include "mstrings.h"
#include "pkg-pcre.h"
#include "regexp.h"
#include "simulate.h"
#include "strfuns.h"
#include "svalue.h"
#include "xalloc.h"
#include "../mudlib/sys/driver_hook.h"
#include "../mudlib/sys/driver_info.h"
#include "../mudlib/sys/regexp.h"
/*--------------------------------------------------------------------*/
/* --- struct regdata_s: the regexp structure wrapper ---
*
* This structure wraps the actual regexp structure into a common
* format.
*
* The regular expressions are compiled as needed, with the pointers
* serving as flags:
* .pProg != NULL: PCRE version compiled
* .rx != NULL: traditional regex version compiled.
*
#ifdef RXCACHE_TABLE
* The regexp cache embeds this structure into a larger one which
* it uses to manage the cached expressions.
#endif
*/
typedef struct regdata_s {
p_uint ref; /* Number of refs */
int opt; /* Additional options, but no package flags */
/* -- PCRE -- */
#ifdef HAS_PCRE
pcre * pProg; /* The generated regular expression */
pcre_extra * pHints; /* Study data */
int num_subs; /* Number of elements in pSubs */
int * pSubs; /* Substring offsets + workarea */
int res; /* Result of last rx_exec() */
#endif // HAS_PCRE
/* -- Traditional -- */
regexp * rx; /* The actual regular expression */
} regdata_t;
#ifdef RXCACHE_TABLE
/* --- struct RxHashEntry: One expression hashtable entry ---
* Derived from regexp_i_t
*/
typedef struct RxHashEntry {
regdata_t base; /* The base regexp_t structure */
string_t * pString; /* Generator string, a counted tabled string
* NULL if unused */
hash32_t hString; /* Hash of pString */
size_t size; /* Size of regexp expressions for statistics */
} RxHashEntry;
#endif /* RXCHACHE_TABLE */
/* --- struct regexp_s: the regexp structure pimpl ---
*
* This structure is a decorated pointer to the regdata_t structure,
* allowing to store unique options and data for shared regexps.
*
* Since this structure is recreated for every rx_compile() call,
* it stores the selection flags of which regexp package to use
* for this call.
*/
struct regexp_s {
int opt; /* Additional options, incl the package flags. */
regdata_t * data; /* Counted pointer to the internal structure */
};
/*--------------------------------------------------------------------*/
/* --- Variables --- */
#ifdef RXCACHE_TABLE
static RxHashEntry * xtable[RXCACHE_TABLE]; /* The Expression Hashtable */
/* Expression cache statistics */
static statcounter_t iNumXRequests = 0; /* Number of calls to rx_compile() */
static statcounter_t iNumXFound = 0; /* Number of calls satisfied from table */
static uint32 iNumXCollisions = 0; /* Number of hashcollisions */
static uint32 iNumXEntries = 0; /* Number of used cache entries */
static uint32 iXSizeAlloc = 0; /* Dynamic memory held in regexp structs */
#endif /* RXCACHE_TABLE */
#ifdef HAS_PCRE
static size_t pcre_malloc_size;
/* Accumulated size from pcre_malloc() calls. Used when creating
* a new PCRE to capture its allocated size for statistics.
*/
static const char* pcre_malloc_err;
/* Set by the caller of PCRE functions so that an out-of-memory
* condition in the pcre_xalloc() wrapper can get the proper
* error message.
*/
#endif // HAS_PCRE
/*--------------------------------------------------------------------*/
/* Declarations */
static void rx_free_data (regdata_t * expr); /* forward */
static void rx_free_subdata (regdata_t * expr); /* forward */
/* --- Helpers --- */
static INLINE regdata_t *
ref_regdata(regdata_t *r)
/* regdata_t *ref_regdata(regdata_t *r)
* Add another ref to regdata <r> and return the regdata <r>.
*/
{
r->ref++;
return r;
}
static INLINE void
free_regdata(regdata_t *r)
/* void free_regdata(regdata_t *r)
* Subtract one ref from regdata <r>, and free the regdata fully if
* the refcount reaches zero.
*/
{
if (--(r->ref) <= 0)
rx_free_data(r);
}
#ifdef RXCACHE_TABLE
/* Hash functions, inspired by interpret.c */
#if !( (RXCACHE_TABLE) & (RXCACHE_TABLE)-1 )
#define RxStrHash(s) ((s) & ((RXCACHE_TABLE)-1))
#else
#define RxStrHash(s) ((s) % RXCACHE_TABLE)
#endif
#endif /* RXCHACHE_TABLE */
/*--------------------------------------------------------------------*/
#ifdef HAS_PCRE
static void *
pcre_xalloc (size_t size)
/* Wrapper function so that PCRE will use our special allocator. */
{
void * p;
if (!pcre_malloc_err)
pcre_malloc_err = "in PCRE function";
xallocate(p, size, pcre_malloc_err);
pcre_malloc_size += size;
return p;
} /* pcre_xalloc() */
#endif // HAS_PCRE
/*--------------------------------------------------------------------*/
const char *
rx_pcre_version (void)
/* Return a string with the name and version of the PCRE package.
*/
{
static char buf[40];
#ifdef HAS_PCRE
snprintf(buf, sizeof(buf), "%d.%d", PCRE_MAJOR, PCRE_MINOR);
#else
sprintf(buf, "not available");
#endif
return buf;
} /* rx_pcre_version() */
/*--------------------------------------------------------------------*/
void rx_init(void)
/* Initialise the module. */
{
#ifdef RXCACHE_TABLE
memset(xtable, 0, sizeof(xtable));
#endif
#ifdef HAS_PCRE
int opt = 0;
pcre_malloc = pcre_xalloc;
pcre_free = xfree;
if (pcre_config(PCRE_CONFIG_UTF8, &opt) || !opt)
fprintf(stderr, "%s PCRE package does not support UTF-8 codes.\n", time_stamp());
if (pcre_config(PCRE_CONFIG_UNICODE_PROPERTIES, &opt) || !opt)
fprintf(stderr, "%s PCRE package does not support Unicode properties.\n", time_stamp());
#endif
} /* rx_init() */
/*--------------------------------------------------------------------*/
static const char *
get_error_message (int code, int package)
/* Return a constant string with the error message for <code>
* generated by <package>.
* If <code> is not an error, NULL is returned.
*/
{
#ifdef HAS_PCRE
if (package & RE_PCRE)
{
const char* text;
if (code >= 0)
return NULL;
switch (code)
{
case PCRE_ERROR_NOMATCH:
text = "too many capturing parentheses"; break;
case PCRE_ERROR_NULL:
text = "code or subject NULL"; break;
case PCRE_ERROR_BADOPTION:
text = "unknown option specified"; break;
case PCRE_ERROR_BADMAGIC:
text = "regex memory invalid"; break;
case PCRE_ERROR_UNKNOWN_NODE:
text = "regex memory violated"; break;
case PCRE_ERROR_NOMEMORY:
text = "out of memory"; break;
case RE_ERROR_BACKTRACK:
text = "too many backtracks"; break;
default:
text = "unknown internal error"; break;
}
return text;
}
#endif // HAS_PCRE
if (package & RE_TRADITIONAL)
{
const char* text;
if (code >= 0)
return NULL;
switch (code)
{
case RE_ERROR_NULL:
text = "program or text NULL"; break;
case RE_ERROR_CORRUPT:
text = "regex memory invalid"; break;
case RE_ERROR_BACKTRACK:
text = "too many backtracks"; break;
default:
text = "unknown internal error"; break;
}
return text;
}
return NULL;
} /* get_error_message() */
/*--------------------------------------------------------------------*/
const char *
rx_error_message (int code, const regexp_t * pRegexp)
/* Return a constant string with the error message for <code>
* and regexp <pRegexp>.
* If <code> is not an error, NULL is returned.
*/
{
return get_error_message(code, pRegexp->opt);
} /* rx_error_message() */
/*--------------------------------------------------------------------*/
static Bool
rx_compile_re (string_t * expr, int opt, Bool from_ed, regdata_t * rdata)
/* Compile the expression <expr> with the options <opt> into a traditional
* expression and store it into *<rdata>.
* On success, return TRUE;
* On failure, if <from_ed> is FALSE, an error is thrown.
* On failure, if <from_ed> is TRUE, the error message is printed directly
* to the user and the function returns with FALSE.
*/
{
char * pErrmsg;
int erridx;
regexp * pRegexp = NULL;
/* Sanitize the <opt> value */
opt = opt & RE_EXCOMPATIBLE & ~(RE_PACKAGE_MASK);
/* Compile the RE */
pRegexp = hs_regcomp((unsigned char *)get_txt(expr)
, mstrsize(expr)
, opt & RE_EXCOMPATIBLE
, &pErrmsg, &erridx);
if (NULL == pRegexp)
{
rx_free_subdata(rdata); /* Might have PCRE data in it already */
if (from_ed)
add_message("re: %s at offset %d\n", pErrmsg, erridx);
else
errorf("re: %s at offset %d\n", pErrmsg, erridx);
return MY_FALSE;
}
/* Compilation complete - store the result in the outgoing regdata
* structure.
*/
rdata->rx = pRegexp;
return MY_TRUE;
} /* rx_compile_re() */
/*--------------------------------------------------------------------*/
#ifdef HAS_PCRE
static size_t
rx_compile_pcre (string_t * expr, int opt, Bool from_ed, regdata_t * rdata)
/* Compile the expression <expr> with the options <opt> into a PCRE
* expression and store it into *<rdata>.
* On success, return the accumulated size of the expression.
* On failure, if <from_ed> is FALSE, an error is thrown.
* On failure, if <from_ed> is TRUE, the error message is printed directly
* to the user and the function returns with 0.
*/
{
const char * pErrmsg;
int erridx;
pcre * pProg = NULL; /* The generated regular expression */
pcre_extra * pHints = NULL; /* Study data */
int * pSubs; /* Capturing and work area */
int pcre_opt; /* <opt> translated into PCRE opts */
int num_subs; /* Number of capturing parentheses */
/* Sanitize the <opt> value */
opt = opt & ~(RE_EXCOMPATIBLE) & ~(RE_PACKAGE_MASK);
/* Determine the RE compilation options */
pcre_opt = PCRE_UTF8 | PCRE_UCP;
if (opt & RE_CASELESS) pcre_opt |= PCRE_CASELESS;
if (opt & RE_MULTILINE) pcre_opt |= PCRE_MULTILINE;
if (opt & RE_DOTALL) pcre_opt |= PCRE_DOTALL;
if (opt & RE_EXTENDED) pcre_opt |= PCRE_EXTENDED;
if (opt & RE_DOLLAR_ENDONLY) pcre_opt |= PCRE_DOLLAR_ENDONLY;
if (opt & RE_UNGREEDY) pcre_opt |= PCRE_UNGREEDY;
/* Compile the RE */
pcre_malloc_size = 0;
pcre_malloc_err = "compiling regex";
pProg = pcre_compile(get_txt(expr), pcre_opt, &pErrmsg, &erridx, NULL);
if (NULL == pProg)
{
rx_free_subdata(rdata); /* Might have HS data in it already */
if (from_ed)
add_message("pcre: %s at offset %d\n", pErrmsg, erridx);
else
errorf("pcre: %s at offset %d\n", pErrmsg, erridx);
return 0;
}
pcre_malloc_err = "studying regex";
pHints = pcre_study(pProg, 0, &pErrmsg);
if (pErrmsg)
{
xfree(pProg);
if (from_ed)
add_message("pcre: %s\n", pErrmsg);
else
errorf("pcre: %s\n", pErrmsg);
return 0;
}
/* We have to ensure to have an initialized pHints structure for
setting the recursion limit later on */
if (pHints == NULL)
{
pcre_malloc_err = "allocating memory for pHints section in regexp";
pHints = (pcre_extra *)pcre_xalloc(sizeof(pcre_extra));
if (pHints == NULL)
{
if (from_ed)
add_message("pcre: Could not allocate memory for pHints\n");
else
errorf("pcre: Could not allocate memory for pHints\n");
return 0;
}
pHints->flags = 0;
}
{
int rc;
rc = pcre_fullinfo(pProg, pHints, PCRE_INFO_CAPTURECOUNT, &num_subs);
if (rc != 0)
{
xfree(pProg);
xfree(pHints);
if (from_ed)
add_message("pcre: %s\n", get_error_message(rc, RE_PCRE));
else
errorf("pcre: %s\n", get_error_message(rc, RE_PCRE));
return 0;
}
num_subs = 3 * (num_subs+1);
}
pSubs = xalloc(num_subs * sizeof (*pSubs));
if (pSubs == NULL)
{
xfree(pProg);
xfree(pHints);
outofmem(num_subs * sizeof (*pSubs), "regexp work area");
}
pcre_malloc_size += num_subs * sizeof (*pSubs);
/* Compilation complete - store the result in the outgoing regdata
* structure.
*/
rdata->pProg = pProg;
rdata->pHints = pHints;
rdata->pSubs = pSubs;
rdata->num_subs = num_subs;
rdata->res = 0;
return pcre_malloc_size;
} /* rx_compile_pcre() */
#endif // HAS_PCRE
/*--------------------------------------------------------------------*/
static regdata_t *
rx_compile_data (string_t * expr, int opt, Bool from_ed)
/* Compile a regdata structure from the expression <expr>, according
* to the options in <opt>. If <from_ed> is TRUE, the RE is used
* from the editor, so error messages will be printed directly
* to the user.
*
* If possible, take a ready-compiled structure from the hashtable,
* else enter the newly compiled structure into the table.
*
* The caller gets his own reference to the structure, which he has
* to free_regdata() after use.
*/
{
size_t pcre_size;
regdata_t rdata; /* Local rdata structure to hold compiled expression */
#ifdef RXCACHE_TABLE
hash32_t hExpr;
int h;
RxHashEntry *pHash;
#endif
#ifdef RXCACHE_TABLE
iNumXRequests++;
hExpr = mstr_get_hash(expr);
h = RxStrHash(hExpr);
pHash = xtable[h];
/* Look for a ready-compiled regexp */
if (pHash != NULL
&& pHash->pString != NULL
&& pHash->hString == hExpr
&& pHash->base.opt == (opt & ~(RE_PACKAGE_MASK))
&& mstreq(pHash->pString, expr)
)
{
iNumXFound++;
/* Regexp found, but it may not have been compiled for us yet.
*/
#ifdef HAS_PCRE
if ((opt & RE_PCRE) && !pHash->base.pProg)
{
pHash->size = rx_compile_pcre(expr, opt, from_ed, &(pHash->base));
if (!pHash->size)
return NULL;
}
#endif // HAS_PCRE
if ((opt & RE_TRADITIONAL) && !pHash->base.rx)
{
if (!rx_compile_re(expr, opt, from_ed, &(pHash->base)))
return NULL;
}
return ref_regdata(&(pHash->base));
}
#endif
/* Regexp not found: compile a new one.
*/
pcre_size = 0;
memset(&rdata, 0, sizeof(rdata));
#ifdef HAS_PCRE
if (opt & RE_PCRE)
{
pcre_size = rx_compile_pcre(expr, opt, from_ed, &rdata);
if (!pcre_size)
{
return NULL;
}
}
#endif // HAS_PCRE
if (opt & RE_TRADITIONAL)
{
if (!rx_compile_re(expr, opt, from_ed, &rdata))
{
return NULL;
}
}
#ifndef RXCACHE_TABLE
/* Wrap up the new regular expression and return it */
{
regdata_t * rc;
rc = xalloc(sizeof(*rc));
if (!rc)
{
rx_free_subdata(&rdata);
outofmem(sizeof(*rc), "Regexp data structure");
return NULL;
}
memcpy(rc, &rdata, sizeof(*rc));
rc->ref = 1;
rc->opt = opt & ~(RE_PACKAGE_MASK);
return rc;
}
#else
/* Wrap up the new regular expression and enter it into the table */
expr = make_tabled_from(expr); /* for faster comparisons */
if (NULL != pHash)
{
iNumXCollisions++;
iNumXEntries--;
iXSizeAlloc -= sizeof(*pHash) + pHash->size;
free_regdata(&(pHash->base));
}
pHash = xalloc(sizeof(*pHash));
if (!pHash)
{
rx_free_subdata(&rdata);
outofmem(sizeof(*pHash), "Regexp cache structure");
return NULL;
}
xtable[h] = pHash;
memcpy(&(pHash->base), &rdata, sizeof(pHash->base));
pHash->base.ref = 1;
pHash->base.opt = opt & ~(RE_PACKAGE_MASK);
pHash->pString = expr; /* refs are transferred */
pHash->hString = hExpr;
pHash->size = pcre_size;
iNumXEntries++;
iXSizeAlloc += sizeof(*pHash) + pHash->size;
return ref_regdata((regdata_t *)pHash);
#endif /* RXCACHE_TABLE */
} /* rx_compile_data() */
/*--------------------------------------------------------------------*/
regexp_t *
rx_compile (string_t * expr, int opt, Bool from_ed)
/* Compile a regexp structure from the expression <expr>, according
* to the options in <opt>. If <from_ed> is TRUE, the RE is used
* from the editor, so error messages will be printed directly
* to the user.
*
* If possible, take a ready-compiled structure from the hashtable,
* else enter the newly compiled structure into the table.
*
* The caller gets his own unique regexp structure, which he has
* to free_regexp() after use.
*/
{
regdata_t * pData;
regexp_t * pRegexp = NULL;
/* Determine for which package to compile */
if (!(opt & RE_PACKAGE_MASK))
{
if (driver_hook[H_REGEXP_PACKAGE].u.number)
opt |= driver_hook[H_REGEXP_PACKAGE].u.number;
else
opt |= regex_package;
}
if (!(opt & RE_PACKAGE_MASK))
{
if (from_ed)
add_message("Missing specification of which regexp package to use.\n");
else
errorf("Missing specification of which regexp package to use.\n");
return NULL;
}
#ifndef HAS_PCRE
if (opt & RE_PCRE)
{
if (from_ed)
add_message("Wrong specification of which regexp package to use: PCREs not available.\n");
else
errorf("Wrong specification of which regexp package to use: PCREs not available.\n");
return NULL;
}
#endif
pData = rx_compile_data(expr, opt, from_ed);
if (pData)
{
pRegexp = xalloc(sizeof(*pRegexp));
if (pRegexp == NULL)
{
free_regdata(pData);
outofmem(sizeof(*pRegexp), "regexp structure");
return NULL;
}
pRegexp->opt = opt;
pRegexp->data = pData;
}
return pRegexp;
} /* rx_compile() */
/*-------------------------------------------------------------------------*/
int
rx_exec (regexp_t *pRegexp, string_t * string, size_t start)
/* Match the regexp <pRegexp> against the <string>, starting the match
* at the position <start>.
*
* Return a positive number if pattern matched, 0 if it did not match,
* or a negative error code (this can be printed with rx_error_message()).
*/
{
regdata_t * prog = pRegexp->data;
#ifdef HAS_PCRE
if (pRegexp->opt & RE_PCRE)
{
int rc;
int pcre_opt;
pcre_extra * pHints; /* Study data */
/* Determine the RE compilation options */
pcre_opt = 0;
if (prog->opt & RE_ANCHORED) pcre_opt |= PCRE_ANCHORED;
if (prog->opt & RE_NOTBOL) pcre_opt |= PCRE_NOTBOL;
if (prog->opt & RE_NOTEOL) pcre_opt |= PCRE_NOTEOL;
if (prog->opt & RE_NOTEMPTY) pcre_opt |= PCRE_NOTEMPTY;
pHints = prog->pHints;
// If LD_PCRE_RECURSION_LIMIT is defined we set a limit for match.
// TODO: make LD_PCRE_RECURSION_LIMIT a run-time configurable setting
#if LD_PCRE_RECURSION_LIMIT > 0
pHints->flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
pHints->match_limit_recursion = LD_PCRE_RECURSION_LIMIT;
#else
pHints->flags &= ~PCRE_EXTRA_MATCH_LIMIT_RECURSION;
#endif /* LD_PCRE_RECURSION_LIMIT */
rc = pcre_exec( prog->pProg, pHints
, get_txt(string), mstrsize(string), start, pcre_opt
, prog->pSubs, prog->num_subs
);
prog->res = rc;
/* Reverse the roles of return codes 0 (not enough entries in subs[])
* and PCRE_ERROR_NOMATCH.
*/
if (rc == PCRE_ERROR_NOMATCH) rc = 0;
else if (rc == 0) rc = PCRE_ERROR_NOMATCH;
return rc;
} /* if (use pcre) */
#endif // HAS_PCRE
/* Fallback: Traditional regexp */
return hs_regexec(prog->rx, get_txt(string)+start, get_txt(string), get_txt(string) + mstrsize(string));
} /* rx_exec() */
/*-------------------------------------------------------------------------*/
int
rx_exec_str (regexp_t *pRegexp, char * string, char * start)
/* Match the regexp <pRegexp> against the <string> whose real begin
* is at <start>.
*
* Return a positive number if pattern matched, 0 if it did not match,
* or a negative error code (this can be printed with rx_error_message()).
*
* This method is used by ed().
*/
{
regdata_t * prog = pRegexp->data;
#ifdef HAS_PCRE
if (pRegexp->opt & RE_PCRE)
{
int rc;
int pcre_opt;
pcre_extra * pHints; /* Study data */
/* Determine the RE compilation options */
pcre_opt = 0;
if (prog->opt & RE_ANCHORED) pcre_opt |= PCRE_ANCHORED;
if (prog->opt & RE_NOTBOL) pcre_opt |= PCRE_NOTBOL;
if (prog->opt & RE_NOTEOL) pcre_opt |= PCRE_NOTEOL;
if (prog->opt & RE_NOTEMPTY) pcre_opt |= PCRE_NOTEMPTY;
pHints = prog->pHints;
// If LD_PCRE_RECURSION_LIMIT is defined we set a limit for match.
// TODO: make LD_PCRE_RECURSION_LIMIT a run-time configurable setting
#if LD_PCRE_RECURSION_LIMIT > 0
pHints->flags |= PCRE_EXTRA_MATCH_LIMIT_RECURSION;
pHints->match_limit_recursion = LD_PCRE_RECURSION_LIMIT;
#else
pHints->flags &= ~PCRE_EXTRA_MATCH_LIMIT_RECURSION;
#endif /* LD_PCRE_RECURSION_LIMIT */
rc = pcre_exec( prog->pProg, pHints
, start, strlen(start), string - start, pcre_opt
, prog->pSubs, prog->num_subs
);
prog->res = rc;
/* Reverse the roles of return codes 0 (not enough entries in subs[])
* and PCRE_ERROR_NOMATCH.
*/
if (rc == PCRE_ERROR_NOMATCH) rc = 0;
else if (rc == 0) rc = PCRE_ERROR_NOMATCH;
return rc;
} /* if (use pcre) */
#endif // HAS_PCRE
/* Fallback: Traditional regexp */
return hs_regexec(prog->rx, string, start, start + strlen(start));
} /* rx_exec_str() */
/*-------------------------------------------------------------------------*/
int
rx_num_matches (regexp_t *pRegexp)
/* After a successful match of <pRegexp>, return the number of matched
* expressions and parenthesized expressions.
* In other words: the result is at least 1 (for the fully matched
* expressions), plus the number of matched '()' sub expressions.
*
* The function tries to detect when it is called after an unsuccessful
* match and return 0 in that case.
*/
{
#ifdef HAS_PCRE
if (pRegexp->opt & RE_PCRE)
{
return pRegexp->data->res >= 1 ? pRegexp->data->res : 0;
} /* if (use pcre) */
#endif
/* Fallback: Traditional regexp */
{
regdata_t * prog = pRegexp->data;
p_uint num, i;
for (num = 0, i = 0
; i < sizeof(prog->rx->startp) / sizeof(prog->rx->startp[0])
; i++)
{
if (prog->rx->startp[i] != NULL
&& prog->rx->endp[i] != NULL
)
num++;
}
return num;
}
} /* rx_num_matches() */
/*-------------------------------------------------------------------------*/
Bool
rx_get_match_n (regexp_t *pRegexp, string_t * str, int n, size_t * start, size_t * end)
/* After a successful match of <pRegexp> against <str>, store the start
* and end position of the match <n> in *<start> and *<end> and retuern TRUE.
* The end position is in fact the position of the first character after
* the match.
*
* <n> is 0 for the whole expression, and any positive number for the
* matched subexpressions.
*
* If the requested match does not exist, *<start> and *<end> are both
* set to 0 and the function returns FALSE.
*/
{
Bool rc;
regdata_t * prog = pRegexp->data;
#ifdef HAS_PCRE
if (pRegexp->opt & RE_PCRE)
{
if (n < 0
|| n >= prog->res
|| prog->pSubs[2*n] < 0
|| prog->pSubs[2*n+1] < 0
)
{
*start = 0;
*end = 0;
rc = MY_FALSE;
}
else
{
*start = (size_t)prog->pSubs[2*n];
*end = (size_t)prog->pSubs[2*n+1];
rc = MY_TRUE;
}
return rc;
} /* if (use pcre) */
#endif // HAS_PCRE
/* Fallback: Traditional regexp */
if (n < 0
|| n >= (int)(sizeof(prog->rx->startp) / sizeof(prog->rx->startp[0]))
|| prog->rx->startp[n] == NULL
|| prog->rx->endp[n] == NULL
)
{
*start = 0;
*end = 0;
rc = MY_FALSE;
}
else
{
*start = prog->rx->startp[n] - get_txt(str);
*end = prog->rx->endp[n] - get_txt(str);
rc = MY_TRUE;
}
return rc;
} /* rx_get_match_n() */
/*-------------------------------------------------------------------------*/
void
rx_get_match (regexp_t *pRegexp, string_t * str, size_t * start, size_t * end)
/* After a successful match of <pRegexp> against <str>, return the start
* and end position of the match in *<start> and *<end>. The end
* position is in fact the position of the first character after the match.
*/
{
regdata_t * prog = pRegexp->data;
#ifdef HAS_PCRE
if (pRegexp->opt & RE_PCRE)
{
*start = (size_t)prog->pSubs[0];
*end = (size_t)prog->pSubs[1];
return;
} /* if (use pcre) */
#endif // HAS_PCRE
/* Fallback: Traditional regexp */
*start = prog->rx->startp[0] - get_txt(str);
*end = prog->rx->endp[0] - get_txt(str);
} /* rx_get_match() */
/*-------------------------------------------------------------------------*/
void
rx_get_match_str (regexp_t *pRegexp, char * str, size_t * start, size_t * end)
/* After a successful match of <pRegexp> against <str>, return the start
* and end position of the match in *<start> and *<end>. The end
* position is in fact the position of the first character after the match.
*/
{
regdata_t * prog = pRegexp->data;
#ifdef HAS_PCRE
if (pRegexp->opt & RE_PCRE)
{
*start = (size_t)prog->pSubs[0];
*end = (size_t)prog->pSubs[1];
return;
} /* if (use pcre) */
#endif // HAS_PCRE
/* Fallback: Traditional regexp */
*start = prog->rx->startp[0] - str;
*end = prog->rx->endp[0] - str;
} /* rx_get_match_str() */
/*-------------------------------------------------------------------------*/
string_t *
rx_sub (regexp_t *pRegexp, string_t *source, string_t *subst)
/* <pRegexp> describes a regexp match in string <source>. Take the
* replacement string <subst> and substitute any matched subparentheses.
* The result is a new string with one reference.
*
* Returns NULL when out of memory.
*/
{
Bool copyPass; /* Pass indicator */
size_t len; /* Computed length of the result */
string_t * result; /* Result string */
regdata_t * prog = pRegexp->data;
result = NULL;
/* Make two passes over the the string: one to compute the size
* of the result, the second to create the result.