regerror.c revision 170528
1193323Sed/*- 2193323Sed * Copyright (c) 1992, 1993, 1994 Henry Spencer. 3193323Sed * Copyright (c) 1992, 1993, 1994 4193323Sed * The Regents of the University of California. All rights reserved. 5193323Sed * 6193323Sed * This code is derived from software contributed to Berkeley by 7193323Sed * Henry Spencer. 8193323Sed * 9193323Sed * Redistribution and use in source and binary forms, with or without 10193323Sed * modification, are permitted provided that the following conditions 11193323Sed * are met: 12193323Sed * 1. Redistributions of source code must retain the above copyright 13193323Sed * notice, this list of conditions and the following disclaimer. 14193323Sed * 2. Redistributions in binary form must reproduce the above copyright 15193323Sed * notice, this list of conditions and the following disclaimer in the 16193323Sed * documentation and/or other materials provided with the distribution. 17193323Sed * 4. Neither the name of the University nor the names of its contributors 18224145Sdim * may be used to endorse or promote products derived from this software 19198090Srdivacky * without specific prior written permission. 20193323Sed * 21193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24193323Sed * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25198090Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26263763Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27263763Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28263763Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31193323Sed * SUCH DAMAGE. 32263508Sdim * 33263508Sdim * @(#)regerror.c 8.4 (Berkeley) 3/20/94 34198090Srdivacky */ 35263763Sdim 36263763Sdim#if defined(LIBC_SCCS) && !defined(lint) 37263763Sdimstatic char sccsid[] = "@(#)regerror.c 8.4 (Berkeley) 3/20/94"; 38193323Sed#endif /* LIBC_SCCS and not lint */ 39193323Sed#include <sys/cdefs.h> 40193323Sed__FBSDID("$FreeBSD: head/lib/libc/regex/regerror.c 170528 2007-06-11 03:05:54Z delphij $"); 41193323Sed 42193323Sed#include <sys/types.h> 43193323Sed#include <stdio.h> 44193323Sed#include <string.h> 45193323Sed#include <limits.h> 46193323Sed#include <stdlib.h> 47193323Sed#include <regex.h> 48193323Sed 49193323Sed#include "utils.h" 50193323Sed 51193323Sed/* ========= begin header generated by ./mkh ========= */ 52193323Sed#ifdef __cplusplus 53193323Sedextern "C" { 54193323Sed#endif 55193323Sed 56193323Sed/* === regerror.c === */ 57193323Sedstatic char *regatoi(const regex_t *preg, char *localbuf); 58193323Sed 59193323Sed#ifdef __cplusplus 60193323Sed} 61263508Sdim#endif 62193323Sed/* ========= end header generated by ./mkh ========= */ 63193323Sed/* 64193323Sed = #define REG_NOMATCH 1 65193323Sed = #define REG_BADPAT 2 66193323Sed = #define REG_ECOLLATE 3 67193323Sed = #define REG_ECTYPE 4 68193323Sed = #define REG_EESCAPE 5 69193323Sed = #define REG_ESUBREG 6 70193323Sed = #define REG_EBRACK 7 71193323Sed = #define REG_EPAREN 8 72193323Sed = #define REG_EBRACE 9 73193323Sed = #define REG_BADBR 10 74193323Sed = #define REG_ERANGE 11 75193323Sed = #define REG_ESPACE 12 76193323Sed = #define REG_BADRPT 13 77193323Sed = #define REG_EMPTY 14 78193323Sed = #define REG_ASSERT 15 79193323Sed = #define REG_INVARG 16 80263508Sdim = #define REG_ILLSEQ 17 81193323Sed = #define REG_ATOI 255 // convert name to number (!) 82193323Sed = #define REG_ITOA 0400 // convert number to name (!) 83193323Sed */ 84193323Sedstatic struct rerr { 85193323Sed int code; 86193323Sed char *name; 87193323Sed char *explain; 88193323Sed} rerrs[] = { 89193323Sed {REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match"}, 90193323Sed {REG_BADPAT, "REG_BADPAT", "invalid regular expression"}, 91193323Sed {REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element"}, 92193323Sed {REG_ECTYPE, "REG_ECTYPE", "invalid character class"}, 93193323Sed {REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)"}, 94193323Sed {REG_ESUBREG, "REG_ESUBREG", "invalid backreference number"}, 95193323Sed {REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced"}, 96193323Sed {REG_EPAREN, "REG_EPAREN", "parentheses not balanced"}, 97193323Sed {REG_EBRACE, "REG_EBRACE", "braces not balanced"}, 98193323Sed {REG_BADBR, "REG_BADBR", "invalid repetition count(s)"}, 99193323Sed {REG_ERANGE, "REG_ERANGE", "invalid character range"}, 100193323Sed {REG_ESPACE, "REG_ESPACE", "out of memory"}, 101193323Sed {REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid"}, 102193323Sed {REG_EMPTY, "REG_EMPTY", "empty (sub)expression"}, 103193323Sed {REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug"}, 104193323Sed {REG_INVARG, "REG_INVARG", "invalid argument to regex routine"}, 105193323Sed {REG_ILLSEQ, "REG_ILLSEQ", "illegal byte sequence"}, 106193323Sed {0, "", "*** unknown regexp error code ***"} 107193323Sed}; 108193323Sed 109193323Sed/* 110193323Sed - regerror - the interface to error numbers 111234353Sdim = extern size_t regerror(int, const regex_t *, char *, size_t); 112234353Sdim */ 113193323Sed/* ARGSUSED */ 114263508Sdimsize_t 115263508Sdimregerror(int errcode, 116263508Sdim const regex_t * __restrict preg, 117263508Sdim char * __restrict errbuf, 118263508Sdim size_t errbuf_size) 119263508Sdim{ 120263508Sdim struct rerr *r; 121263508Sdim size_t len; 122263508Sdim int target = errcode &~ REG_ITOA; 123263508Sdim char *s; 124263508Sdim char convbuf[50]; 125263508Sdim 126263508Sdim if (errcode == REG_ATOI) 127263508Sdim s = regatoi(preg, convbuf); 128263508Sdim else { 129263508Sdim for (r = rerrs; r->code != 0; r++) 130263508Sdim if (r->code == target) 131193323Sed break; 132193323Sed 133 if (errcode®_ITOA) { 134 if (r->code != 0) 135 (void) strcpy(convbuf, r->name); 136 else 137 sprintf(convbuf, "REG_0x%x", target); 138 assert(strlen(convbuf) < sizeof(convbuf)); 139 s = convbuf; 140 } else 141 s = r->explain; 142 } 143 144 len = strlen(s) + 1; 145 if (errbuf_size > 0) { 146 if (errbuf_size > len) 147 (void) strcpy(errbuf, s); 148 else { 149 (void) strncpy(errbuf, s, errbuf_size-1); 150 errbuf[errbuf_size-1] = '\0'; 151 } 152 } 153 154 return(len); 155} 156 157/* 158 - regatoi - internal routine to implement REG_ATOI 159 == static char *regatoi(const regex_t *preg, char *localbuf); 160 */ 161static char * 162regatoi(const regex_t *preg, char *localbuf) 163{ 164 struct rerr *r; 165 166 for (r = rerrs; r->code != 0; r++) 167 if (strcmp(r->name, preg->re_endp) == 0) 168 break; 169 if (r->code == 0) 170 return("0"); 171 172 sprintf(localbuf, "%d", r->code); 173 return(localbuf); 174} 175