xref: /freebsd/lib/libc/regex/regerror.c (revision 54a648d1e5d7baa2e89f141baa263d1e7df70838)
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  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  *
3358f0484fSRodney W. Grimes  *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3758f0484fSRodney W. Grimes static char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
3858f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
398fb3f3f6SDavid E. O'Brien #include <sys/cdefs.h>
408fb3f3f6SDavid E. O'Brien __FBSDID("$FreeBSD$");
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes #include <sys/types.h>
4358f0484fSRodney W. Grimes #include <stdio.h>
4458f0484fSRodney W. Grimes #include <string.h>
4558f0484fSRodney W. Grimes #include <limits.h>
4658f0484fSRodney W. Grimes #include <stdlib.h>
4758f0484fSRodney W. Grimes #include <regex.h>
4858f0484fSRodney W. Grimes 
4958f0484fSRodney W. Grimes #include "utils.h"
5058f0484fSRodney W. Grimes 
5158f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
5258f0484fSRodney W. Grimes #ifdef __cplusplus
5358f0484fSRodney W. Grimes extern "C" {
5458f0484fSRodney W. Grimes #endif
5558f0484fSRodney W. Grimes 
5658f0484fSRodney W. Grimes /* === regerror.c === */
57c05ac53bSDavid E. O'Brien static char *regatoi(const regex_t *preg, char *localbuf);
5858f0484fSRodney W. Grimes 
5958f0484fSRodney W. Grimes #ifdef __cplusplus
6058f0484fSRodney W. Grimes }
6158f0484fSRodney W. Grimes #endif
6258f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
6358f0484fSRodney W. Grimes /*
6458f0484fSRodney W. Grimes  = #define	REG_NOMATCH	 1
6558f0484fSRodney W. Grimes  = #define	REG_BADPAT	 2
6658f0484fSRodney W. Grimes  = #define	REG_ECOLLATE	 3
6758f0484fSRodney W. Grimes  = #define	REG_ECTYPE	 4
6858f0484fSRodney W. Grimes  = #define	REG_EESCAPE	 5
6958f0484fSRodney W. Grimes  = #define	REG_ESUBREG	 6
7058f0484fSRodney W. Grimes  = #define	REG_EBRACK	 7
7158f0484fSRodney W. Grimes  = #define	REG_EPAREN	 8
7258f0484fSRodney W. Grimes  = #define	REG_EBRACE	 9
7358f0484fSRodney W. Grimes  = #define	REG_BADBR	10
7458f0484fSRodney W. Grimes  = #define	REG_ERANGE	11
7558f0484fSRodney W. Grimes  = #define	REG_ESPACE	12
7658f0484fSRodney W. Grimes  = #define	REG_BADRPT	13
7758f0484fSRodney W. Grimes  = #define	REG_EMPTY	14
7858f0484fSRodney W. Grimes  = #define	REG_ASSERT	15
7958f0484fSRodney W. Grimes  = #define	REG_INVARG	16
8067aff189STim J. Robbins  = #define	REG_ILLSEQ	17
8158f0484fSRodney W. Grimes  = #define	REG_ATOI	255	// convert name to number (!)
8258f0484fSRodney W. Grimes  = #define	REG_ITOA	0400	// convert number to name (!)
8358f0484fSRodney W. Grimes  */
8458f0484fSRodney W. Grimes static struct rerr {
8558f0484fSRodney W. Grimes 	int code;
8658f0484fSRodney W. Grimes 	char *name;
8758f0484fSRodney W. Grimes 	char *explain;
8858f0484fSRodney W. Grimes } rerrs[] = {
8951295a4dSJordan K. Hubbard 	{REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match"},
9051295a4dSJordan K. Hubbard 	{REG_BADPAT,	"REG_BADPAT",	"invalid regular expression"},
9151295a4dSJordan K. Hubbard 	{REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element"},
9251295a4dSJordan K. Hubbard 	{REG_ECTYPE,	"REG_ECTYPE",	"invalid character class"},
9351295a4dSJordan K. Hubbard 	{REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)"},
9451295a4dSJordan K. Hubbard 	{REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number"},
9551295a4dSJordan K. Hubbard 	{REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced"},
9651295a4dSJordan K. Hubbard 	{REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced"},
9751295a4dSJordan K. Hubbard 	{REG_EBRACE,	"REG_EBRACE",	"braces not balanced"},
9851295a4dSJordan K. Hubbard 	{REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)"},
9951295a4dSJordan K. Hubbard 	{REG_ERANGE,	"REG_ERANGE",	"invalid character range"},
10051295a4dSJordan K. Hubbard 	{REG_ESPACE,	"REG_ESPACE",	"out of memory"},
10151295a4dSJordan K. Hubbard 	{REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid"},
10251295a4dSJordan K. Hubbard 	{REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression"},
10351295a4dSJordan K. Hubbard 	{REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug"},
10451295a4dSJordan K. Hubbard 	{REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine"},
10567aff189STim J. Robbins 	{REG_ILLSEQ,	"REG_ILLSEQ",	"illegal byte sequence"},
10651295a4dSJordan K. Hubbard 	{0,		"",		"*** unknown regexp error code ***"}
10758f0484fSRodney W. Grimes };
10858f0484fSRodney W. Grimes 
10958f0484fSRodney W. Grimes /*
11058f0484fSRodney W. Grimes  - regerror - the interface to error numbers
11158f0484fSRodney W. Grimes  = extern size_t regerror(int, const regex_t *, char *, size_t);
11258f0484fSRodney W. Grimes  */
11358f0484fSRodney W. Grimes /* ARGSUSED */
11458f0484fSRodney W. Grimes size_t
11554a648d1SXin LI regerror(int errcode,
11654a648d1SXin LI 	 const regex_t * __restrict preg,
11754a648d1SXin LI 	 char * __restrict errbuf,
11854a648d1SXin LI 	 size_t errbuf_size)
11958f0484fSRodney W. Grimes {
1208fb3f3f6SDavid E. O'Brien 	struct rerr *r;
1218fb3f3f6SDavid E. O'Brien 	size_t len;
1228fb3f3f6SDavid E. O'Brien 	int target = errcode &~ REG_ITOA;
1238fb3f3f6SDavid E. O'Brien 	char *s;
12458f0484fSRodney W. Grimes 	char convbuf[50];
12558f0484fSRodney W. Grimes 
12658f0484fSRodney W. Grimes 	if (errcode == REG_ATOI)
12758f0484fSRodney W. Grimes 		s = regatoi(preg, convbuf);
12858f0484fSRodney W. Grimes 	else {
12958f0484fSRodney W. Grimes 		for (r = rerrs; r->code != 0; r++)
13058f0484fSRodney W. Grimes 			if (r->code == target)
13158f0484fSRodney W. Grimes 				break;
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes 		if (errcode&REG_ITOA) {
13458f0484fSRodney W. Grimes 			if (r->code != 0)
13558f0484fSRodney W. Grimes 				(void) strcpy(convbuf, r->name);
13658f0484fSRodney W. Grimes 			else
13758f0484fSRodney W. Grimes 				sprintf(convbuf, "REG_0x%x", target);
13858f0484fSRodney W. Grimes 			assert(strlen(convbuf) < sizeof(convbuf));
13958f0484fSRodney W. Grimes 			s = convbuf;
14058f0484fSRodney W. Grimes 		} else
14158f0484fSRodney W. Grimes 			s = r->explain;
14258f0484fSRodney W. Grimes 	}
14358f0484fSRodney W. Grimes 
14458f0484fSRodney W. Grimes 	len = strlen(s) + 1;
14558f0484fSRodney W. Grimes 	if (errbuf_size > 0) {
14658f0484fSRodney W. Grimes 		if (errbuf_size > len)
14758f0484fSRodney W. Grimes 			(void) strcpy(errbuf, s);
14858f0484fSRodney W. Grimes 		else {
14958f0484fSRodney W. Grimes 			(void) strncpy(errbuf, s, errbuf_size-1);
15058f0484fSRodney W. Grimes 			errbuf[errbuf_size-1] = '\0';
15158f0484fSRodney W. Grimes 		}
15258f0484fSRodney W. Grimes 	}
15358f0484fSRodney W. Grimes 
15458f0484fSRodney W. Grimes 	return(len);
15558f0484fSRodney W. Grimes }
15658f0484fSRodney W. Grimes 
15758f0484fSRodney W. Grimes /*
15858f0484fSRodney W. Grimes  - regatoi - internal routine to implement REG_ATOI
15958f0484fSRodney W. Grimes  == static char *regatoi(const regex_t *preg, char *localbuf);
16058f0484fSRodney W. Grimes  */
16158f0484fSRodney W. Grimes static char *
16254a648d1SXin LI regatoi(const regex_t *preg, char *localbuf)
16358f0484fSRodney W. Grimes {
1648fb3f3f6SDavid E. O'Brien 	struct rerr *r;
16558f0484fSRodney W. Grimes 
16658f0484fSRodney W. Grimes 	for (r = rerrs; r->code != 0; r++)
16758f0484fSRodney W. Grimes 		if (strcmp(r->name, preg->re_endp) == 0)
16858f0484fSRodney W. Grimes 			break;
16958f0484fSRodney W. Grimes 	if (r->code == 0)
17058f0484fSRodney W. Grimes 		return("0");
17158f0484fSRodney W. Grimes 
17258f0484fSRodney W. Grimes 	sprintf(localbuf, "%d", r->code);
17358f0484fSRodney W. Grimes 	return(localbuf);
17458f0484fSRodney W. Grimes }
175