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