1 #ifndef PCRS_H_INCLUDED
2 #define PCRS_H_INCLUDED
4 /*********************************************************************
6 * File : $Source: /cvsroot/ijbswa/current/pcrs.h,v $
8 * Purpose : Header file for pcrs.c
10 * Copyright : Written and Copyright (C) 2000, 2001 by Andreas S. Oesterhelt
11 * <andreas@oesterhelt.org>
13 * Copyright (C) 2006, 2007 Fabian Keil <fk@fabiankeil.de>
15 * This program is free software; you can redistribute it
16 * and/or modify it under the terms of the GNU General
17 * Public License as published by the Free Software
18 * Foundation; either version 2 of the License, or (at
19 * your option) any later version.
21 * This program is distributed in the hope that it will
22 * be useful, but WITHOUT ANY WARRANTY; without even the
23 * implied warranty of MERCHANTABILITY or FITNESS FOR A
24 * PARTICULAR PURPOSE. See the GNU General Public
25 * License for more details.
27 * The GNU General Public License should be included with
28 * this file. If not, you can view it at
29 * http://www.gnu.org/copyleft/gpl.html
30 * or write to the Free Software Foundation, Inc., 59
31 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 *********************************************************************/
37 #define PCRE2_CODE_UNIT_WIDTH 8
38 #define PCREn(x) PCRE2_ ## x
43 #define PCREn(x) PCRE_ ## x
57 #define PCRS_MAX_SUBMATCHES 33 /* Maximum number of capturing subpatterns allowed. MUST be <= 99! FIXME: Should be dynamic */
58 #define PCRS_MAX_MATCH_INIT 40 /* Initial amount of matches that can be stored in global searches */
59 #define PCRS_MAX_MATCH_GROW 1.6 /* Factor by which storage for matches is extended if exhausted */
64 * They are supposed to be handled together with PCRE error
65 * codes and have to start with an offset to prevent overlaps.
67 * PCRE 6.7 uses error codes from -1 to -21,
68 * PCRE2 10.42 uses error codes from -66 to 101.
69 * PCRS error codes below -300 should be safe for a while.
71 #define PCRS_ERR_NOMEM -300 /* Failed to acquire memory. */
72 #define PCRS_ERR_CMDSYNTAX -301 /* Syntax of s///-command */
73 #define PCRS_ERR_STUDY -302 /* pcre error while studying the pattern */
74 #define PCRS_ERR_BADJOB -303 /* NULL job pointer, pattern or substitute */
75 #define PCRS_WARN_BADREF -304 /* Backreference out of range */
76 #define PCRS_WARN_TRUNCATION -305 /* At least one pcrs variable was too big,
77 * only the first part was used. */
80 #define PCRS_GLOBAL 0x08000000u /* Job should be applied globally, as with perl's g option */
81 #define PCRS_TRIVIAL 0x10000000u /* Backreferences in the substitute are ignored */
82 #define PCRS_SUCCESS 0x20000000u /* Job did previously match */
83 #define PCRS_DYNAMIC 0x40000000u /* Job is dynamic (used to disable JIT compilation) */
90 /* A compiled substitute */
93 char *text; /* The plaintext part of the substitute, with all backreferences stripped */
94 size_t length; /* The substitute may not be a valid C string so we can't rely on strlen(). */
95 int backrefs; /* The number of backreferences */
96 int block_offset[PCRS_MAX_SUBMATCHES]; /* Array with the offsets of all plaintext blocks in text */
97 size_t block_length[PCRS_MAX_SUBMATCHES]; /* Array with the lengths of all plaintext blocks in text */
98 int backref[PCRS_MAX_SUBMATCHES]; /* Array with the backref number for all plaintext block borders */
99 int backref_count[PCRS_MAX_SUBMATCHES + 2]; /* Array with the number of references to each backref index */
104 * A match, including all captured subpatterns (submatches)
105 * Note: The zeroth is the whole match, the PCRS_MAX_SUBMATCHES + 0th
106 * is the range before the match, the PCRS_MAX_SUBMATCHES + 1th is the
107 * range after the match.
111 int submatches; /* Number of captured subpatterns */
112 int submatch_offset[PCRS_MAX_SUBMATCHES + 2]; /* Offset for each submatch in the subject */
113 size_t submatch_length[PCRS_MAX_SUBMATCHES + 2]; /* Length of each submatch in the subject */
119 typedef struct PCRS_JOB {
123 pcre *pattern; /* The compiled pcre pattern */
124 pcre_extra *hints; /* The pcre hints for the pattern */
126 int options; /* The pcre options (numeric) */
127 unsigned int flags; /* The pcrs and user flags (see "Flags" above) */
128 pcrs_substitute *substitute; /* The compiled pcrs substitute */
129 struct PCRS_JOB *next; /* Pointer for chaining jobs to joblists */
138 extern pcrs_job *pcrs_compile_command(const char *command, int *errptr);
139 extern pcrs_job *pcrs_compile(const char *pattern, const char *substitute, const char *options, int *errptr);
140 extern int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char **result, size_t *result_length);
141 extern int pcrs_execute_list(pcrs_job *joblist, char *subject, size_t subject_length, char **result, size_t *result_length);
144 extern pcrs_job *pcrs_free_job(pcrs_job *job);
145 extern void pcrs_free_joblist(pcrs_job *joblist);
147 /* Info on errors: */
148 extern const char *pcrs_strerror(const int error);
150 extern int pcrs_job_is_dynamic(char *job);
151 extern char pcrs_get_delimiter(const char *string);
152 extern char *pcrs_execute_single_command(const char *subject, const char *pcrs_command, int *hits);
154 * Variable/value pair for dynamic pcrs commands.
163 extern pcrs_job *pcrs_compile_dynamic_command(char *pcrs_command, const struct pcrs_variable v[], int *error);
165 /* Only relevant for maximum pcrs variable size */
166 #ifndef PCRS_BUFFER_SIZE
167 #define PCRS_BUFFER_SIZE 4000
168 #endif /* ndef PCRS_BUFFER_SIZE */
171 extern pcrs_substitute *pcrs_compile_fuzzed_replacement(const char *replacement, int *errptr);
174 #endif /* ndef PCRS_H_INCLUDED */