1 .\" Copyright (c) 2001 Andreas S. Oesterhelt <oes@oesterhelt.org>
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
23 .TH PCRS 3 "17 August 2001"
25 pcrs - Perl-compatible regular substitution.
28 .BI "#include <pcrs.h>"
31 .BI "pcrs_job *pcrs_compile(const char *" "pattern" ","
33 .BI "const char *" "substitute" ", const char *" "options" ","
35 .BI "int *" "errptr" ");"
38 .BI "pcrs_job *pcrs_compile_command(const char *" "command" ","
40 .BI "int *" "errptr");
43 .BI "int pcrs_execute(pcrs_job *" "job" ", char *" "subject" ","
45 .BI "int " "subject_length" ", char **" "result" ","
47 .BI "int *" "result_length" ");"
50 .BI "int pcrs_execute_list (pcrs_job *" "joblist" ", char *" "subject" ","
52 .BI "int " "subject_length" ", char **" "result" ","
54 .BI "int *" "result_length" ");"
57 .BI "pcrs_job *pcrs_free_job(pcrs_job *" "job" ");"
60 .BI "void pcrs_free_joblist(pcrs_job *" "joblist" ");"
63 .BI "char *pcrs_strerror(int " "err" ");"
70 library is a supplement to the
72 library that implements
73 .RB "regular expression based substitution, like provided by " "Perl(1)" "'s 's'"
74 operator. It uses the same syntax and semantics as Perl 5, with just a few
75 differences (see below).
77 In a first step, the information on a substitution, i.e. the pattern, the
78 substitute and the options are compiled from Perl syntax to an internal form
79 .RB "called " "pcrs_job" " by using either the " "pcrs_compile()" " or "
80 .BR "pcrs_compile_command()" " functions."
82 Once the job is compiled, it can be used on subjects, which are arbitrary
83 memory areas containing string or binary data, by calling
84 .BR "pcrs_execute()" ". Jobs can be chained to joblists and whole"
85 .RB "joblists can be applied to a subject using " "pcrs_execute_list()" "."
87 There are also convenience functions for freeing the jobs and for errno-to-string
88 .RB "conversion, namely " "pcrs_free_job()" ", " "pcrs_free_joblist()" " and "
89 .BR "pcrs_strerror()" "."
92 .RB "The function " "pcrs_compile()" " is called to compile a " "pcrs_job"
93 .RI "from a " "pattern" ", " "substitute" " and " "options" " string.
94 .RB "The resulting " "pcrs_job" " structure is dynamically allocated and it"
95 .RB "is the caller's responsibility to " "free()" " it when it's no longer needed."
97 .BR "pcrs_compile_command()" " is a convenience wrapper function that parses a Perl"
98 .IR "command" " of the form"
99 .BI "s/" "pattern" "/" "substitute" "/[" "options" "]"
100 .RB "into its components and then calls " "pcrs_compile()" ". As in Perl, you"
101 .RB "are not bound to the '" "/" "' character: Whatever"
102 .RB "follows the '" "s" "' will be used as the delimiter. Patterns or substitutes"
103 that contain the delimiter need to quote it:
104 \fBs/th\\/is/th\\/at/\fR
105 .RB "will replace " "th/is" " by " "th/at" " and can be written more simply as"
106 .BR "s|th/is|th/at|" "."
108 .IR "pattern" ", " "substitute" ", " "options" " and " "command" " must be"
109 .RI "zero-terminated C strings. " "substitute" " and " "options" " may be"
110 .BR "NULL" ", in which case they are treated like the empty string."
112 .SS "Return value and diagnostics"
113 On success, both functions return a pointer to the compiled job.
114 .RB "On failure, " "NULL"
115 .RI "is returned. In that case, the pcrs error code is written to *" "err" "."
118 .RI "For the syntax of the " "pattern" ", see the "
119 .BR "PCRE(3)" " manual page."
122 .RI "The " "substitute" " uses"
123 .RB "Perl syntax as documented in the " "perlre(1)" " manual page, with"
126 Most notably and evidently, since
128 is not Perl, variable interpolation or Perl command substitution won't work.
129 Special variables that do get interpolated, are:
132 Like in Perl, these variables refer to what the nth capturing subpattern
133 in the pattern matched.
136 .RB "refer to the whole match. Note that " "$0" " is deprecated in recent"
137 Perl versions and now refers to the program name.
140 refers to what the last capturing subpattern matched.
142 .BR "$` and $'" " (backtick and tick)"
143 .RI "refer to the areas of the " "subject" " before and after the match, respectively."
144 .RB "Note that, like in Perl, the " "unmodified" " subject is used, even"
145 if a global substitution previously matched.
148 Perl4-style references to subpattern matches of the form
150 .RB "which only exist in Perl5 for backwards compatibility, are " "not"
153 Also, since the substitute is a double-quoted string in Perl, you
154 might expect all Perl syntax for special characters to apply. In fact,
155 only the following are supported:
162 carriage return (0x0d)
165 horizontal tab (0x09)
183 .RB "The options " "gmisx" " are supported. " "e" " is not, since it would"
184 .RB "require a Perl interpreter and neither is " o ", because the pattern
185 is explicitly compiled, anyway. Additionally,
187 .RB "honors the options " "U" " and " "T" "."
190 .RB "options are mentioned below, refer to " PCRE(3) " for the subtle differences"
195 .RB "Replace " all " instances of"
196 .IR pattern " in " subject ,
197 not just the first one.
201 .RI "Match the " pattern " without respect to case. This translates to"
206 .RI "Treat the " subject " as consisting of multiple lines, i.e."
207 .RB ' ^ "' matches immediately after, and '" $ "' immediately before each newline."
213 .RI "Treat the " subject " as consisting of one single line, i.e."
214 .RB "let the scope of the '" . "' metacharacter include newlines."
220 .RI "Allow extended regular expression syntax in the " pattern ","
221 .RB "enabling whitespace and comments in complex patterns."
227 .RB "Switch the default behaviour of the '" * "' and '" + "' quantifiers"
228 .RB "to ungreedy. Note that appending a '" ? "' switches back to greedy(!)."
229 .RB "The explicit in-pattern switches " (?U) " and " (?-U) " remain unaffected."
235 .RI "Consider the " substitute " trivial, i.e. do not interpret any references"
236 or special character escape sequences in the substitute. Handy for large
237 user-supplied substitutes, which would otherwise have to be examined and properly
241 Unsupported options are silently ignored.
245 .RI "Calling " pcrs_execute() " produces a modified copy of the " subject ", in which"
246 .RB "the first (or all, if the '" g "' option was given when compiling the job)"
247 .RI "occurance(s) of the job's " pattern " in the " subject " is replaced by the job's"
250 .RI "The first " subject_length " bytes following " subject " are processed, so"
251 .RI "a " subject_length " that exceeds the actual " subject " is dangerous."
252 Note that if you want to get your zero-terminated C strings back including their
253 .RI "termination, you must let " subject_length " include the binary zero, i.e."
255 .BI strlen( subject ") + 1."
257 .RI "The " subject " itself is left untouched, and the " *result " is dynamically"
258 .RB "allocated, so it is the caller's responsibility to " free() " it when it's"
261 .RI "The result's length is written to " *result_length "."
263 .RB "If the job matched, the " PCRS_SUCCESS " flag in"
267 .SS Return value and diagnostics
269 .RB "On success, " pcrs_execute() " returns the number of substitutions that"
270 were made, which is limited to 0 or 1 for non-global searches.
271 .RI "On failure, a negative error code is returned and " *result " is set"
275 .RB "It is not sufficient to call " free() " on a " pcrs_job ", because it "
276 contains pointers to other dynamically allocated structures.
277 .RB "Use " pcrs_free() " instead. It is safe to pass " NULL " pointers "
278 .RB "(or pointers to invalid " pcrs_job "s that contain " NULL " pointers"
279 .RB "to dependant structures) to " pcrs_free() "."
282 .RB "The value of the job's " next " pointer."
288 .RB "supports to some extent the chaining of multiple " pcrs_job " structures by"
289 .RB "means of their " next " member."
291 Chaining the jobs is up to you, but once you have built a linked list of jobs,
292 .RI "you can execute a whole " joblist " on a given subject by"
293 .RB "a single call to " pcrs_execute_list() ", which will sequentially traverse"
294 .RB "the linked list until it reaches a " NULL " pointer, and call " pcrs_execute()
295 .RI "for each job it encounters, feeding the " result " and " result_length " of each"
296 .RI "call into the next as the " subject " and " subject_length ". As in the single"
297 .RI "job case, the original " subject " remains untouched, but all interim " result "s"
298 .RB "are of course " free() "d. The return value is the accumulated number of matches"
299 .RI "for all jobs in the " joblist "."
300 .RI "Note that while this is handy, it reduces the diagnostic value of " err ", since "
301 you won't know which job failed.
303 .RI "In analogy, you can free all jobs in a given " joblist " by calling"
304 .BR pcrs_free_joblist() .
307 The quote character is (surprise!) '\fB\\\fR'. It quotes the delimiter in a
310 .IR substitute ", and, of course, itself. Note that the"
311 .RB ' $ "'doesn't need to be quoted if it isn't followed by " [0-9+'`&] "."
313 .RI "For quoting in the " pattern ", please refer to"
318 .RB "When " compiling " a job either via the " pcrs_compile() " or " pcrs_compile_command()
319 .RB "functions, you know that something went wrong when you are returned a " NULL " pointer."
320 .RI "In that case, or in the event of non-fatal warnings, the integer pointed to by " err
321 contains a nonzero error code, which is either a passed-through
323 error code or one generated by
325 Under normal circumstances, it can take the following values:
327 .B PCRE_ERROR_NOMEMORY
328 While compiling the pattern,
333 While compiling the job,
337 .B PCRS_ERR_CMDSYNTAX
338 .BR pcrs_compile_command() " didn't find four tokens while parsing the"
344 .RB "error occured while studying the compiled pattern. Since " pcre_study()
345 only provides textual diagnostic information, the details are lost.
348 .RI "The " substitute " contains a reference to a capturing subpattern that"
349 .RI "has a higher index than the number of capturing subpatterns in the " pattern
350 or that exceeds the current hard limit of 33 (See LIMITATIONS below). As in Perl,
351 this is non-fatal and results in substitutions with the empty string.
354 .RB "When " executing " jobs via " pcrs_execute() " or " pcrs_execute_list() ","
355 .RI "a negative return code indicates an error. In that case, *" result
356 .RB "is " NULL ". Possible error codes are:"
358 .B PCRE_ERROR_NOMEMORY
359 While matching the pattern,
361 ran out of memory. This can only happen if there are more than 33 backrefrences
362 .RI "in the " pattern "(!)"
363 .BR and " memory is too tight to extend storage for more."
366 While executing the job,
371 .RB "The " pcrs_job "* passed to " pcrs_execute " was NULL, or the"
372 .RB "job is bogus (it contains " NULL " pointers to the compiled
373 pattern, extra, or substitute).
378 error code passed through, you've either messed with the compiled job
381 Please send me an email.
383 .RB "Ah, and don't look for " PCRE_ERROR_NOMATCH ", since this"
384 is not an error in the context of
386 .RI "Should there be no match, an exact copy of the " subject " is"
387 .RI "found at *" result " and the return code is 0 (matches)."
389 All error codes can be translated into human readable text by means
390 .RB "of the " pcrs_strerror() " function."
394 A trivial command-line test program for
402 int main(int Argc, char **Argv)
410 fprintf(stderr, "Usage: %s s/pattern/substitute/[options] subject\\n", Argv[0]);
414 if (NULL == (job = pcrs_compile_command(Argv[1], &err)))
416 printf("Compile error: %s (%d).\\n", pcrs_strerror(err), err);
419 if (0 > (err = pcrs_execute(job, Argv[2], strlen(Argv[2]) + 1, &result, &newsize)))
421 printf("Exec error: %s (%d).\\n", pcrs_strerror(err), err);
424 /* Will tolerate NULL result */
425 printf("Result: *%s*\\n", result);
428 if (result) free(result);
437 The number of matches that a global job can have is only limited by the
438 available memory. An initial storage for 40 matches is reserved, which
439 is dynamically resized by the factor 1.6 if exhausted.
441 The number of capturing subpatterns is currently limited to 33, which
442 is a Bad Thing[tm]. It should be dynamically expanded until it reaches the
446 All of the above values can be adjusted in the "Capacity" section
449 The Perl-style escape sequences for special characters \\\fInnn\fR,
450 \\x\fInn\fR, and \\c\fIX\fR are currently unsupported.
453 This library has only been tested in the context of one application
454 and should be considered high risk.
458 was originally written for the Internet Junkbuster project
459 (http://sourceforge.net/projects/ijbswa/).
462 .B PCRE(3), perl(1), perlre(1)
467 is Copyright 2000, 2001 by Andreas Oesterhelt <oes@oesterhelt.org> and is
468 licensed under the Terms of the GNU Lesser General Public License (LGPL), which
469 should be included in this distribution.
471 If not, refer to http://www.gnu.org/licenses/lgpl.html or write to the Free
472 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.