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,
23 .TH PCRS 3 "4 March 2002" "pcrs-0.0.1"
25 pcrs - Perl-compatible regular substitution.
28 .B "#include <pcrs.h>"
31 .BI "pcrs_job *pcrs_compile(const char *" pattern ","
33 .BI "const char *" substitute ", const char *" options ,
38 .BI "pcrs_job *pcrs_compile_command(const char *" command ,
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 );
71 library is a supplement to the
73 library that implements
74 .RB "regular expression based substitution, like provided by " Perl(1) "'s 's'"
75 operator. It uses the same syntax and semantics as Perl 5, with just a few
76 differences (see below).
78 In a first step, the information on a substitution, i.e. the pattern, the
79 substitute and the options are compiled from Perl syntax to an internal form
80 .RB "called " pcrs_job " by using either the " pcrs_compile() " or "
81 .BR pcrs_compile_command() " functions."
83 Once the job is compiled, it can be used on subjects, which are arbitrary
84 memory areas containing string or binary data, by calling
85 .BR pcrs_execute() ". Jobs can be chained to joblists and whole"
86 .RB "joblists can be applied to a subject using " pcrs_execute_list() .
88 There are also convenience functions for freeing the jobs and for errno-to-string
89 .RB "conversion, namely " pcrs_free_job() ", " pcrs_free_joblist() " and "
94 .RB "The function " pcrs_compile() " is called to compile a " pcrs_job
95 .RI "from a " pattern ", " substitute " and " options " string."
96 .RB "The resulting " "pcrs_job" " structure is dynamically allocated and it"
97 .RB "is the caller's responsibility to call " "pcrs_free_job()" " when it's no longer needed."
99 .BR "pcrs_compile_command()" " is a convenience wrapper function that parses a Perl"
100 .IR "command" " of the form"
101 .BI "s/" "pattern" "/" "substitute" "/[" "options" "]"
102 .RB "into its components and then calls " "pcrs_compile()" ". As in Perl, you"
103 .RB "are not bound to the '" "/" "' character: Whatever"
104 .RB "follows the '" "s" "' will be used as the delimiter. Patterns or substitutes"
105 that contain the delimiter need to quote it:
106 \fBs/th\\/is/th\\/at/\fR
107 .RB "will replace " "th/is" " by " "th/at" " and can be written more simply as"
108 .BR "s|th/is|th/at|" "."
110 .IR "pattern" ", " "substitute" ", " "options" " and " "command" " must be"
111 .RI "zero-terminated C strings. " "substitute" " and " "options" " may be"
112 .BR "NULL" ", in which case they are treated like the empty string."
114 .SS "Return value and diagnostics"
115 On success, both functions return a pointer to the compiled job.
116 .RB "On failure, " "NULL"
117 .RI "is returned. In that case, the pcrs error code is written to *" "err" "."
120 .RI "For the syntax of the " "pattern" ", see the "
121 .BR "PCRE(3)" " manual page."
124 .RI "The " "substitute" " uses"
125 .RB "Perl syntax as documented in the " "perlre(1)" " manual page, with"
128 Most notably and evidently, since
130 is not Perl, variable interpolation or Perl command substitution won't work.
131 Special variables that do get interpolated, are:
134 Like in Perl, these variables refer to what the nth capturing subpattern
135 in the pattern matched.
138 .RB "refer to the whole match. Note that " "$0" " is deprecated in recent"
139 Perl versions and now refers to the program name.
142 refers to what the last capturing subpattern matched.
144 .BR "$` and $'" " (backtick and tick)"
145 .RI "refer to the areas of the " "subject" " before and after the match, respectively."
146 .RB "Note that, like in Perl, the " "unmodified" " subject is used, even"
147 if a global substitution previously matched.
150 Perl4-style references to subpattern matches of the form
152 .RB "which only exist in Perl5 for backwards compatibility, are " "not"
155 Also, since the substitute is a double-quoted string in Perl, you
156 might expect all Perl syntax for special characters to apply. In fact,
157 only the following are supported:
164 carriage return (0x0d)
167 horizontal tab (0x09)
185 .RB "The options " "gmisx" " are supported. " "e" " is not, since it would"
186 .RB "require a Perl interpreter and neither is " o ", because the pattern
187 is explicitly compiled, anyway. Additionally,
189 .RB "honors the options " "U" " and " "T" "."
192 .RB "options are mentioned below, refer to " PCRE(3) " for the subtle differences"
197 .RB "Replace " all " instances of"
198 .IR pattern " in " subject ,
199 not just the first one.
203 .RI "Match the " pattern " without respect to case. This translates to"
208 .RI "Treat the " subject " as consisting of multiple lines, i.e."
209 .RB ' ^ "' matches immediately after, and '" $ "' immediately before each newline."
215 .RI "Treat the " subject " as consisting of one single line, i.e."
216 .RB "let the scope of the '" . "' metacharacter include newlines."
222 .RI "Allow extended regular expression syntax in the " pattern ","
223 .RB "enabling whitespace and comments in complex patterns."
229 .RB "Switch the default behaviour of the '" * "' and '" + "' quantifiers"
230 .RB "to ungreedy. Note that appending a '" ? "' switches back to greedy(!)."
231 .RB "The explicit in-pattern switches " (?U) " and " (?-U) " remain unaffected."
237 .RI "Consider the " substitute " trivial, i.e. do not interpret any references"
238 or special character escape sequences in the substitute. Handy for large
239 user-supplied substitutes, which would otherwise have to be examined and properly
243 Unsupported options are silently ignored.
247 .RI "Calling " pcrs_execute() " produces a modified copy of the " subject ", in which"
248 .RB "the first (or all, if the '" g "' option was given when compiling the job)"
249 .RI "occurance(s) of the job's " pattern " in the " subject " is replaced by the job's"
252 .RI "The first " subject_length " bytes following " subject " are processed, so"
253 .RI "a " subject_length " that exceeds the actual " subject " is dangerous."
254 Note that if you want to get your zero-terminated C strings back including their
255 .RI "termination, you must let " subject_length " include the binary zero, i.e."
257 .BI strlen( subject ") + 1."
259 .RI "The " subject " itself is left untouched, and the " *result " is dynamically"
260 .RB "allocated, so it is the caller's responsibility to " free() " it when it's"
263 .RI "The result's length is written to " *result_length "."
265 .RB "If the job matched, the " PCRS_SUCCESS " flag in"
269 .SS Return value and diagnostics
271 .RB "On success, " pcrs_execute() " returns the number of substitutions that"
272 were made, which is limited to 0 or 1 for non-global searches.
273 .RI "On failure, a negative error code is returned and " result " is set"
277 .RB "It is not sufficient to call " free() " on a " pcrs_job ", because it "
278 contains pointers to other dynamically allocated structures.
279 .RB "Use " pcrs_free_job() " instead. It is safe to pass " NULL " pointers "
280 .RB "(or pointers to invalid " pcrs_job "s that contain " NULL " pointers"
281 .RB "to dependant structures) to " pcrs_free_job() "."
284 .RB "The value of the job's " next " pointer."
290 .RB "supports to some extent the chaining of multiple " pcrs_job " structures by"
291 .RB "means of their " next " member."
293 Chaining the jobs is up to you, but once you have built a linked list of jobs,
294 .RI "you can execute a whole " joblist " on a given subject by"
295 .RB "a single call to " pcrs_execute_list() ", which will sequentially traverse"
296 .RB "the linked list until it reaches a " NULL " pointer, and call " pcrs_execute()
297 .RI "for each job it encounters, feeding the " result " and " result_length " of each"
298 .RI "call into the next as the " subject " and " subject_length ". As in the single"
299 .RI "job case, the original " subject " remains untouched, but all interim " result "s"
300 .RB "are of course " free() "d. The return value is the accumulated number of matches"
301 .RI "for all jobs in the " joblist "."
302 .RI "Note that while this is handy, it reduces the diagnostic value of " err ", since "
303 you won't know which job failed.
305 .RI "In analogy, you can free all jobs in a given " joblist " by calling"
306 .BR pcrs_free_joblist() .
309 The quote character is (surprise!) '\fB\\\fR'. It quotes the delimiter in a
312 .IR substitute ", and, of course, itself. Note that the"
313 .RB ' $ "' doesn't need to be quoted if it isn't followed by " [0-9+'`&] "."
315 .RI "For quoting in the " pattern ", please refer to"
320 .RB "When " compiling " a job either via the " pcrs_compile() " or " pcrs_compile_command()
321 .RB "functions, you know that something went wrong when you are returned a " NULL " pointer."
322 .RI "In that case, or in the event of non-fatal warnings, the integer pointed to by " err
323 contains a nonzero error code, which is either a passed-through
325 error code or one generated by
327 Under normal circumstances, it can take the following values:
329 .B PCRE_ERROR_NOMEMORY
330 While compiling the pattern,
335 While compiling the job,
339 .B PCRS_ERR_CMDSYNTAX
340 .BR pcrs_compile_command() " didn't find four tokens while parsing the"
346 .RB "error occured while studying the compiled pattern. Since " pcre_study()
347 only provides textual diagnostic information, the details are lost.
350 .RI "The " substitute " contains a reference to a capturing subpattern that"
351 .RI "has a higher index than the number of capturing subpatterns in the " pattern
352 or that exceeds the current hard limit of 33 (See LIMITATIONS below). As in Perl,
353 this is non-fatal and results in substitutions with the empty string.
356 .RB "When " executing " jobs via " pcrs_execute() " or " pcrs_execute_list() ","
357 .RI "a negative return code indicates an error. In that case, *" result
358 .RB "is " NULL ". Possible error codes are:"
360 .B PCRE_ERROR_NOMEMORY
361 While matching the pattern,
363 ran out of memory. This can only happen if there are more than 33 backrefrences
364 .RI "in the " pattern "(!)"
365 .BR and " memory is too tight to extend storage for more."
368 While executing the job,
373 .RB "The " pcrs_job "* passed to " pcrs_execute " was NULL, or the"
374 .RB "job is bogus (it contains " NULL " pointers to the compiled
375 pattern, extra, or substitute).
380 error code passed through, you've either messed with the compiled job
383 Please send me an email.
385 .RB "Ah, and don't look for " PCRE_ERROR_NOMATCH ", since this"
386 is not an error in the context of
388 .RI "Should there be no match, an exact copy of the " subject " is"
389 .RI "found at *" result " and the return code is 0 (matches)."
391 All error codes can be translated into human readable text by means
392 .RB "of the " pcrs_strerror() " function."
396 A trivial command-line test program for
404 int main(int Argc, char **Argv)
413 fprintf(stderr, "Usage: %s s/pattern/substitute/[options] subject\\n", Argv[0]);
417 if (NULL == (job = pcrs_compile_command(Argv[1], &err)))
419 fprintf(stderr, "%s: compile error: %s (%d).\\n", Argv[0], pcrs_strerror(err), err);
422 if (0 > (err = pcrs_execute(job, Argv[2], strlen(Argv[2]) + 1, &result, &newsize)))
424 fprintf(stderr, "%s: Exec error: %s (%d).\\n", Argv[0], pcrs_strerror(err), err);
428 printf("Result: *%s*\\n", result);
441 The number of matches that a global job can have is only limited by the
442 available memory. An initial storage for 40 matches is reserved, which
443 is dynamically resized by the factor 1.6 whenever it is exhausted.
445 The number of capturing subpatterns is currently limited to 33, which
446 is a Bad Thing[tm]. It should be dynamically expanded until it reaches the
450 All of the above values can be adjusted in the "Capacity" section
453 The Perl-style escape sequences for special characters \\\fInnn\fR,
454 \\x\fInn\fR, and \\c\fIX\fR are currently unsupported.
457 This library has only been tested in the context of one application
458 and should be considered high risk.
462 was originally written for the Internet Junkbuster project
463 (http://sourceforge.net/projects/ijbswa/).
466 .B PCRE(3), perl(1), perlre(1)
471 is Copyright 2000, 2001 by Andreas Oesterhelt <andreas@oesterhelt.org> and is
472 licensed under the terms of the GNU Lesser General Public License (LGPL),
473 version 2.1, which should be included in this distribution, with the exception
474 that the permission to replace that license with the GNU General Public
475 License (GPL) given in section 3 is restricted to version 2 of the GPL.
477 If it is missing from this distribution, the LGPL can be obtained from
478 http://www.gnu.org/licenses/lgpl.html or by mail: Write to the Free Software
479 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.