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> 8 Return to the <a href="index.html">PCRE2 index page</a>. 9 </p> 10 <p> 11 This page is part of the PCRE2 HTML documentation. It was generated 12 automatically from the original man page. If there is any nonsense in it, 13 please 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> 39 PCRE2 provides a feature called "callout", which is a means of temporarily 40 passing control to the caller of PCRE2 in the middle of pattern matching. The 41 caller of PCRE2 provides an external function by putting its entry point in 42 a match context (see <b>pcre2_set_callout()</b> in the 43 <a href="pcre2api.html"><b>pcre2api</b></a> 44 documentation). 45 </P> 46 <P> 47 Within a regular expression, (?C<arg>) indicates a point at which the external 48 function is to be called. Different callout points can be identified by putting 49 a number less than 256 after the letter C. The default value is zero. 50 Alternatively, the argument may be a delimited string. The starting delimiter 51 must be one of ` ' " ^ % # $ { and the ending delimiter is the same as the 52 start, except for {, where the ending delimiter is }. If the ending delimiter 53 is needed within the string, it must be doubled. For example, this pattern has 54 two callout points: 55 <pre> 56 (?C1)abc(?C"some ""arbitrary"" text")def 57 </pre> 58 If the PCRE2_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE2 59 automatically inserts callouts, all with number 255, before each item in the 60 pattern except for immediately before or after an explicit callout. For 61 example, if PCRE2_AUTO_CALLOUT is used with the pattern 62 <pre> 63 A(?C3)B 64 </pre> 65 it is processed as if it were 66 <pre> 67 (?C255)A(?C3)B(?C255) 68 </pre> 69 Here is a more complicated example: 70 <pre> 71 A(\d{2}|--) 72 </pre> 73 With 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> 77 Notice that there is a callout before and after each parenthesis and 78 alternation bar. If the pattern contains a conditional group whose condition is 79 an assertion, an automatic callout is inserted immediately before the 80 condition. Such a callout may also be inserted explicitly, for example: 81 <pre> 82 (?(?C9)(?=a)ab|de) (?(?C%text%)(?!=d)ab|de) 83 </pre> 84 This applies only to assertion conditions (because they are themselves 85 independent groups). 86 </P> 87 <P> 88 Callouts can be useful for tracking the progress of pattern matching. The 89 <a href="pcre2test.html"><b>pcre2test</b></a> 90 program has a pattern qualifier (/auto_callout) that sets automatic callouts. 91 When any callouts are present, the output from <b>pcre2test</b> indicates how 92 the pattern is being matched. This is useful information when you are trying to 93 optimize the performance of a particular pattern. 94 </P> 95 <br><a name="SEC3" href="#TOC1">MISSING CALLOUTS</a><br> 96 <P> 97 You should be aware that, because of optimizations in the way PCRE2 compiles 98 and matches patterns, callouts sometimes do not happen exactly as you might 99 expect. 100 </P> 101 <br><b> 102 Auto-possessification 103 </b><br> 104 <P> 105 At compile time, PCRE2 "auto-possessifies" repeated items when it knows that 106 what follows cannot be part of the repeat. For example, a+[bc] is compiled as 107 if it were a++[bc]. The <b>pcre2test</b> output when this pattern is compiled 108 with 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> 116 This 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 118 taken for the backtracks do not occur. You can disable the auto-possessify 119 feature by passing PCRE2_NO_AUTO_POSSESS to <b>pcre2_compile()</b>, or starting 120 the 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> 130 This time, when matching [bc] fails, the matcher backtracks into a+ and tries 131 again, repeatedly, until a+ itself fails. 132 </P> 133 <br><b> 134 Automatic .* anchoring 135 </b><br> 136 <P> 137 By default, an optimization is applied when .* is the first significant item in 138 a pattern. If PCRE2_DOTALL is set, so that the dot can match any character, the 139 pattern is automatically anchored. If PCRE2_DOTALL is not set, a match can 140 start 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 142 branch, automatic anchoring occurs if all branches are anchorable. 143 </P> 144 <P> 145 This optimization is disabled, however, if .* is in an atomic group or if there 146 is a backreference to the capturing group in which it appears. It is also 147 disabled if the pattern contains (*PRUNE) or (*SKIP). However, the presence of 148 callouts does not affect it. 149 </P> 150 <P> 151 For example, if the pattern .*\d is compiled with PCRE2_AUTO_CALLOUT and 152 applied 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> 161 This shows that all match attempts start at the beginning of the subject. In 162 other words, the pattern is anchored. You can disable this optimization by 163 passing PCRE2_NO_DOTSTAR_ANCHOR to <b>pcre2_compile()</b>, or starting the 164 pattern 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> 176 This shows more match attempts, starting at the second subject character. 177 Another optimization, described in the next section, means that there is no 178 subsequent attempt to match with an empty subject. 179 </P> 180 <br><b> 181 Other optimizations 182 </b><br> 183 <P> 184 Other optimizations that provide fast "no match" results also affect callouts. 185 For example, if the pattern is 186 <pre> 187 ab(?C4)cd 188 </pre> 189 PCRE2 knows that any matching string must contain the letter "d". If the 190 subject string is "abyz", the lack of "d" means that matching doesn't ever 191 start, and the callout is never reached. However, with "abyd", though the 192 result is still no match, the callout is obeyed. 193 </P> 194 <P> 195 For most patterns PCRE2 also knows the minimum length of a matching string, and 196 will immediately give a "no match" return without actually running a match if 197 the subject is not long enough, or, for unanchored patterns, if it has been 198 scanned far enough. 199 </P> 200 <P> 201 You can disable these optimizations by passing the PCRE2_NO_START_OPTIMIZE 202 option 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 204 callouts 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> 208 During matching, when PCRE2 reaches a callout point, if an external function is 209 provided in the match context, it is called. This applies to both normal, 210 DFA, and JIT matching. The first argument to the callout function is a pointer 211 to a <b>pcre2_callout</b> block. The second argument is the void * callout data 212 that 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> 215 documentation). The callout block structure contains the following fields, not 216 necessarily 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> 235 The <i>version</i> field contains the version number of the block format. The 236 current version is 2; the three callout string fields were added for version 1, 237 and the <i>callout_flags</i> field for version 2. If you are writing an 238 application that might use an earlier release of PCRE2, you should check the 239 version number before accessing any of these fields. The version number will 240 increase in future if more fields are added, but the intention is never to 241 remove any of the existing fields. 242 </P> 243 <br><b> 244 Fields for numerical callouts 245 </b><br> 246 <P> 247 For a numerical callout, <i>callout_string</i> is NULL, and <i>callout_number</i> 248 contains the number of the callout, in the range 0-255. This is the number 249 that follows (?C for callouts that part of the pattern; it is 255 for 250 automatically generated callouts. 251 </P> 252 <br><b> 253 Fields for string callouts 254 </b><br> 255 <P> 256 For 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 258 pattern. Its length is given by <i>callout_string_length</i>. Duplicated ending 259 delimiters that were present in the original pattern string have been turned 260 into single characters, but there is no other processing of the callout string 261 argument. An additional code unit containing binary zero is present after the 262 string, but is not included in the length. The delimiter that was used to start 263 the string is also stored within the pattern, immediately before the string 264 itself. You can access this delimiter as <i>callout_string</i>[-1] if you need 265 it. 266 </P> 267 <P> 268 The <i>callout_string_offset</i> field is the code unit offset to the start of 269 the callout argument string within the original pattern string. This is 270 provided for the benefit of applications such as script languages that might 271 need to report errors in the callout string within the pattern. 272 </P> 273 <br><b> 274 Fields for all callouts 275 </b><br> 276 <P> 277 The remaining fields in the callout block are the same for both kinds of 278 callout. 279 </P> 280 <P> 281 The <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 283 change any of them. 284 </P> 285 <P> 286 For calls to <b>pcre2_match()</b>, the <i>offset_vector</i> field is not (since 287 release 10.30) a pointer to the actual ovector that was passed to the matching 288 function in the match data block. Instead it points to an internal ovector of a 289 size large enough to hold all possible captured substrings in the pattern. Note 290 that whenever a recursion or subroutine call within a pattern completes, the 291 capturing state is reset to what it was before. 292 </P> 293 <P> 294 The <i>capture_last</i> field contains the number of the most recently captured 295 substring, and the <i>capture_top</i> field contains one more than the number of 296 the highest numbered captured substring so far. If no substrings have yet been 297 captured, 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; 299 for 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> 303 The contents of ovector[2] to ovector[<capture_top>*2-1] can be inspected in 304 order to extract substrings that have been matched so far, in the same way as 305 extracting substrings after a match has completed. The values in ovector[0] and 306 ovector[1] are always PCRE2_UNSET because the match is by definition not 307 complete. Substrings that have not been captured but whose numbers are less 308 than <i>capture_top</i> also have both of their ovector slots set to 309 PCRE2_UNSET. 310 </P> 311 <P> 312 For DFA matching, the <i>offset_vector</i> field points to the ovector that was 313 passed to the matching function in the match data block for callouts at the top 314 level, but to an internal ovector during the processing of pattern recursions, 315 lookarounds, and atomic groups. However, these ovectors hold no useful 316 information because <b>pcre2_dfa_match()</b> does not support substring 317 capturing. 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> 321 The <i>subject</i> and <i>subject_length</i> fields contain copies of the values 322 that were passed to the matching function. 323 </P> 324 <P> 325 The <i>start_match</i> field normally contains the offset within the subject at 326 which the current match attempt started. However, if the escape sequence \K 327 has been encountered, this value is changed to reflect the modified starting 328 point. If the pattern is not anchored, the callout function may be called 329 several times from the same point in the pattern for different starting points 330 in the subject. 331 </P> 332 <P> 333 The <i>current_position</i> field contains the offset within the subject of the 334 current match pointer. 335 </P> 336 <P> 337 The <i>pattern_position</i> field contains the offset in the pattern string to 338 the next item to be matched. 339 </P> 340 <P> 341 The <i>next_item_length</i> field contains the length of the next item to be 342 processed in the pattern string. When the callout is at the end of the pattern, 343 the length is zero. When the callout precedes an opening parenthesis, the 344 length includes meta characters that follow the parenthesis. For example, in a 345 callout before an assertion such as (?=ab) the length is 3. For an an 346 alternation bar or a closing parenthesis, the length is one, unless a closing 347 parenthesis 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 349 parenthesis the length was that of the entire subpattern, and before an 350 alternation bar or a closing parenthesis the length was zero.) 351 </P> 352 <P> 353 The <i>pattern_position</i> and <i>next_item_length</i> fields are intended to 354 help in distinguishing between different automatic callouts, which all have the 355 same 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 357 information. 358 </P> 359 <P> 360 In callouts from <b>pcre2_match()</b> the <i>mark</i> field contains a pointer to 361 the 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 363 of (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In 364 callouts from the DFA matching function this field always contains NULL. 365 </P> 366 <P> 367 The <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> 369 without JIT is used, the following bits may be set: 370 <pre> 371 PCRE2_CALLOUT_STARTMATCH 372 </pre> 373 This is set for the first callout after the start of matching for each new 374 starting position in the subject. 375 <pre> 376 PCRE2_CALLOUT_BACKTRACK 377 </pre> 378 This is set if there has been a matching backtrack since the previous callout, 379 or since the start of matching if this is the first callout from a 380 <b>pcre2_match()</b> run. 381 </P> 382 <P> 383 Both bits are set when a backtrack has caused a "bumpalong" to a new starting 384 position in the subject. Output from <b>pcre2test</b> does not indicate the 385 presence of these bits unless the <b>callout_extra</b> modifier is set. 386 </P> 387 <P> 388 The information in the <b>callout_flags</b> field is provided so that 389 applications can track and tell their users how matching with backtracking is 390 done. This can be useful when trying to optimize patterns, or just to 391 understand how PCRE2 works. There is no support in <b>pcre2_dfa_match()</b> 392 because there is no backtracking in DFA matching, and there is no support in 393 JIT because JIT is all about maximimizing matching performance. In both these 394 cases 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> 398 The external callout function returns an integer to PCRE2. If the value is 399 zero, matching proceeds as normal. If the value is greater than zero, matching 400 fails at the current point, but the testing of other matching possibilities 401 goes ahead, just as if a lookahead assertion had failed. If the value is less 402 than zero, the match is abandoned, and the matching function returns the 403 negative value. 404 </P> 405 <P> 406 Negative values should normally be chosen from the set of PCRE2_ERROR_xxx 407 values. In particular, PCRE2_ERROR_NOMATCH forces a standard "no match" 408 failure. The error number PCRE2_ERROR_CALLOUT is reserved for use by callout 409 functions; 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> 418 A script language that supports the use of string arguments in callouts might 419 like to scan all the callouts in a pattern before running the match. This can 420 be done by calling <b>pcre2_callout_enumerate()</b>. The first argument is a 421 pointer to a compiled pattern, the second points to a callback function, and 422 the third is arbitrary user data. The callback function is called for every 423 callout in the pattern in the order in which they appear. Its first argument is 424 a 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 426 data 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> 436 The version number is currently 0. It will increase if new fields are ever 437 added 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 439 described 440 <a href="#calloutinterface">above.</a> 441 </P> 442 <P> 443 Note that the value of <i>pattern_position</i> is unique for each callout. 444 However, if a callout occurs inside a group that is quantified with a non-zero 445 minimum or a fixed maximum, the group is replicated inside the compiled 446 pattern. 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 448 with the same value for <i>pattern_position</i> in each case. 449 </P> 450 <P> 451 The callback function should normally return zero. If it returns a non-zero 452 value, 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> 457 Philip Hazel 458 <br> 459 University Computing Service 460 <br> 461 Cambridge, England. 462 <br> 463 </P> 464 <br><a name="SEC8" href="#TOC1">REVISION</a><br> 465 <P> 466 Last updated: 26 April 2018 467 <br> 468 Copyright © 1997-2018 University of Cambridge. 469 <br> 470 <p> 471 Return to the <a href="index.html">PCRE2 index page</a>. 472 </p> 473