Home | History | Annotate | Line # | Download | only in regex
regerror.c revision 1.20
      1  1.20      yamt /*	$NetBSD: regerror.c,v 1.20 2005/12/02 12:12:29 yamt Exp $	*/
      2   1.4       cgd 
      3   1.3       cgd /*-
      4   1.3       cgd  * Copyright (c) 1992, 1993, 1994
      5   1.3       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.3       cgd  *
      7   1.3       cgd  * This code is derived from software contributed to Berkeley by
      8   1.3       cgd  * Henry Spencer.
      9   1.3       cgd  *
     10   1.3       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.3       cgd  * modification, are permitted provided that the following conditions
     12   1.3       cgd  * are met:
     13   1.3       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.3       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.3       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.3       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.3       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.18       agc  * 3. Neither the name of the University nor the names of its contributors
     19  1.18       agc  *    may be used to endorse or promote products derived from this software
     20  1.18       agc  *    without specific prior written permission.
     21  1.18       agc  *
     22  1.18       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.18       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.18       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.18       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.18       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.18       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.18       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.18       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.18       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.18       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.18       agc  * SUCH DAMAGE.
     33  1.18       agc  *
     34  1.18       agc  *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
     35  1.18       agc  */
     36  1.18       agc 
     37  1.18       agc /*-
     38  1.18       agc  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
     39  1.18       agc  *
     40  1.18       agc  * This code is derived from software contributed to Berkeley by
     41  1.18       agc  * Henry Spencer.
     42  1.18       agc  *
     43  1.18       agc  * Redistribution and use in source and binary forms, with or without
     44  1.18       agc  * modification, are permitted provided that the following conditions
     45  1.18       agc  * are met:
     46  1.18       agc  * 1. Redistributions of source code must retain the above copyright
     47  1.18       agc  *    notice, this list of conditions and the following disclaimer.
     48  1.18       agc  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.18       agc  *    notice, this list of conditions and the following disclaimer in the
     50  1.18       agc  *    documentation and/or other materials provided with the distribution.
     51   1.3       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.3       cgd  *    must display the following acknowledgement:
     53   1.3       cgd  *	This product includes software developed by the University of
     54   1.3       cgd  *	California, Berkeley and its contributors.
     55   1.3       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.3       cgd  *    may be used to endorse or promote products derived from this software
     57   1.3       cgd  *    without specific prior written permission.
     58   1.3       cgd  *
     59   1.3       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.3       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.3       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.3       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.3       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.3       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.3       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.3       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.3       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.3       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.3       cgd  * SUCH DAMAGE.
     70   1.3       cgd  *
     71   1.3       cgd  *	@(#)regerror.c	8.4 (Berkeley) 3/20/94
     72   1.3       cgd  */
     73   1.3       cgd 
     74   1.6  christos #include <sys/cdefs.h>
     75   1.3       cgd #if defined(LIBC_SCCS) && !defined(lint)
     76   1.4       cgd #if 0
     77   1.3       cgd static char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
     78   1.4       cgd #else
     79  1.20      yamt __RCSID("$NetBSD: regerror.c,v 1.20 2005/12/02 12:12:29 yamt Exp $");
     80   1.4       cgd #endif
     81   1.3       cgd #endif /* LIBC_SCCS and not lint */
     82   1.3       cgd 
     83   1.7       jtc #include "namespace.h"
     84   1.1       jtc #include <sys/types.h>
     85  1.13     lukem 
     86  1.13     lukem #include <assert.h>
     87   1.1       jtc #include <ctype.h>
     88   1.1       jtc #include <limits.h>
     89  1.13     lukem #include <regex.h>
     90  1.13     lukem #include <stdio.h>
     91   1.1       jtc #include <stdlib.h>
     92  1.13     lukem #include <string.h>
     93   1.7       jtc 
     94   1.7       jtc #ifdef __weak_alias
     95  1.16   mycroft __weak_alias(regerror,_regerror)
     96   1.7       jtc #endif
     97   1.1       jtc 
     98   1.1       jtc #include "utils.h"
     99   1.1       jtc 
    100   1.3       cgd /* ========= begin header generated by ./mkh ========= */
    101   1.3       cgd #ifdef __cplusplus
    102   1.3       cgd extern "C" {
    103   1.3       cgd #endif
    104   1.3       cgd 
    105   1.3       cgd /* === regerror.c === */
    106  1.20      yamt static const char *regatoi __P((const regex_t *preg, char *localbuf,
    107  1.20      yamt     size_t buflen));
    108   1.3       cgd 
    109   1.3       cgd #ifdef __cplusplus
    110   1.3       cgd }
    111   1.3       cgd #endif
    112   1.3       cgd /* ========= end header generated by ./mkh ========= */
    113   1.1       jtc /*
    114   1.1       jtc  = #define	REG_NOMATCH	 1
    115   1.1       jtc  = #define	REG_BADPAT	 2
    116   1.1       jtc  = #define	REG_ECOLLATE	 3
    117   1.1       jtc  = #define	REG_ECTYPE	 4
    118   1.1       jtc  = #define	REG_EESCAPE	 5
    119   1.1       jtc  = #define	REG_ESUBREG	 6
    120   1.1       jtc  = #define	REG_EBRACK	 7
    121   1.1       jtc  = #define	REG_EPAREN	 8
    122   1.1       jtc  = #define	REG_EBRACE	 9
    123   1.1       jtc  = #define	REG_BADBR	10
    124   1.1       jtc  = #define	REG_ERANGE	11
    125   1.1       jtc  = #define	REG_ESPACE	12
    126   1.1       jtc  = #define	REG_BADRPT	13
    127   1.1       jtc  = #define	REG_EMPTY	14
    128   1.1       jtc  = #define	REG_ASSERT	15
    129   1.1       jtc  = #define	REG_INVARG	16
    130   1.1       jtc  = #define	REG_ATOI	255	// convert name to number (!)
    131   1.1       jtc  = #define	REG_ITOA	0400	// convert number to name (!)
    132   1.1       jtc  */
    133   1.9   mycroft static const struct rerr {
    134   1.1       jtc 	int code;
    135  1.10   mycroft 	const char *name;
    136  1.10   mycroft 	const char *explain;
    137   1.1       jtc } rerrs[] = {
    138   1.6  christos 	{ REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match" },
    139   1.6  christos 	{ REG_BADPAT,	"REG_BADPAT",	"invalid regular expression" },
    140   1.6  christos 	{ REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element" },
    141   1.6  christos 	{ REG_ECTYPE,	"REG_ECTYPE",	"invalid character class" },
    142   1.6  christos 	{ REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)" },
    143   1.6  christos 	{ REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number" },
    144   1.6  christos 	{ REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced" },
    145   1.6  christos 	{ REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced" },
    146   1.6  christos 	{ REG_EBRACE,	"REG_EBRACE",	"braces not balanced" },
    147   1.6  christos 	{ REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)" },
    148   1.6  christos 	{ REG_ERANGE,	"REG_ERANGE",	"invalid character range" },
    149   1.6  christos 	{ REG_ESPACE,	"REG_ESPACE",	"out of memory" },
    150   1.6  christos 	{ REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid" },
    151   1.6  christos 	{ REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression" },
    152   1.6  christos 	{ REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug" },
    153   1.6  christos 	{ REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine" },
    154   1.6  christos 	{ 0,		"",		"*** unknown regexp error code ***" }
    155   1.1       jtc };
    156   1.1       jtc 
    157   1.1       jtc /*
    158   1.5       mrg  * regerror - the interface to error numbers
    159   1.5       mrg  * extern size_t regerror(int, const regex_t *, char *, size_t);
    160   1.1       jtc  */
    161   1.1       jtc /* ARGSUSED */
    162   1.1       jtc size_t
    163   1.1       jtc regerror(errcode, preg, errbuf, errbuf_size)
    164   1.1       jtc int errcode;
    165   1.1       jtc const regex_t *preg;
    166   1.1       jtc char *errbuf;
    167   1.1       jtc size_t errbuf_size;
    168   1.1       jtc {
    169   1.9   mycroft 	const struct rerr *r;
    170   1.8     perry 	size_t len;
    171   1.8     perry 	int target = errcode &~ REG_ITOA;
    172  1.10   mycroft 	const char *s;
    173   1.1       jtc 	char convbuf[50];
    174   1.1       jtc 
    175  1.14    kleink 	_DIAGASSERT(errcode != REG_ATOI || preg != NULL);
    176  1.13     lukem 	_DIAGASSERT(errbuf != NULL);
    177  1.13     lukem 
    178   1.1       jtc 	if (errcode == REG_ATOI)
    179   1.5       mrg 		s = regatoi(preg, convbuf, sizeof convbuf);
    180   1.1       jtc 	else {
    181   1.1       jtc 		for (r = rerrs; r->code != 0; r++)
    182   1.1       jtc 			if (r->code == target)
    183   1.1       jtc 				break;
    184   1.1       jtc 
    185  1.12  drochner 		if (errcode & REG_ITOA) {
    186  1.11  christos 			if (r->code != 0) {
    187  1.17    itojun 				(void)strlcpy(convbuf, r->name, sizeof convbuf);
    188  1.12  drochner 			} else
    189   1.5       mrg 				(void)snprintf(convbuf, sizeof convbuf,
    190   1.5       mrg 				    "REG_0x%x", target);
    191   1.1       jtc 			s = convbuf;
    192   1.1       jtc 		} else
    193   1.1       jtc 			s = r->explain;
    194   1.1       jtc 	}
    195   1.1       jtc 
    196   1.1       jtc 	len = strlen(s) + 1;
    197  1.17    itojun 	if (errbuf_size > 0)
    198  1.17    itojun 		(void)strlcpy(errbuf, s, errbuf_size);
    199   1.1       jtc 
    200   1.1       jtc 	return(len);
    201   1.1       jtc }
    202   1.1       jtc 
    203   1.1       jtc /*
    204   1.5       mrg  * regatoi - internal routine to implement REG_ATOI
    205  1.20      yamt  * static const char *regatoi(const regex_t *preg, char *localbuf,
    206  1.20      yamt  * size_t buflen);
    207   1.1       jtc  */
    208  1.20      yamt static const char *
    209   1.5       mrg regatoi(preg, localbuf, buflen)
    210   1.1       jtc const regex_t *preg;
    211   1.1       jtc char *localbuf;
    212  1.11  christos size_t buflen;
    213   1.1       jtc {
    214   1.9   mycroft 	const struct rerr *r;
    215   1.1       jtc 
    216   1.1       jtc 	for (r = rerrs; r->code != 0; r++)
    217   1.1       jtc 		if (strcmp(r->name, preg->re_endp) == 0)
    218   1.1       jtc 			break;
    219   1.1       jtc 	if (r->code == 0)
    220  1.20      yamt 		return "0";
    221   1.1       jtc 
    222   1.5       mrg 	(void)snprintf(localbuf, buflen, "%d", r->code);
    223  1.19  christos 	return localbuf;
    224   1.1       jtc }
    225