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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)str.c 8.2 (Berkeley) 4/28/95
30 * $FreeBSD: src/usr.bin/tr/str.c,v 1.10.2.2 2002/07/29 12:59:33 tjr Exp $
33 #include <sys/types.h>
44 static int backslash(STR *);
45 static int bracket(STR *);
46 static int c_class(const void *, const void *);
47 static void genclass(STR *);
48 static void genequiv(STR *);
49 static int genrange(STR *);
50 static void genseq(STR *);
63 switch (ch = (u_char)*s->str) {
68 s->lastch = backslash(s);
80 /* We can start a range at any time. */
81 if (s->str[0] == '-' && genrange(s))
98 if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
115 case ':': /* "[:class:]" */
116 if ((p = strchr(s->str + 2, ']')) == NULL)
118 if (*(p - 1) != ':' || p - s->str < 4)
125 case '=': /* "[=equiv=]" */
126 if ((p = strchr(s->str + 2, ']')) == NULL)
128 if (*(p - 1) != '=' || p - s->str < 4)
133 default: /* "[\###*n]" or "[#*n]" */
135 if ((p = strpbrk(s->str + 2, "*]")) == NULL)
137 if (p[0] != '*' || strchr(p, ']') == NULL)
152 static CLASS classes[] = {
154 { "alnum", isalnum, NULL },
156 { "alpha", isalpha, NULL },
158 { "blank", isblank, NULL },
160 { "cntrl", iscntrl, NULL },
162 { "digit", isdigit, NULL },
164 { "graph", isgraph, NULL },
166 { "lower", islower, NULL },
168 { "print", isprint, NULL },
170 { "punct", ispunct, NULL },
172 { "space", isspace, NULL },
174 { "upper", isupper, NULL },
176 { "xdigit", isxdigit, NULL },
182 int cnt, (*func)(int);
187 if ((cp = (CLASS *)bsearch(&tmp, classes, sizeof(classes) /
188 sizeof(CLASS), sizeof(CLASS), c_class)) == NULL)
189 errx(1, "unknown class %s", s->str);
191 if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
193 bzero(p, (NCHARS + 1) * sizeof(int));
194 for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
205 c_class(const void *a, const void *b)
207 return (strcmp(((const CLASS *)a)->name, ((const CLASS *)b)->name));
216 if (*s->str == '\\') {
217 s->equiv[0] = backslash(s);
219 errx(1, "misplaced equivalence equals sign");
222 s->equiv[0] = s->str[0];
223 if (s->str[1] != '=')
224 errx(1, "misplaced equivalence equals sign");
229 * Calculate the set of all characters in the same equivalence class
230 * as the specified character (they will have the same primary
231 * collation weights).
232 * XXX Knows too much about how strxfrm() is implemented. Assumes
233 * it fills the string with primary collation weight bytes. Only one-
234 * to-one mappings are supported.
236 src[0] = s->equiv[0];
238 if (strxfrm(dst, src, sizeof(dst)) == 1) {
239 pri = (unsigned char)*dst;
240 for (p = 1, i = 1; i < NCHARS; i++) {
242 if (strxfrm(dst, src, sizeof(dst)) == 1 && pri &&
243 pri == (unsigned char)*dst)
261 stopval = *++s->str == '\\' ? backslash(s) : (u_char)*s->str++;
262 if (stopval < (u_char)s->lastch) {
266 s->cnt = stopval - s->lastch + 1;
277 if (s->which == STRING1)
278 errx(1, "sequences only valid in string2");
281 s->lastch = backslash(s);
283 s->lastch = *s->str++;
285 errx(1, "misplaced sequence asterisk");
289 s->cnt = backslash(s);
296 if (isdigit((u_char)*s->str)) {
297 s->cnt = strtol(s->str, &ep, 0);
303 errx(1, "illegal sequence count");
307 s->state = s->cnt ? SEQUENCE : INFINITE;
311 * Translate \??? into a character. Up to 3 octal digits, if no digits either
312 * an escape code or a literal character.
319 for (cnt = val = 0;;) {
320 ch = (u_char)*++s->str;
321 if (!isascii(ch) || !isdigit(ch))
323 val = val * 8 + ch - '0';
334 case 'a': /* escape characters */
348 case '\0': /* \" -> \ */
351 default: /* \x" -> x */