This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More safety in free()s
[perl5.git] / regexp.h
CommitLineData
a0d0e21e 1/* regexp.h
d6376244 2 *
4bb101f2
JH
3 * Copyright (C) 1993, 1994, 1996, 1997, 1999, 2000, 2001, 2003,
4 * by Larry Wall and others
d6376244
JH
5 *
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8 *
a0d0e21e
LW
9 */
10
378cc40b
LW
11/*
12 * Definitions etc. for regexp(3) routines.
13 *
14 * Caveat: this is V8 regexp(3) [actually, a reimplementation thereof],
15 * not the System V one.
16 */
be8e71aa
YO
17#ifndef PLUGGABLE_RE_EXTENSION
18/* we don't want to include this stuff if we are inside Nicholas'
19 * pluggable regex engine code */
378cc40b 20
c277df42
IZ
21struct regnode {
22 U8 flags;
23 U8 type;
24 U16 next_off;
25};
26
27typedef struct regnode regnode;
28
cad2e5aa 29struct reg_substr_data;
2779dcf1 30
0ee3c8d0
JH
31struct reg_data;
32
f9f4320a 33struct regexp_engine;
c74340f9
YO
34typedef struct regexp_paren_ofs {
35 I32 *startp;
36 I32 *endp;
37} regexp_paren_ofs;
f9f4320a 38
378cc40b 39typedef struct regexp {
c74340f9 40 I32 *startp;
cf93c79d 41 I32 *endp;
c74340f9 42 regexp_paren_ofs *swap;
c277df42 43 regnode *regstclass;
2779dcf1 44 struct reg_substr_data *substrs;
cf93c79d 45 char *precomp; /* pre-compilation regular expression */
c277df42 46 struct reg_data *data; /* Additional data. */
cf93c79d
IZ
47 char *subbeg; /* saved or original string
48 so \digit works forever. */
f8c7b90f 49#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
50 SV *saved_copy; /* If non-NULL, SV which is COW from original */
51#endif
fac92740 52 U32 *offsets; /* offset annotations 20001228 MJD */
cf93c79d
IZ
53 I32 sublen; /* Length of string pointed by subbeg */
54 I32 refcnt;
de8c5301
YO
55 I32 minlen; /* mininum possible length of string to match */
56 I32 minlenret; /* mininum possible length of $& */
58e23c8d 57 U32 gofs; /* chars left of pos that we search from */
cf93c79d
IZ
58 I32 prelen; /* length of precomp */
59 U32 nparens; /* number of parentheses */
60 U32 lastparen; /* last paren matched */
a01268b5 61 U32 lastcloseparen; /* last paren matched */
cf93c79d
IZ
62 U32 reganch; /* Internal use only +
63 Tainted information used by regexec? */
81714fb9
YO
64 HV *paren_names; /* Paren names */
65 const struct regexp_engine* engine;
c277df42 66 regnode program[1]; /* Unwarranted chumminess with compiler. */
378cc40b
LW
67} regexp;
68
f9f4320a
YO
69
70typedef struct re_scream_pos_data_s
71{
72 char **scream_olds; /* match pos */
73 I32 *scream_pos; /* Internal iterator of scream. */
74} re_scream_pos_data;
75
76typedef struct regexp_engine {
f2f78491
YO
77 regexp* (*comp) (pTHX_ char* exp, char* xend, PMOP* pm);
78 I32 (*exec) (pTHX_ regexp* prog, char* stringarg, char* strend,
f9f4320a
YO
79 char* strbeg, I32 minend, SV* screamer,
80 void* data, U32 flags);
f2f78491
YO
81 char* (*intuit) (pTHX_ regexp *prog, SV *sv, char *strpos,
82 char *strend, U32 flags,
83 struct re_scream_pos_data_s *data);
84 SV* (*checkstr) (pTHX_ regexp *prog);
85 void (*free) (pTHX_ struct regexp* r);
de8c5301 86 char* (*as_str) (pTHX_ MAGIC *mg, STRLEN *lp, U32 *flags, I32 *haseval);
f2f78491
YO
87#ifdef USE_ITHREADS
88 regexp* (*dupe) (pTHX_ const regexp *r, CLONE_PARAMS *param);
f9f4320a
YO
89#endif
90} regexp_engine;
91
cad2e5aa
JH
92#define ROPT_ANCH (ROPT_ANCH_BOL|ROPT_ANCH_MBOL|ROPT_ANCH_GPOS|ROPT_ANCH_SBOL)
93#define ROPT_ANCH_SINGLE (ROPT_ANCH_SBOL|ROPT_ANCH_GPOS)
f2278c82
YO
94#define ROPT_ANCH_BOL 0x00000001
95#define ROPT_ANCH_MBOL 0x00000002
96#define ROPT_ANCH_SBOL 0x00000004
97#define ROPT_ANCH_GPOS 0x00000008
98#define ROPT_SKIP 0x00000010
99#define ROPT_IMPLICIT 0x00000020 /* Converted .* to ^.* */
100#define ROPT_NOSCAN 0x00000040 /* Check-string always at start. */
101#define ROPT_GPOS_SEEN 0x00000080
102#define ROPT_CHECK_ALL 0x00000100
103#define ROPT_LOOKBEHIND_SEEN 0x00000200
104#define ROPT_EVAL_SEEN 0x00000400
105#define ROPT_CANY_SEEN 0x00000800
cce850e4 106#define ROPT_SANY_SEEN ROPT_CANY_SEEN /* src bckwrd cmpt */
f9f4320a 107#define ROPT_GPOS_CHECK (ROPT_GPOS_SEEN|ROPT_ANCH_GPOS)
a0ed51b3 108
5d458dd8 109/* 0xF800 of reganch is used by PMf_COMPILETIME */
c277df42 110
f2278c82
YO
111#define ROPT_UTF8 0x00010000
112#define ROPT_NAUGHTY 0x00020000 /* how exponential is this pattern? */
113#define ROPT_COPY_DONE 0x00040000 /* subbeg is a copy of the string */
114#define ROPT_TAINTED_SEEN 0x00080000
a30b2f1f 115#define ROPT_MATCH_UTF8 0x10000000 /* subbeg is utf-8 */
5d458dd8
YO
116#define ROPT_VERBARG_SEEN 0x20000000
117#define ROPT_CUTGROUP_SEEN 0x40000000
58e23c8d 118#define ROPT_GPOS_FLOAT 0x80000000
a0ed51b3 119
f2278c82
YO
120#define RE_USE_INTUIT_NOML 0x00100000 /* Best to intuit before matching */
121#define RE_USE_INTUIT_ML 0x00200000
122#define REINT_AUTORITATIVE_NOML 0x00400000 /* Can trust a positive answer */
be8e71aa 123#define REINT_AUTORITATIVE_ML 0x00800000
f2278c82
YO
124#define REINT_ONCE_NOML 0x01000000 /* Intuit can succed once only. */
125#define REINT_ONCE_ML 0x02000000
126#define RE_INTUIT_ONECHAR 0x04000000
127#define RE_INTUIT_TAIL 0x08000000
128
cad2e5aa
JH
129
130#define RE_USE_INTUIT (RE_USE_INTUIT_NOML|RE_USE_INTUIT_ML)
131#define REINT_AUTORITATIVE (REINT_AUTORITATIVE_NOML|REINT_AUTORITATIVE_ML)
132#define REINT_ONCE (REINT_ONCE_NOML|REINT_ONCE_ML)
133
5d458dd8 134#define RX_HAS_CUTGROUP(prog) ((prog)->reganch & ROPT_CUTGROUP_SEEN)
c277df42 135#define RX_MATCH_TAINTED(prog) ((prog)->reganch & ROPT_TAINTED_SEEN)
72311751
GS
136#define RX_MATCH_TAINTED_on(prog) ((prog)->reganch |= ROPT_TAINTED_SEEN)
137#define RX_MATCH_TAINTED_off(prog) ((prog)->reganch &= ~ROPT_TAINTED_SEEN)
138#define RX_MATCH_TAINTED_set(prog, t) ((t) \
139 ? RX_MATCH_TAINTED_on(prog) \
140 : RX_MATCH_TAINTED_off(prog))
c277df42 141
cf93c79d
IZ
142#define RX_MATCH_COPIED(prog) ((prog)->reganch & ROPT_COPY_DONE)
143#define RX_MATCH_COPIED_on(prog) ((prog)->reganch |= ROPT_COPY_DONE)
144#define RX_MATCH_COPIED_off(prog) ((prog)->reganch &= ~ROPT_COPY_DONE)
145#define RX_MATCH_COPIED_set(prog,t) ((t) \
146 ? RX_MATCH_COPIED_on(prog) \
147 : RX_MATCH_COPIED_off(prog))
f9f4320a 148
be8e71aa
YO
149#endif /* PLUGGABLE_RE_EXTENSION */
150
151/* Stuff that needs to be included in the plugable extension goes below here */
152
f8c7b90f 153#ifdef PERL_OLD_COPY_ON_WRITE
ed252734
NC
154#define RX_MATCH_COPY_FREE(rx) \
155 STMT_START {if (rx->saved_copy) { \
156 SV_CHECK_THINKFIRST_COW_DROP(rx->saved_copy); \
157 } \
158 if (RX_MATCH_COPIED(rx)) { \
159 Safefree(rx->subbeg); \
160 RX_MATCH_COPIED_off(rx); \
161 }} STMT_END
162#else
163#define RX_MATCH_COPY_FREE(rx) \
164 STMT_START {if (RX_MATCH_COPIED(rx)) { \
165 Safefree(rx->subbeg); \
166 RX_MATCH_COPIED_off(rx); \
167 }} STMT_END
168#endif
169
a30b2f1f
RGS
170#define RX_MATCH_UTF8(prog) ((prog)->reganch & ROPT_MATCH_UTF8)
171#define RX_MATCH_UTF8_on(prog) ((prog)->reganch |= ROPT_MATCH_UTF8)
172#define RX_MATCH_UTF8_off(prog) ((prog)->reganch &= ~ROPT_MATCH_UTF8)
173#define RX_MATCH_UTF8_set(prog, t) ((t) \
174 ? (RX_MATCH_UTF8_on(prog), (PL_reg_match_utf8 = 1)) \
175 : (RX_MATCH_UTF8_off(prog), (PL_reg_match_utf8 = 0)))
176
cad2e5aa
JH
177#define REXEC_COPY_STR 0x01 /* Need to copy the string. */
178#define REXEC_CHECKED 0x02 /* check_substr already checked. */
179#define REXEC_SCREAM 0x04 /* use scream table. */
180#define REXEC_IGNOREPOS 0x08 /* \G matches at start. */
cf93c79d 181#define REXEC_NOT_FIRST 0x10 /* This is another iteration of //g. */
c277df42 182
155aba94 183#define ReREFCNT_inc(re) ((void)(re && re->refcnt++), re)
f9f4320a 184#define ReREFCNT_dec(re) CALLREGFREE(re)
cf93c79d
IZ
185
186#define FBMcf_TAIL_DOLLAR 1
cad2e5aa
JH
187#define FBMcf_TAIL_DOLLARM 2
188#define FBMcf_TAIL_Z 4
189#define FBMcf_TAIL_z 8
190#define FBMcf_TAIL (FBMcf_TAIL_DOLLAR|FBMcf_TAIL_DOLLARM|FBMcf_TAIL_Z|FBMcf_TAIL_z)
cf93c79d
IZ
191
192#define FBMrf_MULTILINE 1
cad2e5aa 193
5d9a96ca
DM
194/* an accepting state/position*/
195struct _reg_trie_accepted {
196 U8 *endpos;
197 U16 wordnum;
198};
199typedef struct _reg_trie_accepted reg_trie_accepted;
200
3b0527fe
DM
201/* some basic information about the current match that is created by
202 * Perl_regexec_flags and then passed to regtry(), regmatch() etc */
203
204typedef struct {
205 regexp *prog;
206 char *bol;
207 char *till;
208 SV *sv;
209 char *ganch;
24b23f37 210 char *cutpoint;
3b0527fe
DM
211} regmatch_info;
212
5d9a96ca
DM
213
214/* structures for holding and saving the state maintained by regmatch() */
215
6bda09f9
YO
216#define MAX_RECURSE_EVAL_NOCHANGE_DEPTH 50
217
5d9a96ca
DM
218typedef I32 CHECKPOINT;
219
a0374537 220typedef struct regmatch_state {
40a82448 221 int resume_state; /* where to jump to on return */
24d3c4a9 222 char *locinput; /* where to backtrack in string on failure */
e822a8b4
DM
223
224 union {
77cb431f
DM
225
226 /* this is a fake union member that matches the first element
227 * of each member that needs to store positive backtrack
228 * information */
229 struct {
230 struct regmatch_state *prev_yes_state;
231 } yes;
232
e822a8b4 233 struct {
5d458dd8
YO
234 /* this first element must match u.yes */
235 struct regmatch_state *prev_yes_state;
e822a8b4 236 reg_trie_accepted *accept_buff;
166ba7cd 237 U32 accepted; /* how many accepting states we have seen */
7f69552c 238 U16 *jump; /* positive offsets from me */
166ba7cd 239 regnode *B; /* node following the trie */
7f69552c 240 regnode *me; /* Which node am I - needed for jump tries*/
e822a8b4
DM
241 } trie;
242
243 struct {
77cb431f
DM
244 /* this first element must match u.yes */
245 struct regmatch_state *prev_yes_state;
faec1544
DM
246 struct regmatch_state *prev_eval;
247 struct regmatch_state *prev_curlyx;
aa283a38 248 regexp *prev_rex;
faec1544
DM
249 U32 toggle_reg_flags; /* what bits in PL_reg_flags to
250 flip when transitioning between
251 inner and outer rexen */
aa283a38
DM
252 CHECKPOINT cp; /* remember current savestack indexes */
253 CHECKPOINT lastcp;
40a82448 254 regnode *B; /* the node following us */
6bda09f9 255 U32 close_paren; /* which close bracket is our end */
e822a8b4
DM
256 } eval;
257
258 struct {
c476f425 259 /* this first element must match u.yes */
262b90c4 260 struct regmatch_state *prev_yes_state;
c476f425
DM
261 struct regmatch_state *prev_curlyx; /* previous cur_curlyx */
262 CHECKPOINT cp; /* remember current savestack index */
263 bool minmod;
a0374537 264 int parenfloor;/* how far back to strip paren data */
c476f425
DM
265 int min; /* the minimal number of A's to match */
266 int max; /* the maximal number of A's to match */
267 regnode *A, *B; /* the nodes corresponding to /A*B/ */
268
269 /* these two are modified by WHILEM */
270 int count; /* how many instances of A we've matched */
271 char *lastloc;/* where previous A matched (0-len detect) */
e822a8b4
DM
272 } curlyx;
273
274 struct {
c476f425 275 /* this first element must match u.yes */
262b90c4 276 struct regmatch_state *prev_yes_state;
c476f425
DM
277 struct regmatch_state *save_curlyx;
278 CHECKPOINT cp; /* remember current savestack indexes */
279 CHECKPOINT lastcp;
280 char *save_lastloc; /* previous curlyx.lastloc */
281 I32 cache_offset;
282 I32 cache_mask;
e822a8b4
DM
283 } whilem;
284
285 struct {
5d458dd8
YO
286 /* this first element must match u.yes */
287 struct regmatch_state *prev_yes_state;
3b6647e0 288 U32 lastparen;
40a82448
DM
289 regnode *next_branch; /* next branch node */
290 CHECKPOINT cp;
291 } branch;
292
293 struct {
dad79028
DM
294 /* this first element must match u.yes */
295 struct regmatch_state *prev_yes_state;
e822a8b4 296 I32 c1, c2; /* case fold search */
40a82448
DM
297 CHECKPOINT cp;
298 I32 alen; /* length of first-matched A string */
299 I32 count;
0cadcf80 300 bool minmod;
40a82448
DM
301 regnode *A, *B; /* the nodes corresponding to /A*B/ */
302 regnode *me; /* the curlym node */
e822a8b4
DM
303 } curlym;
304
305 struct {
3b6647e0 306 U32 paren;
c255a977 307 CHECKPOINT cp;
e822a8b4 308 I32 c1, c2; /* case fold search */
c255a977
DM
309 char *maxpos; /* highest possible point in string to match */
310 char *oldloc; /* the previous locinput */
e822a8b4 311 int count;
c255a977
DM
312 int min, max; /* {m,n} */
313 regnode *A, *B; /* the nodes corresponding to /A*B/ */
314 } curly; /* and CURLYN/PLUS/STAR */
dad79028
DM
315
316 struct {
317 /* this first element must match u.yes */
318 struct regmatch_state *prev_yes_state;
319 I32 wanted;
24d3c4a9 320 I32 logical; /* saved copy of 'logical' var */
40a82448 321 regnode *me; /* the IFMATCH/SUSPEND/UNLESSM node */
dad79028 322 } ifmatch; /* and SUSPEND/UNLESSM */
e2e6a0f1
YO
323
324 struct {
325 /* this first element must match u.yes */
326 struct regmatch_state *prev_yes_state;
327 struct regmatch_state *prev_mark;
328 SV* mark_name;
329 char *mark_loc;
330 } mark;
d8319b27 331 } u;
5d9a96ca
DM
332} regmatch_state;
333
334/* how many regmatch_state structs to allocate as a single slab.
335 * We do it in 4K blocks for efficiency. The "3" is 2 for the next/prev
336 * pointers, plus 1 for any mythical malloc overhead. */
337
338#define PERL_REGMATCH_SLAB_SLOTS \
339 ((4096 - 3 * sizeof (void*)) / sizeof(regmatch_state))
340
341typedef struct regmatch_slab {
342 regmatch_state states[PERL_REGMATCH_SLAB_SLOTS];
343 struct regmatch_slab *prev, *next;
344} regmatch_slab;
1ade1aa1 345
46ab3289
NC
346#define PL_reg_flags PL_reg_state.re_state_reg_flags
347#define PL_bostr PL_reg_state.re_state_bostr
348#define PL_reginput PL_reg_state.re_state_reginput
46ab3289
NC
349#define PL_regeol PL_reg_state.re_state_regeol
350#define PL_regstartp PL_reg_state.re_state_regstartp
351#define PL_regendp PL_reg_state.re_state_regendp
352#define PL_reglastparen PL_reg_state.re_state_reglastparen
353#define PL_reglastcloseparen PL_reg_state.re_state_reglastcloseparen
46ab3289
NC
354#define PL_reg_start_tmp PL_reg_state.re_state_reg_start_tmp
355#define PL_reg_start_tmpl PL_reg_state.re_state_reg_start_tmpl
356#define PL_reg_eval_set PL_reg_state.re_state_reg_eval_set
46ab3289
NC
357#define PL_reg_match_utf8 PL_reg_state.re_state_reg_match_utf8
358#define PL_reg_magic PL_reg_state.re_state_reg_magic
359#define PL_reg_oldpos PL_reg_state.re_state_reg_oldpos
360#define PL_reg_oldcurpm PL_reg_state.re_state_reg_oldcurpm
361#define PL_reg_curpm PL_reg_state.re_state_reg_curpm
362#define PL_reg_oldsaved PL_reg_state.re_state_reg_oldsaved
363#define PL_reg_oldsavedlen PL_reg_state.re_state_reg_oldsavedlen
364#define PL_reg_maxiter PL_reg_state.re_state_reg_maxiter
365#define PL_reg_leftiter PL_reg_state.re_state_reg_leftiter
366#define PL_reg_poscache PL_reg_state.re_state_reg_poscache
367#define PL_reg_poscache_size PL_reg_state.re_state_reg_poscache_size
368#define PL_regsize PL_reg_state.re_state_regsize
369#define PL_reg_starttry PL_reg_state.re_state_reg_starttry
370#define PL_nrs PL_reg_state.re_state_nrs
371
1ade1aa1
NC
372struct re_save_state {
373 U32 re_state_reg_flags; /* from regexec.c */
374 char *re_state_bostr;
375 char *re_state_reginput; /* String-input pointer. */
1ade1aa1
NC
376 char *re_state_regeol; /* End of input, for $ check. */
377 I32 *re_state_regstartp; /* Pointer to startp array. */
378 I32 *re_state_regendp; /* Ditto for endp. */
379 U32 *re_state_reglastparen; /* Similarly for lastparen. */
380 U32 *re_state_reglastcloseparen; /* Similarly for lastcloseparen. */
1ade1aa1
NC
381 char **re_state_reg_start_tmp; /* from regexec.c */
382 U32 re_state_reg_start_tmpl; /* from regexec.c */
383 I32 re_state_reg_eval_set; /* from regexec.c */
1ade1aa1
NC
384 bool re_state_reg_match_utf8; /* from regexec.c */
385 MAGIC *re_state_reg_magic; /* from regexec.c */
386 I32 re_state_reg_oldpos; /* from regexec.c */
387 PMOP *re_state_reg_oldcurpm; /* from regexec.c */
388 PMOP *re_state_reg_curpm; /* from regexec.c */
389 char *re_state_reg_oldsaved; /* old saved substr during match */
390 STRLEN re_state_reg_oldsavedlen; /* old length of saved substr during match */
391 I32 re_state_reg_maxiter; /* max wait until caching pos */
392 I32 re_state_reg_leftiter; /* wait until caching pos */
393 char *re_state_reg_poscache; /* cache of pos of WHILEM */
394 STRLEN re_state_reg_poscache_size; /* size of pos cache of WHILEM */
3b6647e0 395 U32 re_state_regsize; /* from regexec.c */
1ade1aa1
NC
396 char *re_state_reg_starttry; /* from regexec.c */
397#ifdef PERL_OLD_COPY_ON_WRITE
46ab3289 398 SV *re_state_nrs; /* was placeholder: unused since 5.8.0 (5.7.2 patch #12027 for bug ID 20010815.012). Used to save rx->saved_copy */
1ade1aa1
NC
399#endif
400};
401
402#define SAVESTACK_ALLOC_FOR_RE_SAVE_STATE \
403 (1 + ((sizeof(struct re_save_state) - 1) / sizeof(*PL_savestack)))
404/*
405 * Local variables:
406 * c-indentation-style: bsd
407 * c-basic-offset: 4
408 * indent-tabs-mode: t
409 * End:
410 *
411 * ex: set ts=8 sts=4 sw=4 noet:
412 */