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