xref: /minix3/external/bsd/nvi/dist/regex/regerror.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: regerror.c,v 1.5 2014/01/26 21:47:00 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
484d9c625SLionel Sambuc  * Copyright (c) 1992, 1993, 1994
584d9c625SLionel Sambuc  *	The Regents of the University of California.  All rights reserved.
684d9c625SLionel Sambuc  *
784d9c625SLionel Sambuc  * This code is derived from software contributed to Berkeley by
884d9c625SLionel Sambuc  * Henry Spencer of the University of Toronto.
984d9c625SLionel Sambuc  *
1084d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1184d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions
1284d9c625SLionel Sambuc  * are met:
1384d9c625SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1484d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1584d9c625SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1684d9c625SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1784d9c625SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1884d9c625SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
1984d9c625SLionel Sambuc  *    must display the following acknowledgement:
2084d9c625SLionel Sambuc  *	This product includes software developed by the University of
2184d9c625SLionel Sambuc  *	California, Berkeley and its contributors.
2284d9c625SLionel Sambuc  * 4. Neither the name of the University nor the names of its contributors
2384d9c625SLionel Sambuc  *    may be used to endorse or promote products derived from this software
2484d9c625SLionel Sambuc  *    without specific prior written permission.
2584d9c625SLionel Sambuc  *
2684d9c625SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2784d9c625SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2884d9c625SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2984d9c625SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3084d9c625SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3184d9c625SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3284d9c625SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3384d9c625SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3484d9c625SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3584d9c625SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3684d9c625SLionel Sambuc  * SUCH DAMAGE.
3784d9c625SLionel Sambuc  *
3884d9c625SLionel Sambuc  *	@(#)regerror.c	8.3 (Berkeley) 3/19/94
3984d9c625SLionel Sambuc  */
4084d9c625SLionel Sambuc 
41*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
42*0a6a1f1dSLionel Sambuc #if 0
4384d9c625SLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
4484d9c625SLionel Sambuc static char sccsid[] = "@(#)regerror.c	8.3 (Berkeley) 3/19/94";
4584d9c625SLionel Sambuc #endif /* LIBC_SCCS and not lint */
46*0a6a1f1dSLionel Sambuc #else
47*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: regerror.c,v 1.5 2014/01/26 21:47:00 christos Exp $");
48*0a6a1f1dSLionel Sambuc #endif
4984d9c625SLionel Sambuc 
5084d9c625SLionel Sambuc #include <sys/types.h>
5184d9c625SLionel Sambuc #include <stdio.h>
5284d9c625SLionel Sambuc #include <string.h>
5384d9c625SLionel Sambuc #include <ctype.h>
5484d9c625SLionel Sambuc #include <limits.h>
5584d9c625SLionel Sambuc #include <stdlib.h>
5684d9c625SLionel Sambuc #include <regex.h>
5784d9c625SLionel Sambuc 
5884d9c625SLionel Sambuc #include "utils.h"
5984d9c625SLionel Sambuc 
6084d9c625SLionel Sambuc /* ========= begin header generated by ./mkh ========= */
6184d9c625SLionel Sambuc #ifdef __cplusplus
6284d9c625SLionel Sambuc extern "C" {
6384d9c625SLionel Sambuc #endif
6484d9c625SLionel Sambuc 
6584d9c625SLionel Sambuc /* === regerror.c === */
6684d9c625SLionel Sambuc static char *regatoi __P((const regex_t *preg, char *localbuf));
6784d9c625SLionel Sambuc 
6884d9c625SLionel Sambuc #ifdef __cplusplus
6984d9c625SLionel Sambuc }
7084d9c625SLionel Sambuc #endif
7184d9c625SLionel Sambuc /* ========= end header generated by ./mkh ========= */
7284d9c625SLionel Sambuc /*
7384d9c625SLionel Sambuc  = #define	REG_NOMATCH	 1
7484d9c625SLionel Sambuc  = #define	REG_BADPAT	 2
7584d9c625SLionel Sambuc  = #define	REG_ECOLLATE	 3
7684d9c625SLionel Sambuc  = #define	REG_ECTYPE	 4
7784d9c625SLionel Sambuc  = #define	REG_EESCAPE	 5
7884d9c625SLionel Sambuc  = #define	REG_ESUBREG	 6
7984d9c625SLionel Sambuc  = #define	REG_EBRACK	 7
8084d9c625SLionel Sambuc  = #define	REG_EPAREN	 8
8184d9c625SLionel Sambuc  = #define	REG_EBRACE	 9
8284d9c625SLionel Sambuc  = #define	REG_BADBR	10
8384d9c625SLionel Sambuc  = #define	REG_ERANGE	11
8484d9c625SLionel Sambuc  = #define	REG_ESPACE	12
8584d9c625SLionel Sambuc  = #define	REG_BADRPT	13
8684d9c625SLionel Sambuc  = #define	REG_EMPTY	14
8784d9c625SLionel Sambuc  = #define	REG_ASSERT	15
8884d9c625SLionel Sambuc  = #define	REG_INVARG	16
8984d9c625SLionel Sambuc  = #define	REG_ATOI	255	// convert name to number (!)
9084d9c625SLionel Sambuc  = #define	REG_ITOA	0400	// convert number to name (!)
9184d9c625SLionel Sambuc  */
9284d9c625SLionel Sambuc static struct rerr {
9384d9c625SLionel Sambuc 	int code;
9484d9c625SLionel Sambuc 	const char *name;
9584d9c625SLionel Sambuc 	const char *explain;
9684d9c625SLionel Sambuc } rerrs[] = {
9784d9c625SLionel Sambuc 	{ REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match" },
9884d9c625SLionel Sambuc 	{ REG_BADPAT,	"REG_BADPAT",	"invalid regular expression" },
9984d9c625SLionel Sambuc 	{ REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element" },
10084d9c625SLionel Sambuc 	{ REG_ECTYPE,	"REG_ECTYPE",	"invalid character class" },
10184d9c625SLionel Sambuc 	{ REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)" },
10284d9c625SLionel Sambuc 	{ REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number" },
10384d9c625SLionel Sambuc 	{ REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced" },
10484d9c625SLionel Sambuc 	{ REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced" },
10584d9c625SLionel Sambuc 	{ REG_EBRACE,	"REG_EBRACE",	"braces not balanced" },
10684d9c625SLionel Sambuc 	{ REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)" },
10784d9c625SLionel Sambuc 	{ REG_ERANGE,	"REG_ERANGE",	"invalid character range" },
10884d9c625SLionel Sambuc 	{ REG_ESPACE,	"REG_ESPACE",	"out of memory" },
10984d9c625SLionel Sambuc 	{ REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid" },
11084d9c625SLionel Sambuc 	{ REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression" },
11184d9c625SLionel Sambuc 	{ REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug" },
11284d9c625SLionel Sambuc 	{ REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine" },
11384d9c625SLionel Sambuc 	{ 0,		"",		"*** unknown regexp error code ***" },
11484d9c625SLionel Sambuc };
11584d9c625SLionel Sambuc 
11684d9c625SLionel Sambuc /*
11784d9c625SLionel Sambuc  - regerror - the interface to error numbers
11884d9c625SLionel Sambuc  = extern size_t regerror(int, const regex_t *, char *, size_t);
11984d9c625SLionel Sambuc  */
12084d9c625SLionel Sambuc /* ARGSUSED */
12184d9c625SLionel Sambuc size_t
regerror(int errcode,const regex_t * preg,char * errbuf,size_t errbuf_size)12284d9c625SLionel Sambuc regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
12384d9c625SLionel Sambuc {
124*0a6a1f1dSLionel Sambuc 	struct rerr *r;
125*0a6a1f1dSLionel Sambuc 	size_t len;
126*0a6a1f1dSLionel Sambuc 	int target = errcode &~ REG_ITOA;
127*0a6a1f1dSLionel Sambuc 	const char *s;
12884d9c625SLionel Sambuc 	char convbuf[50];
12984d9c625SLionel Sambuc 
13084d9c625SLionel Sambuc 	if (errcode == REG_ATOI)
13184d9c625SLionel Sambuc 		s = regatoi(preg, convbuf);
13284d9c625SLionel Sambuc 	else {
13384d9c625SLionel Sambuc 		for (r = rerrs; r->code != 0; r++)
13484d9c625SLionel Sambuc 			if (r->code == target)
13584d9c625SLionel Sambuc 				break;
13684d9c625SLionel Sambuc 
13784d9c625SLionel Sambuc 		if (errcode&REG_ITOA) {
13884d9c625SLionel Sambuc 			if (r->code != 0)
13984d9c625SLionel Sambuc 				(void) strcpy(convbuf, r->name);
14084d9c625SLionel Sambuc 			else
14184d9c625SLionel Sambuc 				sprintf(convbuf, "REG_0x%x", target);
14284d9c625SLionel Sambuc 			assert(strlen(convbuf) < sizeof(convbuf));
14384d9c625SLionel Sambuc 			s = convbuf;
14484d9c625SLionel Sambuc 		} else
14584d9c625SLionel Sambuc 			s = r->explain;
14684d9c625SLionel Sambuc 	}
14784d9c625SLionel Sambuc 
14884d9c625SLionel Sambuc 	len = strlen(s) + 1;
14984d9c625SLionel Sambuc 	if (errbuf_size > 0) {
15084d9c625SLionel Sambuc 		if (errbuf_size > len)
15184d9c625SLionel Sambuc 			(void) strcpy(errbuf, s);
15284d9c625SLionel Sambuc 		else {
15384d9c625SLionel Sambuc 			(void) strncpy(errbuf, s, errbuf_size-1);
15484d9c625SLionel Sambuc 			errbuf[errbuf_size-1] = '\0';
15584d9c625SLionel Sambuc 		}
15684d9c625SLionel Sambuc 	}
15784d9c625SLionel Sambuc 
15884d9c625SLionel Sambuc 	return(len);
15984d9c625SLionel Sambuc }
16084d9c625SLionel Sambuc 
16184d9c625SLionel Sambuc /*
16284d9c625SLionel Sambuc  - regatoi - internal routine to implement REG_ATOI
16384d9c625SLionel Sambuc  == static char *regatoi(const regex_t *preg, char *localbuf);
16484d9c625SLionel Sambuc  */
16584d9c625SLionel Sambuc static char *
regatoi(const regex_t * preg,char * localbuf)16684d9c625SLionel Sambuc regatoi(const regex_t *preg, char *localbuf)
16784d9c625SLionel Sambuc {
16884d9c625SLionel Sambuc #if 0 /* we don't seem to use this and it gives a warning. */
169*0a6a1f1dSLionel Sambuc 	struct rerr *r;
170*0a6a1f1dSLionel Sambuc 	size_t siz;
171*0a6a1f1dSLionel Sambuc 	char *p;
17284d9c625SLionel Sambuc 
17384d9c625SLionel Sambuc 	for (r = rerrs; r->code != 0; r++)
17484d9c625SLionel Sambuc 		if (strcmp(r->name, preg->re_endp) == 0)
17584d9c625SLionel Sambuc 			break;
17684d9c625SLionel Sambuc 	if (r->code == 0)
17784d9c625SLionel Sambuc 		return("0");
17884d9c625SLionel Sambuc 
17984d9c625SLionel Sambuc 	sprintf(localbuf, "%d", r->code);
18084d9c625SLionel Sambuc #else
18184d9c625SLionel Sambuc 	*localbuf = '\0';
18284d9c625SLionel Sambuc #endif
18384d9c625SLionel Sambuc 	return(localbuf);
18484d9c625SLionel Sambuc }
185