/[pcre]/code/trunk/pcreposix.c
ViewVC logotype

Contents of /code/trunk/pcreposix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 43 - (show annotations) (download)
Sat Feb 24 21:39:21 2007 UTC (7 years, 7 months ago) by nigel
File MIME type: text/plain
File size: 8854 byte(s)
Load pcre-3.0 into code/trunk.

1 /*************************************************
2 * Perl-Compatible Regular Expressions *
3 *************************************************/
4
5 /*
6 This is a library of functions to support regular expressions whose syntax
7 and semantics are as close as possible to those of the Perl 5 language. See
8 the file Tech.Notes for some information on the internals.
9
10 This module is a wrapper that provides a POSIX API to the underlying PCRE
11 functions.
12
13 Written by: Philip Hazel <ph10@cam.ac.uk>
14
15 Copyright (c) 1997-2000 University of Cambridge
16
17 -----------------------------------------------------------------------------
18 Permission is granted to anyone to use this software for any purpose on any
19 computer system, and to redistribute it freely, subject to the following
20 restrictions:
21
22 1. This software is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25
26 2. The origin of this software must not be misrepresented, either by
27 explicit claim or by omission.
28
29 3. Altered versions must be plainly marked as such, and must not be
30 misrepresented as being the original software.
31
32 4. If PCRE is embedded in any software that is released under the GNU
33 General Purpose Licence (GPL), then the terms of that licence shall
34 supersede any condition above with which it is incompatible.
35 -----------------------------------------------------------------------------
36 */
37
38 #include "internal.h"
39 #include "pcreposix.h"
40 #include "stdlib.h"
41
42
43
44 /* Corresponding tables of PCRE error messages and POSIX error codes. */
45
46 static const char *estring[] = {
47 ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10,
48 ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,
49 ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR29, ERR29, ERR30,
50 ERR31 };
51
52 static int eint[] = {
53 REG_EESCAPE, /* "\\ at end of pattern" */
54 REG_EESCAPE, /* "\\c at end of pattern" */
55 REG_EESCAPE, /* "unrecognized character follows \\" */
56 REG_BADBR, /* "numbers out of order in {} quantifier" */
57 REG_BADBR, /* "number too big in {} quantifier" */
58 REG_EBRACK, /* "missing terminating ] for character class" */
59 REG_ECTYPE, /* "invalid escape sequence in character class" */
60 REG_ERANGE, /* "range out of order in character class" */
61 REG_BADRPT, /* "nothing to repeat" */
62 REG_BADRPT, /* "operand of unlimited repeat could match the empty string" */
63 REG_ASSERT, /* "internal error: unexpected repeat" */
64 REG_BADPAT, /* "unrecognized character after (?" */
65 REG_ESIZE, /* "too many capturing parenthesized sub-patterns" */
66 REG_EPAREN, /* "missing )" */
67 REG_ESUBREG, /* "back reference to non-existent subpattern" */
68 REG_INVARG, /* "erroffset passed as NULL" */
69 REG_INVARG, /* "unknown option bit(s) set" */
70 REG_EPAREN, /* "missing ) after comment" */
71 REG_ESIZE, /* "too many sets of parentheses" */
72 REG_ESIZE, /* "regular expression too large" */
73 REG_ESPACE, /* "failed to get memory" */
74 REG_EPAREN, /* "unmatched brackets" */
75 REG_ASSERT, /* "internal error: code overflow" */
76 REG_BADPAT, /* "unrecognized character after (?<" */
77 REG_BADPAT, /* "lookbehind assertion is not fixed length" */
78 REG_BADPAT, /* "malformed number after (?(" */
79 REG_BADPAT, /* "conditional group containe more than two branches" */
80 REG_BADPAT, /* "assertion expected after (?(" */
81 REG_BADPAT, /* "(?p must be followed by )" */
82 REG_ECTYPE, /* "unknown POSIX class name" */
83 REG_BADPAT /* "POSIX collating elements are not supported" */
84 };
85
86 /* Table of texts corresponding to POSIX error codes */
87
88 static const char *pstring[] = {
89 "", /* Dummy for value 0 */
90 "internal error", /* REG_ASSERT */
91 "invalid repeat counts in {}", /* BADBR */
92 "pattern error", /* BADPAT */
93 "? * + invalid", /* BADRPT */
94 "unbalanced {}", /* EBRACE */
95 "unbalanced []", /* EBRACK */
96 "collation error - not relevant", /* ECOLLATE */
97 "bad class", /* ECTYPE */
98 "bad escape sequence", /* EESCAPE */
99 "empty expression", /* EMPTY */
100 "unbalanced ()", /* EPAREN */
101 "bad range inside []", /* ERANGE */
102 "expression too big", /* ESIZE */
103 "failed to get memory", /* ESPACE */
104 "bad back reference", /* ESUBREG */
105 "bad argument", /* INVARG */
106 "match failed" /* NOMATCH */
107 };
108
109
110
111
112 /*************************************************
113 * Translate PCRE text code to int *
114 *************************************************/
115
116 /* PCRE compile-time errors are given as strings defined as macros. We can just
117 look them up in a table to turn them into POSIX-style error codes. */
118
119 static int
120 pcre_posix_error_code(const char *s)
121 {
122 size_t i;
123 for (i = 0; i < sizeof(estring)/sizeof(char *); i++)
124 if (strcmp(s, estring[i]) == 0) return eint[i];
125 return REG_ASSERT;
126 }
127
128
129
130 /*************************************************
131 * Translate error code to string *
132 *************************************************/
133
134 size_t
135 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
136 {
137 const char *message, *addmessage;
138 size_t length, addlength;
139
140 message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
141 "unknown error code" : pstring[errcode];
142 length = strlen(message) + 1;
143
144 addmessage = " at offset ";
145 addlength = (preg != NULL && (int)preg->re_erroffset != -1)?
146 strlen(addmessage) + 6 : 0;
147
148 if (errbuf_size > 0)
149 {
150 if (addlength > 0 && errbuf_size >= length + addlength)
151 sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
152 else
153 {
154 strncpy(errbuf, message, errbuf_size - 1);
155 errbuf[errbuf_size-1] = 0;
156 }
157 }
158
159 return length + addlength;
160 }
161
162
163
164
165 /*************************************************
166 * Free store held by a regex *
167 *************************************************/
168
169 void
170 regfree(regex_t *preg)
171 {
172 (pcre_free)(preg->re_pcre);
173 }
174
175
176
177
178 /*************************************************
179 * Compile a regular expression *
180 *************************************************/
181
182 /*
183 Arguments:
184 preg points to a structure for recording the compiled expression
185 pattern the pattern to compile
186 cflags compilation flags
187
188 Returns: 0 on success
189 various non-zero codes on failure
190 */
191
192 int
193 regcomp(regex_t *preg, const char *pattern, int cflags)
194 {
195 const char *errorptr;
196 int erroffset;
197 int options = 0;
198
199 if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
200 if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
201
202 preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL);
203 preg->re_erroffset = erroffset;
204
205 if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);
206
207 preg->re_nsub = pcre_info(preg->re_pcre, NULL, NULL);
208 return 0;
209 }
210
211
212
213
214 /*************************************************
215 * Match a regular expression *
216 *************************************************/
217
218 /* Unfortunately, PCRE requires 3 ints of working space for each captured
219 substring, so we have to get and release working store instead of just using
220 the POSIX structures as was done in earlier releases when PCRE needed only 2
221 ints. */
222
223 int
224 regexec(regex_t *preg, const char *string, size_t nmatch,
225 regmatch_t pmatch[], int eflags)
226 {
227 int rc;
228 int options = 0;
229 int *ovector = NULL;
230
231 if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
232 if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
233
234 preg->re_erroffset = (size_t)(-1); /* Only has meaning after compile */
235
236 if (nmatch > 0)
237 {
238 ovector = (int *)malloc(sizeof(int) * nmatch * 3);
239 if (ovector == NULL) return REG_ESPACE;
240 }
241
242 rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), 0, options,
243 ovector, nmatch * 3);
244
245 if (rc == 0) rc = nmatch; /* All captured slots were filled in */
246
247 if (rc >= 0)
248 {
249 size_t i;
250 for (i = 0; i < rc; i++)
251 {
252 pmatch[i].rm_so = ovector[i*2];
253 pmatch[i].rm_eo = ovector[i*2+1];
254 }
255 if (ovector != NULL) free(ovector);
256 for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
257 return 0;
258 }
259
260 else
261 {
262 if (ovector != NULL) free(ovector);
263 switch(rc)
264 {
265 case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
266 case PCRE_ERROR_NULL: return REG_INVARG;
267 case PCRE_ERROR_BADOPTION: return REG_INVARG;
268 case PCRE_ERROR_BADMAGIC: return REG_INVARG;
269 case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
270 case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
271 default: return REG_ASSERT;
272 }
273 }
274 }
275
276 /* End of pcreposix.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12