xref: /minix3/lib/libc/regex/regerror.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: regerror.c,v 1.23 2007/02/09 23:44:18 junyoung Exp $	*/
2*2fe8fb19SBen Gras 
3b7061124SArun Thomas /*-
4b7061124SArun Thomas  * Copyright (c) 1992, 1993, 1994
5b7061124SArun Thomas  *	The Regents of the University of California.  All rights reserved.
6b7061124SArun Thomas  *
7b7061124SArun Thomas  * This code is derived from software contributed to Berkeley by
8b7061124SArun Thomas  * Henry Spencer.
9b7061124SArun Thomas  *
10b7061124SArun Thomas  * Redistribution and use in source and binary forms, with or without
11b7061124SArun Thomas  * modification, are permitted provided that the following conditions
12b7061124SArun Thomas  * are met:
13b7061124SArun Thomas  * 1. Redistributions of source code must retain the above copyright
14b7061124SArun Thomas  *    notice, this list of conditions and the following disclaimer.
15b7061124SArun Thomas  * 2. Redistributions in binary form must reproduce the above copyright
16b7061124SArun Thomas  *    notice, this list of conditions and the following disclaimer in the
17b7061124SArun Thomas  *    documentation and/or other materials provided with the distribution.
18*2fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
19*2fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
20*2fe8fb19SBen Gras  *    without specific prior written permission.
21*2fe8fb19SBen Gras  *
22*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*2fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*2fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*2fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*2fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*2fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*2fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*2fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*2fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*2fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*2fe8fb19SBen Gras  * SUCH DAMAGE.
33*2fe8fb19SBen Gras  *
34*2fe8fb19SBen Gras  *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
35*2fe8fb19SBen Gras  */
36*2fe8fb19SBen Gras 
37*2fe8fb19SBen Gras /*-
38*2fe8fb19SBen Gras  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
39*2fe8fb19SBen Gras  *
40*2fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
41*2fe8fb19SBen Gras  * Henry Spencer.
42*2fe8fb19SBen Gras  *
43*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
44*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
45*2fe8fb19SBen Gras  * are met:
46*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
47*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
48*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
49*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
50*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
51b7061124SArun Thomas  * 3. All advertising materials mentioning features or use of this software
52b7061124SArun Thomas  *    must display the following acknowledgement:
53b7061124SArun Thomas  *	This product includes software developed by the University of
54b7061124SArun Thomas  *	California, Berkeley and its contributors.
55b7061124SArun Thomas  * 4. Neither the name of the University nor the names of its contributors
56b7061124SArun Thomas  *    may be used to endorse or promote products derived from this software
57b7061124SArun Thomas  *    without specific prior written permission.
58b7061124SArun Thomas  *
59b7061124SArun Thomas  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60b7061124SArun Thomas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61b7061124SArun Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62b7061124SArun Thomas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63b7061124SArun Thomas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64b7061124SArun Thomas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65b7061124SArun Thomas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66b7061124SArun Thomas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67b7061124SArun Thomas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68b7061124SArun Thomas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69b7061124SArun Thomas  * SUCH DAMAGE.
70b7061124SArun Thomas  *
71b7061124SArun Thomas  *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
72b7061124SArun Thomas  */
73b7061124SArun Thomas 
74*2fe8fb19SBen Gras #include <sys/cdefs.h>
75b7061124SArun Thomas #if defined(LIBC_SCCS) && !defined(lint)
76*2fe8fb19SBen Gras #if 0
77b7061124SArun Thomas static char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
78*2fe8fb19SBen Gras #else
79*2fe8fb19SBen Gras __RCSID("$NetBSD: regerror.c,v 1.23 2007/02/09 23:44:18 junyoung Exp $");
80*2fe8fb19SBen Gras #endif
81b7061124SArun Thomas #endif /* LIBC_SCCS and not lint */
82b7061124SArun Thomas 
83*2fe8fb19SBen Gras #include "namespace.h"
84b7061124SArun Thomas #include <sys/types.h>
85*2fe8fb19SBen Gras 
86*2fe8fb19SBen Gras #include <assert.h>
87b7061124SArun Thomas #include <ctype.h>
88*2fe8fb19SBen Gras #include <limits.h>
89*2fe8fb19SBen Gras #include <stdio.h>
90b7061124SArun Thomas #include <stdlib.h>
91*2fe8fb19SBen Gras #include <string.h>
92b7061124SArun Thomas #include <regex.h>
93b7061124SArun Thomas 
94*2fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(regerror,_regerror)95*2fe8fb19SBen Gras __weak_alias(regerror,_regerror)
96*2fe8fb19SBen Gras #endif
97*2fe8fb19SBen Gras 
98b7061124SArun Thomas #include "utils.h"
99b7061124SArun Thomas 
100b7061124SArun Thomas /* ========= begin header generated by ./mkh ========= */
101b7061124SArun Thomas #ifdef __cplusplus
102b7061124SArun Thomas extern "C" {
103b7061124SArun Thomas #endif
104b7061124SArun Thomas 
105b7061124SArun Thomas /* === regerror.c === */
106*2fe8fb19SBen Gras static const char *regatoi(const regex_t *preg, char *localbuf, size_t buflen);
107b7061124SArun Thomas 
108b7061124SArun Thomas #ifdef __cplusplus
109b7061124SArun Thomas }
110b7061124SArun Thomas #endif
111b7061124SArun Thomas /* ========= end header generated by ./mkh ========= */
112b7061124SArun Thomas /*
113b7061124SArun Thomas  = #define	REG_NOMATCH	 1
114b7061124SArun Thomas  = #define	REG_BADPAT	 2
115b7061124SArun Thomas  = #define	REG_ECOLLATE	 3
116b7061124SArun Thomas  = #define	REG_ECTYPE	 4
117b7061124SArun Thomas  = #define	REG_EESCAPE	 5
118b7061124SArun Thomas  = #define	REG_ESUBREG	 6
119b7061124SArun Thomas  = #define	REG_EBRACK	 7
120b7061124SArun Thomas  = #define	REG_EPAREN	 8
121b7061124SArun Thomas  = #define	REG_EBRACE	 9
122b7061124SArun Thomas  = #define	REG_BADBR	10
123b7061124SArun Thomas  = #define	REG_ERANGE	11
124b7061124SArun Thomas  = #define	REG_ESPACE	12
125b7061124SArun Thomas  = #define	REG_BADRPT	13
126b7061124SArun Thomas  = #define	REG_EMPTY	14
127b7061124SArun Thomas  = #define	REG_ASSERT	15
128b7061124SArun Thomas  = #define	REG_INVARG	16
129b7061124SArun Thomas  = #define	REG_ATOI	255	// convert name to number (!)
130b7061124SArun Thomas  = #define	REG_ITOA	0400	// convert number to name (!)
131b7061124SArun Thomas  */
132*2fe8fb19SBen Gras static const struct rerr {
133b7061124SArun Thomas 	int code;
134*2fe8fb19SBen Gras 	const char *name;
135*2fe8fb19SBen Gras 	const char *explain;
136b7061124SArun Thomas } rerrs[] = {
137e3f68488SBen Gras 	{ REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match" },
138e3f68488SBen Gras 	{ REG_BADPAT,	"REG_BADPAT",	"invalid regular expression" },
139e3f68488SBen Gras 	{ REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element" },
140e3f68488SBen Gras 	{ REG_ECTYPE,	"REG_ECTYPE",	"invalid character class" },
141e3f68488SBen Gras 	{ REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)" },
142e3f68488SBen Gras 	{ REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number" },
143e3f68488SBen Gras 	{ REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced" },
144e3f68488SBen Gras 	{ REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced" },
145e3f68488SBen Gras 	{ REG_EBRACE,	"REG_EBRACE",	"braces not balanced" },
146e3f68488SBen Gras 	{ REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)" },
147e3f68488SBen Gras 	{ REG_ERANGE,	"REG_ERANGE",	"invalid character range" },
148e3f68488SBen Gras 	{ REG_ESPACE,	"REG_ESPACE",	"out of memory" },
149e3f68488SBen Gras 	{ REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid" },
150e3f68488SBen Gras 	{ REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression" },
151e3f68488SBen Gras 	{ REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug" },
152e3f68488SBen Gras 	{ REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine" },
153e3f68488SBen Gras 	{ 0,		"",		"*** unknown regexp error code ***" }
154b7061124SArun Thomas };
155b7061124SArun Thomas 
156b7061124SArun Thomas /*
157*2fe8fb19SBen Gras  * regerror - the interface to error numbers
158*2fe8fb19SBen Gras  * extern size_t regerror(int, const regex_t *, char *, size_t);
159b7061124SArun Thomas  */
160b7061124SArun Thomas /* ARGSUSED */
161b7061124SArun Thomas size_t
regerror(int errcode,const regex_t * preg,char * errbuf,size_t errbuf_size)162*2fe8fb19SBen Gras regerror(
163*2fe8fb19SBen Gras     int errcode,
164*2fe8fb19SBen Gras     const regex_t *preg,
165*2fe8fb19SBen Gras     char *errbuf,
166*2fe8fb19SBen Gras     size_t errbuf_size)
167b7061124SArun Thomas {
168*2fe8fb19SBen Gras 	const struct rerr *r;
169*2fe8fb19SBen Gras 	size_t len;
170*2fe8fb19SBen Gras 	int target = errcode &~ REG_ITOA;
171*2fe8fb19SBen Gras 	const char *s;
172b7061124SArun Thomas 	char convbuf[50];
173b7061124SArun Thomas 
174*2fe8fb19SBen Gras 	_DIAGASSERT(errcode != REG_ATOI || preg != NULL);
175*2fe8fb19SBen Gras 	_DIAGASSERT(errbuf != NULL);
176*2fe8fb19SBen Gras 
177b7061124SArun Thomas 	if (errcode == REG_ATOI)
178*2fe8fb19SBen Gras 		s = regatoi(preg, convbuf, sizeof convbuf);
179b7061124SArun Thomas 	else {
180b7061124SArun Thomas 		for (r = rerrs; r->code != 0; r++)
181b7061124SArun Thomas 			if (r->code == target)
182b7061124SArun Thomas 				break;
183b7061124SArun Thomas 
184b7061124SArun Thomas 		if (errcode & REG_ITOA) {
185*2fe8fb19SBen Gras 			if (r->code != 0) {
186*2fe8fb19SBen Gras 				(void)strlcpy(convbuf, r->name, sizeof convbuf);
187*2fe8fb19SBen Gras 			} else
188*2fe8fb19SBen Gras 				(void)snprintf(convbuf, sizeof convbuf,
189*2fe8fb19SBen Gras 				    "REG_0x%x", target);
190b7061124SArun Thomas 			s = convbuf;
191b7061124SArun Thomas 		} else
192b7061124SArun Thomas 			s = r->explain;
193b7061124SArun Thomas 	}
194b7061124SArun Thomas 
195b7061124SArun Thomas 	len = strlen(s) + 1;
196*2fe8fb19SBen Gras 	if (errbuf_size > 0)
197*2fe8fb19SBen Gras 		(void)strlcpy(errbuf, s, errbuf_size);
198b7061124SArun Thomas 
199b7061124SArun Thomas 	return(len);
200b7061124SArun Thomas }
201b7061124SArun Thomas 
202b7061124SArun Thomas /*
203*2fe8fb19SBen Gras  * regatoi - internal routine to implement REG_ATOI
204*2fe8fb19SBen Gras  * static const char *regatoi(const regex_t *preg, char *localbuf,
205*2fe8fb19SBen Gras  * size_t buflen);
206b7061124SArun Thomas  */
207*2fe8fb19SBen Gras static const char *
regatoi(const regex_t * preg,char * localbuf,size_t buflen)208*2fe8fb19SBen Gras regatoi(
209*2fe8fb19SBen Gras     const regex_t *preg,
210*2fe8fb19SBen Gras     char *localbuf,
211*2fe8fb19SBen Gras     size_t buflen)
212b7061124SArun Thomas {
213*2fe8fb19SBen Gras 	const struct rerr *r;
214b7061124SArun Thomas 
215b7061124SArun Thomas 	for (r = rerrs; r->code != 0; r++)
216b7061124SArun Thomas 		if (strcmp(r->name, preg->re_endp) == 0)
217b7061124SArun Thomas 			break;
218b7061124SArun Thomas 	if (r->code == 0)
219*2fe8fb19SBen Gras 		return "0";
220b7061124SArun Thomas 
221*2fe8fb19SBen Gras 	(void)snprintf(localbuf, buflen, "%d", r->code);
222*2fe8fb19SBen Gras 	return localbuf;
223b7061124SArun Thomas }
224