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