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