/[pcre]/code/trunk/doc/pcrecallout.3
ViewVC logotype

Diff of /code/trunk/doc/pcrecallout.3

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.63  
changed lines
  Added in v.903

  ViewVC Help
Powered by ViewVC 1.1.5