LLVM API Documentation
00001 /*- 00002 * This code is derived from OpenBSD's libc/regex, original license follows: 00003 * 00004 * Copyright (c) 1992, 1993, 1994 Henry Spencer. 00005 * Copyright (c) 1992, 1993, 1994 00006 * The Regents of the University of California. All rights reserved. 00007 * 00008 * This code is derived from software contributed to Berkeley by 00009 * Henry Spencer. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright 00017 * notice, this list of conditions and the following disclaimer in the 00018 * documentation and/or other materials provided with the distribution. 00019 * 3. Neither the name of the University nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00029 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00032 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00033 * SUCH DAMAGE. 00034 * 00035 * @(#)regerror.c 8.4 (Berkeley) 3/20/94 00036 */ 00037 00038 #include <sys/types.h> 00039 #include <stdio.h> 00040 #include <string.h> 00041 #include <ctype.h> 00042 #include <limits.h> 00043 #include <stdlib.h> 00044 #include "regex_impl.h" 00045 00046 #include "regutils.h" 00047 00048 #ifdef _MSC_VER 00049 #define snprintf _snprintf 00050 #endif 00051 00052 static const char *regatoi(const llvm_regex_t *, char *, int); 00053 00054 static struct rerr { 00055 int code; 00056 const char *name; 00057 const char *explain; 00058 } rerrs[] = { 00059 { REG_NOMATCH, "REG_NOMATCH", "llvm_regexec() failed to match" }, 00060 { REG_BADPAT, "REG_BADPAT", "invalid regular expression" }, 00061 { REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element" }, 00062 { REG_ECTYPE, "REG_ECTYPE", "invalid character class" }, 00063 { REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)" }, 00064 { REG_ESUBREG, "REG_ESUBREG", "invalid backreference number" }, 00065 { REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced" }, 00066 { REG_EPAREN, "REG_EPAREN", "parentheses not balanced" }, 00067 { REG_EBRACE, "REG_EBRACE", "braces not balanced" }, 00068 { REG_BADBR, "REG_BADBR", "invalid repetition count(s)" }, 00069 { REG_ERANGE, "REG_ERANGE", "invalid character range" }, 00070 { REG_ESPACE, "REG_ESPACE", "out of memory" }, 00071 { REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid" }, 00072 { REG_EMPTY, "REG_EMPTY", "empty (sub)expression" }, 00073 { REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug" }, 00074 { REG_INVARG, "REG_INVARG", "invalid argument to regex routine" }, 00075 { 0, "", "*** unknown regexp error code ***" } 00076 }; 00077 00078 /* 00079 - llvm_regerror - the interface to error numbers 00080 = extern size_t llvm_regerror(int, const llvm_regex_t *, char *, size_t); 00081 */ 00082 /* ARGSUSED */ 00083 size_t 00084 llvm_regerror(int errcode, const llvm_regex_t *preg, char *errbuf, size_t errbuf_size) 00085 { 00086 struct rerr *r; 00087 size_t len; 00088 int target = errcode &~ REG_ITOA; 00089 const char *s; 00090 char convbuf[50]; 00091 00092 if (errcode == REG_ATOI) 00093 s = regatoi(preg, convbuf, sizeof convbuf); 00094 else { 00095 for (r = rerrs; r->code != 0; r++) 00096 if (r->code == target) 00097 break; 00098 00099 if (errcode®_ITOA) { 00100 if (r->code != 0) { 00101 assert(strlen(r->name) < sizeof(convbuf)); 00102 (void) llvm_strlcpy(convbuf, r->name, sizeof convbuf); 00103 } else 00104 (void)snprintf(convbuf, sizeof convbuf, 00105 "REG_0x%x", target); 00106 s = convbuf; 00107 } else 00108 s = r->explain; 00109 } 00110 00111 len = strlen(s) + 1; 00112 if (errbuf_size > 0) { 00113 llvm_strlcpy(errbuf, s, errbuf_size); 00114 } 00115 00116 return(len); 00117 } 00118 00119 /* 00120 - regatoi - internal routine to implement REG_ATOI 00121 */ 00122 static const char * 00123 regatoi(const llvm_regex_t *preg, char *localbuf, int localbufsize) 00124 { 00125 struct rerr *r; 00126 00127 for (r = rerrs; r->code != 0; r++) 00128 if (strcmp(r->name, preg->re_endp) == 0) 00129 break; 00130 if (r->code == 0) 00131 return("0"); 00132 00133 (void)snprintf(localbuf, localbufsize, "%d", r->code); 00134 return(localbuf); 00135 }