1 |
/* $Header$ */
|
2 |
/*
|
3 |
* regerror - error-code expansion
|
4 |
*
|
5 |
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
|
6 |
*
|
7 |
* Development of this software was funded, in part, by Cray Research Inc.,
|
8 |
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
|
9 |
* Corporation, none of whom are responsible for the results. The author
|
10 |
* thanks all of them.
|
11 |
*
|
12 |
* Redistribution and use in source and binary forms -- with or without
|
13 |
* modification -- are permitted for any purpose, provided that
|
14 |
* redistributions in source form retain this entire copyright notice and
|
15 |
* indicate the origin and nature of any modifications.
|
16 |
*
|
17 |
* I'd appreciate being given credit for this package in the documentation
|
18 |
* of software which uses it, but that is not a requirement.
|
19 |
*
|
20 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
21 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
22 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
23 |
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
26 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
27 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
28 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
29 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30 |
*
|
31 |
*/
|
32 |
|
33 |
#include "regguts.h"
|
34 |
|
35 |
/* unknown-error explanation */
|
36 |
static char unk[] = "*** unknown regex error code 0x%x ***";
|
37 |
|
38 |
/* struct to map among codes, code names, and explanations */
|
39 |
static struct rerr {
|
40 |
int code;
|
41 |
char *name;
|
42 |
char *explain;
|
43 |
} rerrs[] = {
|
44 |
/* the actual table is built from regex.h */
|
45 |
# include "regerrs.h"
|
46 |
{ -1, "", "oops" }, /* explanation special-cased in code */
|
47 |
};
|
48 |
|
49 |
/*
|
50 |
- regerror - the interface to error numbers
|
51 |
*/
|
52 |
/* ARGSUSED */
|
53 |
size_t /* actual space needed (including NUL) */
|
54 |
regerror(errcode, preg, errbuf, errbuf_size)
|
55 |
int errcode; /* error code, or REG_ATOI or REG_ITOA */
|
56 |
CONST regex_t *preg; /* associated regex_t (unused at present) */
|
57 |
char *errbuf; /* result buffer (unless errbuf_size==0) */
|
58 |
size_t errbuf_size; /* available space in errbuf, can be 0 */
|
59 |
{
|
60 |
struct rerr *r;
|
61 |
char *msg;
|
62 |
char convbuf[sizeof(unk)+50]; /* 50 = plenty for int */
|
63 |
size_t len;
|
64 |
int icode;
|
65 |
|
66 |
switch (errcode) {
|
67 |
case REG_ATOI: /* convert name to number */
|
68 |
for (r = rerrs; r->code >= 0; r++)
|
69 |
if (strcmp(r->name, errbuf) == 0)
|
70 |
break;
|
71 |
sprintf(convbuf, "%d", r->code); /* -1 for unknown */
|
72 |
msg = convbuf;
|
73 |
break;
|
74 |
case REG_ITOA: /* convert number to name */
|
75 |
icode = atoi(errbuf); /* not our problem if this fails */
|
76 |
for (r = rerrs; r->code >= 0; r++)
|
77 |
if (r->code == icode)
|
78 |
break;
|
79 |
if (r->code >= 0)
|
80 |
msg = r->name;
|
81 |
else { /* unknown; tell him the number */
|
82 |
sprintf(convbuf, "REG_%u", (unsigned)icode);
|
83 |
msg = convbuf;
|
84 |
}
|
85 |
break;
|
86 |
default: /* a real, normal error code */
|
87 |
for (r = rerrs; r->code >= 0; r++)
|
88 |
if (r->code == errcode)
|
89 |
break;
|
90 |
if (r->code >= 0)
|
91 |
msg = r->explain;
|
92 |
else { /* unknown; say so */
|
93 |
sprintf(convbuf, unk, errcode);
|
94 |
msg = convbuf;
|
95 |
}
|
96 |
break;
|
97 |
}
|
98 |
|
99 |
len = strlen(msg) + 1; /* space needed, including NUL */
|
100 |
if (errbuf_size > 0) {
|
101 |
if (errbuf_size > len)
|
102 |
strcpy(errbuf, msg);
|
103 |
else { /* truncate to fit */
|
104 |
strncpy(errbuf, msg, errbuf_size-1);
|
105 |
errbuf[errbuf_size-1] = '\0';
|
106 |
}
|
107 |
}
|
108 |
|
109 |
return len;
|
110 |
}
|
111 |
|
112 |
/* End of regerror.c */
|