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