2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid[] = "@(#)str.c 8.2 (Berkeley) 4/28/95";
38 static const char rcsid[] =
42 #include <sys/cdefs.h>
43 #include <sys/types.h>
54 static int backslash __P((STR *));
55 static int bracket __P((STR *));
56 static int c_class __P((const void *, const void *));
57 static void genclass __P((STR *));
58 static void genequiv __P((STR *));
59 static int genrange __P((STR *));
60 static void genseq __P((STR *));
74 switch (ch = (u_char)*s->str) {
79 s->lastch = backslash(s);
91 /* We can start a range at any time. */
92 if (s->str[0] == '-' && genrange(s))
109 if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
125 case ':': /* "[:class:]" */
126 if ((p = strstr(s->str + 2, ":]")) == NULL)
133 case '=': /* "[=equiv=]" */
134 if ((p = strstr(s->str + 2, "=]")) == NULL)
139 default: /* "[\###*n]" or "[#*n]" */
140 if ((p = strpbrk(s->str + 2, "*]")) == NULL)
142 if (p[0] != '*' || index(p, ']') == NULL)
153 int (*func) __P((int));
157 static CLASS classes[] = {
159 { "alnum", isalnum, },
161 { "alpha", isalpha, },
163 { "blank", isblank, },
165 { "cntrl", iscntrl, },
167 { "digit", isdigit, },
169 { "graph", isgraph, },
171 { "lower", islower, },
173 { "print", isprint, },
175 { "punct", ispunct, },
177 { "space", isspace, },
179 { "upper", isupper, },
181 { "xdigit", isxdigit, },
188 register int cnt, (*func) __P((int));
193 if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
194 sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
195 errx(1, "unknown class %s", s->str);
197 if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
199 bzero(p, (NCHARS + 1) * sizeof(int));
200 for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
214 return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name));
218 * English doesn't have any equivalence classes, so for now
219 * we just syntax check and grab the character.
225 if (*s->str == '\\') {
226 s->equiv[0] = backslash(s);
228 errx(1, "misplaced equivalence equals sign");
230 s->equiv[0] = s->str[0];
231 if (s->str[1] != '=')
232 errx(1, "misplaced equivalence equals sign");
248 stopval = *++s->str == '\\' ? backslash(s) : (u_char)*s->str++;
249 if (stopval < (u_char)s->lastch) {
253 s->cnt = stopval - s->lastch + 1;
265 if (s->which == STRING1)
266 errx(1, "sequences only valid in string2");
269 s->lastch = backslash(s);
271 s->lastch = *s->str++;
273 errx(1, "misplaced sequence asterisk");
277 s->cnt = backslash(s);
284 if (isdigit((u_char)*s->str)) {
285 s->cnt = strtol(s->str, &ep, 0);
291 errx(1, "illegal sequence count");
295 s->state = s->cnt ? SEQUENCE : INFINITE;
299 * Translate \??? into a character. Up to 3 octal digits, if no digits either
300 * an escape code or a literal character.
306 register int ch, cnt, val;
308 for (cnt = val = 0;;) {
309 ch = (u_char)*++s->str;
310 if (!isascii(ch) || !isdigit(ch))
312 val = val * 8 + ch - '0';
323 case 'a': /* escape characters */
337 case '\0': /* \" -> \ */
340 default: /* \x" -> x */