1*55848Sbostic /*- 2*55848Sbostic * Copyright (c) 1992 Henry Spencer. 3*55848Sbostic * Copyright (c) 1992 The Regents of the University of California. 4*55848Sbostic * All rights reserved. 5*55848Sbostic * 6*55848Sbostic * This code is derived from software contributed to Berkeley by 7*55848Sbostic * Henry Spencer of the University of Toronto. 8*55848Sbostic * 9*55848Sbostic * %sccs.include.redist.c% 10*55848Sbostic * 11*55848Sbostic * @(#)regerror.c 5.1 (Berkeley) 08/06/92 12*55848Sbostic */ 13*55848Sbostic 14*55848Sbostic #if defined(LIBC_SCCS) && !defined(lint) 15*55848Sbostic static char sccsid[] = "@(#)regerror.c 5.1 (Berkeley) 08/06/92"; 16*55848Sbostic #endif /* LIBC_SCCS and not lint */ 17*55848Sbostic 18*55848Sbostic #include <sys/types.h> 19*55848Sbostic 20*55848Sbostic #include <stdio.h> 21*55848Sbostic #include <string.h> 22*55848Sbostic #include <ctype.h> 23*55848Sbostic #include <limits.h> 24*55848Sbostic #include <stdlib.h> 25*55848Sbostic #include <assert.h> 26*55848Sbostic #include <regex.h> 27*55848Sbostic 28*55848Sbostic #include "utils.h" 29*55848Sbostic 30*55848Sbostic static struct rerr { 31*55848Sbostic int code; 32*55848Sbostic char *name; 33*55848Sbostic char *explain; 34*55848Sbostic } rerrs[] = { 35*55848Sbostic REG_NOMATCH, "NOMATCH", "regexec() failed to match", 36*55848Sbostic REG_BADPAT, "BADPAT", "invalid regular expression", 37*55848Sbostic REG_ECOLLATE, "ECOLLATE", "invalid collating element", 38*55848Sbostic REG_ECTYPE, "ECTYPE", "invalid character class", 39*55848Sbostic REG_EESCAPE, "EESCAPE", "\\ applied to unescapable character", 40*55848Sbostic REG_ESUBREG, "ESUBREG", "invalid backreference number", 41*55848Sbostic REG_EBRACK, "EBRACK", "brackets ([ ]) not balanced", 42*55848Sbostic REG_EPAREN, "EPAREN", "parentheses not balanced", 43*55848Sbostic REG_EBRACE, "EBRACE", "braces not balanced", 44*55848Sbostic REG_BADBR, "BADBR", "invalid repetition count(s)", 45*55848Sbostic REG_ERANGE, "ERANGE", "invalid character range", 46*55848Sbostic REG_ESPACE, "ESPACE", "out of memory", 47*55848Sbostic REG_BADRPT, "BADRPT", "repetition-operator operand invalid", 48*55848Sbostic REG_EMPTY, "EMPTY", "empty (sub)expression", 49*55848Sbostic REG_ASSERT, "ASSERT", "\"can't happen\" -- you found a bug", 50*55848Sbostic 0, "", "*** unknown regexp error code ***", 51*55848Sbostic }; 52*55848Sbostic 53*55848Sbostic /* 54*55848Sbostic - regerror - the interface to error numbers 55*55848Sbostic */ 56*55848Sbostic /* ARGSUSED */ 57*55848Sbostic size_t 58*55848Sbostic regerror(errcode, preg, errbuf, errbuf_size) 59*55848Sbostic int errcode; 60*55848Sbostic const regex_t *preg; 61*55848Sbostic char *errbuf; 62*55848Sbostic size_t errbuf_size; 63*55848Sbostic { 64*55848Sbostic register struct rerr *r; 65*55848Sbostic register size_t len; 66*55848Sbostic 67*55848Sbostic for (r = rerrs; r->code != 0; r++) 68*55848Sbostic if (r->code == errcode) 69*55848Sbostic break; 70*55848Sbostic 71*55848Sbostic len = strlen(r->explain) + 1; 72*55848Sbostic if (errbuf_size > 0) { 73*55848Sbostic if (errbuf_size > len) 74*55848Sbostic (void) strcpy(errbuf, r->explain); 75*55848Sbostic else { 76*55848Sbostic (void) strncpy(errbuf, r->explain, errbuf_size-1); 77*55848Sbostic errbuf[errbuf_size-1] = '\0'; 78*55848Sbostic } 79*55848Sbostic } 80*55848Sbostic 81*55848Sbostic return(len); 82*55848Sbostic } 83*55848Sbostic 84*55848Sbostic #ifndef NDEBUG 85*55848Sbostic /* 86*55848Sbostic - eprint - express an error number as a string 87*55848Sbostic */ 88*55848Sbostic char * 89*55848Sbostic eprint(eno) 90*55848Sbostic int eno; 91*55848Sbostic { 92*55848Sbostic register struct rerr *r; 93*55848Sbostic static char eval[10]; 94*55848Sbostic 95*55848Sbostic for (r = rerrs; r->code != 0; r++) 96*55848Sbostic if (r->code == eno) 97*55848Sbostic return(r->name); 98*55848Sbostic sprintf(eval, "#%d", r->code); 99*55848Sbostic return(eval); 100*55848Sbostic } 101*55848Sbostic 102*55848Sbostic /* 103*55848Sbostic - efind - find an error name 104*55848Sbostic */ 105*55848Sbostic int 106*55848Sbostic efind(ename) 107*55848Sbostic char *ename; 108*55848Sbostic { 109*55848Sbostic register struct rerr *r; 110*55848Sbostic 111*55848Sbostic for (r = rerrs; r->code != 0; r++) 112*55848Sbostic if (strcmp(r->name, ename) == 0) 113*55848Sbostic return(r->code); 114*55848Sbostic return(0); /* it'll do */ 115*55848Sbostic } 116*55848Sbostic #endif 117