8f2af81c1bdd36b18edaa350dc7ecca98b0e9da7
[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     :  This is the pre-pre-alpha realease of libpcrs. It is only
9  *                published at this (ugly) stage of development, because it is
10  *                needed for a new feature in JunkBuster.
11  *
12  *                Apart from the code being quite a mess, no inconsistencies,
13  *                memory leaks or functional bugs **should** be present.
14  *
15  *                While you ROTFL at the code, you could just as well mail me
16  *                (oes@paradis.rhein.de) with advice for improvement.
17  *
18  *                pcrs is a supplement to the brilliant pcre library by Philip
19  *                Hazel (ph10@cam.ac.uk) and adds Perl-style substitution. That
20  *                is, it mimics Perl's 's' operator.
21  *
22  *                Currently, there's no documentation besides comments and the
23  *                source itself ;-)
24  *
25  * Copyright   :  Written and copyright 2001 by Sourceforge IJBSWA team.
26  *
27  * Revisions   :
28  *    $Log: pcrs.h,v $
29  *    Revision 1.6  2001/07/29 18:52:06  jongfoster
30  *    Renaming _PCRS_H, and adding "extern C {}"
31  *
32  *    Revision 1.5  2001/07/18 17:27:00  oes
33  *    Changed interface; Cosmetics
34  *
35  *    Revision 1.4  2001/06/29 13:33:19  oes
36  *    - Cleaned up, commented and adapted to reflect the
37  *      changes in pcrs.c
38  *    - Introduced the PCRS_* flags
39  *
40  *    Revision 1.3  2001/06/09 10:58:57  jongfoster
41  *    Removing a single unused #define which referenced BUFSIZ
42  *
43  *    Revision 1.2  2001/05/25 11:03:55  oes
44  *    Added sanity check for NULL jobs to pcrs_exec_substitution
45  *
46  *    Revision 1.1.1.1  2001/05/15 13:59:02  oes
47  *    Initial import of version 2.9.3 source tree
48  *
49  *    Revision 1.4  2001/05/11 01:57:02  rodney
50  *    Added new file header standard w/RCS control tags.
51  *
52  *    revision 1.3  2001/05/08 02:38:13  rodney
53  *    Changed C++ "//" style comment to C style comments.
54  *
55  *    revision 1.2  2001/04/30 02:39:24  rodney
56  *    Made this pcrs.h file conditionally included.
57  *
58  *    revision 1.1  2001/04/16 21:10:38  rodney
59  *    Initial checkin
60  *
61  *********************************************************************/
62
63 #define PCRS_H_VERSION "$Id: pcrs.h,v 1.6 2001/07/29 18:52:06 jongfoster Exp $"
64 \f
65
66 #include <pcre.h>
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 /*
73  * Constants:
74  */
75
76 #define FALSE 0
77 #define TRUE 1
78
79 /* Capacity */
80 #define PCRS_MAX_MATCHES 300
81 #define PCRS_MAX_SUBMATCHES 33
82
83 /* Error codes */
84 #define PCRS_ERR_NOMEM     -10      /* Failed to acquire memory. */
85 #define PCRS_ERR_CMDSYNTAX -11      /* Syntax of s///-command */
86 #define PCRS_ERR_STUDY     -12      /* pcre error while studying the pattern */
87 #define PCRS_ERR_BADJOB    -13      /* NULL job pointer, pattern or substitute */
88
89 /* Flags */
90 #define PCRS_GLOBAL          1      /* Job should be applied globally, as with perl's g option */
91 #define PCRS_SUCCESS         2      /* Job did previously match */
92 #define PCRS_TRIVIAL         4      /* Backreferences in the substitute are ignored */
93
94 /*
95  * Data types:
96  */
97
98 /* A compiled substitute */
99 typedef struct PCRS_SUBSTITUTE {
100   char *text;                               /* The plaintext part of the substitute, with all backreferences stripped */
101   int backrefs;                             /* The number of backreferences */
102   int block_offset[PCRS_MAX_SUBMATCHES];    /* Array with the offsets of all plaintext blocks in text */
103   int block_length[PCRS_MAX_SUBMATCHES];    /* Array with the lengths of all plaintext blocks in text */
104   int backref[PCRS_MAX_SUBMATCHES];         /* Array with the backref number for all plaintext block borders */
105   int backref_count[PCRS_MAX_SUBMATCHES];   /* Array with the number of reference to each backref index */
106 } pcrs_substitute;
107
108 typedef struct PCRS_MATCH {
109   /* char *buffer; */
110   int submatches;                           /* Number of submatches. Note: The zeroth is the whole match */
111   int submatch_offset[PCRS_MAX_SUBMATCHES]; /* Offset for each submatch in the subject */
112   int submatch_length[PCRS_MAX_SUBMATCHES]; /* Length of each submatch in the subject */
113 } pcrs_match;
114
115 typedef struct PCRS_JOB {
116   pcre *pattern;                            /* The compiled pcre pattern */
117   pcre_extra *hints;                        /* The pcre hints for the pattern */
118   int options;                              /* The pcre options (numeric) */
119   int flags;                                /* The pcrs and user flags (see "Flags" above) */
120   pcrs_substitute *substitute;              /* The compiles pcrs substitute */
121   struct PCRS_JOB *next;                    /* Pointer for chaining jobs to joblists */
122 } pcrs_job;
123
124 /*
125  * Prototypes:
126  */
127
128 /* Main usage */
129 extern pcrs_job        *pcrs_compile_command(const char *command, int *errptr);
130 extern pcrs_job        *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
131 extern int              pcrs_execute(pcrs_job *job, char *subject, int subject_length, char **result, int *result_length);
132
133 /* Freeing jobs */
134 extern pcrs_job        *pcrs_free_job(pcrs_job *job);
135 extern void             pcrs_free_joblist(pcrs_job *joblist);
136
137 /* Expert usage */
138 extern int              pcrs_compile_perl_options(const char *optstring, int *flags);
139 extern pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag, int *errptr);
140
141 #ifdef __cplusplus
142 } /* extern "C" */
143 #endif
144
145 #endif /* ndef PCRS_H_INCLUDED */
146
147 /*
148   Local Variables:
149   tab-width: 3
150   end:
151 */