xref: /freebsd/lib/libc/regex/regerror.c (revision 51295a4d3e4c551df85249433c490208dc7fd23d)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
358f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994
458f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
558f0484fSRodney W. Grimes  *
658f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
758f0484fSRodney W. Grimes  * Henry Spencer.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  *
3758f0484fSRodney W. Grimes  *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
3858f0484fSRodney W. Grimes  */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
4158f0484fSRodney W. Grimes static char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
4258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes #include <sys/types.h>
4558f0484fSRodney W. Grimes #include <stdio.h>
4658f0484fSRodney W. Grimes #include <string.h>
4758f0484fSRodney W. Grimes #include <ctype.h>
4858f0484fSRodney W. Grimes #include <limits.h>
4958f0484fSRodney W. Grimes #include <stdlib.h>
5058f0484fSRodney W. Grimes #include <regex.h>
5158f0484fSRodney W. Grimes 
5258f0484fSRodney W. Grimes #include "utils.h"
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
5558f0484fSRodney W. Grimes #ifdef __cplusplus
5658f0484fSRodney W. Grimes extern "C" {
5758f0484fSRodney W. Grimes #endif
5858f0484fSRodney W. Grimes 
5958f0484fSRodney W. Grimes /* === regerror.c === */
6058f0484fSRodney W. Grimes static char *regatoi __P((const regex_t *preg, char *localbuf));
6158f0484fSRodney W. Grimes 
6258f0484fSRodney W. Grimes #ifdef __cplusplus
6358f0484fSRodney W. Grimes }
6458f0484fSRodney W. Grimes #endif
6558f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
6658f0484fSRodney W. Grimes /*
6758f0484fSRodney W. Grimes  = #define	REG_NOMATCH	 1
6858f0484fSRodney W. Grimes  = #define	REG_BADPAT	 2
6958f0484fSRodney W. Grimes  = #define	REG_ECOLLATE	 3
7058f0484fSRodney W. Grimes  = #define	REG_ECTYPE	 4
7158f0484fSRodney W. Grimes  = #define	REG_EESCAPE	 5
7258f0484fSRodney W. Grimes  = #define	REG_ESUBREG	 6
7358f0484fSRodney W. Grimes  = #define	REG_EBRACK	 7
7458f0484fSRodney W. Grimes  = #define	REG_EPAREN	 8
7558f0484fSRodney W. Grimes  = #define	REG_EBRACE	 9
7658f0484fSRodney W. Grimes  = #define	REG_BADBR	10
7758f0484fSRodney W. Grimes  = #define	REG_ERANGE	11
7858f0484fSRodney W. Grimes  = #define	REG_ESPACE	12
7958f0484fSRodney W. Grimes  = #define	REG_BADRPT	13
8058f0484fSRodney W. Grimes  = #define	REG_EMPTY	14
8158f0484fSRodney W. Grimes  = #define	REG_ASSERT	15
8258f0484fSRodney W. Grimes  = #define	REG_INVARG	16
8358f0484fSRodney W. Grimes  = #define	REG_ATOI	255	// convert name to number (!)
8458f0484fSRodney W. Grimes  = #define	REG_ITOA	0400	// convert number to name (!)
8558f0484fSRodney W. Grimes  */
8658f0484fSRodney W. Grimes static struct rerr {
8758f0484fSRodney W. Grimes 	int code;
8858f0484fSRodney W. Grimes 	char *name;
8958f0484fSRodney W. Grimes 	char *explain;
9058f0484fSRodney W. Grimes } rerrs[] = {
9151295a4dSJordan K. Hubbard 	{REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match"},
9251295a4dSJordan K. Hubbard 	{REG_BADPAT,	"REG_BADPAT",	"invalid regular expression"},
9351295a4dSJordan K. Hubbard 	{REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element"},
9451295a4dSJordan K. Hubbard 	{REG_ECTYPE,	"REG_ECTYPE",	"invalid character class"},
9551295a4dSJordan K. Hubbard 	{REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)"},
9651295a4dSJordan K. Hubbard 	{REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number"},
9751295a4dSJordan K. Hubbard 	{REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced"},
9851295a4dSJordan K. Hubbard 	{REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced"},
9951295a4dSJordan K. Hubbard 	{REG_EBRACE,	"REG_EBRACE",	"braces not balanced"},
10051295a4dSJordan K. Hubbard 	{REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)"},
10151295a4dSJordan K. Hubbard 	{REG_ERANGE,	"REG_ERANGE",	"invalid character range"},
10251295a4dSJordan K. Hubbard 	{REG_ESPACE,	"REG_ESPACE",	"out of memory"},
10351295a4dSJordan K. Hubbard 	{REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid"},
10451295a4dSJordan K. Hubbard 	{REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression"},
10551295a4dSJordan K. Hubbard 	{REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug"},
10651295a4dSJordan K. Hubbard 	{REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine"},
10751295a4dSJordan K. Hubbard 	{0,		"",		"*** unknown regexp error code ***"}
10858f0484fSRodney W. Grimes };
10958f0484fSRodney W. Grimes 
11058f0484fSRodney W. Grimes /*
11158f0484fSRodney W. Grimes  - regerror - the interface to error numbers
11258f0484fSRodney W. Grimes  = extern size_t regerror(int, const regex_t *, char *, size_t);
11358f0484fSRodney W. Grimes  */
11458f0484fSRodney W. Grimes /* ARGSUSED */
11558f0484fSRodney W. Grimes size_t
11658f0484fSRodney W. Grimes regerror(errcode, preg, errbuf, errbuf_size)
11758f0484fSRodney W. Grimes int errcode;
11858f0484fSRodney W. Grimes const regex_t *preg;
11958f0484fSRodney W. Grimes char *errbuf;
12058f0484fSRodney W. Grimes size_t errbuf_size;
12158f0484fSRodney W. Grimes {
12258f0484fSRodney W. Grimes 	register struct rerr *r;
12358f0484fSRodney W. Grimes 	register size_t len;
12458f0484fSRodney W. Grimes 	register int target = errcode &~ REG_ITOA;
12558f0484fSRodney W. Grimes 	register char *s;
12658f0484fSRodney W. Grimes 	char convbuf[50];
12758f0484fSRodney W. Grimes 
12858f0484fSRodney W. Grimes 	if (errcode == REG_ATOI)
12958f0484fSRodney W. Grimes 		s = regatoi(preg, convbuf);
13058f0484fSRodney W. Grimes 	else {
13158f0484fSRodney W. Grimes 		for (r = rerrs; r->code != 0; r++)
13258f0484fSRodney W. Grimes 			if (r->code == target)
13358f0484fSRodney W. Grimes 				break;
13458f0484fSRodney W. Grimes 
13558f0484fSRodney W. Grimes 		if (errcode&REG_ITOA) {
13658f0484fSRodney W. Grimes 			if (r->code != 0)
13758f0484fSRodney W. Grimes 				(void) strcpy(convbuf, r->name);
13858f0484fSRodney W. Grimes 			else
13958f0484fSRodney W. Grimes 				sprintf(convbuf, "REG_0x%x", target);
14058f0484fSRodney W. Grimes 			assert(strlen(convbuf) < sizeof(convbuf));
14158f0484fSRodney W. Grimes 			s = convbuf;
14258f0484fSRodney W. Grimes 		} else
14358f0484fSRodney W. Grimes 			s = r->explain;
14458f0484fSRodney W. Grimes 	}
14558f0484fSRodney W. Grimes 
14658f0484fSRodney W. Grimes 	len = strlen(s) + 1;
14758f0484fSRodney W. Grimes 	if (errbuf_size > 0) {
14858f0484fSRodney W. Grimes 		if (errbuf_size > len)
14958f0484fSRodney W. Grimes 			(void) strcpy(errbuf, s);
15058f0484fSRodney W. Grimes 		else {
15158f0484fSRodney W. Grimes 			(void) strncpy(errbuf, s, errbuf_size-1);
15258f0484fSRodney W. Grimes 			errbuf[errbuf_size-1] = '\0';
15358f0484fSRodney W. Grimes 		}
15458f0484fSRodney W. Grimes 	}
15558f0484fSRodney W. Grimes 
15658f0484fSRodney W. Grimes 	return(len);
15758f0484fSRodney W. Grimes }
15858f0484fSRodney W. Grimes 
15958f0484fSRodney W. Grimes /*
16058f0484fSRodney W. Grimes  - regatoi - internal routine to implement REG_ATOI
16158f0484fSRodney W. Grimes  == static char *regatoi(const regex_t *preg, char *localbuf);
16258f0484fSRodney W. Grimes  */
16358f0484fSRodney W. Grimes static char *
16458f0484fSRodney W. Grimes regatoi(preg, localbuf)
16558f0484fSRodney W. Grimes const regex_t *preg;
16658f0484fSRodney W. Grimes char *localbuf;
16758f0484fSRodney W. Grimes {
16858f0484fSRodney W. Grimes 	register struct rerr *r;
16958f0484fSRodney W. Grimes 
17058f0484fSRodney W. Grimes 	for (r = rerrs; r->code != 0; r++)
17158f0484fSRodney W. Grimes 		if (strcmp(r->name, preg->re_endp) == 0)
17258f0484fSRodney W. Grimes 			break;
17358f0484fSRodney W. Grimes 	if (r->code == 0)
17458f0484fSRodney W. Grimes 		return("0");
17558f0484fSRodney W. Grimes 
17658f0484fSRodney W. Grimes 	sprintf(localbuf, "%d", r->code);
17758f0484fSRodney W. Grimes 	return(localbuf);
17858f0484fSRodney W. Grimes }
179