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
33 * @(#)str.c 8.2 (Berkeley) 4/28/95
34 * $FreeBSD: src/usr.bin/tr/str.c,v 1.10.2.2 2002/07/29 12:59:33 tjr Exp $
37 #include <sys/types.h>
48 static int backslash(STR *);
49 static int bracket(STR *);
50 static int c_class(const void *, const void *);
51 static void genclass(STR *);
52 static void genequiv(STR *);
53 static int genrange(STR *);
54 static void genseq(STR *);
67 switch (ch = (u_char)*s->str) {
72 s->lastch = backslash(s);
84 /* We can start a range at any time. */
85 if (s->str[0] == '-' && genrange(s))
102 if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
119 case ':': /* "[:class:]" */
120 if ((p = strchr(s->str + 2, ']')) == NULL)
122 if (*(p - 1) != ':' || p - s->str < 4)
129 case '=': /* "[=equiv=]" */
130 if ((p = strchr(s->str + 2, ']')) == NULL)
132 if (*(p - 1) != '=' || p - s->str < 4)
137 default: /* "[\###*n]" or "[#*n]" */
139 if ((p = strpbrk(s->str + 2, "*]")) == NULL)
141 if (p[0] != '*' || strchr(p, ']') == NULL)
156 static CLASS classes[] = {
158 { "alnum", isalnum, NULL },
160 { "alpha", isalpha, NULL },
162 { "blank", isblank, NULL },
164 { "cntrl", iscntrl, NULL },
166 { "digit", isdigit, NULL },
168 { "graph", isgraph, NULL },
170 { "lower", islower, NULL },
172 { "print", isprint, NULL },
174 { "punct", ispunct, NULL },
176 { "space", isspace, NULL },
178 { "upper", isupper, NULL },
180 { "xdigit", isxdigit, NULL },
186 int cnt, (*func)(int);
191 if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
192 sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
193 errx(1, "unknown class %s", s->str);
195 if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
197 bzero(p, (NCHARS + 1) * sizeof(int));
198 for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
209 c_class(const void *a, const void *b)
211 return (strcmp(((const CLASS *)a)->name, ((const CLASS *)b)->name));
220 if (*s->str == '\\') {
221 s->equiv[0] = backslash(s);
223 errx(1, "misplaced equivalence equals sign");
226 s->equiv[0] = s->str[0];
227 if (s->str[1] != '=')
228 errx(1, "misplaced equivalence equals sign");
233 * Calculate the set of all characters in the same equivalence class
234 * as the specified character (they will have the same primary
235 * collation weights).
236 * XXX Knows too much about how strxfrm() is implemented. Assumes
237 * it fills the string with primary collation weight bytes. Only one-
238 * to-one mappings are supported.
240 src[0] = s->equiv[0];
242 if (strxfrm(dst, src, sizeof(dst)) == 1) {
243 pri = (unsigned char)*dst;
244 for (p = 1, i = 1; i < NCHARS; i++) {
246 if (strxfrm(dst, src, sizeof(dst)) == 1 && pri &&
247 pri == (unsigned char)*dst)
265 stopval = *++s->str == '\\' ? backslash(s) : (u_char)*s->str++;
266 if (stopval < (u_char)s->lastch) {
270 s->cnt = stopval - s->lastch + 1;
281 if (s->which == STRING1)
282 errx(1, "sequences only valid in string2");
285 s->lastch = backslash(s);
287 s->lastch = *s->str++;
289 errx(1, "misplaced sequence asterisk");
293 s->cnt = backslash(s);
300 if (isdigit((u_char)*s->str)) {
301 s->cnt = strtol(s->str, &ep, 0);
307 errx(1, "illegal sequence count");
311 s->state = s->cnt ? SEQUENCE : INFINITE;
315 * Translate \??? into a character. Up to 3 octal digits, if no digits either
316 * an escape code or a literal character.
323 for (cnt = val = 0;;) {
324 ch = (u_char)*++s->str;
325 if (!isascii(ch) || !isdigit(ch))
327 val = val * 8 + ch - '0';
338 case 'a': /* escape characters */
352 case '\0': /* \" -> \ */
355 default: /* \x" -> x */