1 /*********************************************************************** 2 * * 3 * This software is part of the ast package * 4 * Copyright (c) 1985-2007 AT&T Knowledge Ventures * 5 * and is licensed under the * 6 * Common Public License, Version 1.0 * 7 * by AT&T Knowledge Ventures * 8 * * 9 * A copy of the License is available at * 10 * http://www.opensource.org/licenses/cpl1.0.txt * 11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) * 12 * * 13 * Information and Software Systems Research * 14 * AT&T Research * 15 * Florham Park NJ * 16 * * 17 * Glenn Fowler <gsf@research.att.com> * 18 * David Korn <dgk@research.att.com> * 19 * Phong Vo <kpv@research.att.com> * 20 * * 21 ***********************************************************************/ 22 #pragma prototyped 23 24 /* 25 * posix regex error message handler 26 */ 27 28 static const char id[] = "\n@(#)$Id: regex (AT&T Research) 2004-05-24 $\0\n"; 29 30 #include "reglib.h" 31 32 static const char* reg_error[] = 33 { 34 /* REG_ENOSYS */ "not supported", 35 /* REG_SUCCESS */ "success", 36 /* REG_NOMATCH */ "no match", 37 /* REG_BADPAT */ "invalid regular expression", 38 /* REG_ECOLLATE */ "invalid collation element", 39 /* REG_ECTYPE */ "invalid character class", 40 /* REG_EESCAPE */ "trailing \\ in pattern", 41 /* REG_ESUBREG */ "invalid \\digit backreference", 42 /* REG_EBRACK */ "[...] imbalance", 43 /* REG_EPAREN */ "\\(...\\) or (...) imbalance", 44 /* REG_EBRACE */ "\\{...\\} or {...} imbalance", 45 /* REG_BADBR */ "invalid {...} digits", 46 /* REG_ERANGE */ "invalid [...] range endpoint", 47 /* REG_ESPACE */ "out of space", 48 /* REG_BADRPT */ "unary op not preceeded by re", 49 /* REG_ENULL */ "empty subexpr in pattern", 50 /* REG_ECOUNT */ "re component count overflow", 51 /* REG_BADESC */ "invalid \\char escape", 52 /* REG_VERSIONID*/ &id[10], 53 /* REG_EFLAGS */ "conflicting flags", 54 /* REG_EDELIM */ "invalid or omitted delimiter", 55 /* REG_PANIC */ "unrecoverable internal error", 56 }; 57 58 size_t 59 regerror(int code, const regex_t* p, char* buf, size_t size) 60 { 61 const char* s; 62 63 NoP(p); 64 if (code++ == REG_VERSIONID) 65 s = (const char*)fmtident(&id[1]); 66 else if (code >= 0 && code < elementsof(reg_error)) 67 s = reg_error[code]; 68 else 69 s = (const char*)"unknown error"; 70 if (size) 71 { 72 strncpy(buf, s, size); 73 buf[size - 1] = 0; 74 } 75 else 76 buf = (char*)s; 77 return strlen(buf) + 1; 78 } 79 80 /* 81 * discipline error intercept 82 */ 83 84 int 85 fatal(regdisc_t* disc, int code, const char* pattern) 86 { 87 if (disc->re_errorf) 88 { 89 if (pattern) 90 (*disc->re_errorf)(NiL, disc, disc->re_errorlevel, "regular expression: %s: %s", pattern, reg_error[code+1]); 91 else 92 (*disc->re_errorf)(NiL, disc, disc->re_errorlevel, "regular expression: %s", reg_error[code+1]); 93 } 94 return code; 95 } 96