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

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

  ViewVC Help
Powered by ViewVC 1.1.5