-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpcrs.h
166 lines (137 loc) · 6.47 KB
/
pcrs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef PCRS_H_INCLUDED
#define PCRS_H_INCLUDED
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/pcrs.h,v $
*
* Purpose : Header file for pcrs.c
*
* Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
*
* Copyright (C) 2006, 2007 Fabian Keil <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will
* be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* The GNU General Public License should be included with
* this file. If not, you can view it at
* http://www.gnu.org/copyleft/gpl.html
* or write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*********************************************************************/
#ifndef _PCRE_H
#include <pcre.h>
#endif
/*
* Constants:
*/
#define FALSE 0
#define TRUE 1
/* Capacity */
#define PCRS_MAX_SUBMATCHES 33 /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
#define PCRS_MAX_MATCH_INIT 40 /* Initial amount of matches that can be stored in global searches */
#define PCRS_MAX_MATCH_GROW 1.6 /* Factor by which storage for matches is extended if exhausted */
/*
* PCRS error codes
*
* They are supposed to be handled together with PCRE error
* codes and have to start with an offset to prevent overlaps.
*
* PCRE 6.7 uses error codes from -1 to -21, PCRS error codes
* below -100 should be safe for a while.
*/
#define PCRS_ERR_NOMEM -100 /* Failed to acquire memory. */
#define PCRS_ERR_CMDSYNTAX -101 /* Syntax of s///-command */
#define PCRS_ERR_STUDY -102 /* pcre error while studying the pattern */
#define PCRS_ERR_BADJOB -103 /* NULL job pointer, pattern or substitute */
#define PCRS_WARN_BADREF -104 /* Backreference out of range */
#define PCRS_WARN_TRUNCATION -105 /* At least one pcrs variable was too big,
* only the first part was used. */
/* Flags */
#define PCRS_GLOBAL 1 /* Job should be applied globally, as with perl's g option */
#define PCRS_TRIVIAL 2 /* Backreferences in the substitute are ignored */
#define PCRS_SUCCESS 4 /* Job did previously match */
#define PCRS_DYNAMIC 8 /* Job is dynamic (used to disable JIT compilation) */
/*
* Data types:
*/
/* A compiled substitute */
typedef struct {
char *text; /* The plaintext part of the substitute, with all backreferences stripped */
size_t length; /* The substitute may not be a valid C string so we can't rely on strlen(). */
int backrefs; /* The number of backreferences */
int block_offset[PCRS_MAX_SUBMATCHES]; /* Array with the offsets of all plaintext blocks in text */
size_t block_length[PCRS_MAX_SUBMATCHES]; /* Array with the lengths of all plaintext blocks in text */
int backref[PCRS_MAX_SUBMATCHES]; /* Array with the backref number for all plaintext block borders */
int backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
} pcrs_substitute;
/*
* A match, including all captured subpatterns (submatches)
* Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
* is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
* range after the match.
*/
typedef struct {
int submatches; /* Number of captured subpatterns */
int submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
} pcrs_match;
/* A PCRS job */
typedef struct PCRS_JOB {
pcre *pattern; /* The compiled pcre pattern */
pcre_extra *hints; /* The pcre hints for the pattern */
int options; /* The pcre options (numeric) */
int flags; /* The pcrs and user flags (see "Flags" above) */
pcrs_substitute *substitute; /* The compiled pcrs substitute */
struct PCRS_JOB *next; /* Pointer for chaining jobs to joblists */
} pcrs_job;
/*
* Prototypes:
*/
/* Main usage */
extern pcrs_job *pcrs_compile_command(const char *command, int *errptr);
extern pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
extern int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
extern int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
/* Freeing jobs */
extern pcrs_job *pcrs_free_job(pcrs_job *job);
extern void pcrs_free_joblist(pcrs_job *joblist);
/* Info on errors: */
extern const char *pcrs_strerror(const int error);
extern int pcrs_job_is_dynamic(char *job);
extern char pcrs_get_delimiter(const char *string);
extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
/*
* Variable/value pair for dynamic pcrs commands.
*/
struct pcrs_variable
{
const char *name;
char *value;
int static_value;
};
extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
/* Only relevant for maximum pcrs variable size */
#ifndef PCRS_BUFFER_SIZE
#define PCRS_BUFFER_SIZE 4000
#endif /* ndef PCRS_BUFFER_SIZE */
#ifdef FUZZ
extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
#endif
#endif /* ndef PCRS_H_INCLUDED */
/*
Local Variables:
tab-width: 3
end:
*/