1<html> 2<head> 3<title>pcrecallout specification</title> 4</head> 5<body bgcolor="#FFFFFF" text="#00005A" link="#0066FF" alink="#3399FF" vlink="#2222BB"> 6<h1>pcrecallout man page</h1> 7<p> 8Return to the <a href="index.html">PCRE index page</a>. 9</p> 10<p> 11This page is part of the PCRE HTML documentation. It was generated automatically 12from the original man page. If there is any nonsense in it, please consult the 13man page, in case the conversion went wrong. 14<br> 15<ul> 16<li><a name="TOC1" href="#SEC1">PCRE CALLOUTS</a> 17<li><a name="TOC2" href="#SEC2">MISSING CALLOUTS</a> 18<li><a name="TOC3" href="#SEC3">THE CALLOUT INTERFACE</a> 19<li><a name="TOC4" href="#SEC4">RETURN VALUES</a> 20<li><a name="TOC5" href="#SEC5">AUTHOR</a> 21<li><a name="TOC6" href="#SEC6">REVISION</a> 22</ul> 23<br><a name="SEC1" href="#TOC1">PCRE CALLOUTS</a><br> 24<P> 25<b>int (*pcre_callout)(pcre_callout_block *);</b> 26</P> 27<P> 28PCRE provides a feature called "callout", which is a means of temporarily 29passing control to the caller of PCRE in the middle of pattern matching. The 30caller of PCRE provides an external function by putting its entry point in the 31global variable <i>pcre_callout</i>. By default, this variable contains NULL, 32which disables all calling out. 33</P> 34<P> 35Within a regular expression, (?C) indicates the points at which the external 36function is to be called. Different callout points can be identified by putting 37a number less than 256 after the letter C. The default value is zero. 38For example, this pattern has two callout points: 39<pre> 40 (?C1)abc(?C2)def 41</pre> 42If the PCRE_AUTO_CALLOUT option bit is set when <b>pcre_compile()</b> or 43<b>pcre_compile2()</b> is called, PCRE automatically inserts callouts, all with 44number 255, before each item in the pattern. For example, if PCRE_AUTO_CALLOUT 45is used with the pattern 46<pre> 47 A(\d{2}|--) 48</pre> 49it is processed as if it were 50<br> 51<br> 52(?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255) 53<br> 54<br> 55Notice that there is a callout before and after each parenthesis and 56alternation bar. Automatic callouts can be used for tracking the progress of 57pattern matching. The 58<a href="pcretest.html"><b>pcretest</b></a> 59command has an option that sets automatic callouts; when it is used, the output 60indicates how the pattern is matched. This is useful information when you are 61trying to optimize the performance of a particular pattern. 62</P> 63<br><a name="SEC2" href="#TOC1">MISSING CALLOUTS</a><br> 64<P> 65You should be aware that, because of optimizations in the way PCRE matches 66patterns by default, callouts sometimes do not happen. For example, if the 67pattern is 68<pre> 69 ab(?C4)cd 70</pre> 71PCRE knows that any matching string must contain the letter "d". If the subject 72string is "abyz", the lack of "d" means that matching doesn't ever start, and 73the callout is never reached. However, with "abyd", though the result is still 74no match, the callout is obeyed. 75</P> 76<P> 77If the pattern is studied, PCRE knows the minimum length of a matching string, 78and will immediately give a "no match" return without actually running a match 79if the subject is not long enough, or, for unanchored patterns, if it has 80been scanned far enough. 81</P> 82<P> 83You can disable these optimizations by passing the PCRE_NO_START_OPTIMIZE 84option to <b>pcre_compile()</b>, <b>pcre_exec()</b>, or <b>pcre_dfa_exec()</b>, 85or by starting the pattern with (*NO_START_OPT). This slows down the matching 86process, but does ensure that callouts such as the example above are obeyed. 87</P> 88<br><a name="SEC3" href="#TOC1">THE CALLOUT INTERFACE</a><br> 89<P> 90During matching, when PCRE reaches a callout point, the external function 91defined by <i>pcre_callout</i> is called (if it is set). This applies to both 92the <b>pcre_exec()</b> and the <b>pcre_dfa_exec()</b> matching functions. The 93only argument to the callout function is a pointer to a <b>pcre_callout</b> 94block. This structure contains the following fields: 95<pre> 96 int <i>version</i>; 97 int <i>callout_number</i>; 98 int *<i>offset_vector</i>; 99 const char *<i>subject</i>; 100 int <i>subject_length</i>; 101 int <i>start_match</i>; 102 int <i>current_position</i>; 103 int <i>capture_top</i>; 104 int <i>capture_last</i>; 105 void *<i>callout_data</i>; 106 int <i>pattern_position</i>; 107 int <i>next_item_length</i>; 108</pre> 109The <i>version</i> field is an integer containing the version number of the 110block format. The initial version was 0; the current version is 1. The version 111number will change again in future if additional fields are added, but the 112intention is never to remove any of the existing fields. 113</P> 114<P> 115The <i>callout_number</i> field contains the number of the callout, as compiled 116into the pattern (that is, the number after ?C for manual callouts, and 255 for 117automatically generated callouts). 118</P> 119<P> 120The <i>offset_vector</i> field is a pointer to the vector of offsets that was 121passed by the caller to <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b>. When 122<b>pcre_exec()</b> is used, the contents can be inspected in order to extract 123substrings that have been matched so far, in the same way as for extracting 124substrings after a match has completed. For <b>pcre_dfa_exec()</b> this field is 125not useful. 126</P> 127<P> 128The <i>subject</i> and <i>subject_length</i> fields contain copies of the values 129that were passed to <b>pcre_exec()</b>. 130</P> 131<P> 132The <i>start_match</i> field normally contains the offset within the subject at 133which the current match attempt started. However, if the escape sequence \K 134has been encountered, this value is changed to reflect the modified starting 135point. If the pattern is not anchored, the callout function may be called 136several times from the same point in the pattern for different starting points 137in the subject. 138</P> 139<P> 140The <i>current_position</i> field contains the offset within the subject of the 141current match pointer. 142</P> 143<P> 144When the <b>pcre_exec()</b> function is used, the <i>capture_top</i> field 145contains one more than the number of the highest numbered captured substring so 146far. If no substrings have been captured, the value of <i>capture_top</i> is 147one. This is always the case when <b>pcre_dfa_exec()</b> is used, because it 148does not support captured substrings. 149</P> 150<P> 151The <i>capture_last</i> field contains the number of the most recently captured 152substring. If no substrings have been captured, its value is -1. This is always 153the case when <b>pcre_dfa_exec()</b> is used. 154</P> 155<P> 156The <i>callout_data</i> field contains a value that is passed to 157<b>pcre_exec()</b> or <b>pcre_dfa_exec()</b> specifically so that it can be 158passed back in callouts. It is passed in the <i>pcre_callout</i> field of the 159<b>pcre_extra</b> data structure. If no such data was passed, the value of 160<i>callout_data</i> in a <b>pcre_callout</b> block is NULL. There is a 161description of the <b>pcre_extra</b> structure in the 162<a href="pcreapi.html"><b>pcreapi</b></a> 163documentation. 164</P> 165<P> 166The <i>pattern_position</i> field is present from version 1 of the 167<i>pcre_callout</i> structure. It contains the offset to the next item to be 168matched in the pattern string. 169</P> 170<P> 171The <i>next_item_length</i> field is present from version 1 of the 172<i>pcre_callout</i> structure. It contains the length of the next item to be 173matched in the pattern string. When the callout immediately precedes an 174alternation bar, a closing parenthesis, or the end of the pattern, the length 175is zero. When the callout precedes an opening parenthesis, the length is that 176of the entire subpattern. 177</P> 178<P> 179The <i>pattern_position</i> and <i>next_item_length</i> fields are intended to 180help in distinguishing between different automatic callouts, which all have the 181same callout number. However, they are set for all callouts. 182</P> 183<br><a name="SEC4" href="#TOC1">RETURN VALUES</a><br> 184<P> 185The external callout function returns an integer to PCRE. If the value is zero, 186matching proceeds as normal. If the value is greater than zero, matching fails 187at the current point, but the testing of other matching possibilities goes 188ahead, just as if a lookahead assertion had failed. If the value is less than 189zero, the match is abandoned, and <b>pcre_exec()</b> or <b>pcre_dfa_exec()</b> 190returns the negative value. 191</P> 192<P> 193Negative values should normally be chosen from the set of PCRE_ERROR_xxx 194values. In particular, PCRE_ERROR_NOMATCH forces a standard "no match" failure. 195The error number PCRE_ERROR_CALLOUT is reserved for use by callout functions; 196it will never be used by PCRE itself. 197</P> 198<br><a name="SEC5" href="#TOC1">AUTHOR</a><br> 199<P> 200Philip Hazel 201<br> 202University Computing Service 203<br> 204Cambridge CB2 3QH, England. 205<br> 206</P> 207<br><a name="SEC6" href="#TOC1">REVISION</a><br> 208<P> 209Last updated: 21 November 2010 210<br> 211Copyright © 1997-2010 University of Cambridge. 212<br> 213<p> 214Return to the <a href="index.html">PCRE index page</a>. 215</p> 216