Bump copyright
[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  *********************************************************************/
13
14 #define PCRS_H_VERSION "$Id: pcrs.h,v 1.16 2007/04/30 15:02:19 fabiankeil Exp $"
15
16
17 #ifndef _PCRE_H
18 #include <pcre.h>
19 #endif
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24
25 /*
26  * Constants:
27  */
28
29 #define FALSE 0
30 #define TRUE 1
31
32 /* Capacity */
33 #define PCRS_MAX_SUBMATCHES  33     /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
34 #define PCRS_MAX_MATCH_INIT  40     /* Initial amount of matches that can be stored in global searches */
35 #define PCRS_MAX_MATCH_GROW  1.6    /* Factor by which storage for matches is extended if exhausted */
36
37 /*
38  * PCRS error codes
39  *
40  * They are supposed to be handled together with PCRE error
41  * codes and have to start with an offset to prevent overlaps.
42  *
43  * PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
44  * below -100 should be safe for a while.
45  */
46 #define PCRS_ERR_NOMEM           -100      /* Failed to acquire memory. */
47 #define PCRS_ERR_CMDSYNTAX       -101      /* Syntax of s///-command */
48 #define PCRS_ERR_STUDY           -102      /* pcre error while studying the pattern */
49 #define PCRS_ERR_BADJOB          -103      /* NULL job pointer, pattern or substitute */
50 #define PCRS_WARN_BADREF         -104      /* Backreference out of range */
51 #define PCRS_WARN_TRUNCATION     -105      /* At least one pcrs variable was too big,
52                                             * only the first part was used. */
53
54 /* Flags */
55 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
56 #define PCRS_TRIVIAL         2      /* Backreferences in the substitute are ignored */
57 #define PCRS_SUCCESS         4      /* Job did previously match */
58
59
60 /*
61  * Data types:
62  */
63
64 /* A compiled substitute */
65
66 typedef struct {
67   char  *text;                                   /* The plaintext part of the substitute, with all backreferences stripped */
68   size_t length;                                 /* The substitute may not be a valid C string so we can't rely on strlen(). */
69   int    backrefs;                               /* The number of backreferences */
70   int    block_offset[PCRS_MAX_SUBMATCHES];      /* Array with the offsets of all plaintext blocks in text */
71   size_t block_length[PCRS_MAX_SUBMATCHES];      /* Array with the lengths of all plaintext blocks in text */
72   int    backref[PCRS_MAX_SUBMATCHES];           /* Array with the backref number for all plaintext block borders */
73   int    backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
74 } pcrs_substitute;
75
76
77 /*
78  * A match, including all captured subpatterns (submatches)
79  * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
80  * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
81  * range after the match.
82  */
83
84 typedef struct {
85   int    submatches;                               /* Number of captured subpatterns */
86   int    submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
87   size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
88 } pcrs_match;
89
90
91 /* A PCRS job */
92
93 typedef struct PCRS_JOB {
94   pcre *pattern;                            /* The compiled pcre pattern */
95   pcre_extra *hints;                        /* The pcre hints for the pattern */
96   int options;                              /* The pcre options (numeric) */
97   int flags;                                /* The pcrs and user flags (see "Flags" above) */
98   pcrs_substitute *substitute;              /* The compiled pcrs substitute */
99   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
100 } pcrs_job;
101
102
103 /*
104  * Prototypes:
105  */
106
107 /* Main usage */
108 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
109 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
110 extern int              pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
111 extern int              pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
112
113 /* Freeing jobs */
114 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
115 extern void             pcrs_free_joblist(pcrs_job *joblist);
116
117 /* Info on errors: */
118 extern const char *pcrs_strerror(const int error);
119
120 extern int pcrs_job_is_dynamic(char *job);
121 extern char pcrs_get_delimiter(const char *string);
122 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
123 /*
124  * Variable/value pair for dynamic pcrs commands.
125  */
126 struct pcrs_variable
127 {
128    const char *name;
129    char *value;
130    int static_value;
131 };
132
133 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
134
135 /* Only relevant for maximum pcrs variable size */
136 #ifndef PCRS_BUFFER_SIZE
137 #define PCRS_BUFFER_SIZE 4000
138 #endif /* ndef PCRS_BUFFER_SIZE */
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 */