regerror.c revision 1.6 1 1.6 christos /* $NetBSD: regerror.c,v 1.6 1997/07/13 20:05:37 christos Exp $ */
2 1.4 cgd
3 1.3 cgd /*-
4 1.3 cgd * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5 1.3 cgd * Copyright (c) 1992, 1993, 1994
6 1.3 cgd * The Regents of the University of California. All rights reserved.
7 1.3 cgd *
8 1.3 cgd * This code is derived from software contributed to Berkeley by
9 1.3 cgd * Henry Spencer.
10 1.3 cgd *
11 1.3 cgd * Redistribution and use in source and binary forms, with or without
12 1.3 cgd * modification, are permitted provided that the following conditions
13 1.3 cgd * are met:
14 1.3 cgd * 1. Redistributions of source code must retain the above copyright
15 1.3 cgd * notice, this list of conditions and the following disclaimer.
16 1.3 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.3 cgd * notice, this list of conditions and the following disclaimer in the
18 1.3 cgd * documentation and/or other materials provided with the distribution.
19 1.3 cgd * 3. All advertising materials mentioning features or use of this software
20 1.3 cgd * must display the following acknowledgement:
21 1.3 cgd * This product includes software developed by the University of
22 1.3 cgd * California, Berkeley and its contributors.
23 1.3 cgd * 4. Neither the name of the University nor the names of its contributors
24 1.3 cgd * may be used to endorse or promote products derived from this software
25 1.3 cgd * without specific prior written permission.
26 1.3 cgd *
27 1.3 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 1.3 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 1.3 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 1.3 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 1.3 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 1.3 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 1.3 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 1.3 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 1.3 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 1.3 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 1.3 cgd * SUCH DAMAGE.
38 1.3 cgd *
39 1.3 cgd * @(#)regerror.c 8.4 (Berkeley) 3/20/94
40 1.3 cgd */
41 1.3 cgd
42 1.6 christos #include <sys/cdefs.h>
43 1.3 cgd #if defined(LIBC_SCCS) && !defined(lint)
44 1.4 cgd #if 0
45 1.3 cgd static char sccsid[] = "@(#)regerror.c 8.4 (Berkeley) 3/20/94";
46 1.4 cgd #else
47 1.6 christos __RCSID("$NetBSD: regerror.c,v 1.6 1997/07/13 20:05:37 christos Exp $");
48 1.4 cgd #endif
49 1.3 cgd #endif /* LIBC_SCCS and not lint */
50 1.3 cgd
51 1.1 jtc #include <sys/types.h>
52 1.1 jtc #include <stdio.h>
53 1.1 jtc #include <string.h>
54 1.1 jtc #include <ctype.h>
55 1.1 jtc #include <limits.h>
56 1.1 jtc #include <stdlib.h>
57 1.1 jtc #include <regex.h>
58 1.1 jtc
59 1.1 jtc #include "utils.h"
60 1.1 jtc
61 1.3 cgd /* ========= begin header generated by ./mkh ========= */
62 1.3 cgd #ifdef __cplusplus
63 1.3 cgd extern "C" {
64 1.3 cgd #endif
65 1.3 cgd
66 1.3 cgd /* === regerror.c === */
67 1.5 mrg static char *regatoi __P((const regex_t *preg, char *localbuf, int buflen));
68 1.3 cgd
69 1.3 cgd #ifdef __cplusplus
70 1.3 cgd }
71 1.3 cgd #endif
72 1.3 cgd /* ========= end header generated by ./mkh ========= */
73 1.1 jtc /*
74 1.1 jtc = #define REG_NOMATCH 1
75 1.1 jtc = #define REG_BADPAT 2
76 1.1 jtc = #define REG_ECOLLATE 3
77 1.1 jtc = #define REG_ECTYPE 4
78 1.1 jtc = #define REG_EESCAPE 5
79 1.1 jtc = #define REG_ESUBREG 6
80 1.1 jtc = #define REG_EBRACK 7
81 1.1 jtc = #define REG_EPAREN 8
82 1.1 jtc = #define REG_EBRACE 9
83 1.1 jtc = #define REG_BADBR 10
84 1.1 jtc = #define REG_ERANGE 11
85 1.1 jtc = #define REG_ESPACE 12
86 1.1 jtc = #define REG_BADRPT 13
87 1.1 jtc = #define REG_EMPTY 14
88 1.1 jtc = #define REG_ASSERT 15
89 1.1 jtc = #define REG_INVARG 16
90 1.1 jtc = #define REG_ATOI 255 // convert name to number (!)
91 1.1 jtc = #define REG_ITOA 0400 // convert number to name (!)
92 1.1 jtc */
93 1.1 jtc static struct rerr {
94 1.1 jtc int code;
95 1.1 jtc char *name;
96 1.1 jtc char *explain;
97 1.1 jtc } rerrs[] = {
98 1.6 christos { REG_NOMATCH, "REG_NOMATCH", "regexec() failed to match" },
99 1.6 christos { REG_BADPAT, "REG_BADPAT", "invalid regular expression" },
100 1.6 christos { REG_ECOLLATE, "REG_ECOLLATE", "invalid collating element" },
101 1.6 christos { REG_ECTYPE, "REG_ECTYPE", "invalid character class" },
102 1.6 christos { REG_EESCAPE, "REG_EESCAPE", "trailing backslash (\\)" },
103 1.6 christos { REG_ESUBREG, "REG_ESUBREG", "invalid backreference number" },
104 1.6 christos { REG_EBRACK, "REG_EBRACK", "brackets ([ ]) not balanced" },
105 1.6 christos { REG_EPAREN, "REG_EPAREN", "parentheses not balanced" },
106 1.6 christos { REG_EBRACE, "REG_EBRACE", "braces not balanced" },
107 1.6 christos { REG_BADBR, "REG_BADBR", "invalid repetition count(s)" },
108 1.6 christos { REG_ERANGE, "REG_ERANGE", "invalid character range" },
109 1.6 christos { REG_ESPACE, "REG_ESPACE", "out of memory" },
110 1.6 christos { REG_BADRPT, "REG_BADRPT", "repetition-operator operand invalid" },
111 1.6 christos { REG_EMPTY, "REG_EMPTY", "empty (sub)expression" },
112 1.6 christos { REG_ASSERT, "REG_ASSERT", "\"can't happen\" -- you found a bug" },
113 1.6 christos { REG_INVARG, "REG_INVARG", "invalid argument to regex routine" },
114 1.6 christos { 0, "", "*** unknown regexp error code ***" }
115 1.1 jtc };
116 1.1 jtc
117 1.1 jtc /*
118 1.5 mrg * regerror - the interface to error numbers
119 1.5 mrg * extern size_t regerror(int, const regex_t *, char *, size_t);
120 1.1 jtc */
121 1.1 jtc /* ARGSUSED */
122 1.1 jtc size_t
123 1.1 jtc regerror(errcode, preg, errbuf, errbuf_size)
124 1.1 jtc int errcode;
125 1.1 jtc const regex_t *preg;
126 1.1 jtc char *errbuf;
127 1.1 jtc size_t errbuf_size;
128 1.1 jtc {
129 1.1 jtc register struct rerr *r;
130 1.1 jtc register size_t len;
131 1.1 jtc register int target = errcode &~ REG_ITOA;
132 1.1 jtc register char *s;
133 1.1 jtc char convbuf[50];
134 1.1 jtc
135 1.1 jtc if (errcode == REG_ATOI)
136 1.5 mrg s = regatoi(preg, convbuf, sizeof convbuf);
137 1.1 jtc else {
138 1.1 jtc for (r = rerrs; r->code != 0; r++)
139 1.1 jtc if (r->code == target)
140 1.1 jtc break;
141 1.1 jtc
142 1.1 jtc if (errcode®_ITOA) {
143 1.1 jtc if (r->code != 0)
144 1.5 mrg (void)strncpy(convbuf, r->name, sizeof convbuf);
145 1.1 jtc else
146 1.5 mrg (void)snprintf(convbuf, sizeof convbuf,
147 1.5 mrg "REG_0x%x", target);
148 1.1 jtc s = convbuf;
149 1.1 jtc } else
150 1.1 jtc s = r->explain;
151 1.1 jtc }
152 1.1 jtc
153 1.1 jtc len = strlen(s) + 1;
154 1.1 jtc if (errbuf_size > 0) {
155 1.5 mrg (void)strncpy(errbuf, s, errbuf_size - 1);
156 1.5 mrg errbuf[errbuf_size-1] = '\0';
157 1.1 jtc }
158 1.1 jtc
159 1.1 jtc return(len);
160 1.1 jtc }
161 1.1 jtc
162 1.1 jtc /*
163 1.5 mrg * regatoi - internal routine to implement REG_ATOI
164 1.5 mrg * static char *regatoi(const regex_t *preg, char *localbuf, int buflen);
165 1.1 jtc */
166 1.1 jtc static char *
167 1.5 mrg regatoi(preg, localbuf, buflen)
168 1.1 jtc const regex_t *preg;
169 1.1 jtc char *localbuf;
170 1.5 mrg int buflen;
171 1.1 jtc {
172 1.1 jtc register struct rerr *r;
173 1.1 jtc
174 1.1 jtc for (r = rerrs; r->code != 0; r++)
175 1.1 jtc if (strcmp(r->name, preg->re_endp) == 0)
176 1.1 jtc break;
177 1.1 jtc if (r->code == 0)
178 1.1 jtc return("0");
179 1.1 jtc
180 1.5 mrg (void)snprintf(localbuf, buflen, "%d", r->code);
181 1.1 jtc return(localbuf);
182 1.1 jtc }
183