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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (hide annotations) (download)
Sat Feb 24 21:38:01 2007 UTC (7 years, 7 months ago) by nigel
Original Path: code/trunk/pcreposix.c
File MIME type: text/plain
File size: 7546 byte(s)
Load pcre-1.00 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     Copyright (c) 1997 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 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 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     int 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     char *message, *addmessage;
125     int length, adlength;
126    
127     message = (errcode >= sizeof(pstring)/sizeof(char *))?
128     "unknown error code" : pstring[errcode];
129    
130     length = (int)strlen(message) + 1;
131    
132     if (preg != NULL && (int)preg->re_erroffset != -1)
133     {
134     addmessage = " at offset ";
135     adlength = (int)strlen(addmessage) + 6;
136     }
137     else adlength = 0;
138    
139     if (errbuf_size > 0)
140     {
141     if (adlength > 0 && errbuf_size >= length + adlength)
142     sprintf(errbuf, "%s%s%-6d", message, addmessage, preg->re_erroffset);
143     else
144     {
145     strncpy(errbuf, message, errbuf_size - 1);
146     errbuf[errbuf_size-1] = 0;
147     }
148     }
149    
150     return length + adlength;
151     }
152    
153    
154    
155    
156     /*************************************************
157     * Free store held by a regex *
158     *************************************************/
159    
160     void
161     regfree(regex_t *preg)
162     {
163     (pcre_free)(preg->re_pcre);
164     }
165    
166    
167    
168    
169     /*************************************************
170     * Compile a regular expression *
171     *************************************************/
172    
173     /*
174     Arguments:
175     preg points to a structure for recording the compiled expression
176     pattern the pattern to compile
177     cflags compilation flags
178    
179     Returns: 0 on success
180     various non-zero codes on failure
181     */
182    
183     int
184     regcomp(regex_t *preg, const char *pattern, int cflags)
185     {
186     char *errorptr;
187     int erroffset;
188     int options = 0;
189    
190     if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS;
191     if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE;
192    
193     preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset);
194     preg->re_erroffset = erroffset;
195    
196     if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr);
197    
198     preg->re_nsub = pcre_info(preg->re_pcre, NULL, NULL);
199     return 0;
200     }
201    
202    
203    
204    
205     /*************************************************
206     * Match a regular expression *
207     *************************************************/
208    
209     int
210     regexec(regex_t *preg, const char *string, size_t nmatch,
211     regmatch_t pmatch[], int eflags)
212     {
213     int rc;
214     int options = 0;
215    
216     if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL;
217     if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL;
218    
219     preg->re_erroffset = -1; /* Only has meaning after compile */
220    
221     rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), options,
222     (int *)pmatch, nmatch * 2);
223    
224     if (rc == 0) return 0; /* All pmatch were filled in */
225    
226     if (rc > 0)
227     {
228     int i;
229     for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1;
230     return 0;
231     }
232    
233     else switch(rc)
234     {
235     case PCRE_ERROR_NOMATCH: return REG_NOMATCH;
236     case PCRE_ERROR_BADREF: return REG_ESUBREG;
237     case PCRE_ERROR_NULL: return REG_INVARG;
238     case PCRE_ERROR_BADOPTION: return REG_INVARG;
239     case PCRE_ERROR_BADMAGIC: return REG_INVARG;
240     case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT;
241     case PCRE_ERROR_NOMEMORY: return REG_ESPACE;
242     default: return REG_ASSERT;
243     }
244     }
245    
246     /* End of pcreposix.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12