4b22a1b83ea37c8bf1b72d57d56555af403d392d
[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   :  see pcrs.c
11  *
12  * Revisions   :
13  *    $Log: pcrs.h,v $
14  *    Revision 1.13  2006/07/18 14:48:47  david__schmidt
15  *    Reorganizing the repository: swapping out what was HEAD (the old 3.1 branch)
16  *    with what was really the latest development (the v_3_0_branch branch)
17  *
18  *    Revision 1.11  2002/03/08 14:18:23  oes
19  *    Fixing -Wconversion warnings
20  *
21  *    Revision 1.10  2002/03/08 13:44:48  oes
22  *    Hiding internal functions, preventing double inclusion of pcre.h
23  *
24  *    Revision 1.9  2001/08/18 11:35:29  oes
25  *    - Introduced pcrs_strerror()
26  *    - added pcrs_execute_list()
27  *
28  *    Revision 1.8  2001/08/15 15:32:50  oes
29  *    Replaced the hard limit for the maximum number of matches
30  *    by dynamic reallocation
31  *
32  *    Revision 1.7  2001/08/05 13:13:11  jongfoster
33  *    Making parameters "const" where possible.
34  *
35  *    Revision 1.6  2001/07/29 18:52:06  jongfoster
36  *    Renaming _PCRS_H, and adding "extern C {}"
37  *
38  *    Revision 1.5  2001/07/18 17:27:00  oes
39  *    Changed interface; Cosmetics
40  *
41  *    Revision 1.4  2001/06/29 13:33:19  oes
42  *    - Cleaned up, commented and adapted to reflect the
43  *      changes in pcrs.c
44  *    - Introduced the PCRS_* flags
45  *
46  *    Revision 1.3  2001/06/09 10:58:57  jongfoster
47  *    Removing a single unused #define which referenced BUFSIZ
48  *
49  *    Revision 1.2  2001/05/25 11:03:55  oes
50  *    Added sanity check for NULL jobs to pcrs_exec_substitution
51  *
52  *    Revision 1.1.1.1  2001/05/15 13:59:02  oes
53  *    Initial import of version 2.9.3 source tree
54  *
55  *    Revision 1.4  2001/05/11 01:57:02  rodney
56  *    Added new file header standard w/RCS control tags.
57  *
58  *    revision 1.3  2001/05/08 02:38:13  rodney
59  *    Changed C++ "//" style comment to C style comments.
60  *
61  *    revision 1.2  2001/04/30 02:39:24  rodney
62  *    Made this pcrs.h file conditionally included.
63  *
64  *    revision 1.1  2001/04/16 21:10:38  rodney
65  *    Initial checkin
66  *
67  *********************************************************************/
68
69 #define PCRS_H_VERSION "$Id: pcrs.h,v 1.13 2006/07/18 14:48:47 david__schmidt Exp $"
70 \f
71
72 #ifndef _PCRE_H
73 #include <pcre.h>
74 #endif
75
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79
80 /*
81  * Constants:
82  */
83
84 #define FALSE 0
85 #define TRUE 1
86
87 /* Capacity */
88 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
89 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
90 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
91
92 /*
93  * PCRS error codes
94  *
95  * They are supposed to be handled together with PCRE error
96  * codes and have to start with an offset to prevent overlaps.
97  *
98  * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
99  * below -100 should be safe for a while.
100  */
101 #define PCRS_ERR_NOMEM     -100      /* Failed to acquire memory. */
102 #define PCRS_ERR_CMDSYNTAX -101      /* Syntax of s///-command */
103 #define PCRS_ERR_STUDY     -102      /* pcre error while studying the pattern */
104 #define PCRS_ERR_BADJOB    -103      /* NULL job pointer, pattern or substitute */
105 #define PCRS_WARN_BADREF   -104      /* Backreference out of range */
106
107 /* Flags */
108 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
109 #define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
110 #define PCRS_SUCCESS         4      /* Job did previously match */
111
112
113 /*
114  * Data types:
115  */
116
117 /* A compiled substitute */
118
119 typedef struct {
120   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
121   int    backrefs;                               /* The number of backreferences */
122   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
123   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
124   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
125   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
126 } pcrs_substitute;
127
128
129 /*
130  * A match, including all captured subpatterns (submatches)
131  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
132  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
133  * range after the match.
134  */
135
136 typedef struct {
137   int    submatches;                               /* Number of captured subpatterns */
138   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
139   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
140 } pcrs_match;
141
142
143 /* A PCRS job */
144
145 typedef struct PCRS_JOB {
146   pcre *pattern;                            /* The compiled pcre pattern */
147   pcre_extra *hints;                        /* The pcre hints for the pattern */
148   int options;                              /* The pcre options (numeric) */
149   int flags;                                /* The pcrs and user flags (see "Flags" above) */
150   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
151   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
152 } pcrs_job;
153
154
155 /*
156  * Prototypes:
157  */
158
159 /* Main usage */
160 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
161 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
162 extern int              pcrs_execute(pcrs_job *job, char *subject, size_t subject_length, char **result, size_t *result_length);
163 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
164
165 /* Freeing jobs */
166 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
167 extern void             pcrs_free_joblist(pcrs_job *joblist);
168
169 /* Info on errors: */
170 extern const char *pcrs_strerror(const int error);
171
172
173 #ifdef __cplusplus
174 } /* extern "C" */
175 #endif
176
177 #endif /* ndef PCRS_H_INCLUDED */
178
179 /*
180   Local Variables:
181   tab-width: 3
182   end:
183 */