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

Contents of /code/trunk/pcreposix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 75 - (hide annotations) (download)
Sat Feb 24 21:40:37 2007 UTC (7 years, 4 months ago) by nigel
File MIME type: text/plain
File size: 11279 byte(s)
Load pcre-5.0 into code/trunk.

1 nigel 3 /*************************************************
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 nigel 75 Copyright (c) 1997-2004 University of Cambridge
16 nigel 3
17     -----------------------------------------------------------------------------
18 nigel 75 Redistribution and use in source and binary forms, with or without
19     modification, are permitted provided that the following conditions are met:
20 nigel 3
21 nigel 75 * Redistributions of source code must retain the above copyright notice,
22     this list of conditions and the following disclaimer.
23 nigel 3
24 nigel 75 * Redistributions in binary form must reproduce the above copyright
25     notice, this list of conditions and the following disclaimer in the
26     documentation and/or other materials provided with the distribution.
27 nigel 3
28 nigel 75 * Neither the name of the University of Cambridge nor the names of its
29     contributors may be used to endorse or promote products derived from
30     this software without specific prior written permission.
31 nigel 29
32 nigel 75 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35     ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
36     LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
40     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42     POSSIBILITY OF SUCH DAMAGE.
43 nigel 3 -----------------------------------------------------------------------------
44     */
45    
46     #include "internal.h"
47     #include "pcreposix.h"
48     #include "stdlib.h"
49    
50    
51    
52     /* Corresponding tables of PCRE error messages and POSIX error codes. */
53    
54 nigel 73 static const char *const estring[] = {
55 nigel 3 ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10,
56     ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,
57 nigel 43 ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR29, ERR29, ERR30,
58 nigel 63 ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, ERR40,
59 nigel 75 ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47 };
60 nigel 3
61 nigel 73 static const int eint[] = {
62 nigel 3 REG_EESCAPE, /* "\\ at end of pattern" */
63     REG_EESCAPE, /* "\\c at end of pattern" */
64     REG_EESCAPE, /* "unrecognized character follows \\" */
65     REG_BADBR, /* "numbers out of order in {} quantifier" */
66     REG_BADBR, /* "number too big in {} quantifier" */
67     REG_EBRACK, /* "missing terminating ] for character class" */
68     REG_ECTYPE, /* "invalid escape sequence in character class" */
69     REG_ERANGE, /* "range out of order in character class" */
70     REG_BADRPT, /* "nothing to repeat" */
71     REG_BADRPT, /* "operand of unlimited repeat could match the empty string" */
72     REG_ASSERT, /* "internal error: unexpected repeat" */
73     REG_BADPAT, /* "unrecognized character after (?" */
74 nigel 63 REG_BADPAT, /* "POSIX named classes are supported only within a class" */
75 nigel 3 REG_EPAREN, /* "missing )" */
76 nigel 63 REG_ESUBREG, /* "reference to non-existent subpattern" */
77 nigel 3 REG_INVARG, /* "erroffset passed as NULL" */
78     REG_INVARG, /* "unknown option bit(s) set" */
79     REG_EPAREN, /* "missing ) after comment" */
80 nigel 53 REG_ESIZE, /* "parentheses nested too deeply" */
81 nigel 3 REG_ESIZE, /* "regular expression too large" */
82     REG_ESPACE, /* "failed to get memory" */
83     REG_EPAREN, /* "unmatched brackets" */
84 nigel 23 REG_ASSERT, /* "internal error: code overflow" */
85     REG_BADPAT, /* "unrecognized character after (?<" */
86     REG_BADPAT, /* "lookbehind assertion is not fixed length" */
87     REG_BADPAT, /* "malformed number after (?(" */
88     REG_BADPAT, /* "conditional group containe more than two branches" */
89 nigel 43 REG_BADPAT, /* "assertion expected after (?(" */
90 nigel 63 REG_BADPAT, /* "(?R or (?digits must be followed by )" */
91 nigel 43 REG_ECTYPE, /* "unknown POSIX class name" */
92 nigel 49 REG_BADPAT, /* "POSIX collating elements are not supported" */
93     REG_INVARG, /* "this version of PCRE is not compiled with PCRE_UTF8 support" */
94 nigel 63 REG_BADPAT, /* "spare error" */
95 nigel 51 REG_BADPAT, /* "character value in \x{...} sequence is too large" */
96 nigel 63 REG_BADPAT, /* "invalid condition (?(0)" */
97     REG_BADPAT, /* "\\C not allowed in lookbehind assertion" */
98 nigel 75 REG_EESCAPE, /* "PCRE does not support \\L, \\l, \\N, \\U, or \\u" */
99 nigel 63 REG_BADPAT, /* "number after (?C is > 255" */
100     REG_BADPAT, /* "closing ) for (?C expected" */
101     REG_BADPAT, /* "recursive call could loop indefinitely" */
102     REG_BADPAT, /* "unrecognized character after (?P" */
103     REG_BADPAT, /* "syntax error after (?P" */
104 nigel 71 REG_BADPAT, /* "two named groups have the same name" */
105 nigel 75 REG_BADPAT, /* "invalid UTF-8 string" */
106     REG_BADPAT, /* "support for \\P, \\p, and \\X has not been compiled" */
107     REG_BADPAT, /* "malformed \\P or \\p sequence" */
108     REG_BADPAT /* "unknown property name after \\P or \\p" */
109 nigel 3 };
110    
111     /* Table of texts corresponding to POSIX error codes */
112    
113 nigel 73 static const char *const pstring[] = {
114 nigel 3 "", /* Dummy for value 0 */
115     "internal error", /* REG_ASSERT */
116     "invalid repeat counts in {}", /* BADBR */
117     "pattern error", /* BADPAT */
118     "? * + invalid", /* BADRPT */
119     "unbalanced {}", /* EBRACE */
120     "unbalanced []", /* EBRACK */
121     "collation error - not relevant", /* ECOLLATE */
122     "bad class", /* ECTYPE */
123     "bad escape sequence", /* EESCAPE */
124     "empty expression", /* EMPTY */
125     "unbalanced ()", /* EPAREN */
126     "bad range inside []", /* ERANGE */
127     "expression too big", /* ESIZE */
128     "failed to get memory", /* ESPACE */
129     "bad back reference", /* ESUBREG */
130     "bad argument", /* INVARG */
131     "match failed" /* NOMATCH */
132     };
133    
134    
135    
136    
137     /*************************************************
138     * Translate PCRE text code to int *
139     *************************************************/
140    
141     /* PCRE compile-time errors are given as strings defined as macros. We can just
142     look them up in a table to turn them into POSIX-style error codes. */
143    
144     static int
145     pcre_posix_error_code(const char *s)
146     {
147 nigel 7 size_t i;
148 nigel 3 for (i = 0; i < sizeof(estring)/sizeof(char *); i++)
149     if (strcmp(s, estring[i]) == 0) return eint[i];
150     return REG_ASSERT;
151     }
152    
153    
154    
155     /*************************************************
156     * Translate error code to string *
157     *************************************************/
158    
159 nigel 73 EXPORT size_t
160 nigel 3 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
161     {
162 nigel 7 const char *message, *addmessage;
163     size_t length, addlength;
164 nigel 3
165 nigel 7 message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
166 nigel 3 "unknown error code" : pstring[errcode];
167 nigel 7 length = strlen(message) + 1;
168 nigel 3
169 nigel 7 addmessage = " at offset ";
170     addlength = (preg != NULL && (int)preg->re_erroffset != -1)?
171     strlen(addmessage) + 6 : 0;
172 nigel 3
173     if (errbuf_size > 0)
174     {
175 nigel 7 if (addlength > 0 && errbuf_size >= length + addlength)
176 nigel 23 sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
177 nigel 3 else
178     {
179     strncpy(errbuf, message, errbuf_size - 1);
180     errbuf[errbuf_size-1] = 0;
181     }
182     }
183    
184 nigel 7 return length + addlength;
185 nigel 3 }
186    
187    
188    
189    
190     /*************************************************
191     * Free store held by a regex *
192     *************************************************/
193    
194 nigel 73 EXPORT void
195 nigel 3 regfree(regex_t *preg)
196     {
197     (pcre_free)(preg->re_pcre);
198     }
199    
200    
201    
202    
203     /*************************************************
204     * Compile a regular expression *
205     *************************************************/
206    
207     /*
208     Arguments:
209     preg points to a structure for recording the compiled expression
210     pattern the pattern to compile
211     cflags compilation flags
212    
213     Returns: 0 on success
214     various non-zero codes on failure
215     */
216    
217 nigel 73 EXPORT int
218 nigel 3 regcomp(regex_t *preg, const char *pattern, int cflags)
219     {
220 nigel 7 const char *errorptr;
221 nigel 3 int erroffset;
222     int options = 0;
223    
224     if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
225     if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
226    
227 nigel 25 preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL);
228 nigel 3 preg->re_erroffset = erroffset;
229    
230     if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);
231    
232 nigel 71 preg->re_nsub = pcre_info((const pcre *)preg->re_pcre, NULL, NULL);
233 nigel 3 return 0;
234     }
235    
236    
237    
238    
239     /*************************************************
240     * Match a regular expression *
241     *************************************************/
242    
243 nigel 41 /* Unfortunately, PCRE requires 3 ints of working space for each captured
244     substring, so we have to get and release working store instead of just using
245     the POSIX structures as was done in earlier releases when PCRE needed only 2
246 nigel 63 ints. However, if the number of possible capturing brackets is small, use a
247     block of store on the stack, to reduce the use of malloc/free. The threshold is
248     in a macro that can be changed at configure time. */
249 nigel 41
250 nigel 73 EXPORT int
251 nigel 69 regexec(const regex_t *preg, const char *string, size_t nmatch,
252 nigel 3 regmatch_t pmatch[], int eflags)
253     {
254     int rc;
255     int options = 0;
256 nigel 41 int *ovector = NULL;
257 nigel 63 int small_ovector[POSIX_MALLOC_THRESHOLD * 3];
258     BOOL allocated_ovector = FALSE;
259 nigel 3
260     if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
261     if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
262    
263 nigel 69 ((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */
264 nigel 3
265 nigel 41 if (nmatch > 0)
266     {
267 nigel 63 if (nmatch <= POSIX_MALLOC_THRESHOLD)
268     {
269     ovector = &(small_ovector[0]);
270     }
271     else
272     {
273     ovector = (int *)malloc(sizeof(int) * nmatch * 3);
274     if (ovector == NULL) return REG_ESPACE;
275     allocated_ovector = TRUE;
276     }
277 nigel 41 }
278    
279 nigel 71 rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string, (int)strlen(string),
280     0, options, ovector, nmatch * 3);
281 nigel 3
282 nigel 41 if (rc == 0) rc = nmatch; /* All captured slots were filled in */
283 nigel 3
284 nigel 41 if (rc >= 0)
285 nigel 3 {
286 nigel 7 size_t i;
287 nigel 63 for (i = 0; i < (size_t)rc; i++)
288 nigel 41 {
289     pmatch[i].rm_so = ovector[i*2];
290     pmatch[i].rm_eo = ovector[i*2+1];
291     }
292 nigel 63 if (allocated_ovector) free(ovector);
293 nigel 41 for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
294 nigel 3 return 0;
295     }
296    
297 nigel 41 else
298 nigel 3 {
299 nigel 63 if (allocated_ovector) free(ovector);
300 nigel 41 switch(rc)
301     {
302     case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
303     case PCRE_ERROR_NULL: return REG_INVARG;
304     case PCRE_ERROR_BADOPTION: return REG_INVARG;
305     case PCRE_ERROR_BADMAGIC: return REG_INVARG;
306     case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
307     case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
308 nigel 73 case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE;
309     case PCRE_ERROR_BADUTF8: return REG_INVARG;
310     case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG;
311 nigel 41 default: return REG_ASSERT;
312     }
313 nigel 3 }
314     }
315    
316     /* End of pcreposix.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12