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