1 /* $NetBSD: str.c,v 1.11 2007/01/17 00:21:44 hubertf Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
37 __RCSID("$NetBSD: str.c,v 1.11 2007/01/17 00:21:44 hubertf Exp $");
40 #include <sys/types.h>
52 static int backslash
__P((STR
*));
53 static int bracket
__P((STR
*));
54 static int c_class
__P((const void *, const void *));
55 static void genclass
__P((STR
*));
56 static void genequiv
__P((STR
*));
57 static int genrange
__P((STR
*));
58 static void genseq
__P((STR
*));
72 switch (ch
= *s
->str
) {
77 s
->lastch
= backslash(s
);
89 /* We can start a range at any time. */
90 if (s
->str
[0] == '-' && genrange(s
))
107 if ((s
->lastch
= s
->set
[s
->cnt
++]) == OOBCH
) {
124 case ':': /* "[:class:]" */
125 if ((p
= strstr(s
->str
+ 2, ":]")) == NULL
)
132 case '=': /* "[=equiv=]" */
133 if ((p
= strstr(s
->str
+ 2, "=]")) == NULL
)
138 default: /* "[\###*n]" or "[#*n]" */
139 if ((p
= strpbrk(s
->str
+ 2, "*]")) == NULL
)
141 if (p
[0] != '*' || strchr(p
, ']') == NULL
)
152 int (*func
) __P((int));
156 static CLASS classes
[] = {
157 { "alnum", isalnum
, NULL
, },
158 { "alpha", isalpha
, NULL
, },
159 { "blank", isblank
, NULL
, },
160 { "cntrl", iscntrl
, NULL
, },
161 { "digit", isdigit
, NULL
, },
162 { "graph", isgraph
, NULL
, },
163 { "lower", islower
, NULL
, },
164 { "print", isprint
, NULL
, },
165 { "punct", ispunct
, NULL
, },
166 { "space", isspace
, NULL
, },
167 { "upper", isupper
, NULL
, },
168 { "xdigit", isxdigit
, NULL
, },
175 int cnt
, (*func
) __P((int));
180 if ((cp
= (CLASS
*)bsearch(&tmp
, classes
, sizeof(classes
) /
181 sizeof(CLASS
), sizeof(CLASS
), c_class
)) == NULL
)
182 errx(1, "unknown class %s", s
->str
);
184 if ((cp
->set
= p
= malloc((NCHARS
+ 1) * sizeof(int))) == NULL
)
186 memset(p
, 0, (NCHARS
+ 1) * sizeof(int));
187 for (cnt
= 0, func
= cp
->func
; cnt
< NCHARS
; ++cnt
)
201 return (strcmp(((const CLASS
*)a
)->name
, ((const CLASS
*)b
)->name
));
205 * English doesn't have any equivalence classes, so for now
206 * we just syntax check and grab the character.
212 if (*s
->str
== '\\') {
213 s
->equiv
[0] = backslash(s
);
215 errx(1, "misplaced equivalence equals sign");
217 s
->equiv
[0] = s
->str
[0];
218 if (s
->str
[1] != '=')
219 errx(1, "misplaced equivalence equals sign");
235 stopval
= *++s
->str
== '\\' ? backslash(s
) : *s
->str
++;
236 if (stopval
< (u_char
)s
->lastch
) {
240 s
->cnt
= stopval
- s
->lastch
+ 1;
252 if (s
->which
== STRING1
)
253 errx(1, "sequences only valid in string2");
256 s
->lastch
= backslash(s
);
258 s
->lastch
= *s
->str
++;
260 errx(1, "misplaced sequence asterisk");
264 s
->cnt
= backslash(s
);
271 if (isdigit(*s
->str
)) {
272 s
->cnt
= strtol(s
->str
, &ep
, 0);
278 errx(1, "illegal sequence count");
282 s
->state
= s
->cnt
? SEQUENCE
: INFINITE
;
286 * Translate \??? into a character. Up to 3 octal digits, if no digits either
287 * an escape code or a literal character.
295 for (cnt
= val
= 0;;) {
297 if (!isascii(ch
) || !isdigit(ch
))
299 val
= val
* 8 + ch
- '0';
310 case 'a': /* escape characters */
324 case '\0': /* \" -> \ */
327 default: /* \x" -> x */