• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<title>pcre2callout specification</title>
4</head>
5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB">
6<h1>pcre2callout man page</h1>
7<p>
8Return to the <a href="index.html">PCRE2 index page</a>.
9</p>
10<p>
11This page is part of the PCRE2 HTML documentation. It was generated
12automatically from the original man page. If there is any nonsense in it,
13please consult the man page, in case the conversion went wrong.
14<br>
15<ul>
16<li><a name="TOC1" href="#SEC1">SYNOPSIS</a>
17<li><a name="TOC2" href="#SEC2">DESCRIPTION</a>
18<li><a name="TOC3" href="#SEC3">MISSING CALLOUTS</a>
19<li><a name="TOC4" href="#SEC4">THE CALLOUT INTERFACE</a>
20<li><a name="TOC5" href="#SEC5">RETURN VALUES FROM CALLOUTS</a>
21<li><a name="TOC6" href="#SEC6">CALLOUT ENUMERATION</a>
22<li><a name="TOC7" href="#SEC7">AUTHOR</a>
23<li><a name="TOC8" href="#SEC8">REVISION</a>
24</ul>
25<br><a name="SEC1" href="#TOC1">SYNOPSIS</a><br>
26<P>
27<b>#include &#60;pcre2.h&#62;</b>
28</P>
29<P>
30<b>int (*pcre2_callout)(pcre2_callout_block *, void *);</b>
31<br>
32<br>
33<b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
34<b>  int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
35<b>  void *<i>user_data</i>);</b>
36</P>
37<br><a name="SEC2" href="#TOC1">DESCRIPTION</a><br>
38<P>
39PCRE2 provides a feature called "callout", which is a means of temporarily
40passing control to the caller of PCRE2 in the middle of pattern matching. The
41caller of PCRE2 provides an external function by putting its entry point in
42a match context (see <b>pcre2_set_callout()</b> in the
43<a href="pcre2api.html"><b>pcre2api</b></a>
44documentation).
45</P>
46<P>
47Within a regular expression, (?C&#60;arg&#62;) indicates a point at which the external
48function is to be called. Different callout points can be identified by putting
49a number less than 256 after the letter C. The default value is zero.
50Alternatively, the argument may be a delimited string. The starting delimiter
51must be one of ` ' " ^ % # $ { and the ending delimiter is the same as the
52start, except for {, where the ending delimiter is }. If the ending delimiter
53is needed within the string, it must be doubled. For example, this pattern has
54two callout points:
55<pre>
56  (?C1)abc(?C"some ""arbitrary"" text")def
57</pre>
58If the PCRE2_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE2
59automatically inserts callouts, all with number 255, before each item in the
60pattern. For example, if PCRE2_AUTO_CALLOUT is used with the pattern
61<pre>
62  A(\d{2}|--)
63</pre>
64it is processed as if it were
65<br>
66<br>
67(?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
68<br>
69<br>
70Notice that there is a callout before and after each parenthesis and
71alternation bar. If the pattern contains a conditional group whose condition is
72an assertion, an automatic callout is inserted immediately before the
73condition. Such a callout may also be inserted explicitly, for example:
74<pre>
75  (?(?C9)(?=a)ab|de)  (?(?C%text%)(?!=d)ab|de)
76</pre>
77This applies only to assertion conditions (because they are themselves
78independent groups).
79</P>
80<P>
81Callouts can be useful for tracking the progress of pattern matching. The
82<a href="pcre2test.html"><b>pcre2test</b></a>
83program has a pattern qualifier (/auto_callout) that sets automatic callouts.
84When any callouts are present, the output from <b>pcre2test</b> indicates how
85the pattern is being matched. This is useful information when you are trying to
86optimize the performance of a particular pattern.
87</P>
88<br><a name="SEC3" href="#TOC1">MISSING CALLOUTS</a><br>
89<P>
90You should be aware that, because of optimizations in the way PCRE2 compiles
91and matches patterns, callouts sometimes do not happen exactly as you might
92expect.
93</P>
94<br><b>
95Auto-possessification
96</b><br>
97<P>
98At compile time, PCRE2 "auto-possessifies" repeated items when it knows that
99what follows cannot be part of the repeat. For example, a+[bc] is compiled as
100if it were a++[bc]. The <b>pcre2test</b> output when this pattern is compiled
101with PCRE2_ANCHORED and PCRE2_AUTO_CALLOUT and then applied to the string
102"aaaa" is:
103<pre>
104  ---&#62;aaaa
105   +0 ^        a+
106   +2 ^   ^    [bc]
107  No match
108</pre>
109This indicates that when matching [bc] fails, there is no backtracking into a+
110and therefore the callouts that would be taken for the backtracks do not occur.
111You can disable the auto-possessify feature by passing PCRE2_NO_AUTO_POSSESS to
112<b>pcre2_compile()</b>, or starting the pattern with (*NO_AUTO_POSSESS). In this
113case, the output changes to this:
114<pre>
115  ---&#62;aaaa
116   +0 ^        a+
117   +2 ^   ^    [bc]
118   +2 ^  ^     [bc]
119   +2 ^ ^      [bc]
120   +2 ^^       [bc]
121  No match
122</pre>
123This time, when matching [bc] fails, the matcher backtracks into a+ and tries
124again, repeatedly, until a+ itself fails.
125</P>
126<br><b>
127Automatic .* anchoring
128</b><br>
129<P>
130By default, an optimization is applied when .* is the first significant item in
131a pattern. If PCRE2_DOTALL is set, so that the dot can match any character, the
132pattern is automatically anchored. If PCRE2_DOTALL is not set, a match can
133start only after an internal newline or at the beginning of the subject, and
134<b>pcre2_compile()</b> remembers this. This optimization is disabled, however,
135if .* is in an atomic group or if there is a back reference to the capturing
136group in which it appears. It is also disabled if the pattern contains (*PRUNE)
137or (*SKIP). However, the presence of callouts does not affect it.
138</P>
139<P>
140For example, if the pattern .*\d is compiled with PCRE2_AUTO_CALLOUT and
141applied to the string "aa", the <b>pcre2test</b> output is:
142<pre>
143  ---&#62;aa
144   +0 ^      .*
145   +2 ^ ^    \d
146   +2 ^^     \d
147   +2 ^      \d
148  No match
149</pre>
150This shows that all match attempts start at the beginning of the subject. In
151other words, the pattern is anchored. You can disable this optimization by
152passing PCRE2_NO_DOTSTAR_ANCHOR to <b>pcre2_compile()</b>, or starting the
153pattern with (*NO_DOTSTAR_ANCHOR). In this case, the output changes to:
154<pre>
155  ---&#62;aa
156   +0 ^      .*
157   +2 ^ ^    \d
158   +2 ^^     \d
159   +2 ^      \d
160   +0  ^     .*
161   +2  ^^    \d
162   +2  ^     \d
163  No match
164</pre>
165This shows more match attempts, starting at the second subject character.
166Another optimization, described in the next section, means that there is no
167subsequent attempt to match with an empty subject.
168</P>
169<P>
170If a pattern has more than one top-level branch, automatic anchoring occurs if
171all branches are anchorable.
172</P>
173<br><b>
174Other optimizations
175</b><br>
176<P>
177Other optimizations that provide fast "no match" results also affect callouts.
178For example, if the pattern is
179<pre>
180  ab(?C4)cd
181</pre>
182PCRE2 knows that any matching string must contain the letter "d". If the
183subject string is "abyz", the lack of "d" means that matching doesn't ever
184start, and the callout is never reached. However, with "abyd", though the
185result is still no match, the callout is obeyed.
186</P>
187<P>
188PCRE2 also knows the minimum length of a matching string, and will immediately
189give a "no match" return without actually running a match if the subject is not
190long enough, or, for unanchored patterns, if it has been scanned far enough.
191</P>
192<P>
193You can disable these optimizations by passing the PCRE2_NO_START_OPTIMIZE
194option to <b>pcre2_compile()</b>, or by starting the pattern with
195(*NO_START_OPT). This slows down the matching process, but does ensure that
196callouts such as the example above are obeyed.
197<a name="calloutinterface"></a></P>
198<br><a name="SEC4" href="#TOC1">THE CALLOUT INTERFACE</a><br>
199<P>
200During matching, when PCRE2 reaches a callout point, if an external function is
201set in the match context, it is called. This applies to both normal and DFA
202matching. The first argument to the callout function is a pointer to a
203<b>pcre2_callout</b> block. The second argument is the void * callout data that
204was supplied when the callout was set up by calling <b>pcre2_set_callout()</b>
205(see the
206<a href="pcre2api.html"><b>pcre2api</b></a>
207documentation). The callout block structure contains the following fields:
208<pre>
209  uint32_t      <i>version</i>;
210  uint32_t      <i>callout_number</i>;
211  uint32_t      <i>capture_top</i>;
212  uint32_t      <i>capture_last</i>;
213  PCRE2_SIZE   *<i>offset_vector</i>;
214  PCRE2_SPTR    <i>mark</i>;
215  PCRE2_SPTR    <i>subject</i>;
216  PCRE2_SIZE    <i>subject_length</i>;
217  PCRE2_SIZE    <i>start_match</i>;
218  PCRE2_SIZE    <i>current_position</i>;
219  PCRE2_SIZE    <i>pattern_position</i>;
220  PCRE2_SIZE    <i>next_item_length</i>;
221  PCRE2_SIZE    <i>callout_string_offset</i>;
222  PCRE2_SIZE    <i>callout_string_length</i>;
223  PCRE2_SPTR    <i>callout_string</i>;
224</pre>
225The <i>version</i> field contains the version number of the block format. The
226current version is 1; the three callout string fields were added for this
227version. If you are writing an application that might use an earlier release of
228PCRE2, you should check the version number before accessing any of these
229fields. The version number will increase in future if more fields are added,
230but the intention is never to remove any of the existing fields.
231</P>
232<br><b>
233Fields for numerical callouts
234</b><br>
235<P>
236For a numerical callout, <i>callout_string</i> is NULL, and <i>callout_number</i>
237contains the number of the callout, in the range 0-255. This is the number
238that follows (?C for manual callouts; it is 255 for automatically generated
239callouts.
240</P>
241<br><b>
242Fields for string callouts
243</b><br>
244<P>
245For callouts with string arguments, <i>callout_number</i> is always zero, and
246<i>callout_string</i> points to the string that is contained within the compiled
247pattern. Its length is given by <i>callout_string_length</i>. Duplicated ending
248delimiters that were present in the original pattern string have been turned
249into single characters, but there is no other processing of the callout string
250argument. An additional code unit containing binary zero is present after the
251string, but is not included in the length. The delimiter that was used to start
252the string is also stored within the pattern, immediately before the string
253itself. You can access this delimiter as <i>callout_string</i>[-1] if you need
254it.
255</P>
256<P>
257The <i>callout_string_offset</i> field is the code unit offset to the start of
258the callout argument string within the original pattern string. This is
259provided for the benefit of applications such as script languages that might
260need to report errors in the callout string within the pattern.
261</P>
262<br><b>
263Fields for all callouts
264</b><br>
265<P>
266The remaining fields in the callout block are the same for both kinds of
267callout.
268</P>
269<P>
270The <i>offset_vector</i> field is a pointer to the vector of capturing offsets
271(the "ovector") that was passed to the matching function in the match data
272block. When <b>pcre2_match()</b> is used, the contents can be inspected in
273order to extract substrings that have been matched so far, in the same way as
274for extracting substrings after a match has completed. For the DFA matching
275function, this field is not useful.
276</P>
277<P>
278The <i>subject</i> and <i>subject_length</i> fields contain copies of the values
279that were passed to the matching function.
280</P>
281<P>
282The <i>start_match</i> field normally contains the offset within the subject at
283which the current match attempt started. However, if the escape sequence \K
284has been encountered, this value is changed to reflect the modified starting
285point. If the pattern is not anchored, the callout function may be called
286several times from the same point in the pattern for different starting points
287in the subject.
288</P>
289<P>
290The <i>current_position</i> field contains the offset within the subject of the
291current match pointer.
292</P>
293<P>
294When the <b>pcre2_match()</b> is used, the <i>capture_top</i> field contains one
295more than the number of the highest numbered captured substring so far. If no
296substrings have been captured, the value of <i>capture_top</i> is one. This is
297always the case when the DFA functions are used, because they do not support
298captured substrings.
299</P>
300<P>
301The <i>capture_last</i> field contains the number of the most recently captured
302substring. However, when a recursion exits, the value reverts to what it was
303outside the recursion, as do the values of all captured substrings. If no
304substrings have been captured, the value of <i>capture_last</i> is 0. This is
305always the case for the DFA matching functions.
306</P>
307<P>
308The <i>pattern_position</i> field contains the offset in the pattern string to
309the next item to be matched.
310</P>
311<P>
312The <i>next_item_length</i> field contains the length of the next item to be
313matched in the pattern string. When the callout immediately precedes an
314alternation bar, a closing parenthesis, or the end of the pattern, the length
315is zero. When the callout precedes an opening parenthesis, the length is that
316of the entire subpattern.
317</P>
318<P>
319The <i>pattern_position</i> and <i>next_item_length</i> fields are intended to
320help in distinguishing between different automatic callouts, which all have the
321same callout number. However, they are set for all callouts, and are used by
322<b>pcre2test</b> to show the next item to be matched when displaying callout
323information.
324</P>
325<P>
326In callouts from <b>pcre2_match()</b> the <i>mark</i> field contains a pointer to
327the zero-terminated name of the most recently passed (*MARK), (*PRUNE), or
328(*THEN) item in the match, or NULL if no such items have been passed. Instances
329of (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In
330callouts from the DFA matching function this field always contains NULL.
331</P>
332<br><a name="SEC5" href="#TOC1">RETURN VALUES FROM CALLOUTS</a><br>
333<P>
334The external callout function returns an integer to PCRE2. If the value is
335zero, matching proceeds as normal. If the value is greater than zero, matching
336fails at the current point, but the testing of other matching possibilities
337goes ahead, just as if a lookahead assertion had failed. If the value is less
338than zero, the match is abandoned, and the matching function returns the
339negative value.
340</P>
341<P>
342Negative values should normally be chosen from the set of PCRE2_ERROR_xxx
343values. In particular, PCRE2_ERROR_NOMATCH forces a standard "no match"
344failure. The error number PCRE2_ERROR_CALLOUT is reserved for use by callout
345functions; it will never be used by PCRE2 itself.
346</P>
347<br><a name="SEC6" href="#TOC1">CALLOUT ENUMERATION</a><br>
348<P>
349<b>int pcre2_callout_enumerate(const pcre2_code *<i>code</i>,</b>
350<b>  int (*<i>callback</i>)(pcre2_callout_enumerate_block *, void *),</b>
351<b>  void *<i>user_data</i>);</b>
352<br>
353<br>
354A script language that supports the use of string arguments in callouts might
355like to scan all the callouts in a pattern before running the match. This can
356be done by calling <b>pcre2_callout_enumerate()</b>. The first argument is a
357pointer to a compiled pattern, the second points to a callback function, and
358the third is arbitrary user data. The callback function is called for every
359callout in the pattern in the order in which they appear. Its first argument is
360a pointer to a callout enumeration block, and its second argument is the
361<i>user_data</i> value that was passed to <b>pcre2_callout_enumerate()</b>. The
362data block contains the following fields:
363<pre>
364  <i>version</i>                Block version number
365  <i>pattern_position</i>       Offset to next item in pattern
366  <i>next_item_length</i>       Length of next item in pattern
367  <i>callout_number</i>         Number for numbered callouts
368  <i>callout_string_offset</i>  Offset to string within pattern
369  <i>callout_string_length</i>  Length of callout string
370  <i>callout_string</i>         Points to callout string or is NULL
371</pre>
372The version number is currently 0. It will increase if new fields are ever
373added to the block. The remaining fields are the same as their namesakes in the
374<b>pcre2_callout</b> block that is used for callouts during matching, as
375described
376<a href="#calloutinterface">above.</a>
377</P>
378<P>
379Note that the value of <i>pattern_position</i> is unique for each callout.
380However, if a callout occurs inside a group that is quantified with a non-zero
381minimum or a fixed maximum, the group is replicated inside the compiled
382pattern. For example, a pattern such as /(a){2}/ is compiled as if it were
383/(a)(a)/. This means that the callout will be enumerated more than once, but
384with the same value for <i>pattern_position</i> in each case.
385</P>
386<P>
387The callback function should normally return zero. If it returns a non-zero
388value, scanning the pattern stops, and that value is returned from
389<b>pcre2_callout_enumerate()</b>.
390</P>
391<br><a name="SEC7" href="#TOC1">AUTHOR</a><br>
392<P>
393Philip Hazel
394<br>
395University Computing Service
396<br>
397Cambridge, England.
398<br>
399</P>
400<br><a name="SEC8" href="#TOC1">REVISION</a><br>
401<P>
402Last updated: 23 March 2015
403<br>
404Copyright &copy; 1997-2015 University of Cambridge.
405<br>
406<p>
407Return to the <a href="index.html">PCRE2 index page</a>.
408</p>
409