2 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
3 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/types.h>
45 #include "../gen/_libc_gettext.h"
47 static const char *regatoi(const regex_t
*preg
, char *localbuf
);
49 #define RERR(x, msg) { x, #x, msg }
56 RERR(REG_NOMATCH
, "regexec() failed to match"),
57 RERR(REG_BADPAT
, "invalid regular expression"),
58 RERR(REG_ECOLLATE
, "invalid collating element"),
59 RERR(REG_ECTYPE
, "invalid character class"),
60 RERR(REG_EESCAPE
, "trailing backslash (\\)"),
61 RERR(REG_ESUBREG
, "invalid backreference number"),
62 RERR(REG_EBRACK
, "brackets ([ ]) not balanced"),
63 RERR(REG_EPAREN
, "parentheses not balanced"),
64 RERR(REG_EBRACE
, "braces not balanced"),
65 RERR(REG_BADBR
, "invalid repetition count(s)"),
66 RERR(REG_ERANGE
, "invalid character range"),
67 RERR(REG_ESPACE
, "out of memory"),
68 RERR(REG_BADRPT
, "repetition-operator operand invalid"),
70 RERR(REG_EMPTY
, "empty (sub)expression"),
72 RERR(REG_EFATAL
, "fatal internal error"),
74 RERR(REG_INVARG
, "invalid argument to regex routine"),
76 RERR(REG_ECHAR
, "illegal byte sequence"),
77 RERR(REG_ENOSYS
, "function not supported"),
78 RERR(REG_STACK
, "backtrack stack overflow"),
79 RERR(REG_ENSUB
, "more than 9 \\( \\) pairs"),
80 RERR(REG_ENEWLINE
, "\n found before end of pattern"),
81 {0, "", "*** unknown regexp error code ***"}
86 * regerror - the interface to error numbers
90 regerror(int errcode
, const regex_t
*_RESTRICT_KYWD preg
,
91 char *_RESTRICT_KYWD errbuf
, size_t errbuf_size
)
95 int target
= errcode
&~ REG_ITOA
;
99 if (errcode
== REG_ATOI
) {
100 s
= regatoi(preg
, convbuf
);
102 for (r
= rerrs
; r
->code
!= 0; r
++) {
103 if (r
->code
== target
)
107 if (errcode
®_ITOA
) {
109 (void) strcpy(convbuf
, r
->name
);
111 (void) sprintf(convbuf
, "REG_0x%x", target
);
112 assert(strlen(convbuf
) < sizeof (convbuf
));
115 s
= _libc_gettext(r
->explain
);
120 if (errbuf_size
> 0) {
121 if (errbuf_size
> len
) {
122 (void) strcpy(errbuf
, s
);
124 (void) strncpy(errbuf
, s
, errbuf_size
-1);
125 errbuf
[errbuf_size
-1] = '\0';
133 * regatoi - internal routine to implement REG_ATOI
136 regatoi(const regex_t
*preg
, char *localbuf
)
140 for (r
= rerrs
; r
->code
!= 0; r
++) {
141 if (strcmp(r
->name
, preg
->re_endp
) == 0)
147 (void) sprintf(localbuf
, "%d", r
->code
);