Home | History | Annotate | Line # | Download | only in regex
      1  1.26  riastrad /*	$NetBSD: regerror.c,v 1.26 2022/11/05 11:33:55 riastradh 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.25  christos #if HAVE_NBTOOL_CONFIG_H
     41  1.25  christos #include "nbtool_config.h"
     42  1.25  christos #endif
     43  1.25  christos 
     44   1.6  christos #include <sys/cdefs.h>
     45   1.4       cgd #if 0
     46   1.3       cgd static char sccsid[] = "@(#)regerror.c	8.4 (Berkeley) 3/20/94";
     47  1.24  christos __FBSDID("$FreeBSD: head/lib/libc/regex/regerror.c 326025 2017-11-20 19:49:47Z pfg $");
     48   1.4       cgd #endif
     49  1.26  riastrad __RCSID("$NetBSD: regerror.c,v 1.26 2022/11/05 11:33:55 riastradh Exp $");
     50   1.3       cgd 
     51   1.7       jtc #include "namespace.h"
     52   1.1       jtc #include <sys/types.h>
     53  1.24  christos #include <stdio.h>
     54  1.24  christos #include <string.h>
     55   1.1       jtc #include <limits.h>
     56   1.1       jtc #include <stdlib.h>
     57  1.23  junyoung #include <regex.h>
     58   1.7       jtc 
     59  1.24  christos #include "utils.h"
     60  1.24  christos 
     61   1.7       jtc #ifdef __weak_alias
     62  1.16   mycroft __weak_alias(regerror,_regerror)
     63   1.7       jtc #endif
     64   1.1       jtc 
     65   1.3       cgd /* ========= begin header generated by ./mkh ========= */
     66   1.3       cgd #ifdef __cplusplus
     67   1.3       cgd extern "C" {
     68   1.3       cgd #endif
     69   1.3       cgd 
     70   1.3       cgd /* === regerror.c === */
     71  1.24  christos static const char *regatoi(const regex_t *preg, char *localbufm, size_t buflen);
     72   1.3       cgd 
     73   1.3       cgd #ifdef __cplusplus
     74   1.3       cgd }
     75   1.3       cgd #endif
     76   1.3       cgd /* ========= end header generated by ./mkh ========= */
     77   1.1       jtc /*
     78   1.1       jtc  = #define	REG_NOMATCH	 1
     79   1.1       jtc  = #define	REG_BADPAT	 2
     80   1.1       jtc  = #define	REG_ECOLLATE	 3
     81   1.1       jtc  = #define	REG_ECTYPE	 4
     82   1.1       jtc  = #define	REG_EESCAPE	 5
     83   1.1       jtc  = #define	REG_ESUBREG	 6
     84   1.1       jtc  = #define	REG_EBRACK	 7
     85   1.1       jtc  = #define	REG_EPAREN	 8
     86   1.1       jtc  = #define	REG_EBRACE	 9
     87   1.1       jtc  = #define	REG_BADBR	10
     88   1.1       jtc  = #define	REG_ERANGE	11
     89   1.1       jtc  = #define	REG_ESPACE	12
     90   1.1       jtc  = #define	REG_BADRPT	13
     91   1.1       jtc  = #define	REG_EMPTY	14
     92   1.1       jtc  = #define	REG_ASSERT	15
     93   1.1       jtc  = #define	REG_INVARG	16
     94  1.24  christos  = #define	REG_ENOSYS	17
     95  1.24  christos  = #define	REG_ILLSEQ	18
     96   1.1       jtc  = #define	REG_ATOI	255	// convert name to number (!)
     97   1.1       jtc  = #define	REG_ITOA	0400	// convert number to name (!)
     98   1.1       jtc  */
     99   1.9   mycroft static const struct rerr {
    100   1.1       jtc 	int code;
    101  1.10   mycroft 	const char *name;
    102  1.10   mycroft 	const char *explain;
    103   1.1       jtc } rerrs[] = {
    104  1.24  christos 	{REG_NOMATCH,	"REG_NOMATCH",	"regexec() failed to match"},
    105  1.24  christos 	{REG_BADPAT,	"REG_BADPAT",	"invalid regular expression"},
    106  1.24  christos 	{REG_ECOLLATE,	"REG_ECOLLATE",	"invalid collating element"},
    107  1.24  christos 	{REG_ECTYPE,	"REG_ECTYPE",	"invalid character class"},
    108  1.24  christos 	{REG_EESCAPE,	"REG_EESCAPE",	"trailing backslash (\\)"},
    109  1.24  christos 	{REG_ESUBREG,	"REG_ESUBREG",	"invalid backreference number"},
    110  1.24  christos 	{REG_EBRACK,	"REG_EBRACK",	"brackets ([ ]) not balanced"},
    111  1.24  christos 	{REG_EPAREN,	"REG_EPAREN",	"parentheses not balanced"},
    112  1.24  christos 	{REG_EBRACE,	"REG_EBRACE",	"braces not balanced"},
    113  1.24  christos 	{REG_BADBR,	"REG_BADBR",	"invalid repetition count(s)"},
    114  1.24  christos 	{REG_ERANGE,	"REG_ERANGE",	"invalid character range"},
    115  1.24  christos 	{REG_ESPACE,	"REG_ESPACE",	"out of memory"},
    116  1.24  christos 	{REG_BADRPT,	"REG_BADRPT",	"repetition-operator operand invalid"},
    117  1.24  christos 	{REG_EMPTY,	"REG_EMPTY",	"empty (sub)expression"},
    118  1.24  christos 	{REG_ASSERT,	"REG_ASSERT",	"\"can't happen\" -- you found a bug"},
    119  1.24  christos 	{REG_INVARG,	"REG_INVARG",	"invalid argument to regex routine"},
    120  1.24  christos 	{REG_ILLSEQ,	"REG_ILLSEQ",	"illegal byte sequence"},
    121  1.24  christos 	{0,		"",		"*** unknown regexp error code ***"}
    122   1.1       jtc };
    123   1.1       jtc 
    124   1.1       jtc /*
    125  1.24  christos  - regerror - the interface to error numbers
    126  1.24  christos  = extern size_t regerror(int, const regex_t *, char *, size_t);
    127   1.1       jtc  */
    128   1.1       jtc /* ARGSUSED */
    129   1.1       jtc size_t
    130  1.24  christos regerror(int errcode,
    131  1.24  christos 	 const regex_t * __restrict preg,
    132  1.24  christos 	 char * __restrict errbuf,
    133  1.24  christos 	 size_t errbuf_size)
    134   1.1       jtc {
    135   1.9   mycroft 	const struct rerr *r;
    136   1.8     perry 	size_t len;
    137   1.8     perry 	int target = errcode &~ REG_ITOA;
    138  1.10   mycroft 	const char *s;
    139   1.1       jtc 	char convbuf[50];
    140   1.1       jtc 
    141  1.14    kleink 	_DIAGASSERT(errcode != REG_ATOI || preg != NULL);
    142  1.26  riastrad 	_DIAGASSERT(errbuf_size == 0 || errbuf != NULL);
    143  1.13     lukem 
    144  1.24  christos 	if (errcode == REG_ATOI) {
    145   1.5       mrg 		s = regatoi(preg, convbuf, sizeof convbuf);
    146  1.24  christos 	} else {
    147   1.1       jtc 		for (r = rerrs; r->code != 0; r++)
    148   1.1       jtc 			if (r->code == target)
    149   1.1       jtc 				break;
    150  1.24  christos 
    151  1.24  christos 		if (errcode&REG_ITOA) {
    152  1.24  christos 			if (r->code != 0)
    153  1.24  christos 				(void) strlcpy(convbuf, r->name, sizeof(convbuf));
    154  1.24  christos 			else
    155  1.24  christos 				snprintf(convbuf, sizeof(convbuf), "REG_0x%x", target);
    156   1.1       jtc 			s = convbuf;
    157   1.1       jtc 		} else
    158   1.1       jtc 			s = r->explain;
    159   1.1       jtc 	}
    160   1.1       jtc 
    161   1.1       jtc 	len = strlen(s) + 1;
    162  1.17    itojun 	if (errbuf_size > 0)
    163  1.24  christos 		(void) strlcpy(errbuf, s, errbuf_size);
    164   1.1       jtc 
    165   1.1       jtc 	return(len);
    166   1.1       jtc }
    167   1.1       jtc 
    168   1.1       jtc /*
    169  1.24  christos  - regatoi - internal routine to implement REG_ATOI
    170  1.24  christos  == static char *regatoi(const regex_t *preg, char *localbuf);
    171   1.1       jtc  */
    172  1.20      yamt static const char *
    173  1.24  christos regatoi(const regex_t *preg, char *localbuf, size_t buflen)
    174   1.1       jtc {
    175   1.9   mycroft 	const struct rerr *r;
    176   1.1       jtc 
    177   1.1       jtc 	for (r = rerrs; r->code != 0; r++)
    178   1.1       jtc 		if (strcmp(r->name, preg->re_endp) == 0)
    179   1.1       jtc 			break;
    180   1.1       jtc 	if (r->code == 0)
    181  1.20      yamt 		return "0";
    182   1.1       jtc 
    183  1.24  christos 	snprintf(localbuf, buflen, "%d", r->code);
    184  1.19  christos 	return localbuf;
    185   1.1       jtc }
    186