317 lines
13 KiB
HTML
317 lines
13 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PCRECALLOUT</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PCRECALLOUT</H1>
|
|
Section: C Library Functions (3)<BR>Updated: 12 November 2013<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
PCRE - Perl-compatible regular expressions
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
|
|
<P>
|
|
<B>#include <<A HREF="file:///usr/include/pcre.h">pcre.h</A>></B>
|
|
|
|
<P>
|
|
|
|
<FONT SIZE="-1"><B>int (*pcre_callout)(pcre_callout_block *);</B>
|
|
|
|
</FONT>
|
|
<P>
|
|
|
|
<B>int (*pcre16_callout)(pcre16_callout_block *);</B>
|
|
|
|
<P>
|
|
|
|
<B>int (*pcre32_callout)(pcre32_callout_block *);</B>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
|
|
<P>
|
|
PCRE provides a feature called "callout", which is a means of temporarily
|
|
passing control to the caller of PCRE in the middle of pattern matching. The
|
|
caller of PCRE provides an external function by putting its entry point in the
|
|
global variable <I>pcre_callout</I> (<I>pcre16_callout</I> for the 16-bit
|
|
library, <I>pcre32_callout</I> for the 32-bit library). By default, this
|
|
variable contains NULL, which disables all calling out.
|
|
<P>
|
|
|
|
Within a regular expression, (?C) indicates the points at which the external
|
|
function is to be called. Different callout points can be identified by putting
|
|
a number less than 256 after the letter C. The default value is zero.
|
|
For example, this pattern has two callout points:
|
|
<P>
|
|
<BR> (?C1)abc(?C2)def
|
|
<P>
|
|
If the PCRE_AUTO_CALLOUT option bit is set when a pattern is compiled, PCRE
|
|
automatically inserts callouts, all with number 255, before each item in the
|
|
pattern. For example, if PCRE_AUTO_CALLOUT is used with the pattern
|
|
<P>
|
|
<BR> A(\d{2}|--)
|
|
<P>
|
|
it is processed as if it were
|
|
<P>
|
|
(?C255)A(?C255)((?C255)\d{2}(?C255)|(?C255)-(?C255)-(?C255))(?C255)
|
|
<P>
|
|
Notice that there is a callout before and after each parenthesis and
|
|
alternation bar. If the pattern contains a conditional group whose condition is
|
|
an assertion, an automatic callout is inserted immediately before the
|
|
condition. Such a callout may also be inserted explicitly, for example:
|
|
<P>
|
|
<BR> (?(?C9)(?=a)ab|de)
|
|
<P>
|
|
This applies only to assertion conditions (because they are themselves
|
|
independent groups).
|
|
<P>
|
|
|
|
Automatic callouts can be used for tracking the progress of pattern matching.
|
|
The
|
|
|
|
<B>pcretest</B>
|
|
|
|
program has a pattern qualifier (/C) that sets automatic callouts; when it is
|
|
used, the output indicates how the pattern is being matched. This is useful
|
|
information when you are trying to optimize the performance of a particular
|
|
pattern.
|
|
<A NAME="lbAE"> </A>
|
|
<H2>MISSING CALLOUTS</H2>
|
|
|
|
|
|
<P>
|
|
You should be aware that, because of optimizations in the way PCRE compiles and
|
|
matches patterns, callouts sometimes do not happen exactly as you might expect.
|
|
<P>
|
|
|
|
At compile time, PCRE "auto-possessifies" repeated items when it knows that
|
|
what follows cannot be part of the repeat. For example, a+[bc] is compiled as
|
|
if it were a++[bc]. The <B>pcretest</B> output when this pattern is anchored and
|
|
then applied with automatic callouts to the string "aaaa" is:
|
|
<P>
|
|
<BR> --->aaaa
|
|
<BR> +0 ^ ^
|
|
<BR> +1 ^ a+
|
|
<BR> +3 ^ ^ [bc]
|
|
<BR> No match
|
|
<P>
|
|
This indicates that when matching [bc] fails, there is no backtracking into a+
|
|
and therefore the callouts that would be taken for the backtracks do not occur.
|
|
You can disable the auto-possessify feature by passing PCRE_NO_AUTO_POSSESS
|
|
to <B>pcre_compile()</B>, or starting the pattern with (*NO_AUTO_POSSESS). If
|
|
this is done in <B>pcretest</B> (using the /O qualifier), the output changes to
|
|
this:
|
|
<P>
|
|
<BR> --->aaaa
|
|
<BR> +0 ^ ^
|
|
<BR> +1 ^ a+
|
|
<BR> +3 ^ ^ [bc]
|
|
<BR> +3 ^ ^ [bc]
|
|
<BR> +3 ^ ^ [bc]
|
|
<BR> +3 ^^ [bc]
|
|
<BR> No match
|
|
<P>
|
|
This time, when matching [bc] fails, the matcher backtracks into a+ and tries
|
|
again, repeatedly, until a+ itself fails.
|
|
<P>
|
|
|
|
Other optimizations that provide fast "no match" results also affect callouts.
|
|
For example, if the pattern is
|
|
<P>
|
|
<BR> ab(?C4)cd
|
|
<P>
|
|
PCRE knows that any matching string must contain the letter "d". If the subject
|
|
string is "abyz", the lack of "d" means that matching doesn't ever start, and
|
|
the callout is never reached. However, with "abyd", though the result is still
|
|
no match, the callout is obeyed.
|
|
<P>
|
|
|
|
If the pattern is studied, PCRE knows the minimum length of a matching string,
|
|
and will immediately give a "no match" return without actually running a match
|
|
if the subject is not long enough, or, for unanchored patterns, if it has
|
|
been scanned far enough.
|
|
<P>
|
|
|
|
You can disable these optimizations by passing the PCRE_NO_START_OPTIMIZE
|
|
option to the matching function, or by starting the pattern with
|
|
(*NO_START_OPT). This slows down the matching process, but does ensure that
|
|
callouts such as the example above are obeyed.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>THE CALLOUT INTERFACE</H2>
|
|
|
|
|
|
<P>
|
|
During matching, when PCRE reaches a callout point, the external function
|
|
defined by <I>pcre_callout</I> or <I>pcre[16|32]_callout</I> is called (if it is
|
|
set). This applies to both normal and DFA matching. The only argument to the
|
|
callout function is a pointer to a <B>pcre_callout</B> or
|
|
<B>pcre[16|32]_callout</B> block. These structures contains the following
|
|
fields:
|
|
<P>
|
|
<BR> int <I>version</I>;
|
|
<BR> int <I>callout_number</I>;
|
|
<BR> int *<I>offset_vector</I>;
|
|
<BR> const char *<I>subject</I>; (8-bit version)
|
|
<BR> PCRE_SPTR16 <I>subject</I>; (16-bit version)
|
|
<BR> PCRE_SPTR32 <I>subject</I>; (32-bit version)
|
|
<BR> int <I>subject_length</I>;
|
|
<BR> int <I>start_match</I>;
|
|
<BR> int <I>current_position</I>;
|
|
<BR> int <I>capture_top</I>;
|
|
<BR> int <I>capture_last</I>;
|
|
<BR> void *<I>callout_data</I>;
|
|
<BR> int <I>pattern_position</I>;
|
|
<BR> int <I>next_item_length</I>;
|
|
<BR> const unsigned char *<I>mark</I>; (8-bit version)
|
|
<BR> const PCRE_UCHAR16 *<I>mark</I>; (16-bit version)
|
|
<BR> const PCRE_UCHAR32 *<I>mark</I>; (32-bit version)
|
|
<P>
|
|
The <I>version</I> field is an integer containing the version number of the
|
|
block format. The initial version was 0; the current version is 2. The version
|
|
number will change again in future if additional fields are added, but the
|
|
intention is never to remove any of the existing fields.
|
|
<P>
|
|
|
|
The <I>callout_number</I> field contains the number of the callout, as compiled
|
|
into the pattern (that is, the number after ?C for manual callouts, and 255 for
|
|
automatically generated callouts).
|
|
<P>
|
|
|
|
The <I>offset_vector</I> field is a pointer to the vector of offsets that was
|
|
passed by the caller to the matching function. When <B>pcre_exec()</B> or
|
|
<B>pcre[16|32]_exec()</B> is used, the contents can be inspected, in order to
|
|
extract substrings that have been matched so far, in the same way as for
|
|
extracting substrings after a match has completed. For the DFA matching
|
|
functions, this field is not useful.
|
|
<P>
|
|
|
|
The <I>subject</I> and <I>subject_length</I> fields contain copies of the values
|
|
that were passed to the matching function.
|
|
<P>
|
|
|
|
The <I>start_match</I> field normally contains the offset within the subject at
|
|
which the current match attempt started. However, if the escape sequence \K
|
|
has been encountered, this value is changed to reflect the modified starting
|
|
point. If the pattern is not anchored, the callout function may be called
|
|
several times from the same point in the pattern for different starting points
|
|
in the subject.
|
|
<P>
|
|
|
|
The <I>current_position</I> field contains the offset within the subject of the
|
|
current match pointer.
|
|
<P>
|
|
|
|
When the <B>pcre_exec()</B> or <B>pcre[16|32]_exec()</B> is used, the
|
|
<I>capture_top</I> field contains one more than the number of the highest
|
|
numbered captured substring so far. If no substrings have been captured, the
|
|
value of <I>capture_top</I> is one. This is always the case when the DFA
|
|
functions are used, because they do not support captured substrings.
|
|
<P>
|
|
|
|
The <I>capture_last</I> field contains the number of the most recently captured
|
|
substring. However, when a recursion exits, the value reverts to what it was
|
|
outside the recursion, as do the values of all captured substrings. If no
|
|
substrings have been captured, the value of <I>capture_last</I> is -1. This is
|
|
always the case for the DFA matching functions.
|
|
<P>
|
|
|
|
The <I>callout_data</I> field contains a value that is passed to a matching
|
|
function specifically so that it can be passed back in callouts. It is passed
|
|
in the <I>callout_data</I> field of a <B>pcre_extra</B> or <B>pcre[16|32]_extra</B>
|
|
data structure. If no such data was passed, the value of <I>callout_data</I> in
|
|
a callout block is NULL. There is a description of the <B>pcre_extra</B>
|
|
structure in the
|
|
|
|
<B>pcreapi</B>
|
|
|
|
documentation.
|
|
<P>
|
|
|
|
The <I>pattern_position</I> field is present from version 1 of the callout
|
|
structure. It contains the offset to the next item to be matched in the pattern
|
|
string.
|
|
<P>
|
|
|
|
The <I>next_item_length</I> field is present from version 1 of the callout
|
|
structure. It contains the length of the next item to be matched in the pattern
|
|
string. When the callout immediately precedes an alternation bar, a closing
|
|
parenthesis, or the end of the pattern, the length is zero. When the callout
|
|
precedes an opening parenthesis, the length is that of the entire subpattern.
|
|
<P>
|
|
|
|
The <I>pattern_position</I> and <I>next_item_length</I> fields are intended to
|
|
help in distinguishing between different automatic callouts, which all have the
|
|
same callout number. However, they are set for all callouts.
|
|
<P>
|
|
|
|
The <I>mark</I> field is present from version 2 of the callout structure. In
|
|
callouts from <B>pcre_exec()</B> or <B>pcre[16|32]_exec()</B> it contains a
|
|
pointer to the zero-terminated name of the most recently passed (*MARK),
|
|
(*PRUNE), or (*THEN) item in the match, or NULL if no such items have been
|
|
passed. Instances of (*PRUNE) or (*THEN) without a name do not obliterate a
|
|
previous (*MARK). In callouts from the DFA matching functions this field always
|
|
contains NULL.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>RETURN VALUES</H2>
|
|
|
|
|
|
<P>
|
|
The external callout function returns an integer to PCRE. If the value is zero,
|
|
matching proceeds as normal. If the value is greater than zero, matching fails
|
|
at the current point, but the testing of other matching possibilities goes
|
|
ahead, just as if a lookahead assertion had failed. If the value is less than
|
|
zero, the match is abandoned, the matching function returns the negative value.
|
|
<P>
|
|
|
|
Negative values should normally be chosen from the set of PCRE_ERROR_xxx
|
|
values. In particular, PCRE_ERROR_NOMATCH forces a standard "no match" failure.
|
|
The error number PCRE_ERROR_CALLOUT is reserved for use by callout functions;
|
|
it will never be used by PCRE itself.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>AUTHOR</H2>
|
|
|
|
|
|
<P>
|
|
<PRE>
|
|
Philip Hazel
|
|
University Computing Service
|
|
Cambridge CB2 3QH, England.
|
|
</PRE>
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>REVISION</H2>
|
|
|
|
|
|
<P>
|
|
<PRE>
|
|
Last updated: 12 November 2013
|
|
Copyright (c) 1997-2013 University of Cambridge.
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="4"><A HREF="#lbAE">MISSING CALLOUTS</A><DD>
|
|
<DT id="5"><A HREF="#lbAF">THE CALLOUT INTERFACE</A><DD>
|
|
<DT id="6"><A HREF="#lbAG">RETURN VALUES</A><DD>
|
|
<DT id="7"><A HREF="#lbAH">AUTHOR</A><DD>
|
|
<DT id="8"><A HREF="#lbAI">REVISION</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:51 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|