new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / external / pcre2-10.32 / doc / pcre2callout.3
1 .TH PCRE2CALLOUT 3 "26 April 2018" "PCRE2 10.32"
2 .SH NAME
3 PCRE2 - Perl-compatible regular expressions (revised API)
4 .SH SYNOPSIS
5 .rs
6 .sp
7 .B #include <pcre2.h>
8 .PP
9 .SM
10 .nf
11 .B int (*pcre2_callout)(pcre2_callout_block *, void *);
12 .sp
13 .B int pcre2_callout_enumerate(const pcre2_code *\fIcode\fP,
14 .B "  int (*\fIcallback\fP)(pcre2_callout_enumerate_block *, void *),"
15 .B "  void *\fIuser_data\fP);"
16 .fi
17 .
18 .SH DESCRIPTION
19 .rs
20 .sp
21 PCRE2 provides a feature called "callout", which is a means of temporarily
22 passing control to the caller of PCRE2 in the middle of pattern matching. The
23 caller of PCRE2 provides an external function by putting its entry point in
24 a match context (see \fBpcre2_set_callout()\fP in the
25 .\" HREF
26 \fBpcre2api\fP
27 .\"
28 documentation).
29 .P
30 Within a regular expression, (?C<arg>) indicates a point at which the external
31 function is to be called. Different callout points can be identified by putting
32 a number less than 256 after the letter C. The default value is zero.
33 Alternatively, the argument may be a delimited string. The starting delimiter
34 must be one of ` ' " ^ % # $ { and the ending delimiter is the same as the
35 start, except for {, where the ending delimiter is }. If the ending delimiter
36 is needed within the string, it must be doubled. For example, this pattern has
37 two callout points:
38 .sp
39   (?C1)abc(?C"some ""arbitrary"" text")def
40 .sp
41 If the PCRE2_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE2
42 automatically inserts callouts, all with number 255, before each item in the
43 pattern except for immediately before or after an explicit callout. For
44 example, if PCRE2_AUTO_CALLOUT is used with the pattern
45 .sp
46   A(?C3)B
47 .sp
48 it is processed as if it were
49 .sp
50   (?C255)A(?C3)B(?C255)
51 .sp
52 Here is a more complicated example:
53 .sp
54   A(\ed{2}|--)
55 .sp
56 With PCRE2_AUTO_CALLOUT, this pattern is processed as if it were
57 .sp
58   (?C255)A(?C255)((?C255)\ed{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
59 .sp
60 Notice that there is a callout before and after each parenthesis and
61 alternation bar. If the pattern contains a conditional group whose condition is
62 an assertion, an automatic callout is inserted immediately before the
63 condition. Such a callout may also be inserted explicitly, for example:
64 .sp
65   (?(?C9)(?=a)ab|de)  (?(?C%text%)(?!=d)ab|de)
66 .sp
67 This applies only to assertion conditions (because they are themselves
68 independent groups).
69 .P
70 Callouts can be useful for tracking the progress of pattern matching. The
71 .\" HREF
72 \fBpcre2test\fP
73 .\"
74 program has a pattern qualifier (/auto_callout) that sets automatic callouts.
75 When any callouts are present, the output from \fBpcre2test\fP indicates how
76 the pattern is being matched. This is useful information when you are trying to
77 optimize the performance of a particular pattern.
78 .
79 .
80 .SH "MISSING CALLOUTS"
81 .rs
82 .sp
83 You should be aware that, because of optimizations in the way PCRE2 compiles
84 and matches patterns, callouts sometimes do not happen exactly as you might
85 expect.
86 .
87 .
88 .SS "Auto-possessification"
89 .rs
90 .sp
91 At compile time, PCRE2 "auto-possessifies" repeated items when it knows that
92 what follows cannot be part of the repeat. For example, a+[bc] is compiled as
93 if it were a++[bc]. The \fBpcre2test\fP output when this pattern is compiled
94 with PCRE2_ANCHORED and PCRE2_AUTO_CALLOUT and then applied to the string
95 "aaaa" is:
96 .sp
97   --->aaaa
98    +0 ^        a+
99    +2 ^   ^    [bc]
100   No match
101 .sp
102 This indicates that when matching [bc] fails, there is no backtracking into a+
103 (because it is being treated as a++) and therefore the callouts that would be
104 taken for the backtracks do not occur. You can disable the auto-possessify
105 feature by passing PCRE2_NO_AUTO_POSSESS to \fBpcre2_compile()\fP, or starting
106 the pattern with (*NO_AUTO_POSSESS). In this case, the output changes to this:
107 .sp
108   --->aaaa
109    +0 ^        a+
110    +2 ^   ^    [bc]
111    +2 ^  ^     [bc]
112    +2 ^ ^      [bc]
113    +2 ^^       [bc]
114   No match
115 .sp
116 This time, when matching [bc] fails, the matcher backtracks into a+ and tries
117 again, repeatedly, until a+ itself fails.
118 .
119 .
120 .SS "Automatic .* anchoring"
121 .rs
122 .sp
123 By default, an optimization is applied when .* is the first significant item in
124 a pattern. If PCRE2_DOTALL is set, so that the dot can match any character, the
125 pattern is automatically anchored. If PCRE2_DOTALL is not set, a match can
126 start only after an internal newline or at the beginning of the subject, and
127 \fBpcre2_compile()\fP remembers this. If a pattern has more than one top-level
128 branch, automatic anchoring occurs if all branches are anchorable.
129 .P
130 This optimization is disabled, however, if .* is in an atomic group or if there
131 is a backreference to the capturing group in which it appears. It is also
132 disabled if the pattern contains (*PRUNE) or (*SKIP). However, the presence of
133 callouts does not affect it.
134 .P
135 For example, if the pattern .*\ed is compiled with PCRE2_AUTO_CALLOUT and
136 applied to the string "aa", the \fBpcre2test\fP output is:
137 .sp
138   --->aa
139    +0 ^      .*
140    +2 ^ ^    \ed
141    +2 ^^     \ed
142    +2 ^      \ed
143   No match
144 .sp
145 This shows that all match attempts start at the beginning of the subject. In
146 other words, the pattern is anchored. You can disable this optimization by
147 passing PCRE2_NO_DOTSTAR_ANCHOR to \fBpcre2_compile()\fP, or starting the
148 pattern with (*NO_DOTSTAR_ANCHOR). In this case, the output changes to:
149 .sp
150   --->aa
151    +0 ^      .*
152    +2 ^ ^    \ed
153    +2 ^^     \ed
154    +2 ^      \ed
155    +0  ^     .*
156    +2  ^^    \ed
157    +2  ^     \ed
158   No match
159 .sp
160 This shows more match attempts, starting at the second subject character.
161 Another optimization, described in the next section, means that there is no
162 subsequent attempt to match with an empty subject.
163 .
164 .
165 .SS "Other optimizations"
166 .rs
167 .sp
168 Other optimizations that provide fast "no match" results also affect callouts.
169 For example, if the pattern is
170 .sp
171   ab(?C4)cd
172 .sp
173 PCRE2 knows that any matching string must contain the letter "d". If the
174 subject string is "abyz", the lack of "d" means that matching doesn't ever
175 start, and the callout is never reached. However, with "abyd", though the
176 result is still no match, the callout is obeyed.
177 .P
178 For most patterns PCRE2 also knows the minimum length of a matching string, and
179 will immediately give a "no match" return without actually running a match if
180 the subject is not long enough, or, for unanchored patterns, if it has been
181 scanned far enough.
182 .P
183 You can disable these optimizations by passing the PCRE2_NO_START_OPTIMIZE
184 option to \fBpcre2_compile()\fP, or by starting the pattern with
185 (*NO_START_OPT). This slows down the matching process, but does ensure that
186 callouts such as the example above are obeyed.
187 .
188 .
189 .\" HTML <a name="calloutinterface"></a>
190 .SH "THE CALLOUT INTERFACE"
191 .rs
192 .sp
193 During matching, when PCRE2 reaches a callout point, if an external function is
194 provided in the match context, it is called. This applies to both normal,
195 DFA, and JIT matching. The first argument to the callout function is a pointer
196 to a \fBpcre2_callout\fP block. The second argument is the void * callout data
197 that was supplied when the callout was set up by calling
198 \fBpcre2_set_callout()\fP (see the
199 .\" HREF
200 \fBpcre2api\fP
201 .\"
202 documentation). The callout block structure contains the following fields, not
203 necessarily in this order:
204 .sp
205   uint32_t      \fIversion\fP;
206   uint32_t      \fIcallout_number\fP;
207   uint32_t      \fIcapture_top\fP;
208   uint32_t      \fIcapture_last\fP;
209   uint32_t      \fIcallout_flags\fP;
210   PCRE2_SIZE   *\fIoffset_vector\fP;
211   PCRE2_SPTR    \fImark\fP;
212   PCRE2_SPTR    \fIsubject\fP;
213   PCRE2_SIZE    \fIsubject_length\fP;
214   PCRE2_SIZE    \fIstart_match\fP;
215   PCRE2_SIZE    \fIcurrent_position\fP;
216   PCRE2_SIZE    \fIpattern_position\fP;
217   PCRE2_SIZE    \fInext_item_length\fP;
218   PCRE2_SIZE    \fIcallout_string_offset\fP;
219   PCRE2_SIZE    \fIcallout_string_length\fP;
220   PCRE2_SPTR    \fIcallout_string\fP;
221 .sp
222 The \fIversion\fP field contains the version number of the block format. The
223 current version is 2; the three callout string fields were added for version 1,
224 and the \fIcallout_flags\fP field for version 2. If you are writing an
225 application that might use an earlier release of PCRE2, you should check the
226 version number before accessing any of these fields. The version number will
227 increase in future if more fields are added, but the intention is never to
228 remove any of the existing fields.
229 .
230 .
231 .SS "Fields for numerical callouts"
232 .rs
233 .sp
234 For a numerical callout, \fIcallout_string\fP is NULL, and \fIcallout_number\fP
235 contains the number of the callout, in the range 0-255. This is the number
236 that follows (?C for callouts that part of the pattern; it is 255 for
237 automatically generated callouts.
238 .
239 .
240 .SS "Fields for string callouts"
241 .rs
242 .sp
243 For callouts with string arguments, \fIcallout_number\fP is always zero, and
244 \fIcallout_string\fP points to the string that is contained within the compiled
245 pattern. Its length is given by \fIcallout_string_length\fP. Duplicated ending
246 delimiters that were present in the original pattern string have been turned
247 into single characters, but there is no other processing of the callout string
248 argument. An additional code unit containing binary zero is present after the
249 string, but is not included in the length. The delimiter that was used to start
250 the string is also stored within the pattern, immediately before the string
251 itself. You can access this delimiter as \fIcallout_string\fP[-1] if you need
252 it.
253 .P
254 The \fIcallout_string_offset\fP field is the code unit offset to the start of
255 the callout argument string within the original pattern string. This is
256 provided for the benefit of applications such as script languages that might
257 need to report errors in the callout string within the pattern.
258 .
259 .
260 .SS "Fields for all callouts"
261 .rs
262 .sp
263 The remaining fields in the callout block are the same for both kinds of
264 callout.
265 .P
266 The \fIoffset_vector\fP field is a pointer to a vector of capturing offsets
267 (the "ovector"). You may read the elements in this vector, but you must not
268 change any of them.
269 .P
270 For calls to \fBpcre2_match()\fP, the \fIoffset_vector\fP field is not (since
271 release 10.30) a pointer to the actual ovector that was passed to the matching
272 function in the match data block. Instead it points to an internal ovector of a
273 size large enough to hold all possible captured substrings in the pattern. Note
274 that whenever a recursion or subroutine call within a pattern completes, the
275 capturing state is reset to what it was before.
276 .P
277 The \fIcapture_last\fP field contains the number of the most recently captured
278 substring, and the \fIcapture_top\fP field contains one more than the number of
279 the highest numbered captured substring so far. If no substrings have yet been
280 captured, the value of \fIcapture_last\fP is 0 and the value of
281 \fIcapture_top\fP is 1. The values of these fields do not always differ by one;
282 for example, when the callout in the pattern ((a)(b))(?C2) is taken,
283 \fIcapture_last\fP is 1 but \fIcapture_top\fP is 4.
284 .P
285 The contents of ovector[2] to ovector[<capture_top>*2-1] can be inspected in
286 order to extract substrings that have been matched so far, in the same way as
287 extracting substrings after a match has completed. The values in ovector[0] and
288 ovector[1] are always PCRE2_UNSET because the match is by definition not
289 complete. Substrings that have not been captured but whose numbers are less
290 than \fIcapture_top\fP also have both of their ovector slots set to
291 PCRE2_UNSET.
292 .P
293 For DFA matching, the \fIoffset_vector\fP field points to the ovector that was
294 passed to the matching function in the match data block for callouts at the top
295 level, but to an internal ovector during the processing of pattern recursions,
296 lookarounds, and atomic groups. However, these ovectors hold no useful
297 information because \fBpcre2_dfa_match()\fP does not support substring
298 capturing. The value of \fIcapture_top\fP is always 1 and the value of
299 \fIcapture_last\fP is always 0 for DFA matching.
300 .P
301 The \fIsubject\fP and \fIsubject_length\fP fields contain copies of the values
302 that were passed to the matching function.
303 .P
304 The \fIstart_match\fP field normally contains the offset within the subject at
305 which the current match attempt started. However, if the escape sequence \eK
306 has been encountered, this value is changed to reflect the modified starting
307 point. If the pattern is not anchored, the callout function may be called
308 several times from the same point in the pattern for different starting points
309 in the subject.
310 .P
311 The \fIcurrent_position\fP field contains the offset within the subject of the
312 current match pointer.
313 .P
314 The \fIpattern_position\fP field contains the offset in the pattern string to
315 the next item to be matched.
316 .P
317 The \fInext_item_length\fP field contains the length of the next item to be
318 processed in the pattern string. When the callout is at the end of the pattern,
319 the length is zero. When the callout precedes an opening parenthesis, the
320 length includes meta characters that follow the parenthesis. For example, in a
321 callout before an assertion such as (?=ab) the length is 3. For an an
322 alternation bar or a closing parenthesis, the length is one, unless a closing
323 parenthesis is followed by a quantifier, in which case its length is included.
324 (This changed in release 10.23. In earlier releases, before an opening
325 parenthesis the length was that of the entire subpattern, and before an
326 alternation bar or a closing parenthesis the length was zero.)
327 .P
328 The \fIpattern_position\fP and \fInext_item_length\fP fields are intended to
329 help in distinguishing between different automatic callouts, which all have the
330 same callout number. However, they are set for all callouts, and are used by
331 \fBpcre2test\fP to show the next item to be matched when displaying callout
332 information.
333 .P
334 In callouts from \fBpcre2_match()\fP the \fImark\fP field contains a pointer to
335 the zero-terminated name of the most recently passed (*MARK), (*PRUNE), or
336 (*THEN) item in the match, or NULL if no such items have been passed. Instances
337 of (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In
338 callouts from the DFA matching function this field always contains NULL.
339 .P
340 The \fIcallout_flags\fP field is always zero in callouts from
341 \fBpcre2_dfa_match()\fP or when JIT is being used. When \fBpcre2_match()\fP
342 without JIT is used, the following bits may be set:
343 .sp
344   PCRE2_CALLOUT_STARTMATCH
345 .sp
346 This is set for the first callout after the start of matching for each new
347 starting position in the subject.
348 .sp
349   PCRE2_CALLOUT_BACKTRACK
350 .sp
351 This is set if there has been a matching backtrack since the previous callout,
352 or since the start of matching if this is the first callout from a
353 \fBpcre2_match()\fP run.
354 .P
355 Both bits are set when a backtrack has caused a "bumpalong" to a new starting
356 position in the subject. Output from \fBpcre2test\fP does not indicate the
357 presence of these bits unless the \fBcallout_extra\fP modifier is set.
358 .P
359 The information in the \fBcallout_flags\fP field is provided so that
360 applications can track and tell their users how matching with backtracking is
361 done. This can be useful when trying to optimize patterns, or just to
362 understand how PCRE2 works. There is no support in \fBpcre2_dfa_match()\fP
363 because there is no backtracking in DFA matching, and there is no support in
364 JIT because JIT is all about maximimizing matching performance. In both these
365 cases the \fBcallout_flags\fP field is always zero.
366 .
367 .
368 .SH "RETURN VALUES FROM CALLOUTS"
369 .rs
370 .sp
371 The external callout function returns an integer to PCRE2. If the value is
372 zero, matching proceeds as normal. If the value is greater than zero, matching
373 fails at the current point, but the testing of other matching possibilities
374 goes ahead, just as if a lookahead assertion had failed. If the value is less
375 than zero, the match is abandoned, and the matching function returns the
376 negative value.
377 .P
378 Negative values should normally be chosen from the set of PCRE2_ERROR_xxx
379 values. In particular, PCRE2_ERROR_NOMATCH forces a standard "no match"
380 failure. The error number PCRE2_ERROR_CALLOUT is reserved for use by callout
381 functions; it will never be used by PCRE2 itself.
382 .
383 .
384 .SH "CALLOUT ENUMERATION"
385 .rs
386 .sp
387 .nf
388 .B int pcre2_callout_enumerate(const pcre2_code *\fIcode\fP,
389 .B "  int (*\fIcallback\fP)(pcre2_callout_enumerate_block *, void *),"
390 .B "  void *\fIuser_data\fP);"
391 .fi
392 .sp
393 A script language that supports the use of string arguments in callouts might
394 like to scan all the callouts in a pattern before running the match. This can
395 be done by calling \fBpcre2_callout_enumerate()\fP. The first argument is a
396 pointer to a compiled pattern, the second points to a callback function, and
397 the third is arbitrary user data. The callback function is called for every
398 callout in the pattern in the order in which they appear. Its first argument is
399 a pointer to a callout enumeration block, and its second argument is the
400 \fIuser_data\fP value that was passed to \fBpcre2_callout_enumerate()\fP. The
401 data block contains the following fields:
402 .sp
403   \fIversion\fP                Block version number
404   \fIpattern_position\fP       Offset to next item in pattern
405   \fInext_item_length\fP       Length of next item in pattern
406   \fIcallout_number\fP         Number for numbered callouts
407   \fIcallout_string_offset\fP  Offset to string within pattern
408   \fIcallout_string_length\fP  Length of callout string
409   \fIcallout_string\fP         Points to callout string or is NULL
410 .sp
411 The version number is currently 0. It will increase if new fields are ever
412 added to the block. The remaining fields are the same as their namesakes in the
413 \fBpcre2_callout\fP block that is used for callouts during matching, as
414 described
415 .\" HTML <a href="#calloutinterface">
416 .\" </a>
417 above.
418 .\"
419 .P
420 Note that the value of \fIpattern_position\fP is unique for each callout.
421 However, if a callout occurs inside a group that is quantified with a non-zero
422 minimum or a fixed maximum, the group is replicated inside the compiled
423 pattern. For example, a pattern such as /(a){2}/ is compiled as if it were
424 /(a)(a)/. This means that the callout will be enumerated more than once, but
425 with the same value for \fIpattern_position\fP in each case.
426 .P
427 The callback function should normally return zero. If it returns a non-zero
428 value, scanning the pattern stops, and that value is returned from
429 \fBpcre2_callout_enumerate()\fP.
430 .
431 .
432 .SH AUTHOR
433 .rs
434 .sp
435 .nf
436 Philip Hazel
437 University Computing Service
438 Cambridge, England.
439 .fi
440 .
441 .
442 .SH REVISION
443 .rs
444 .sp
445 .nf
446 Last updated: 26 April 2018
447 Copyright (c) 1997-2018 University of Cambridge.
448 .fi