00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #if defined(LIBC_SCCS) && !defined(lint)
00041 static char sccsid[] = "@(#)regerror.c 8.4 (Berkeley) 3/20/94";
00042 #endif
00043
00044 #include <sys/types.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047 #include <ctype.h>
00048 #include <limits.h>
00049 #include <stdlib.h>
00050 #include "regex.h"
00051
00052 #include "utils.h"
00053
00054
00055 #ifdef __cplusplus
00056 extern "C" {
00057 #endif
00058
00059
00060 static char *regatoi __P((const regex_t *preg, char *localbuf));
00061
00062 #ifdef __cplusplus
00063 }
00064 #endif
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 static struct rerr {
00087 int code;
00088 char *name;
00089 char *explain;
00090 } rerrs[] = {
00091 REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match",
00092 REG_BADPAT, "REG_BADPAT", "invalid regular expression",
00093 REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element",
00094 REG_ECTYPE, "REG_ECTYPE", "invalid character class",
00095 REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)",
00096 REG_ESUBREG, "REG_ESUBREG", "invalid backreference number",
00097 REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced",
00098 REG_EPAREN, "REG_EPAREN", "parentheses not balanced",
00099 REG_EBRACE, "REG_EBRACE", "braces not balanced",
00100 REG_BADBR, "REG_BADBR", "invalid repetition count(s)",
00101 REG_ERANGE, "REG_ERANGE", "invalid character range",
00102 REG_ESPACE, "REG_ESPACE", "out of memory",
00103 REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid",
00104 REG_EMPTY, "REG_EMPTY", "empty (sub)expression",
00105 REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug",
00106 REG_INVARG, "REG_INVARG", "invalid argument to regex routine",
00107 0, "", "*** unknown regexp error code ***",
00108 };
00109
00110
00111
00112
00113
00114
00115 size_t __stdcall
00116 regerror(errcode, preg, errbuf, errbuf_size)
00117 int errcode;
00118 const regex_t *preg;
00119 char *errbuf;
00120 size_t errbuf_size;
00121 {
00122 register struct rerr *r;
00123 register size_t len;
00124 register int target = errcode &~ REG_ITOA;
00125 register char *s;
00126 char convbuf[50];
00127
00128 if (errcode == REG_ATOI)
00129 s = regatoi(preg, convbuf);
00130 else {
00131 for (r = rerrs; r->code != 0; r++)
00132 if (r->code == target)
00133 break;
00134
00135 if (errcode®_ITOA) {
00136 if (r->code != 0)
00137 (void) strcpy(convbuf, r->name);
00138 else
00139 sprintf(convbuf, "REG_0x%x", target);
00140 assert(strlen(convbuf) < sizeof(convbuf));
00141 s = convbuf;
00142 } else
00143 s = r->explain;
00144 }
00145
00146 len = strlen(s) + 1;
00147 if (errbuf_size > 0) {
00148 if (errbuf_size > len)
00149 (void) strcpy(errbuf, s);
00150 else {
00151 (void) strncpy(errbuf, s, errbuf_size-1);
00152 errbuf[errbuf_size-1] = '\0';
00153 }
00154 }
00155
00156 return(len);
00157 }
00158
00159
00160
00161
00162
00163 static char *
00164 regatoi(preg, localbuf)
00165 const regex_t *preg;
00166 char *localbuf;
00167 {
00168 register struct rerr *r;
00169 register size_t siz;
00170 register char *p;
00171
00172 for (r = rerrs; r->code != 0; r++)
00173 if (strcmp(r->name, preg->re_endp) == 0)
00174 break;
00175 if (r->code == 0)
00176 return("0");
00177
00178 sprintf(localbuf, "%d", r->code);
00179 return(localbuf);
00180 }