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