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