c6925652e51279c66bbf0df7164b076c14116c52
[privoxy.git] / doc / pcrs.3
1 .\" Copyright (c) 2001 Andreas S. Oesterhelt <oes@oesterhelt.org>
2 .\"
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.
7 .\"
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.
12 .\"
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.
17 .\"
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,
21 .\" USA.
22 .\"
23 .TH PCRS 3 "17 August 2001"
24 .SH NAME
25 pcrs - Perl-compatible regular substitution.
26 .SH SYNOPSIS
27 .br
28 .BI "#include <pcrs.h>"
29 .PP
30 .br
31 .BI "pcrs_job *pcrs_compile(const char *" "pattern" "," 
32 .ti +5n
33 .BI "const char *" "substitute" ", const char *" "options" ","
34 .ti +5n
35 .BI "int *" "errptr" ");"
36 .PP
37 .br
38 .BI "pcrs_job *pcrs_compile_command(const char *" "command" ","
39 .ti +5n
40 .BI "int *" "errptr");
41 .PP
42 .br
43 .BI "int pcrs_execute(pcrs_job *" "job" ", char *" "subject" ","
44 .ti +5n
45 .BI "int " "subject_length" ", char **" "result" ","
46 .ti +5n
47 .BI "int *" "result_length" ");"
48 .PP
49 .br
50 .BI "int pcrs_execute_list (pcrs_job *" "joblist" ", char *" "subject" ","
51 .ti +5n
52 .BI "int " "subject_length" ", char **" "result" ","
53 .ti +5n
54 .BI "int *" "result_length" ");"
55 .PP
56 .br
57 .BI "pcrs_job *pcrs_free_job(pcrs_job *" "job" ");"
58 .PP
59 .br
60 .BI "void pcrs_free_joblist(pcrs_job *" "joblist" ");"
61 .PP
62 .br
63 .BI "char *pcrs_strerror(int " "err" ");"
64 .PP
65 .br
66
67 .SH DESCRIPTION
68 The
69 .SM PCRS
70 library is a supplement to the 
71 .SB PCRE(3)
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).
76
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."
81
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()" "."
86
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()" "."
90
91 .SH COMPILING JOBS
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."
96
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|" "."
107
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."
111
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" "."
116
117 .SS Patterns
118 .RI "For the syntax of the " "pattern" ", see the "
119 .BR "PCRE(3)" " manual page."
120
121 .SS Substitutes
122 .RI "The " "substitute" " uses"
123 .RB "Perl syntax as documented in the " "perlre(1)" " manual page, with"
124 some exceptions: 
125
126 Most notably and evidently, since
127 .SM PCRS
128 is not Perl, variable interpolation or Perl command substitution won't work.
129 Special variables that do get interpolated, are:
130 .TP
131 .B "$1, $2, ..., $n"
132 Like in Perl, these variables refer to what the nth capturing subpattern
133 in the pattern matched.
134 .TP
135 .B "$& and $0"
136 .RB "refer to the whole match. Note that " "$0" " is deprecated in recent"
137 Perl versions and now refers to the program name.
138 .TP
139 .B "$+"
140 refers to what the last capturing subpattern matched.
141 .TP
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.
146
147 .PP
148 Perl4-style references to subpattern matches of the form 
149 \fB\\1, \\2, ...\fR
150 .RB "which only exist in Perl5 for backwards compatibility, are " "not" 
151 supported.
152
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:
156
157 .TP
158 \fB\\n\fR
159 newline (0x0a)
160 .TP
161 \fB\\r\fR
162 carriage return (0x0d)
163 .TP
164 \fB\\t\fR
165 horizontal tab (0x09)
166 .TP
167 \fB\\f\fR
168 form feed (0x0c)
169 .TP
170 \fB\\b\fR
171 backspace (0x08)
172 .TP
173 \fB\\a\fR
174 alarm, bell (0x07)
175 .TP
176 \fB\\e\fR
177 escape (0x1b)
178 .TP
179 \fB\\0\fR
180 binary zero (0x00)
181
182 .SS "Options"
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,
186 .SM PCRS
187 .RB "honors the options " "U" " and " "T" "."
188 Where
189 .SM PCRE
190 .RB "options are mentioned below, refer to " PCRE(3) " for the subtle differences"
191 to Perl behaviour.
192
193 .TP
194 .B g
195 .RB "Replace " all " instances of"
196 .IR pattern " in " subject ,
197 not just the first one.
198
199 .TP
200 .B i
201 .RI "Match the " pattern " without respect to case. This translates to"
202 .SM PCRE_CASELESS.
203
204 .TP
205 .B m
206 .RI "Treat the " subject " as consisting of multiple lines, i.e."
207 .RB ' ^ "' matches immediately after, and '" $ "' immediately before each newline."
208 Translates to
209 .SM PCRE_MULTILINE.
210
211 .TP
212 .B s
213 .RI "Treat the " subject " as consisting of one single line, i.e."
214 .RB "let the scope of the '" . "' metacharacter include newlines."
215 Translates to
216 .SM PCRE_DOTALL.
217
218 .TP
219 .B x
220 .RI "Allow extended regular expression syntax in the " pattern ","
221 .RB "enabling whitespace and comments in complex patterns."
222 Translates to
223 .SM PCRE_EXTENDED.
224
225 .TP
226 .B U
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."
230 Translates to
231 .SM PCRE_UNGREEDY.
232
233 .TP
234 .B T
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
238 quoted.
239
240 .PP
241 Unsupported options are silently ignored.
242
243 .SH EXECUTING JOBS
244
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"
248 .IR substitute .
249
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."
254 set it to
255 .BI strlen( subject ") + 1."
256
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"
259 no longer needed.
260
261 .RI "The result's length is written to " *result_length "."
262
263 .RB "If the job matched, the " PCRS_SUCCESS " flag in"
264 .IB job ->flags
265 is set.
266
267 .SS Return value and diagnostics
268
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"
272 .RB "to " NULL .
273
274 .SH FREEING JOBS
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() "."
280
281 .SS Return value
282 .RB "The value of the job's " next " pointer."
283
284
285 .SH CHAINING JOBS
286
287 .SM PCRS
288 .RB "supports to some extent the chaining of multiple " pcrs_job " structures by"
289 .RB "means of their " next " member."
290
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.
302
303 .RI "In analogy, you can free all jobs in a given " joblist " by calling"
304 .BR pcrs_free_joblist() .
305
306 .SH QUOTING
307 The quote character is (surprise!) '\fB\\\fR'. It quotes the delimiter in a
308 .IR command ", the"
309 .RB ' $ "' 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+'`&] "."
312
313 .RI "For quoting in the " pattern ", please refer to"
314 .BR PCRE(3) .
315
316 .SH DIAGNOSTICS
317
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
322 .SM PCRE
323 error code or one generated by
324 .SM PCRS.
325 Under normal circumstances, it can take the following values:
326 .TP
327 .B PCRE_ERROR_NOMEMORY
328 While compiling the pattern,
329 .SM PCRE
330 ran out of memory.
331 .TP  
332 .B  PCRS_ERR_NOMEM
333 While compiling the job,
334 .SM PCRS
335 ran out of memory.
336 .TP  
337 .B  PCRS_ERR_CMDSYNTAX
338 .BR pcrs_compile_command() " didn't find four tokens while parsing the"
339 .IR command .
340 .TP  
341 .B  PCRS_ERR_STUDY
342 A
343 .SM PCRE
344 .RB "error occured while studying the compiled pattern. Since " pcre_study()
345 only provides textual diagnostic information, the details are lost.
346 .TP  
347 .B  PCRS_WARN_BADREF
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.
352
353 .PP
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:"
357 .TP
358 .B PCRE_ERROR_NOMEMORY
359 While matching the pattern,
360 .SM PCRE
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."
364 .TP  
365 .B  PCRS_ERR_NOMEM
366 While executing the job,
367 .SM PCRS
368 ran out of memory.
369 .TP  
370 .B  PCRS_ERR_BADJOB
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).
374
375 .PP
376 If you see any other
377 .SM PCRE
378 error code passed through, you've either messed with the compiled job
379 or found a bug in
380 .SM PCRS.
381 Please send me an email.
382
383 .RB "Ah, and don't look for " PCRE_ERROR_NOMATCH ", since this"
384 is not an error in the context of
385 .SM PCRS.
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)."
388
389 All error codes can be translated into human readable text by means
390 .RB "of the " pcrs_strerror() " function."
391
392
393 .SH EXAMPLE
394 A trivial command-line test program for
395 .SM PCRS
396 might look like:
397
398 .nf
399 #include <pcrs.h>
400 #include <stdio.h>
401
402 int main(int Argc, char **Argv)
403 {
404    pcrs_job *job;
405    char *result;
406    int newsize, err;
407
408    if (Argc != 3)
409    {
410       fprintf(stderr, "Usage: %s s/pattern/substitute/[options]  subject\\n", Argv[0]);
411       return 1;
412    }
413
414    if (NULL == (job = pcrs_compile_command(Argv[1], &err)))
415    {
416       printf("Compile error:  %s (%d).\\n", pcrs_strerror(err), err);
417    }
418
419    if (0 > (err = pcrs_execute(job, Argv[2], strlen(Argv[2]) + 1, &result, &newsize)))
420    {
421       printf("Exec error:  %s (%d).\\n", pcrs_strerror(err), err);
422    }
423
424    /* Will tolerate NULL result */
425    printf("Result: *%s*\\n", result);
426
427    pcrs_free_job(job);
428    if (result) free(result);
429
430    return 0;
431 }
432
433 .fi
434
435
436 .SH LIMITATIONS
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.
440
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
443 .SM PCRE
444 limit of 99.
445
446 All of the above values can be adjusted in the "Capacity" section
447 .RB "of " pcrs.h "."
448
449 The Perl-style escape sequences for special characters \\\fInnn\fR,
450 \\x\fInn\fR, and \\c\fIX\fR are currently unsupported.
451
452 .SH BUGS
453 This library has only been tested in the context of one application
454 and should be considered high risk.
455
456 .SH HISTORY
457 .SM PCRS
458 was originally written for the Internet Junkbuster project
459 (http://sourceforge.net/projects/ijbswa/).
460
461 .SH SEE ALSO
462 .B PCRE(3), perl(1), perlre(1)
463
464 .SH AUTHOR
465
466 .SM PCRS
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.
470
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.