Tristan Matthews | 0461646 | 2013-11-14 16:09:34 -0500 | [diff] [blame] | 1 | .TH PCRECALLOUT 3 |
| 2 | .SH NAME |
| 3 | PCRE - Perl-compatible regular expressions |
| 4 | .SH "PCRE CALLOUTS" |
| 5 | .rs |
| 6 | .sp |
| 7 | .B int (*pcre_callout)(pcre_callout_block *); |
| 8 | .PP |
| 9 | PCRE provides a feature called "callout", which is a means of temporarily |
| 10 | passing control to the caller of PCRE in the middle of pattern matching. The |
| 11 | caller of PCRE provides an external function by putting its entry point in the |
| 12 | global variable \fIpcre_callout\fP. By default, this variable contains NULL, |
| 13 | which disables all calling out. |
| 14 | .P |
| 15 | Within a regular expression, (?C) indicates the points at which the external |
| 16 | function is to be called. Different callout points can be identified by putting |
| 17 | a number less than 256 after the letter C. The default value is zero. |
| 18 | For example, this pattern has two callout points: |
| 19 | .sp |
| 20 | (?C1)abc(?C2)def |
| 21 | .sp |
| 22 | If the PCRE_AUTO_CALLOUT option bit is set when \fBpcre_compile()\fP or |
| 23 | \fBpcre_compile2()\fP is called, PCRE automatically inserts callouts, all with |
| 24 | number 255, before each item in the pattern. For example, if PCRE_AUTO_CALLOUT |
| 25 | is used with the pattern |
| 26 | .sp |
| 27 | A(\ed{2}|--) |
| 28 | .sp |
| 29 | it is processed as if it were |
| 30 | .sp |
| 31 | (?C255)A(?C255)((?C255)\ed{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255) |
| 32 | .sp |
| 33 | Notice that there is a callout before and after each parenthesis and |
| 34 | alternation bar. Automatic callouts can be used for tracking the progress of |
| 35 | pattern matching. The |
| 36 | .\" HREF |
| 37 | \fBpcretest\fP |
| 38 | .\" |
| 39 | command has an option that sets automatic callouts; when it is used, the output |
| 40 | indicates how the pattern is matched. This is useful information when you are |
| 41 | trying to optimize the performance of a particular pattern. |
| 42 | .P |
| 43 | The use of callouts in a pattern makes it ineligible for optimization by the |
| 44 | just-in-time compiler. Studying such a pattern with the PCRE_STUDY_JIT_COMPILE |
| 45 | option always fails. |
| 46 | . |
| 47 | . |
| 48 | .SH "MISSING CALLOUTS" |
| 49 | .rs |
| 50 | .sp |
| 51 | You should be aware that, because of optimizations in the way PCRE matches |
| 52 | patterns by default, callouts sometimes do not happen. For example, if the |
| 53 | pattern is |
| 54 | .sp |
| 55 | ab(?C4)cd |
| 56 | .sp |
| 57 | PCRE knows that any matching string must contain the letter "d". If the subject |
| 58 | string is "abyz", the lack of "d" means that matching doesn't ever start, and |
| 59 | the callout is never reached. However, with "abyd", though the result is still |
| 60 | no match, the callout is obeyed. |
| 61 | .P |
| 62 | If the pattern is studied, PCRE knows the minimum length of a matching string, |
| 63 | and will immediately give a "no match" return without actually running a match |
| 64 | if the subject is not long enough, or, for unanchored patterns, if it has |
| 65 | been scanned far enough. |
| 66 | .P |
| 67 | You can disable these optimizations by passing the PCRE_NO_START_OPTIMIZE |
| 68 | option to \fBpcre_compile()\fP, \fBpcre_exec()\fP, or \fBpcre_dfa_exec()\fP, |
| 69 | or by starting the pattern with (*NO_START_OPT). This slows down the matching |
| 70 | process, but does ensure that callouts such as the example above are obeyed. |
| 71 | . |
| 72 | . |
| 73 | .SH "THE CALLOUT INTERFACE" |
| 74 | .rs |
| 75 | .sp |
| 76 | During matching, when PCRE reaches a callout point, the external function |
| 77 | defined by \fIpcre_callout\fP is called (if it is set). This applies to both |
| 78 | the \fBpcre_exec()\fP and the \fBpcre_dfa_exec()\fP matching functions. The |
| 79 | only argument to the callout function is a pointer to a \fBpcre_callout\fP |
| 80 | block. This structure contains the following fields: |
| 81 | .sp |
| 82 | int \fIversion\fP; |
| 83 | int \fIcallout_number\fP; |
| 84 | int *\fIoffset_vector\fP; |
| 85 | const char *\fIsubject\fP; |
| 86 | int \fIsubject_length\fP; |
| 87 | int \fIstart_match\fP; |
| 88 | int \fIcurrent_position\fP; |
| 89 | int \fIcapture_top\fP; |
| 90 | int \fIcapture_last\fP; |
| 91 | void *\fIcallout_data\fP; |
| 92 | int \fIpattern_position\fP; |
| 93 | int \fInext_item_length\fP; |
| 94 | const unsigned char *\fImark\fP; |
| 95 | .sp |
| 96 | The \fIversion\fP field is an integer containing the version number of the |
| 97 | block format. The initial version was 0; the current version is 2. The version |
| 98 | number will change again in future if additional fields are added, but the |
| 99 | intention is never to remove any of the existing fields. |
| 100 | .P |
| 101 | The \fIcallout_number\fP field contains the number of the callout, as compiled |
| 102 | into the pattern (that is, the number after ?C for manual callouts, and 255 for |
| 103 | automatically generated callouts). |
| 104 | .P |
| 105 | The \fIoffset_vector\fP field is a pointer to the vector of offsets that was |
| 106 | passed by the caller to \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP. When |
| 107 | \fBpcre_exec()\fP is used, the contents can be inspected in order to extract |
| 108 | substrings that have been matched so far, in the same way as for extracting |
| 109 | substrings after a match has completed. For \fBpcre_dfa_exec()\fP this field is |
| 110 | not useful. |
| 111 | .P |
| 112 | The \fIsubject\fP and \fIsubject_length\fP fields contain copies of the values |
| 113 | that were passed to \fBpcre_exec()\fP. |
| 114 | .P |
| 115 | The \fIstart_match\fP field normally contains the offset within the subject at |
| 116 | which the current match attempt started. However, if the escape sequence \eK |
| 117 | has been encountered, this value is changed to reflect the modified starting |
| 118 | point. If the pattern is not anchored, the callout function may be called |
| 119 | several times from the same point in the pattern for different starting points |
| 120 | in the subject. |
| 121 | .P |
| 122 | The \fIcurrent_position\fP field contains the offset within the subject of the |
| 123 | current match pointer. |
| 124 | .P |
| 125 | When the \fBpcre_exec()\fP function is used, the \fIcapture_top\fP field |
| 126 | contains one more than the number of the highest numbered captured substring so |
| 127 | far. If no substrings have been captured, the value of \fIcapture_top\fP is |
| 128 | one. This is always the case when \fBpcre_dfa_exec()\fP is used, because it |
| 129 | does not support captured substrings. |
| 130 | .P |
| 131 | The \fIcapture_last\fP field contains the number of the most recently captured |
| 132 | substring. If no substrings have been captured, its value is -1. This is always |
| 133 | the case when \fBpcre_dfa_exec()\fP is used. |
| 134 | .P |
| 135 | The \fIcallout_data\fP field contains a value that is passed to |
| 136 | \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP specifically so that it can be |
| 137 | passed back in callouts. It is passed in the \fIpcre_callout\fP field of the |
| 138 | \fBpcre_extra\fP data structure. If no such data was passed, the value of |
| 139 | \fIcallout_data\fP in a \fBpcre_callout\fP block is NULL. There is a |
| 140 | description of the \fBpcre_extra\fP structure in the |
| 141 | .\" HREF |
| 142 | \fBpcreapi\fP |
| 143 | .\" |
| 144 | documentation. |
| 145 | .P |
| 146 | The \fIpattern_position\fP field is present from version 1 of the |
| 147 | \fIpcre_callout\fP structure. It contains the offset to the next item to be |
| 148 | matched in the pattern string. |
| 149 | .P |
| 150 | The \fInext_item_length\fP field is present from version 1 of the |
| 151 | \fIpcre_callout\fP structure. It contains the length of the next item to be |
| 152 | matched in the pattern string. When the callout immediately precedes an |
| 153 | alternation bar, a closing parenthesis, or the end of the pattern, the length |
| 154 | is zero. When the callout precedes an opening parenthesis, the length is that |
| 155 | of the entire subpattern. |
| 156 | .P |
| 157 | The \fIpattern_position\fP and \fInext_item_length\fP fields are intended to |
| 158 | help in distinguishing between different automatic callouts, which all have the |
| 159 | same callout number. However, they are set for all callouts. |
| 160 | .P |
| 161 | The \fImark\fP field is present from version 2 of the \fIpcre_callout\fP |
| 162 | structure. In callouts from \fBpcre_exec()\fP it contains a pointer to the |
| 163 | zero-terminated name of the most recently passed (*MARK), (*PRUNE), or (*THEN) |
| 164 | item in the match, or NULL if no such items have been passed. Instances of |
| 165 | (*PRUNE) or (*THEN) without a name do not obliterate a previous (*MARK). In |
| 166 | callouts from \fBpcre_dfa_exec()\fP this field always contains NULL. |
| 167 | . |
| 168 | . |
| 169 | .SH "RETURN VALUES" |
| 170 | .rs |
| 171 | .sp |
| 172 | The external callout function returns an integer to PCRE. If the value is zero, |
| 173 | matching proceeds as normal. If the value is greater than zero, matching fails |
| 174 | at the current point, but the testing of other matching possibilities goes |
| 175 | ahead, just as if a lookahead assertion had failed. If the value is less than |
| 176 | zero, the match is abandoned, and \fBpcre_exec()\fP or \fBpcre_dfa_exec()\fP |
| 177 | returns the negative value. |
| 178 | .P |
| 179 | Negative values should normally be chosen from the set of PCRE_ERROR_xxx |
| 180 | values. In particular, PCRE_ERROR_NOMATCH forces a standard "no match" failure. |
| 181 | The error number PCRE_ERROR_CALLOUT is reserved for use by callout functions; |
| 182 | it will never be used by PCRE itself. |
| 183 | . |
| 184 | . |
| 185 | .SH AUTHOR |
| 186 | .rs |
| 187 | .sp |
| 188 | .nf |
| 189 | Philip Hazel |
| 190 | University Computing Service |
| 191 | Cambridge CB2 3QH, England. |
| 192 | .fi |
| 193 | . |
| 194 | . |
| 195 | .SH REVISION |
| 196 | .rs |
| 197 | .sp |
| 198 | .nf |
| 199 | Last updated: 30 November 2011 |
| 200 | Copyright (c) 1997-2011 University of Cambridge. |
| 201 | .fi |