/[pcre]/code/tags/pcre-1.07/pcreposix.c
ViewVC logotype

Contents of /code/tags/pcre-1.07/pcreposix.c

Parent Directory Parent Directory | Revision Log Revision Log


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

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12