155848Sbostic /*- 255848Sbostic * Copyright (c) 1992 Henry Spencer. 3*61162Sbostic * Copyright (c) 1992, 1993 4*61162Sbostic * The Regents of the University of California. 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*61162Sbostic * @(#)regerror.c 8.1 (Berkeley) 06/04/93 1255848Sbostic */ 1355848Sbostic 1455848Sbostic #if defined(LIBC_SCCS) && !defined(lint) 15*61162Sbostic static char sccsid[] = "@(#)regerror.c 8.1 (Berkeley) 06/04/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 2860201Sbostic static char *regatoi __P((const regex_t *, char *)); 2960201Sbostic 3060201Sbostic /* 3160201Sbostic = #define REG_NOMATCH 1 3260201Sbostic = #define REG_BADPAT 2 3360201Sbostic = #define REG_ECOLLATE 3 3460201Sbostic = #define REG_ECTYPE 4 3560201Sbostic = #define REG_EESCAPE 5 3660201Sbostic = #define REG_ESUBREG 6 3760201Sbostic = #define REG_EBRACK 7 3860201Sbostic = #define REG_EPAREN 8 3960201Sbostic = #define REG_EBRACE 9 4060201Sbostic = #define REG_BADBR 10 4160201Sbostic = #define REG_ERANGE 11 4260201Sbostic = #define REG_ESPACE 12 4360201Sbostic = #define REG_BADRPT 13 4460201Sbostic = #define REG_EMPTY 14 4560201Sbostic = #define REG_ASSERT 15 4660201Sbostic = #define REG_INVARG 16 4760201Sbostic = #define REG_ATOI 255 // convert name to number (!) 4860201Sbostic = #define REG_ITOA 0400 // convert number to name (!) 4960201Sbostic */ 5055848Sbostic static struct rerr { 5155848Sbostic int code; 5255848Sbostic char *name; 5355848Sbostic char *explain; 5455848Sbostic } rerrs[] = { 5560201Sbostic REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match", 5660201Sbostic REG_BADPAT, "REG_BADPAT", "invalid regular expression", 5760201Sbostic REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element", 5860201Sbostic REG_ECTYPE, "REG_ECTYPE", "invalid character class", 5960201Sbostic REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)", 6060201Sbostic REG_ESUBREG, "REG_ESUBREG", "invalid backreference number", 6160201Sbostic REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced", 6260201Sbostic REG_EPAREN, "REG_EPAREN", "parentheses not balanced", 6360201Sbostic REG_EBRACE, "REG_EBRACE", "braces not balanced", 6460201Sbostic REG_BADBR, "REG_BADBR", "invalid repetition count(s)", 6560201Sbostic REG_ERANGE, "REG_ERANGE", "invalid character range", 6660201Sbostic REG_ESPACE, "REG_ESPACE", "out of memory", 6760201Sbostic REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid", 6860201Sbostic REG_EMPTY, "REG_EMPTY", "empty (sub)expression", 6960201Sbostic REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug", 7060201Sbostic 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 7660201Sbostic = extern size_t regerror(int errcode, const regex_t *preg, char *errbuf, \ 7760201Sbostic = 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; 8960201Sbostic register int target = errcode &~ REG_ITOA; 9060201Sbostic register char *s; 9160201Sbostic char convbuf[50]; 9255848Sbostic 9360201Sbostic if (errcode == REG_ATOI) 9460201Sbostic s = regatoi(preg, convbuf); 9560201Sbostic else { 9660201Sbostic for (r = rerrs; r->code != 0; r++) 9760201Sbostic if (r->code == target) 9860201Sbostic break; 9960201Sbostic 10060201Sbostic if (errcode®_ITOA) { 10160201Sbostic if (r->code != 0) 10260201Sbostic (void) strcpy(convbuf, r->name); 10360201Sbostic else 10460201Sbostic sprintf(convbuf, "REG_0x%x", target); 10560201Sbostic assert(strlen(convbuf) < sizeof(convbuf)); 10660201Sbostic s = convbuf; 10760201Sbostic } else 10860201Sbostic s = r->explain; 10960201Sbostic } 11055848Sbostic 11160201Sbostic len = strlen(s) + 1; 11255848Sbostic if (errbuf_size > 0) { 11355848Sbostic if (errbuf_size > len) 11460201Sbostic (void) strcpy(errbuf, s); 11555848Sbostic else { 11660201Sbostic (void) strncpy(errbuf, s, errbuf_size-1); 11755848Sbostic errbuf[errbuf_size-1] = '\0'; 11855848Sbostic } 11955848Sbostic } 12055848Sbostic 12155848Sbostic return(len); 12255848Sbostic } 12355848Sbostic 12455848Sbostic /* 12560201Sbostic - regatoi - internal routine to implement REG_ATOI 12660201Sbostic = static char *regatoi(const regex_t *preg, char *localbuf); 12755848Sbostic */ 12860201Sbostic static char * 12960201Sbostic regatoi(preg, localbuf) 13060201Sbostic const regex_t *preg; 13160201Sbostic char *localbuf; 13255848Sbostic { 13355848Sbostic register struct rerr *r; 13460201Sbostic register size_t siz; 13560201Sbostic register char *p; 13655848Sbostic 13755848Sbostic for (r = rerrs; r->code != 0; r++) 13860201Sbostic if (strcmp(r->name, preg->re_endp) == 0) 13960201Sbostic break; 14060201Sbostic if (r->code == 0) 14160201Sbostic return("0"); 14255848Sbostic 14360201Sbostic sprintf(localbuf, "%d", r->code); 14460201Sbostic return(localbuf); 14555848Sbostic } 146