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