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