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