xref: /freebsd/lib/libc/regex/regerror.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
558f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994
658f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
958f0484fSRodney W. Grimes  * Henry Spencer.
1058f0484fSRodney W. Grimes  *
1158f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1258f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1358f0484fSRodney W. Grimes  * are met:
1458f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1658f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1758f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1858f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2058f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2158f0484fSRodney W. Grimes  *    without specific prior written permission.
2258f0484fSRodney W. Grimes  *
2358f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2458f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2558f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2658f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2758f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2858f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2958f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3058f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3158f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3258f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3358f0484fSRodney W. Grimes  * SUCH DAMAGE.
3458f0484fSRodney W. Grimes  */
3558f0484fSRodney W. Grimes 
3658f0484fSRodney W. Grimes #include <sys/types.h>
3758f0484fSRodney W. Grimes #include <stdio.h>
3858f0484fSRodney W. Grimes #include <string.h>
3958f0484fSRodney W. Grimes #include <limits.h>
4058f0484fSRodney W. Grimes #include <stdlib.h>
4158f0484fSRodney W. Grimes #include <regex.h>
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include "utils.h"
4458f0484fSRodney W. Grimes 
4558f0484fSRodney W. Grimes /* ========= begin header generated by ./mkh ========= */
4658f0484fSRodney W. Grimes #ifdef __cplusplus
4758f0484fSRodney W. Grimes extern "C" {
4858f0484fSRodney W. Grimes #endif
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /* === regerror.c === */
518d0f9a93SPedro F. Giffuni static const char *regatoi(const regex_t *preg, char *localbuf);
5258f0484fSRodney W. Grimes 
5358f0484fSRodney W. Grimes #ifdef __cplusplus
5458f0484fSRodney W. Grimes }
5558f0484fSRodney W. Grimes #endif
5658f0484fSRodney W. Grimes /* ========= end header generated by ./mkh ========= */
5758f0484fSRodney W. Grimes /*
5858f0484fSRodney W. Grimes  = #define	REG_NOMATCH	 1
5958f0484fSRodney W. Grimes  = #define	REG_BADPAT	 2
6058f0484fSRodney W. Grimes  = #define	REG_ECOLLATE	 3
6158f0484fSRodney W. Grimes  = #define	REG_ECTYPE	 4
6258f0484fSRodney W. Grimes  = #define	REG_EESCAPE	 5
6358f0484fSRodney W. Grimes  = #define	REG_ESUBREG	 6
6458f0484fSRodney W. Grimes  = #define	REG_EBRACK	 7
6558f0484fSRodney W. Grimes  = #define	REG_EPAREN	 8
6658f0484fSRodney W. Grimes  = #define	REG_EBRACE	 9
6758f0484fSRodney W. Grimes  = #define	REG_BADBR	10
6858f0484fSRodney W. Grimes  = #define	REG_ERANGE	11
6958f0484fSRodney W. Grimes  = #define	REG_ESPACE	12
7058f0484fSRodney W. Grimes  = #define	REG_BADRPT	13
7158f0484fSRodney W. Grimes  = #define	REG_EMPTY	14
7258f0484fSRodney W. Grimes  = #define	REG_ASSERT	15
7358f0484fSRodney W. Grimes  = #define	REG_INVARG	16
7467aff189STim J. Robbins  = #define	REG_ILLSEQ	17
7558f0484fSRodney W. Grimes  = #define	REG_ATOI	255	// convert name to number (!)
7658f0484fSRodney W. Grimes  = #define	REG_ITOA	0400	// convert number to name (!)
7758f0484fSRodney W. Grimes  */
7858f0484fSRodney W. Grimes static struct rerr {
7958f0484fSRodney W. Grimes 	int code;
808d0f9a93SPedro F. Giffuni 	const char *name;
818d0f9a93SPedro F. Giffuni 	const char *explain;
8258f0484fSRodney W. Grimes } rerrs[] = {
8351295a4dSJordan K. Hubbard 	{REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match"},
8451295a4dSJordan K. Hubbard 	{REG_BADPAT,	"REG_BADPAT",	"invalid regular expression"},
8551295a4dSJordan K. Hubbard 	{REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element"},
8651295a4dSJordan K. Hubbard 	{REG_ECTYPE,	"REG_ECTYPE",	"invalid character class"},
8751295a4dSJordan K. Hubbard 	{REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)"},
8851295a4dSJordan K. Hubbard 	{REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number"},
8951295a4dSJordan K. Hubbard 	{REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced"},
9051295a4dSJordan K. Hubbard 	{REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced"},
9151295a4dSJordan K. Hubbard 	{REG_EBRACE,	"REG_EBRACE",	"braces not balanced"},
9251295a4dSJordan K. Hubbard 	{REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)"},
9351295a4dSJordan K. Hubbard 	{REG_ERANGE,	"REG_ERANGE",	"invalid character range"},
9451295a4dSJordan K. Hubbard 	{REG_ESPACE,	"REG_ESPACE",	"out of memory"},
9551295a4dSJordan K. Hubbard 	{REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid"},
9651295a4dSJordan K. Hubbard 	{REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression"},
9751295a4dSJordan K. Hubbard 	{REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug"},
9851295a4dSJordan K. Hubbard 	{REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine"},
9967aff189STim J. Robbins 	{REG_ILLSEQ,	"REG_ILLSEQ",	"illegal byte sequence"},
10051295a4dSJordan K. Hubbard 	{0,		"",		"*** unknown regexp error code ***"}
10158f0484fSRodney W. Grimes };
10258f0484fSRodney W. Grimes 
10358f0484fSRodney W. Grimes /*
10458f0484fSRodney W. Grimes  - regerror - the interface to error numbers
10558f0484fSRodney W. Grimes  = extern size_t regerror(int, const regex_t *, char *, size_t);
10658f0484fSRodney W. Grimes  */
10758f0484fSRodney W. Grimes /* ARGSUSED */
10858f0484fSRodney W. Grimes size_t
regerror(int errcode,const regex_t * __restrict preg,char * __restrict errbuf,size_t errbuf_size)10954a648d1SXin LI regerror(int errcode,
11054a648d1SXin LI 	 const regex_t * __restrict preg,
11154a648d1SXin LI 	 char * __restrict errbuf,
11254a648d1SXin LI 	 size_t errbuf_size)
11358f0484fSRodney W. Grimes {
1148fb3f3f6SDavid E. O'Brien 	struct rerr *r;
1158fb3f3f6SDavid E. O'Brien 	size_t len;
1168fb3f3f6SDavid E. O'Brien 	int target = errcode &~ REG_ITOA;
1178d0f9a93SPedro F. Giffuni 	const char *s;
11858f0484fSRodney W. Grimes 	char convbuf[50];
11958f0484fSRodney W. Grimes 
12058f0484fSRodney W. Grimes 	if (errcode == REG_ATOI)
12158f0484fSRodney W. Grimes 		s = regatoi(preg, convbuf);
12258f0484fSRodney W. Grimes 	else {
12358f0484fSRodney W. Grimes 		for (r = rerrs; r->code != 0; r++)
12458f0484fSRodney W. Grimes 			if (r->code == target)
12558f0484fSRodney W. Grimes 				break;
12658f0484fSRodney W. Grimes 
12758f0484fSRodney W. Grimes 		if (errcode&REG_ITOA) {
12858f0484fSRodney W. Grimes 			if (r->code != 0)
12958f0484fSRodney W. Grimes 				(void) strcpy(convbuf, r->name);
13058f0484fSRodney W. Grimes 			else
13158f0484fSRodney W. Grimes 				sprintf(convbuf, "REG_0x%x", target);
13258f0484fSRodney W. Grimes 			assert(strlen(convbuf) < sizeof(convbuf));
13358f0484fSRodney W. Grimes 			s = convbuf;
13458f0484fSRodney W. Grimes 		} else
13558f0484fSRodney W. Grimes 			s = r->explain;
13658f0484fSRodney W. Grimes 	}
13758f0484fSRodney W. Grimes 
13858f0484fSRodney W. Grimes 	len = strlen(s) + 1;
13958f0484fSRodney W. Grimes 	if (errbuf_size > 0) {
14058f0484fSRodney W. Grimes 		if (errbuf_size > len)
14158f0484fSRodney W. Grimes 			(void) strcpy(errbuf, s);
14258f0484fSRodney W. Grimes 		else {
14358f0484fSRodney W. Grimes 			(void) strncpy(errbuf, s, errbuf_size-1);
14458f0484fSRodney W. Grimes 			errbuf[errbuf_size-1] = '\0';
14558f0484fSRodney W. Grimes 		}
14658f0484fSRodney W. Grimes 	}
14758f0484fSRodney W. Grimes 
14858f0484fSRodney W. Grimes 	return(len);
14958f0484fSRodney W. Grimes }
15058f0484fSRodney W. Grimes 
15158f0484fSRodney W. Grimes /*
15258f0484fSRodney W. Grimes  - regatoi - internal routine to implement REG_ATOI
15358f0484fSRodney W. Grimes  == static char *regatoi(const regex_t *preg, char *localbuf);
15458f0484fSRodney W. Grimes  */
1558d0f9a93SPedro F. Giffuni static const char *
regatoi(const regex_t * preg,char * localbuf)15654a648d1SXin LI regatoi(const regex_t *preg, char *localbuf)
15758f0484fSRodney W. Grimes {
1588fb3f3f6SDavid E. O'Brien 	struct rerr *r;
15958f0484fSRodney W. Grimes 
16058f0484fSRodney W. Grimes 	for (r = rerrs; r->code != 0; r++)
16158f0484fSRodney W. Grimes 		if (strcmp(r->name, preg->re_endp) == 0)
16258f0484fSRodney W. Grimes 			break;
16358f0484fSRodney W. Grimes 	if (r->code == 0)
16458f0484fSRodney W. Grimes 		return("0");
16558f0484fSRodney W. Grimes 
16658f0484fSRodney W. Grimes 	sprintf(localbuf, "%d", r->code);
16758f0484fSRodney W. Grimes 	return(localbuf);
16858f0484fSRodney W. Grimes }
169