/[pcre]/code/branches/pcre16/pcreposix.c
ViewVC logotype

Contents of /code/branches/pcre16/pcreposix.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (hide annotations) (download)
Sat Feb 24 21:39:05 2007 UTC (7 years, 1 month ago) by nigel
Original Path: code/trunk/pcreposix.c
File MIME type: text/plain
File size: 8051 byte(s)
Load pcre-2.06 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 27 Copyright (c) 1997-1999 University of Cambridge
16 nigel 3
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 nigel 29
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 nigel 3 -----------------------------------------------------------------------------
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 nigel 7 static const char *estring[] = {
47 nigel 3 ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10,
48     ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20,
49 nigel 23 ERR21, ERR22, ERR23, ERR24, ERR25 };
50 nigel 3
51     static int eint[] = {
52     REG_EESCAPE, /* "\\ at end of pattern" */
53     REG_EESCAPE, /* "\\c at end of pattern" */
54     REG_EESCAPE, /* "unrecognized character follows \\" */
55     REG_BADBR, /* "numbers out of order in {} quantifier" */
56     REG_BADBR, /* "number too big in {} quantifier" */
57     REG_EBRACK, /* "missing terminating ] for character class" */
58     REG_ECTYPE, /* "invalid escape sequence in character class" */
59     REG_ERANGE, /* "range out of order in character class" */
60     REG_BADRPT, /* "nothing to repeat" */
61     REG_BADRPT, /* "operand of unlimited repeat could match the empty string" */
62     REG_ASSERT, /* "internal error: unexpected repeat" */
63     REG_BADPAT, /* "unrecognized character after (?" */
64     REG_ESIZE, /* "too many capturing parenthesized sub-patterns" */
65     REG_EPAREN, /* "missing )" */
66     REG_ESUBREG, /* "back reference to non-existent subpattern" */
67     REG_INVARG, /* "erroffset passed as NULL" */
68     REG_INVARG, /* "unknown option bit(s) set" */
69     REG_EPAREN, /* "missing ) after comment" */
70     REG_ESIZE, /* "too many sets of parentheses" */
71     REG_ESIZE, /* "regular expression too large" */
72     REG_ESPACE, /* "failed to get memory" */
73     REG_EPAREN, /* "unmatched brackets" */
74 nigel 23 REG_ASSERT, /* "internal error: code overflow" */
75     REG_BADPAT, /* "unrecognized character after (?<" */
76     REG_BADPAT, /* "lookbehind assertion is not fixed length" */
77     REG_BADPAT, /* "malformed number after (?(" */
78     REG_BADPAT, /* "conditional group containe more than two branches" */
79     REG_BADPAT /* "assertion expected after (?(" */
80 nigel 3 };
81    
82     /* Table of texts corresponding to POSIX error codes */
83    
84 nigel 7 static const char *pstring[] = {
85 nigel 3 "", /* Dummy for value 0 */
86     "internal error", /* REG_ASSERT */
87     "invalid repeat counts in {}", /* BADBR */
88     "pattern error", /* BADPAT */
89     "? * + invalid", /* BADRPT */
90     "unbalanced {}", /* EBRACE */
91     "unbalanced []", /* EBRACK */
92     "collation error - not relevant", /* ECOLLATE */
93     "bad class", /* ECTYPE */
94     "bad escape sequence", /* EESCAPE */
95     "empty expression", /* EMPTY */
96     "unbalanced ()", /* EPAREN */
97     "bad range inside []", /* ERANGE */
98     "expression too big", /* ESIZE */
99     "failed to get memory", /* ESPACE */
100     "bad back reference", /* ESUBREG */
101     "bad argument", /* INVARG */
102     "match failed" /* NOMATCH */
103     };
104    
105    
106    
107    
108     /*************************************************
109     * Translate PCRE text code to int *
110     *************************************************/
111    
112     /* PCRE compile-time errors are given as strings defined as macros. We can just
113     look them up in a table to turn them into POSIX-style error codes. */
114    
115     static int
116     pcre_posix_error_code(const char *s)
117     {
118 nigel 7 size_t i;
119 nigel 3 for (i = 0; i < sizeof(estring)/sizeof(char *); i++)
120     if (strcmp(s, estring[i]) == 0) return eint[i];
121     return REG_ASSERT;
122     }
123    
124    
125    
126     /*************************************************
127     * Translate error code to string *
128     *************************************************/
129    
130     size_t
131     regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
132     {
133 nigel 7 const char *message, *addmessage;
134     size_t length, addlength;
135 nigel 3
136 nigel 7 message = (errcode >= (int)(sizeof(pstring)/sizeof(char *)))?
137 nigel 3 "unknown error code" : pstring[errcode];
138 nigel 7 length = strlen(message) + 1;
139 nigel 3
140 nigel 7 addmessage = " at offset ";
141     addlength = (preg != NULL && (int)preg->re_erroffset != -1)?
142     strlen(addmessage) + 6 : 0;
143 nigel 3
144     if (errbuf_size > 0)
145     {
146 nigel 7 if (addlength > 0 && errbuf_size >= length + addlength)
147 nigel 23 sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset);
148 nigel 3 else
149     {
150     strncpy(errbuf, message, errbuf_size - 1);
151     errbuf[errbuf_size-1] = 0;
152     }
153     }
154    
155 nigel 7 return length + addlength;
156 nigel 3 }
157    
158    
159    
160    
161     /*************************************************
162     * Free store held by a regex *
163     *************************************************/
164    
165     void
166     regfree(regex_t *preg)
167     {
168     (pcre_free)(preg->re_pcre);
169     }
170    
171    
172    
173    
174     /*************************************************
175     * Compile a regular expression *
176     *************************************************/
177    
178     /*
179     Arguments:
180     preg points to a structure for recording the compiled expression
181     pattern the pattern to compile
182     cflags compilation flags
183    
184     Returns: 0 on success
185     various non-zero codes on failure
186     */
187    
188     int
189     regcomp(regex_t *preg, const char *pattern, int cflags)
190     {
191 nigel 7 const char *errorptr;
192 nigel 3 int erroffset;
193     int options = 0;
194    
195     if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
196     if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
197    
198 nigel 25 preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL);
199 nigel 3 preg->re_erroffset = erroffset;
200    
201     if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);
202    
203     preg->re_nsub = pcre_info(preg->re_pcre, NULL, NULL);
204     return 0;
205     }
206    
207    
208    
209    
210     /*************************************************
211     * Match a regular expression *
212     *************************************************/
213    
214     int
215     regexec(regex_t *preg, const char *string, size_t nmatch,
216     regmatch_t pmatch[], int eflags)
217     {
218     int rc;
219     int options = 0;
220    
221     if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
222     if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
223    
224 nigel 15 preg->re_erroffset = (size_t)(-1); /* Only has meaning after compile */
225 nigel 3
226 nigel 35 rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), 0, options,
227 nigel 3 (int *)pmatch, nmatch * 2);
228    
229     if (rc == 0) return 0; /* All pmatch were filled in */
230    
231     if (rc > 0)
232     {
233 nigel 7 size_t i;
234 nigel 3 for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
235     return 0;
236     }
237    
238     else switch(rc)
239     {
240     case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
241     case PCRE_ERROR_NULL: return REG_INVARG;
242     case PCRE_ERROR_BADOPTION: return REG_INVARG;
243     case PCRE_ERROR_BADMAGIC: return REG_INVARG;
244     case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
245     case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
246     default: return REG_ASSERT;
247     }
248     }
249    
250     /* End of pcreposix.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12