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