Ditch a couple of spaces in pointer declarations
[privoxy.git] / pcrs.h
1 #ifndef PCRS_H_INCLUDED
2 #define PCRS_H_INCLUDED
3
4 /*********************************************************************
5  *
6  * File        :  $Source: /cvsroot/ijbswa/current/pcrs.h,v $
7  *
8  * Purpose     :  Header file for pcrs.c
9  *
10  * Copyright   :  Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
11  *                <andreas@oesterhelt.org>
12  *
13  *                Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
14  *
15  *                This program is free software; you can redistribute it
16  *                and/or modify it under the terms of the GNU General
17  *                Public License as published by the Free Software
18  *                Foundation; either version 2 of the License, or (at
19  *                your option) any later version.
20  *
21  *                This program is distributed in the hope that it will
22  *                be useful, but WITHOUT ANY WARRANTY; without even the
23  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
24  *                PARTICULAR PURPOSE.  See the GNU General Public
25  *                License for more details.
26  *
27  *                The GNU General Public License should be included with
28  *                this file.  If not, you can view it at
29  *                http://www.gnu.org/copyleft/gpl.html
30  *                or write to the Free Software Foundation, Inc., 59
31  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
32  *
33  *********************************************************************/
34
35
36 #ifndef _PCRE_H
37 #include <pcre.h>
38 #endif
39
40 /*
41  * Constants:
42  */
43
44 #define FALSE 0
45 #define TRUE 1
46
47 /* Capacity */
48 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
49 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
50 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
51
52 /*
53  * PCRS error codes
54  *
55  * They are supposed to be handled together with PCRE error
56  * codes and have to start with an offset to prevent overlaps.
57  *
58  * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
59  * below -100 should be safe for a while.
60  */
61 #define PCRS_ERR_NOMEM           -100      /* Failed to acquire memory. */
62 #define PCRS_ERR_CMDSYNTAX       -101      /* Syntax of s///-command */
63 #define PCRS_ERR_STUDY           -102      /* pcre error while studying the pattern */
64 #define PCRS_ERR_BADJOB          -103      /* NULL job pointer, pattern or substitute */
65 #define PCRS_WARN_BADREF         -104      /* Backreference out of range */
66 #define PCRS_WARN_TRUNCATION     -105      /* At least one pcrs variable was too big,
67                                             * only the first part was used. */
68
69 /* Flags */
70 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
71 #define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
72 #define PCRS_SUCCESS         4      /* Job did previously match */
73
74
75 /*
76  * Data types:
77  */
78
79 /* A compiled substitute */
80
81 typedef struct {
82   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
83   size_t length;                                 /* The substitute may not be a valid C string so we can't rely on strlen(). */
84   int    backrefs;                               /* The number of backreferences */
85   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
86   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
87   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
88   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
89 } pcrs_substitute;
90
91
92 /*
93  * A match, including all captured subpatterns (submatches)
94  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
95  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
96  * range after the match.
97  */
98
99 typedef struct {
100   int    submatches;                               /* Number of captured subpatterns */
101   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
102   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
103 } pcrs_match;
104
105
106 /* A PCRS job */
107
108 typedef struct PCRS_JOB {
109   pcre *pattern;                            /* The compiled pcre pattern */
110   pcre_extra *hints;                        /* The pcre hints for the pattern */
111   int options;                              /* The pcre options (numeric) */
112   int flags;                                /* The pcrs and user flags (see "Flags" above) */
113   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
114   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
115 } pcrs_job;
116
117
118 /*
119  * Prototypes:
120  */
121
122 /* Main usage */
123 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
124 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
125 extern int              pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
126 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
127
128 /* Freeing jobs */
129 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
130 extern void             pcrs_free_joblist(pcrs_job *joblist);
131
132 /* Info on errors: */
133 extern const char *pcrs_strerror(const int error);
134
135 extern int pcrs_job_is_dynamic(char *job);
136 extern char pcrs_get_delimiter(const char *string);
137 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
138 /*
139  * Variable/value pair for dynamic pcrs commands.
140  */
141 struct pcrs_variable
142 {
143    const char *name;
144    char *value;
145    int static_value;
146 };
147
148 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
149
150 /* Only relevant for maximum pcrs variable size */
151 #ifndef PCRS_BUFFER_SIZE
152 #define PCRS_BUFFER_SIZE 4000
153 #endif /* ndef PCRS_BUFFER_SIZE */
154
155 #ifdef FUZZ
156 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
157 #endif
158
159 #endif /* ndef PCRS_H_INCLUDED */
160
161 /*
162   Local Variables:
163   tab-width: 3
164   end:
165 */