155848Sbostic /*- 255848Sbostic * Copyright (c) 1992 Henry Spencer. 355848Sbostic * Copyright (c) 1992 The Regents of the University of California. 455848Sbostic * All rights reserved. 555848Sbostic * 655848Sbostic * This code is derived from software contributed to Berkeley by 755848Sbostic * Henry Spencer of the University of Toronto. 855848Sbostic * 955848Sbostic * %sccs.include.redist.c% 1055848Sbostic * 11*60201Sbostic * @(#)regerror.c 5.4 (Berkeley) 05/21/93 1255848Sbostic */ 1355848Sbostic 1455848Sbostic #if defined(LIBC_SCCS) && !defined(lint) 15*60201Sbostic static char sccsid[] = "@(#)regerror.c 5.4 (Berkeley) 05/21/93"; 1655848Sbostic #endif /* LIBC_SCCS and not lint */ 1755848Sbostic 1855848Sbostic #include <sys/types.h> 1955848Sbostic #include <stdio.h> 2055848Sbostic #include <string.h> 2155848Sbostic #include <ctype.h> 2255848Sbostic #include <limits.h> 2355848Sbostic #include <stdlib.h> 2455848Sbostic #include <regex.h> 2555848Sbostic 2655848Sbostic #include "utils.h" 2755848Sbostic 28*60201Sbostic static char *regatoi __P((const regex_t *, char *)); 29*60201Sbostic 30*60201Sbostic /* 31*60201Sbostic = #define REG_NOMATCH 1 32*60201Sbostic = #define REG_BADPAT 2 33*60201Sbostic = #define REG_ECOLLATE 3 34*60201Sbostic = #define REG_ECTYPE 4 35*60201Sbostic = #define REG_EESCAPE 5 36*60201Sbostic = #define REG_ESUBREG 6 37*60201Sbostic = #define REG_EBRACK 7 38*60201Sbostic = #define REG_EPAREN 8 39*60201Sbostic = #define REG_EBRACE 9 40*60201Sbostic = #define REG_BADBR 10 41*60201Sbostic = #define REG_ERANGE 11 42*60201Sbostic = #define REG_ESPACE 12 43*60201Sbostic = #define REG_BADRPT 13 44*60201Sbostic = #define REG_EMPTY 14 45*60201Sbostic = #define REG_ASSERT 15 46*60201Sbostic = #define REG_INVARG 16 47*60201Sbostic = #define REG_ATOI 255 // convert name to number (!) 48*60201Sbostic = #define REG_ITOA 0400 // convert number to name (!) 49*60201Sbostic */ 5055848Sbostic static struct rerr { 5155848Sbostic int code; 5255848Sbostic char *name; 5355848Sbostic char *explain; 5455848Sbostic } rerrs[] = { 55*60201Sbostic REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match", 56*60201Sbostic REG_BADPAT, "REG_BADPAT", "invalid regular expression", 57*60201Sbostic REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element", 58*60201Sbostic REG_ECTYPE, "REG_ECTYPE", "invalid character class", 59*60201Sbostic REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)", 60*60201Sbostic REG_ESUBREG, "REG_ESUBREG", "invalid backreference number", 61*60201Sbostic REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced", 62*60201Sbostic REG_EPAREN, "REG_EPAREN", "parentheses not balanced", 63*60201Sbostic REG_EBRACE, "REG_EBRACE", "braces not balanced", 64*60201Sbostic REG_BADBR, "REG_BADBR", "invalid repetition count(s)", 65*60201Sbostic REG_ERANGE, "REG_ERANGE", "invalid character range", 66*60201Sbostic REG_ESPACE, "REG_ESPACE", "out of memory", 67*60201Sbostic REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid", 68*60201Sbostic REG_EMPTY, "REG_EMPTY", "empty (sub)expression", 69*60201Sbostic REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug", 70*60201Sbostic REG_INVARG, "REG_INVARG", "invalid argument to regex routine", 7155848Sbostic 0, "", "*** unknown regexp error code ***", 7255848Sbostic }; 7355848Sbostic 7455848Sbostic /* 7555848Sbostic - regerror - the interface to error numbers 76*60201Sbostic = extern size_t regerror(int errcode, const regex_t *preg, char *errbuf, \ 77*60201Sbostic = size_t errbuf_size); 7855848Sbostic */ 7955848Sbostic /* ARGSUSED */ 8055848Sbostic size_t 8155848Sbostic regerror(errcode, preg, errbuf, errbuf_size) 8255848Sbostic int errcode; 8355848Sbostic const regex_t *preg; 8455848Sbostic char *errbuf; 8555848Sbostic size_t errbuf_size; 8655848Sbostic { 8755848Sbostic register struct rerr *r; 8855848Sbostic register size_t len; 89*60201Sbostic register int target = errcode &~ REG_ITOA; 90*60201Sbostic register char *s; 91*60201Sbostic char convbuf[50]; 9255848Sbostic 93*60201Sbostic if (errcode == REG_ATOI) 94*60201Sbostic s = regatoi(preg, convbuf); 95*60201Sbostic else { 96*60201Sbostic for (r = rerrs; r->code != 0; r++) 97*60201Sbostic if (r->code == target) 98*60201Sbostic break; 99*60201Sbostic 100*60201Sbostic if (errcode®_ITOA) { 101*60201Sbostic if (r->code != 0) 102*60201Sbostic (void) strcpy(convbuf, r->name); 103*60201Sbostic else 104*60201Sbostic sprintf(convbuf, "REG_0x%x", target); 105*60201Sbostic assert(strlen(convbuf) < sizeof(convbuf)); 106*60201Sbostic s = convbuf; 107*60201Sbostic } else 108*60201Sbostic s = r->explain; 109*60201Sbostic } 11055848Sbostic 111*60201Sbostic len = strlen(s) + 1; 11255848Sbostic if (errbuf_size > 0) { 11355848Sbostic if (errbuf_size > len) 114*60201Sbostic (void) strcpy(errbuf, s); 11555848Sbostic else { 116*60201Sbostic (void) strncpy(errbuf, s, errbuf_size-1); 11755848Sbostic errbuf[errbuf_size-1] = '\0'; 11855848Sbostic } 11955848Sbostic } 12055848Sbostic 12155848Sbostic return(len); 12255848Sbostic } 12355848Sbostic 12455848Sbostic /* 125*60201Sbostic - regatoi - internal routine to implement REG_ATOI 126*60201Sbostic = static char *regatoi(const regex_t *preg, char *localbuf); 12755848Sbostic */ 128*60201Sbostic static char * 129*60201Sbostic regatoi(preg, localbuf) 130*60201Sbostic const regex_t *preg; 131*60201Sbostic char *localbuf; 13255848Sbostic { 13355848Sbostic register struct rerr *r; 134*60201Sbostic register size_t siz; 135*60201Sbostic register char *p; 13655848Sbostic 13755848Sbostic for (r = rerrs; r->code != 0; r++) 138*60201Sbostic if (strcmp(r->name, preg->re_endp) == 0) 139*60201Sbostic break; 140*60201Sbostic if (r->code == 0) 141*60201Sbostic return("0"); 14255848Sbostic 143*60201Sbostic sprintf(localbuf, "%d", r->code); 144*60201Sbostic return(localbuf); 14555848Sbostic } 146