11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993, 1994 Henry Spencer.
31573Srgrimes * Copyright (c) 1992, 1993, 1994
41573Srgrimes *	The Regents of the University of California.  All rights reserved.
51573Srgrimes *
61573Srgrimes * This code is derived from software contributed to Berkeley by
71573Srgrimes * Henry Spencer.
81573Srgrimes *
91573Srgrimes * Redistribution and use in source and binary forms, with or without
101573Srgrimes * modification, are permitted provided that the following conditions
111573Srgrimes * are met:
121573Srgrimes * 1. Redistributions of source code must retain the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes *
331573Srgrimes *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
341573Srgrimes */
351573Srgrimes
361573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
371573Srgrimesstatic char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
381573Srgrimes#endif /* LIBC_SCCS and not lint */
3992889Sobrien#include <sys/cdefs.h>
4092889Sobrien__FBSDID("$FreeBSD$");
411573Srgrimes
421573Srgrimes#include <sys/types.h>
431573Srgrimes#include <stdio.h>
441573Srgrimes#include <string.h>
451573Srgrimes#include <limits.h>
461573Srgrimes#include <stdlib.h>
471573Srgrimes#include <regex.h>
481573Srgrimes
491573Srgrimes#include "utils.h"
501573Srgrimes
511573Srgrimes/* ========= begin header generated by ./mkh ========= */
521573Srgrimes#ifdef __cplusplus
531573Srgrimesextern "C" {
541573Srgrimes#endif
551573Srgrimes
561573Srgrimes/* === regerror.c === */
5792905Sobrienstatic char *regatoi(const regex_t *preg, char *localbuf);
581573Srgrimes
591573Srgrimes#ifdef __cplusplus
601573Srgrimes}
611573Srgrimes#endif
621573Srgrimes/* ========= end header generated by ./mkh ========= */
631573Srgrimes/*
641573Srgrimes = #define	REG_NOMATCH	 1
651573Srgrimes = #define	REG_BADPAT	 2
661573Srgrimes = #define	REG_ECOLLATE	 3
671573Srgrimes = #define	REG_ECTYPE	 4
681573Srgrimes = #define	REG_EESCAPE	 5
691573Srgrimes = #define	REG_ESUBREG	 6
701573Srgrimes = #define	REG_EBRACK	 7
711573Srgrimes = #define	REG_EPAREN	 8
721573Srgrimes = #define	REG_EBRACE	 9
731573Srgrimes = #define	REG_BADBR	10
741573Srgrimes = #define	REG_ERANGE	11
751573Srgrimes = #define	REG_ESPACE	12
761573Srgrimes = #define	REG_BADRPT	13
771573Srgrimes = #define	REG_EMPTY	14
781573Srgrimes = #define	REG_ASSERT	15
791573Srgrimes = #define	REG_INVARG	16
80132017Stjr = #define	REG_ILLSEQ	17
811573Srgrimes = #define	REG_ATOI	255	// convert name to number (!)
821573Srgrimes = #define	REG_ITOA	0400	// convert number to name (!)
831573Srgrimes */
841573Srgrimesstatic struct rerr {
851573Srgrimes	int code;
861573Srgrimes	char *name;
871573Srgrimes	char *explain;
881573Srgrimes} rerrs[] = {
8917141Sjkh	{REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match"},
9017141Sjkh	{REG_BADPAT,	"REG_BADPAT",	"invalid regular expression"},
9117141Sjkh	{REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element"},
9217141Sjkh	{REG_ECTYPE,	"REG_ECTYPE",	"invalid character class"},
9317141Sjkh	{REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)"},
9417141Sjkh	{REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number"},
9517141Sjkh	{REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced"},
9617141Sjkh	{REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced"},
9717141Sjkh	{REG_EBRACE,	"REG_EBRACE",	"braces not balanced"},
9817141Sjkh	{REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)"},
9917141Sjkh	{REG_ERANGE,	"REG_ERANGE",	"invalid character range"},
10017141Sjkh	{REG_ESPACE,	"REG_ESPACE",	"out of memory"},
10117141Sjkh	{REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid"},
10217141Sjkh	{REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression"},
10317141Sjkh	{REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug"},
10417141Sjkh	{REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine"},
105132017Stjr	{REG_ILLSEQ,	"REG_ILLSEQ",	"illegal byte sequence"},
10617141Sjkh	{0,		"",		"*** unknown regexp error code ***"}
1071573Srgrimes};
1081573Srgrimes
1091573Srgrimes/*
1101573Srgrimes - regerror - the interface to error numbers
1111573Srgrimes = extern size_t regerror(int, const regex_t *, char *, size_t);
1121573Srgrimes */
1131573Srgrimes/* ARGSUSED */
1141573Srgrimessize_t
115170528Sdelphijregerror(int errcode,
116170528Sdelphij	 const regex_t * __restrict preg,
117170528Sdelphij	 char * __restrict errbuf,
118170528Sdelphij	 size_t errbuf_size)
1191573Srgrimes{
12092889Sobrien	struct rerr *r;
12192889Sobrien	size_t len;
12292889Sobrien	int target = errcode &~ REG_ITOA;
12392889Sobrien	char *s;
1241573Srgrimes	char convbuf[50];
1251573Srgrimes
1261573Srgrimes	if (errcode == REG_ATOI)
1271573Srgrimes		s = regatoi(preg, convbuf);
1281573Srgrimes	else {
1291573Srgrimes		for (r = rerrs; r->code != 0; r++)
1301573Srgrimes			if (r->code == target)
1311573Srgrimes				break;
1328870Srgrimes
1331573Srgrimes		if (errcode&REG_ITOA) {
1341573Srgrimes			if (r->code != 0)
1351573Srgrimes				(void) strcpy(convbuf, r->name);
1361573Srgrimes			else
1371573Srgrimes				sprintf(convbuf, "REG_0x%x", target);
1381573Srgrimes			assert(strlen(convbuf) < sizeof(convbuf));
1391573Srgrimes			s = convbuf;
1401573Srgrimes		} else
1411573Srgrimes			s = r->explain;
1421573Srgrimes	}
1431573Srgrimes
1441573Srgrimes	len = strlen(s) + 1;
1451573Srgrimes	if (errbuf_size > 0) {
1461573Srgrimes		if (errbuf_size > len)
1471573Srgrimes			(void) strcpy(errbuf, s);
1481573Srgrimes		else {
1491573Srgrimes			(void) strncpy(errbuf, s, errbuf_size-1);
1501573Srgrimes			errbuf[errbuf_size-1] = '\0';
1511573Srgrimes		}
1521573Srgrimes	}
1531573Srgrimes
1541573Srgrimes	return(len);
1551573Srgrimes}
1561573Srgrimes
1571573Srgrimes/*
1581573Srgrimes - regatoi - internal routine to implement REG_ATOI
1591573Srgrimes == static char *regatoi(const regex_t *preg, char *localbuf);
1601573Srgrimes */
1611573Srgrimesstatic char *
162170528Sdelphijregatoi(const regex_t *preg, char *localbuf)
1631573Srgrimes{
16492889Sobrien	struct rerr *r;
1651573Srgrimes
1661573Srgrimes	for (r = rerrs; r->code != 0; r++)
1671573Srgrimes		if (strcmp(r->name, preg->re_endp) == 0)
1681573Srgrimes			break;
1691573Srgrimes	if (r->code == 0)
1701573Srgrimes		return("0");
1711573Srgrimes
1721573Srgrimes	sprintf(localbuf, "%d", r->code);
1731573Srgrimes	return(localbuf);
1741573Srgrimes}
175