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