Home | History | Annotate | Download | only in html
      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 &#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>
     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&#60;arg&#62;) 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. For example, if PCRE2_AUTO_CALLOUT is used with the pattern
     61 <pre>
     62   A(\d{2}|--)
     63 </pre>
     64 it 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>
     70 Notice that there is a callout before and after each parenthesis and
     71 alternation bar. If the pattern contains a conditional group whose condition is
     72 an assertion, an automatic callout is inserted immediately before the
     73 condition. Such a callout may also be inserted explicitly, for example:
     74 <pre>
     75   (?(?C9)(?=a)ab|de)  (?(?C%text%)(?!=d)ab|de)
     76 </pre>
     77 This applies only to assertion conditions (because they are themselves
     78 independent groups).
     79 </P>
     80 <P>
     81 Callouts can be useful for tracking the progress of pattern matching. The
     82 <a href="pcre2test.html"><b>pcre2test</b></a>
     83 program has a pattern qualifier (/auto_callout) that sets automatic callouts.
     84 When any callouts are present, the output from <b>pcre2test</b> indicates how
     85 the pattern is being matched. This is useful information when you are trying to
     86 optimize the performance of a particular pattern.
     87 </P>
     88 <br><a name="SEC3" href="#TOC1">MISSING CALLOUTS</a><br>
     89 <P>
     90 You should be aware that, because of optimizations in the way PCRE2 compiles
     91 and matches patterns, callouts sometimes do not happen exactly as you might
     92 expect.
     93 </P>
     94 <br><b>
     95 Auto-possessification
     96 </b><br>
     97 <P>
     98 At compile time, PCRE2 "auto-possessifies" repeated items when it knows that
     99 what follows cannot be part of the repeat. For example, a+[bc] is compiled as
    100 if it were a++[bc]. The <b>pcre2test</b> output when this pattern is compiled
    101 with 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>
    109 This indicates that when matching [bc] fails, there is no backtracking into a+
    110 and therefore the callouts that would be taken for the backtracks do not occur.
    111 You 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
    113 case, 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>
    123 This time, when matching [bc] fails, the matcher backtracks into a+ and tries
    124 again, repeatedly, until a+ itself fails.
    125 </P>
    126 <br><b>
    127 Automatic .* anchoring
    128 </b><br>
    129 <P>
    130 By default, an optimization is applied when .* is the first significant item in
    131 a pattern. If PCRE2_DOTALL is set, so that the dot can match any character, the
    132 pattern is automatically anchored. If PCRE2_DOTALL is not set, a match can
    133 start 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,
    135 if .* is in an atomic group or if there is a back reference to the capturing
    136 group in which it appears. It is also disabled if the pattern contains (*PRUNE)
    137 or (*SKIP). However, the presence of callouts does not affect it.
    138 </P>
    139 <P>
    140 For example, if the pattern .*\d is compiled with PCRE2_AUTO_CALLOUT and
    141 applied 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>
    150 This shows that all match attempts start at the beginning of the subject. In
    151 other words, the pattern is anchored. You can disable this optimization by
    152 passing PCRE2_NO_DOTSTAR_ANCHOR to <b>pcre2_compile()</b>, or starting the
    153 pattern 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>
    165 This shows more match attempts, starting at the second subject character.
    166 Another optimization, described in the next section, means that there is no
    167 subsequent attempt to match with an empty subject.
    168 </P>
    169 <P>
    170 If a pattern has more than one top-level branch, automatic anchoring occurs if
    171 all branches are anchorable.
    172 </P>
    173 <br><b>
    174 Other optimizations
    175 </b><br>
    176 <P>
    177 Other optimizations that provide fast "no match" results also affect callouts.
    178 For example, if the pattern is
    179 <pre>
    180   ab(?C4)cd
    181 </pre>
    182 PCRE2 knows that any matching string must contain the letter "d". If the
    183 subject string is "abyz", the lack of "d" means that matching doesn't ever
    184 start, and the callout is never reached. However, with "abyd", though the
    185 result is still no match, the callout is obeyed.
    186 </P>
    187 <P>
    188 PCRE2 also knows the minimum length of a matching string, and will immediately
    189 give a "no match" return without actually running a match if the subject is not
    190 long enough, or, for unanchored patterns, if it has been scanned far enough.
    191 </P>
    192 <P>
    193 You can disable these optimizations by passing the PCRE2_NO_START_OPTIMIZE
    194 option 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
    196 callouts 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>
    200 During matching, when PCRE2 reaches a callout point, if an external function is
    201 set in the match context, it is called. This applies to both normal and DFA
    202 matching. 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
    204 was 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>
    207 documentation). 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>
    225 The <i>version</i> field contains the version number of the block format. The
    226 current version is 1; the three callout string fields were added for this
    227 version. If you are writing an application that might use an earlier release of
    228 PCRE2, you should check the version number before accessing any of these
    229 fields. The version number will increase in future if more fields are added,
    230 but the intention is never to remove any of the existing fields.
    231 </P>
    232 <br><b>
    233 Fields for numerical callouts
    234 </b><br>
    235 <P>
    236 For a numerical callout, <i>callout_string</i> is NULL, and <i>callout_number</i>
    237 contains the number of the callout, in the range 0-255. This is the number
    238 that follows (?C for manual callouts; it is 255 for automatically generated
    239 callouts.
    240 </P>
    241 <br><b>
    242 Fields for string callouts
    243 </b><br>
    244 <P>
    245 For 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
    247 pattern. Its length is given by <i>callout_string_length</i>. Duplicated ending
    248 delimiters that were present in the original pattern string have been turned
    249 into single characters, but there is no other processing of the callout string
    250 argument. An additional code unit containing binary zero is present after the
    251 string, but is not included in the length. The delimiter that was used to start
    252 the string is also stored within the pattern, immediately before the string
    253 itself. You can access this delimiter as <i>callout_string</i>[-1] if you need
    254 it.
    255 </P>
    256 <P>
    257 The <i>callout_string_offset</i> field is the code unit offset to the start of
    258 the callout argument string within the original pattern string. This is
    259 provided for the benefit of applications such as script languages that might
    260 need to report errors in the callout string within the pattern.
    261 </P>
    262 <br><b>
    263 Fields for all callouts
    264 </b><br>
    265 <P>
    266 The remaining fields in the callout block are the same for both kinds of
    267 callout.
    268 </P>
    269 <P>
    270 The <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
    272 block. When <b>pcre2_match()</b> is used, the contents can be inspected in
    273 order to extract substrings that have been matched so far, in the same way as
    274 for extracting substrings after a match has completed. For the DFA matching
    275 function, this field is not useful.
    276 </P>
    277 <P>
    278 The <i>subject</i> and <i>subject_length</i> fields contain copies of the values
    279 that were passed to the matching function.
    280 </P>
    281 <P>
    282 The <i>start_match</i> field normally contains the offset within the subject at
    283 which the current match attempt started. However, if the escape sequence \K
    284 has been encountered, this value is changed to reflect the modified starting
    285 point. If the pattern is not anchored, the callout function may be called
    286 several times from the same point in the pattern for different starting points
    287 in the subject.
    288 </P>
    289 <P>
    290 The <i>current_position</i> field contains the offset within the subject of the
    291 current match pointer.
    292 </P>
    293 <P>
    294 When the <b>pcre2_match()</b> is used, the <i>capture_top</i> field contains one
    295 more than the number of the highest numbered captured substring so far. If no
    296 substrings have been captured, the value of <i>capture_top</i> is one. This is
    297 always the case when the DFA functions are used, because they do not support
    298 captured substrings.
    299 </P>
    300 <P>
    301 The <i>capture_last</i> field contains the number of the most recently captured
    302 substring. However, when a recursion exits, the value reverts to what it was
    303 outside the recursion, as do the values of all captured substrings. If no
    304 substrings have been captured, the value of <i>capture_last</i> is 0. This is
    305 always the case for the DFA matching functions.
    306 </P>
    307 <P>
    308 The <i>pattern_position</i> field contains the offset in the pattern string to
    309 the next item to be matched.
    310 </P>
    311 <P>
    312 The <i>next_item_length</i> field contains the length of the next item to be
    313 matched in the pattern string. When the callout immediately precedes an
    314 alternation bar, a closing parenthesis, or the end of the pattern, the length
    315 is zero. When the callout precedes an opening parenthesis, the length is that
    316 of the entire subpattern.
    317 </P>
    318 <P>
    319 The <i>pattern_position</i> and <i>next_item_length</i> fields are intended to
    320 help in distinguishing between different automatic callouts, which all have the
    321 same 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
    323 information.
    324 </P>
    325 <P>
    326 In callouts from <b>pcre2_match()</b> the <i>mark</i> field contains a pointer to
    327 the 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
    329 of (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In
    330 callouts 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>
    334 The external callout function returns an integer to PCRE2. If the value is
    335 zero, matching proceeds as normal. If the value is greater than zero, matching
    336 fails at the current point, but the testing of other matching possibilities
    337 goes ahead, just as if a lookahead assertion had failed. If the value is less
    338 than zero, the match is abandoned, and the matching function returns the
    339 negative value.
    340 </P>
    341 <P>
    342 Negative values should normally be chosen from the set of PCRE2_ERROR_xxx
    343 values. In particular, PCRE2_ERROR_NOMATCH forces a standard "no match"
    344 failure. The error number PCRE2_ERROR_CALLOUT is reserved for use by callout
    345 functions; 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>
    354 A script language that supports the use of string arguments in callouts might
    355 like to scan all the callouts in a pattern before running the match. This can
    356 be done by calling <b>pcre2_callout_enumerate()</b>. The first argument is a
    357 pointer to a compiled pattern, the second points to a callback function, and
    358 the third is arbitrary user data. The callback function is called for every
    359 callout in the pattern in the order in which they appear. Its first argument is
    360 a 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
    362 data 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>
    372 The version number is currently 0. It will increase if new fields are ever
    373 added 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
    375 described
    376 <a href="#calloutinterface">above.</a>
    377 </P>
    378 <P>
    379 Note that the value of <i>pattern_position</i> is unique for each callout.
    380 However, if a callout occurs inside a group that is quantified with a non-zero
    381 minimum or a fixed maximum, the group is replicated inside the compiled
    382 pattern. 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
    384 with the same value for <i>pattern_position</i> in each case.
    385 </P>
    386 <P>
    387 The callback function should normally return zero. If it returns a non-zero
    388 value, 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>
    393 Philip Hazel
    394 <br>
    395 University Computing Service
    396 <br>
    397 Cambridge, England.
    398 <br>
    399 </P>
    400 <br><a name="SEC8" href="#TOC1">REVISION</a><br>
    401 <P>
    402 Last updated: 23 March 2015
    403 <br>
    404 Copyright &copy; 1997-2015 University of Cambridge.
    405 <br>
    406 <p>
    407 Return to the <a href="index.html">PCRE2 index page</a>.
    408 </p>
    409