regerror.cGo to the documentation of this file.00001 /* 00002 * regerror - error-code expansion 00003 * 00004 * Copyright (c) 1998, 1999 Henry Spencer. All rights reserved. 00005 * 00006 * Development of this software was funded, in part, by Cray Research Inc., 00007 * UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics 00008 * Corporation, none of whom are responsible for the results. The author 00009 * thanks all of them. 00010 * 00011 * Redistribution and use in source and binary forms -- with or without 00012 * modification -- are permitted for any purpose, provided that 00013 * redistributions in source form retain this entire copyright notice and 00014 * indicate the origin and nature of any modifications. 00015 * 00016 * I'd appreciate being given credit for this package in the documentation of 00017 * software which uses it, but that is not a requirement. 00018 * 00019 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 00020 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00021 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 00022 * HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00023 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00024 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 00025 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00026 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00027 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 00028 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 * 00030 */ 00031 00032 #include "regguts.h" 00033 00034 /* 00035 * Unknown-error explanation. 00036 */ 00037 00038 static char unk[] = "*** unknown regex error code 0x%x ***"; 00039 00040 /* 00041 * Struct to map among codes, code names, and explanations. 00042 */ 00043 00044 static struct rerr { 00045 int code; 00046 const char *name; 00047 const char *explain; 00048 } rerrs[] = { 00049 /* The actual table is built from regex.h */ 00050 #include "regerrs.h" 00051 { -1, "", "oops" }, /* explanation special-cased in code */ 00052 }; 00053 00054 /* 00055 - regerror - the interface to error numbers 00056 */ 00057 /* ARGSUSED */ 00058 size_t /* Actual space needed (including NUL) */ 00059 regerror( 00060 int code, /* Error code, or REG_ATOI or REG_ITOA */ 00061 const regex_t *preg, /* Associated regex_t (unused at present) */ 00062 char *errbuf, /* Result buffer (unless errbuf_size==0) */ 00063 size_t errbuf_size) /* Available space in errbuf, can be 0 */ 00064 { 00065 struct rerr *r; 00066 const char *msg; 00067 char convbuf[sizeof(unk)+50]; /* 50 = plenty for int */ 00068 size_t len; 00069 int icode; 00070 00071 switch (code) { 00072 case REG_ATOI: /* Convert name to number */ 00073 for (r = rerrs; r->code >= 0; r++) { 00074 if (strcmp(r->name, errbuf) == 0) { 00075 break; 00076 } 00077 } 00078 sprintf(convbuf, "%d", r->code); /* -1 for unknown */ 00079 msg = convbuf; 00080 break; 00081 case REG_ITOA: /* Convert number to name */ 00082 icode = atoi(errbuf); /* Not our problem if this fails */ 00083 for (r = rerrs; r->code >= 0; r++) { 00084 if (r->code == icode) { 00085 break; 00086 } 00087 } 00088 if (r->code >= 0) { 00089 msg = r->name; 00090 } else { /* Unknown; tell him the number */ 00091 sprintf(convbuf, "REG_%u", (unsigned)icode); 00092 msg = convbuf; 00093 } 00094 break; 00095 default: /* A real, normal error code */ 00096 for (r = rerrs; r->code >= 0; r++) { 00097 if (r->code == code) { 00098 break; 00099 } 00100 } 00101 if (r->code >= 0) { 00102 msg = r->explain; 00103 } else { /* Unknown; say so */ 00104 sprintf(convbuf, unk, code); 00105 msg = convbuf; 00106 } 00107 break; 00108 } 00109 00110 len = strlen(msg) + 1; /* Space needed, including NUL */ 00111 if (errbuf_size > 0) { 00112 if (errbuf_size > len) { 00113 strcpy(errbuf, msg); 00114 } else { /* Truncate to fit */ 00115 strncpy(errbuf, msg, errbuf_size-1); 00116 errbuf[errbuf_size-1] = '\0'; 00117 } 00118 } 00119 00120 return len; 00121 } 00122 00123 /* 00124 * Local Variables: 00125 * mode: c 00126 * c-basic-offset: 4 00127 * fill-column: 78 00128 * End: 00129 */
Generated on Wed Mar 12 12:18:10 2008 by 1.5.1 |