1*06f32e7eSjoerg /*-
2*06f32e7eSjoerg * This code is derived from OpenBSD's libc/regex, original license follows:
3*06f32e7eSjoerg *
4*06f32e7eSjoerg * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5*06f32e7eSjoerg * Copyright (c) 1992, 1993, 1994
6*06f32e7eSjoerg * The Regents of the University of California. All rights reserved.
7*06f32e7eSjoerg *
8*06f32e7eSjoerg * This code is derived from software contributed to Berkeley by
9*06f32e7eSjoerg * Henry Spencer.
10*06f32e7eSjoerg *
11*06f32e7eSjoerg * Redistribution and use in source and binary forms, with or without
12*06f32e7eSjoerg * modification, are permitted provided that the following conditions
13*06f32e7eSjoerg * are met:
14*06f32e7eSjoerg * 1. Redistributions of source code must retain the above copyright
15*06f32e7eSjoerg * notice, this list of conditions and the following disclaimer.
16*06f32e7eSjoerg * 2. Redistributions in binary form must reproduce the above copyright
17*06f32e7eSjoerg * notice, this list of conditions and the following disclaimer in the
18*06f32e7eSjoerg * documentation and/or other materials provided with the distribution.
19*06f32e7eSjoerg * 3. Neither the name of the University nor the names of its contributors
20*06f32e7eSjoerg * may be used to endorse or promote products derived from this software
21*06f32e7eSjoerg * without specific prior written permission.
22*06f32e7eSjoerg *
23*06f32e7eSjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*06f32e7eSjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*06f32e7eSjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*06f32e7eSjoerg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*06f32e7eSjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*06f32e7eSjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*06f32e7eSjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*06f32e7eSjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*06f32e7eSjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*06f32e7eSjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*06f32e7eSjoerg * SUCH DAMAGE.
34*06f32e7eSjoerg *
35*06f32e7eSjoerg * @(#)regerror.c 8.4 (Berkeley) 3/20/94
36*06f32e7eSjoerg */
37*06f32e7eSjoerg
38*06f32e7eSjoerg #include <sys/types.h>
39*06f32e7eSjoerg #include <stdio.h>
40*06f32e7eSjoerg #include <string.h>
41*06f32e7eSjoerg #include <ctype.h>
42*06f32e7eSjoerg #include <limits.h>
43*06f32e7eSjoerg #include <stdlib.h>
44*06f32e7eSjoerg #include "regex_impl.h"
45*06f32e7eSjoerg
46*06f32e7eSjoerg #include "regutils.h"
47*06f32e7eSjoerg
48*06f32e7eSjoerg #ifdef _MSC_VER
49*06f32e7eSjoerg #define snprintf _snprintf
50*06f32e7eSjoerg #endif
51*06f32e7eSjoerg
52*06f32e7eSjoerg static const char *regatoi(const llvm_regex_t *, char *, int);
53*06f32e7eSjoerg
54*06f32e7eSjoerg static struct rerr {
55*06f32e7eSjoerg int code;
56*06f32e7eSjoerg const char *name;
57*06f32e7eSjoerg const char *explain;
58*06f32e7eSjoerg } rerrs[] = {
59*06f32e7eSjoerg { REG_NOMATCH, "REG_NOMATCH", "llvm_regexec() failed to match" },
60*06f32e7eSjoerg { REG_BADPAT, "REG_BADPAT", "invalid regular expression" },
61*06f32e7eSjoerg { REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element" },
62*06f32e7eSjoerg { REG_ECTYPE, "REG_ECTYPE", "invalid character class" },
63*06f32e7eSjoerg { REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)" },
64*06f32e7eSjoerg { REG_ESUBREG, "REG_ESUBREG", "invalid backreference number" },
65*06f32e7eSjoerg { REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced" },
66*06f32e7eSjoerg { REG_EPAREN, "REG_EPAREN", "parentheses not balanced" },
67*06f32e7eSjoerg { REG_EBRACE, "REG_EBRACE", "braces not balanced" },
68*06f32e7eSjoerg { REG_BADBR, "REG_BADBR", "invalid repetition count(s)" },
69*06f32e7eSjoerg { REG_ERANGE, "REG_ERANGE", "invalid character range" },
70*06f32e7eSjoerg { REG_ESPACE, "REG_ESPACE", "out of memory" },
71*06f32e7eSjoerg { REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid" },
72*06f32e7eSjoerg { REG_EMPTY, "REG_EMPTY", "empty (sub)expression" },
73*06f32e7eSjoerg { REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug" },
74*06f32e7eSjoerg { REG_INVARG, "REG_INVARG", "invalid argument to regex routine" },
75*06f32e7eSjoerg { 0, "", "*** unknown regexp error code ***" }
76*06f32e7eSjoerg };
77*06f32e7eSjoerg
78*06f32e7eSjoerg /*
79*06f32e7eSjoerg - llvm_regerror - the interface to error numbers
80*06f32e7eSjoerg = extern size_t llvm_regerror(int, const llvm_regex_t *, char *, size_t);
81*06f32e7eSjoerg */
82*06f32e7eSjoerg /* ARGSUSED */
83*06f32e7eSjoerg size_t
llvm_regerror(int errcode,const llvm_regex_t * preg,char * errbuf,size_t errbuf_size)84*06f32e7eSjoerg llvm_regerror(int errcode, const llvm_regex_t *preg, char *errbuf, size_t errbuf_size)
85*06f32e7eSjoerg {
86*06f32e7eSjoerg struct rerr *r;
87*06f32e7eSjoerg size_t len;
88*06f32e7eSjoerg int target = errcode &~ REG_ITOA;
89*06f32e7eSjoerg const char *s;
90*06f32e7eSjoerg char convbuf[50];
91*06f32e7eSjoerg
92*06f32e7eSjoerg if (errcode == REG_ATOI)
93*06f32e7eSjoerg s = regatoi(preg, convbuf, sizeof convbuf);
94*06f32e7eSjoerg else {
95*06f32e7eSjoerg for (r = rerrs; r->code != 0; r++)
96*06f32e7eSjoerg if (r->code == target)
97*06f32e7eSjoerg break;
98*06f32e7eSjoerg
99*06f32e7eSjoerg if (errcode®_ITOA) {
100*06f32e7eSjoerg if (r->code != 0) {
101*06f32e7eSjoerg assert(strlen(r->name) < sizeof(convbuf));
102*06f32e7eSjoerg (void) llvm_strlcpy(convbuf, r->name, sizeof convbuf);
103*06f32e7eSjoerg } else
104*06f32e7eSjoerg (void)snprintf(convbuf, sizeof convbuf,
105*06f32e7eSjoerg "REG_0x%x", target);
106*06f32e7eSjoerg s = convbuf;
107*06f32e7eSjoerg } else
108*06f32e7eSjoerg s = r->explain;
109*06f32e7eSjoerg }
110*06f32e7eSjoerg
111*06f32e7eSjoerg len = strlen(s) + 1;
112*06f32e7eSjoerg if (errbuf_size > 0) {
113*06f32e7eSjoerg llvm_strlcpy(errbuf, s, errbuf_size);
114*06f32e7eSjoerg }
115*06f32e7eSjoerg
116*06f32e7eSjoerg return(len);
117*06f32e7eSjoerg }
118*06f32e7eSjoerg
119*06f32e7eSjoerg /*
120*06f32e7eSjoerg - regatoi - internal routine to implement REG_ATOI
121*06f32e7eSjoerg */
122*06f32e7eSjoerg static const char *
regatoi(const llvm_regex_t * preg,char * localbuf,int localbufsize)123*06f32e7eSjoerg regatoi(const llvm_regex_t *preg, char *localbuf, int localbufsize)
124*06f32e7eSjoerg {
125*06f32e7eSjoerg struct rerr *r;
126*06f32e7eSjoerg
127*06f32e7eSjoerg for (r = rerrs; r->code != 0; r++)
128*06f32e7eSjoerg if (strcmp(r->name, preg->re_endp) == 0)
129*06f32e7eSjoerg break;
130*06f32e7eSjoerg if (r->code == 0)
131*06f32e7eSjoerg return("0");
132*06f32e7eSjoerg
133*06f32e7eSjoerg (void)snprintf(localbuf, localbufsize, "%d", r->code);
134*06f32e7eSjoerg return(localbuf);
135*06f32e7eSjoerg }
136