regerror.c revision 1573
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 * 3. All advertising materials mentioning features or use of this software
181573Srgrimes *    must display the following acknowledgement:
191573Srgrimes *	This product includes software developed by the University of
201573Srgrimes *	California, Berkeley and its contributors.
211573Srgrimes * 4. Neither the name of the University nor the names of its contributors
221573Srgrimes *    may be used to endorse or promote products derived from this software
231573Srgrimes *    without specific prior written permission.
241573Srgrimes *
251573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351573Srgrimes * SUCH DAMAGE.
361573Srgrimes *
371573Srgrimes *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
381573Srgrimes */
391573Srgrimes
401573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
411573Srgrimesstatic char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
421573Srgrimes#endif /* LIBC_SCCS and not lint */
431573Srgrimes
441573Srgrimes#include <sys/types.h>
451573Srgrimes#include <stdio.h>
461573Srgrimes#include <string.h>
471573Srgrimes#include <ctype.h>
481573Srgrimes#include <limits.h>
491573Srgrimes#include <stdlib.h>
501573Srgrimes#include <regex.h>
511573Srgrimes
521573Srgrimes#include "utils.h"
531573Srgrimes
541573Srgrimes/* ========= begin header generated by ./mkh ========= */
551573Srgrimes#ifdef __cplusplus
561573Srgrimesextern "C" {
571573Srgrimes#endif
581573Srgrimes
591573Srgrimes/* === regerror.c === */
601573Srgrimesstatic char *regatoi __P((const regex_t *preg, char *localbuf));
611573Srgrimes
621573Srgrimes#ifdef __cplusplus
631573Srgrimes}
641573Srgrimes#endif
651573Srgrimes/* ========= end header generated by ./mkh ========= */
661573Srgrimes/*
671573Srgrimes = #define	REG_NOMATCH	 1
681573Srgrimes = #define	REG_BADPAT	 2
691573Srgrimes = #define	REG_ECOLLATE	 3
701573Srgrimes = #define	REG_ECTYPE	 4
711573Srgrimes = #define	REG_EESCAPE	 5
721573Srgrimes = #define	REG_ESUBREG	 6
731573Srgrimes = #define	REG_EBRACK	 7
741573Srgrimes = #define	REG_EPAREN	 8
751573Srgrimes = #define	REG_EBRACE	 9
761573Srgrimes = #define	REG_BADBR	10
771573Srgrimes = #define	REG_ERANGE	11
781573Srgrimes = #define	REG_ESPACE	12
791573Srgrimes = #define	REG_BADRPT	13
801573Srgrimes = #define	REG_EMPTY	14
811573Srgrimes = #define	REG_ASSERT	15
821573Srgrimes = #define	REG_INVARG	16
831573Srgrimes = #define	REG_ATOI	255	// convert name to number (!)
841573Srgrimes = #define	REG_ITOA	0400	// convert number to name (!)
851573Srgrimes */
861573Srgrimesstatic struct rerr {
871573Srgrimes	int code;
881573Srgrimes	char *name;
891573Srgrimes	char *explain;
901573Srgrimes} rerrs[] = {
911573Srgrimes	REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match",
921573Srgrimes	REG_BADPAT,	"REG_BADPAT",	"invalid regular expression",
931573Srgrimes	REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element",
941573Srgrimes	REG_ECTYPE,	"REG_ECTYPE",	"invalid character class",
951573Srgrimes	REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)",
961573Srgrimes	REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number",
971573Srgrimes	REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced",
981573Srgrimes	REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced",
991573Srgrimes	REG_EBRACE,	"REG_EBRACE",	"braces not balanced",
1001573Srgrimes	REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)",
1011573Srgrimes	REG_ERANGE,	"REG_ERANGE",	"invalid character range",
1021573Srgrimes	REG_ESPACE,	"REG_ESPACE",	"out of memory",
1031573Srgrimes	REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid",
1041573Srgrimes	REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression",
1051573Srgrimes	REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug",
1061573Srgrimes	REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine",
1071573Srgrimes	0,		"",		"*** unknown regexp error code ***",
1081573Srgrimes};
1091573Srgrimes
1101573Srgrimes/*
1111573Srgrimes - regerror - the interface to error numbers
1121573Srgrimes = extern size_t regerror(int, const regex_t *, char *, size_t);
1131573Srgrimes */
1141573Srgrimes/* ARGSUSED */
1151573Srgrimessize_t
1161573Srgrimesregerror(errcode, preg, errbuf, errbuf_size)
1171573Srgrimesint errcode;
1181573Srgrimesconst regex_t *preg;
1191573Srgrimeschar *errbuf;
1201573Srgrimessize_t errbuf_size;
1211573Srgrimes{
1221573Srgrimes	register struct rerr *r;
1231573Srgrimes	register size_t len;
1241573Srgrimes	register int target = errcode &~ REG_ITOA;
1251573Srgrimes	register char *s;
1261573Srgrimes	char convbuf[50];
1271573Srgrimes
1281573Srgrimes	if (errcode == REG_ATOI)
1291573Srgrimes		s = regatoi(preg, convbuf);
1301573Srgrimes	else {
1311573Srgrimes		for (r = rerrs; r->code != 0; r++)
1321573Srgrimes			if (r->code == target)
1331573Srgrimes				break;
1341573Srgrimes
1351573Srgrimes		if (errcode&REG_ITOA) {
1361573Srgrimes			if (r->code != 0)
1371573Srgrimes				(void) strcpy(convbuf, r->name);
1381573Srgrimes			else
1391573Srgrimes				sprintf(convbuf, "REG_0x%x", target);
1401573Srgrimes			assert(strlen(convbuf) < sizeof(convbuf));
1411573Srgrimes			s = convbuf;
1421573Srgrimes		} else
1431573Srgrimes			s = r->explain;
1441573Srgrimes	}
1451573Srgrimes
1461573Srgrimes	len = strlen(s) + 1;
1471573Srgrimes	if (errbuf_size > 0) {
1481573Srgrimes		if (errbuf_size > len)
1491573Srgrimes			(void) strcpy(errbuf, s);
1501573Srgrimes		else {
1511573Srgrimes			(void) strncpy(errbuf, s, errbuf_size-1);
1521573Srgrimes			errbuf[errbuf_size-1] = '\0';
1531573Srgrimes		}
1541573Srgrimes	}
1551573Srgrimes
1561573Srgrimes	return(len);
1571573Srgrimes}
1581573Srgrimes
1591573Srgrimes/*
1601573Srgrimes - regatoi - internal routine to implement REG_ATOI
1611573Srgrimes == static char *regatoi(const regex_t *preg, char *localbuf);
1621573Srgrimes */
1631573Srgrimesstatic char *
1641573Srgrimesregatoi(preg, localbuf)
1651573Srgrimesconst regex_t *preg;
1661573Srgrimeschar *localbuf;
1671573Srgrimes{
1681573Srgrimes	register struct rerr *r;
1691573Srgrimes	register size_t siz;
1701573Srgrimes	register char *p;
1711573Srgrimes
1721573Srgrimes	for (r = rerrs; r->code != 0; r++)
1731573Srgrimes		if (strcmp(r->name, preg->re_endp) == 0)
1741573Srgrimes			break;
1751573Srgrimes	if (r->code == 0)
1761573Srgrimes		return("0");
1771573Srgrimes
1781573Srgrimes	sprintf(localbuf, "%d", r->code);
1791573Srgrimes	return(localbuf);
1801573Srgrimes}
181