pcrecallout.3 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. .TH PCRECALLOUT 3 "12 November 2013" "PCRE 8.34"
  2. .SH NAME
  3. PCRE - Perl-compatible regular expressions
  4. .SH SYNOPSIS
  5. .rs
  6. .sp
  7. .B #include <pcre.h>
  8. .PP
  9. .SM
  10. .B int (*pcre_callout)(pcre_callout_block *);
  11. .PP
  12. .B int (*pcre16_callout)(pcre16_callout_block *);
  13. .PP
  14. .B int (*pcre32_callout)(pcre32_callout_block *);
  15. .
  16. .SH DESCRIPTION
  17. .rs
  18. .sp
  19. PCRE provides a feature called "callout", which is a means of temporarily
  20. passing control to the caller of PCRE in the middle of pattern matching. The
  21. caller of PCRE provides an external function by putting its entry point in the
  22. global variable \fIpcre_callout\fP (\fIpcre16_callout\fP for the 16-bit
  23. library, \fIpcre32_callout\fP for the 32-bit library). By default, this
  24. variable contains NULL, which disables all calling out.
  25. .P
  26. Within a regular expression, (?C) indicates the points at which the external
  27. function is to be called. Different callout points can be identified by putting
  28. a number less than 256 after the letter C. The default value is zero.
  29. For example, this pattern has two callout points:
  30. .sp
  31. (?C1)abc(?C2)def
  32. .sp
  33. If the PCRE_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE
  34. automatically inserts callouts, all with number 255, before each item in the
  35. pattern. For example, if PCRE_AUTO_CALLOUT is used with the pattern
  36. .sp
  37. A(\ed{2}|--)
  38. .sp
  39. it is processed as if it were
  40. .sp
  41. (?C255)A(?C255)((?C255)\ed{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
  42. .sp
  43. Notice that there is a callout before and after each parenthesis and
  44. alternation bar. If the pattern contains a conditional group whose condition is
  45. an assertion, an automatic callout is inserted immediately before the
  46. condition. Such a callout may also be inserted explicitly, for example:
  47. .sp
  48. (?(?C9)(?=a)ab|de)
  49. .sp
  50. This applies only to assertion conditions (because they are themselves
  51. independent groups).
  52. .P
  53. Automatic callouts can be used for tracking the progress of pattern matching.
  54. The
  55. .\" HREF
  56. \fBpcretest\fP
  57. .\"
  58. program has a pattern qualifier (/C) that sets automatic callouts; when it is
  59. used, the output indicates how the pattern is being matched. This is useful
  60. information when you are trying to optimize the performance of a particular
  61. pattern.
  62. .
  63. .
  64. .SH "MISSING CALLOUTS"
  65. .rs
  66. .sp
  67. You should be aware that, because of optimizations in the way PCRE compiles and
  68. matches patterns, callouts sometimes do not happen exactly as you might expect.
  69. .P
  70. At compile time, PCRE "auto-possessifies" repeated items when it knows that
  71. what follows cannot be part of the repeat. For example, a+[bc] is compiled as
  72. if it were a++[bc]. The \fBpcretest\fP output when this pattern is anchored and
  73. then applied with automatic callouts to the string "aaaa" is:
  74. .sp
  75. --->aaaa
  76. +0 ^ ^
  77. +1 ^ a+
  78. +3 ^ ^ [bc]
  79. No match
  80. .sp
  81. This indicates that when matching [bc] fails, there is no backtracking into a+
  82. and therefore the callouts that would be taken for the backtracks do not occur.
  83. You can disable the auto-possessify feature by passing PCRE_NO_AUTO_POSSESS
  84. to \fBpcre_compile()\fP, or starting the pattern with (*NO_AUTO_POSSESS). If
  85. this is done in \fBpcretest\fP (using the /O qualifier), the output changes to
  86. this:
  87. .sp
  88. --->aaaa
  89. +0 ^ ^
  90. +1 ^ a+
  91. +3 ^ ^ [bc]
  92. +3 ^ ^ [bc]
  93. +3 ^ ^ [bc]
  94. +3 ^^ [bc]
  95. No match
  96. .sp
  97. This time, when matching [bc] fails, the matcher backtracks into a+ and tries
  98. again, repeatedly, until a+ itself fails.
  99. .P
  100. Other optimizations that provide fast "no match" results also affect callouts.
  101. For example, if the pattern is
  102. .sp
  103. ab(?C4)cd
  104. .sp
  105. PCRE knows that any matching string must contain the letter "d". If the subject
  106. string is "abyz", the lack of "d" means that matching doesn't ever start, and
  107. the callout is never reached. However, with "abyd", though the result is still
  108. no match, the callout is obeyed.
  109. .P
  110. If the pattern is studied, PCRE knows the minimum length of a matching string,
  111. and will immediately give a "no match" return without actually running a match
  112. if the subject is not long enough, or, for unanchored patterns, if it has
  113. been scanned far enough.
  114. .P
  115. You can disable these optimizations by passing the PCRE_NO_START_OPTIMIZE
  116. option to the matching function, or by starting the pattern with
  117. (*NO_START_OPT). This slows down the matching process, but does ensure that
  118. callouts such as the example above are obeyed.
  119. .
  120. .
  121. .SH "THE CALLOUT INTERFACE"
  122. .rs
  123. .sp
  124. During matching, when PCRE reaches a callout point, the external function
  125. defined by \fIpcre_callout\fP or \fIpcre[16|32]_callout\fP is called (if it is
  126. set). This applies to both normal and DFA matching. The only argument to the
  127. callout function is a pointer to a \fBpcre_callout\fP or
  128. \fBpcre[16|32]_callout\fP block. These structures contains the following
  129. fields:
  130. .sp
  131. int \fIversion\fP;
  132. int \fIcallout_number\fP;
  133. int *\fIoffset_vector\fP;
  134. const char *\fIsubject\fP; (8-bit version)
  135. PCRE_SPTR16 \fIsubject\fP; (16-bit version)
  136. PCRE_SPTR32 \fIsubject\fP; (32-bit version)
  137. int \fIsubject_length\fP;
  138. int \fIstart_match\fP;
  139. int \fIcurrent_position\fP;
  140. int \fIcapture_top\fP;
  141. int \fIcapture_last\fP;
  142. void *\fIcallout_data\fP;
  143. int \fIpattern_position\fP;
  144. int \fInext_item_length\fP;
  145. const unsigned char *\fImark\fP; (8-bit version)
  146. const PCRE_UCHAR16 *\fImark\fP; (16-bit version)
  147. const PCRE_UCHAR32 *\fImark\fP; (32-bit version)
  148. .sp
  149. The \fIversion\fP field is an integer containing the version number of the
  150. block format. The initial version was 0; the current version is 2. The version
  151. number will change again in future if additional fields are added, but the
  152. intention is never to remove any of the existing fields.
  153. .P
  154. The \fIcallout_number\fP field contains the number of the callout, as compiled
  155. into the pattern (that is, the number after ?C for manual callouts, and 255 for
  156. automatically generated callouts).
  157. .P
  158. The \fIoffset_vector\fP field is a pointer to the vector of offsets that was
  159. passed by the caller to the matching function. When \fBpcre_exec()\fP or
  160. \fBpcre[16|32]_exec()\fP is used, the contents can be inspected, in order to
  161. extract substrings that have been matched so far, in the same way as for
  162. extracting substrings after a match has completed. For the DFA matching
  163. functions, this field is not useful.
  164. .P
  165. The \fIsubject\fP and \fIsubject_length\fP fields contain copies of the values
  166. that were passed to the matching function.
  167. .P
  168. The \fIstart_match\fP field normally contains the offset within the subject at
  169. which the current match attempt started. However, if the escape sequence \eK
  170. has been encountered, this value is changed to reflect the modified starting
  171. point. If the pattern is not anchored, the callout function may be called
  172. several times from the same point in the pattern for different starting points
  173. in the subject.
  174. .P
  175. The \fIcurrent_position\fP field contains the offset within the subject of the
  176. current match pointer.
  177. .P
  178. When the \fBpcre_exec()\fP or \fBpcre[16|32]_exec()\fP is used, the
  179. \fIcapture_top\fP field contains one more than the number of the highest
  180. numbered captured substring so far. If no substrings have been captured, the
  181. value of \fIcapture_top\fP is one. This is always the case when the DFA
  182. functions are used, because they do not support captured substrings.
  183. .P
  184. The \fIcapture_last\fP field contains the number of the most recently captured
  185. substring. However, when a recursion exits, the value reverts to what it was
  186. outside the recursion, as do the values of all captured substrings. If no
  187. substrings have been captured, the value of \fIcapture_last\fP is -1. This is
  188. always the case for the DFA matching functions.
  189. .P
  190. The \fIcallout_data\fP field contains a value that is passed to a matching
  191. function specifically so that it can be passed back in callouts. It is passed
  192. in the \fIcallout_data\fP field of a \fBpcre_extra\fP or \fBpcre[16|32]_extra\fP
  193. data structure. If no such data was passed, the value of \fIcallout_data\fP in
  194. a callout block is NULL. There is a description of the \fBpcre_extra\fP
  195. structure in the
  196. .\" HREF
  197. \fBpcreapi\fP
  198. .\"
  199. documentation.
  200. .P
  201. The \fIpattern_position\fP field is present from version 1 of the callout
  202. structure. It contains the offset to the next item to be matched in the pattern
  203. string.
  204. .P
  205. The \fInext_item_length\fP field is present from version 1 of the callout
  206. structure. It contains the length of the next item to be matched in the pattern
  207. string. When the callout immediately precedes an alternation bar, a closing
  208. parenthesis, or the end of the pattern, the length is zero. When the callout
  209. precedes an opening parenthesis, the length is that of the entire subpattern.
  210. .P
  211. The \fIpattern_position\fP and \fInext_item_length\fP fields are intended to
  212. help in distinguishing between different automatic callouts, which all have the
  213. same callout number. However, they are set for all callouts.
  214. .P
  215. The \fImark\fP field is present from version 2 of the callout structure. In
  216. callouts from \fBpcre_exec()\fP or \fBpcre[16|32]_exec()\fP it contains a
  217. pointer to the zero-terminated name of the most recently passed (*MARK),
  218. (*PRUNE), or (*THEN) item in the match, or NULL if no such items have been
  219. passed. Instances of (*PRUNE) or (*THEN) without a name do not obliterate a
  220. previous (*MARK). In callouts from the DFA matching functions this field always
  221. contains NULL.
  222. .
  223. .
  224. .SH "RETURN VALUES"
  225. .rs
  226. .sp
  227. The external callout function returns an integer to PCRE. If the value is zero,
  228. matching proceeds as normal. If the value is greater than zero, matching fails
  229. at the current point, but the testing of other matching possibilities goes
  230. ahead, just as if a lookahead assertion had failed. If the value is less than
  231. zero, the match is abandoned, the matching function returns the negative value.
  232. .P
  233. Negative values should normally be chosen from the set of PCRE_ERROR_xxx
  234. values. In particular, PCRE_ERROR_NOMATCH forces a standard "no match" failure.
  235. The error number PCRE_ERROR_CALLOUT is reserved for use by callout functions;
  236. it will never be used by PCRE itself.
  237. .
  238. .
  239. .SH AUTHOR
  240. .rs
  241. .sp
  242. .nf
  243. Philip Hazel
  244. University Computing Service
  245. Cambridge CB2 3QH, England.
  246. .fi
  247. .
  248. .
  249. .SH REVISION
  250. .rs
  251. .sp
  252. .nf
  253. Last updated: 12 November 2013
  254. Copyright (c) 1997-2013 University of Cambridge.
  255. .fi