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