G:/ScriptBasic/source/extensions/re/regerror.c

Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
00003  * Copyright (c) 1992, 1993, 1994
00004  *      The Regents of the University of California.  All rights reserved.
00005  *
00006  * This code is derived from software contributed to Berkeley by
00007  * Henry Spencer.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. All advertising materials mentioning features or use of this software
00018  *    must display the following acknowledgement:
00019  *      This product includes software developed by the University of
00020  *      California, Berkeley and its contributors.
00021  * 4. Neither the name of the University nor the names of its contributors
00022  *    may be used to endorse or promote products derived from this software
00023  *    without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00026  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00031  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00032  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00034  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00035  * SUCH DAMAGE.
00036  *
00037  *      @(#)regerror.c  8.4 (Berkeley) 3/20/94
00038  */
00039 
00040 #if defined(LIBC_SCCS) && !defined(lint)
00041 static char sccsid[] = "@(#)regerror.c  8.4 (Berkeley) 3/20/94";
00042 #endif /* LIBC_SCCS and not lint */
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 /* ========= begin header generated by ./mkh ========= */
00055 #ifdef __cplusplus
00056 extern "C" {
00057 #endif
00058 
00059 /* === regerror.c === */
00060 static char *regatoi __P((const regex_t *preg, char *localbuf));
00061 
00062 #ifdef __cplusplus
00063 }
00064 #endif
00065 /* ========= end header generated by ./mkh ========= */
00066 /*
00067  = #define      REG_NOMATCH      1
00068  = #define      REG_BADPAT       2
00069  = #define      REG_ECOLLATE     3
00070  = #define      REG_ECTYPE       4
00071  = #define      REG_EESCAPE      5
00072  = #define      REG_ESUBREG      6
00073  = #define      REG_EBRACK       7
00074  = #define      REG_EPAREN       8
00075  = #define      REG_EBRACE       9
00076  = #define      REG_BADBR       10
00077  = #define      REG_ERANGE      11
00078  = #define      REG_ESPACE      12
00079  = #define      REG_BADRPT      13
00080  = #define      REG_EMPTY       14
00081  = #define      REG_ASSERT      15
00082  = #define      REG_INVARG      16
00083  = #define      REG_ATOI        255     // convert name to number (!)
00084  = #define      REG_ITOA        0400    // convert number to name (!)
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  - regerror - the interface to error numbers
00112  = extern size_t regerror(int, const regex_t *, char *, size_t);
00113  */
00114 /* ARGSUSED */
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&REG_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  - regatoi - internal routine to implement REG_ATOI
00161  == static char *regatoi(const regex_t *preg, char *localbuf);
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 }

Generated on Sun Mar 12 23:56:30 2006 for ScriptBasic by  doxygen 1.4.6-NO