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 $
35 * $DragonFly: src/usr.bin/tr/str.c,v 1.4 2004/08/30 18:06:50 eirikn Exp $
38 #include <sys/cdefs.h>
39 #include <sys/types.h>
50 static int backslash(STR
*);
51 static int bracket(STR
*);
52 static int c_class(const void *, const void *);
53 static void genclass(STR
*);
54 static void genequiv(STR
*);
55 static int genrange(STR
*);
56 static void genseq(STR
*);
69 switch (ch
= (u_char
)*s
->str
) {
74 s
->lastch
= backslash(s
);
86 /* We can start a range at any time. */
87 if (s
->str
[0] == '-' && genrange(s
))
104 if ((s
->lastch
= s
->set
[s
->cnt
++]) == OOBCH
) {
121 case ':': /* "[:class:]" */
122 if ((p
= strchr(s
->str
+ 2, ']')) == NULL
)
124 if (*(p
- 1) != ':' || p
- s
->str
< 4)
131 case '=': /* "[=equiv=]" */
132 if ((p
= strchr(s
->str
+ 2, ']')) == NULL
)
134 if (*(p
- 1) != '=' || p
- s
->str
< 4)
139 default: /* "[\###*n]" or "[#*n]" */
141 if ((p
= strpbrk(s
->str
+ 2, "*]")) == NULL
)
143 if (p
[0] != '*' || strchr(p
, ']') == NULL
)
158 static CLASS classes
[] = {
160 { "alnum", isalnum
, NULL
},
162 { "alpha", isalpha
, NULL
},
164 { "blank", isblank
, NULL
},
166 { "cntrl", iscntrl
, NULL
},
168 { "digit", isdigit
, NULL
},
170 { "graph", isgraph
, NULL
},
172 { "lower", islower
, NULL
},
174 { "print", isprint
, NULL
},
176 { "punct", ispunct
, NULL
},
178 { "space", isspace
, NULL
},
180 { "upper", isupper
, NULL
},
182 { "xdigit", isxdigit
, NULL
},
188 int cnt
, (*func
)(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
)
211 c_class(const void *a
, const void *b
)
213 return (strcmp(((const CLASS
*)a
)->name
, ((const CLASS
*)b
)->name
));
222 if (*s
->str
== '\\') {
223 s
->equiv
[0] = backslash(s
);
225 errx(1, "misplaced equivalence equals sign");
228 s
->equiv
[0] = s
->str
[0];
229 if (s
->str
[1] != '=')
230 errx(1, "misplaced equivalence equals sign");
235 * Calculate the set of all characters in the same equivalence class
236 * as the specified character (they will have the same primary
237 * collation weights).
238 * XXX Knows too much about how strxfrm() is implemented. Assumes
239 * it fills the string with primary collation weight bytes. Only one-
240 * to-one mappings are supported.
242 src
[0] = s
->equiv
[0];
244 if (strxfrm(dst
, src
, sizeof(dst
)) == 1) {
245 pri
= (unsigned char)*dst
;
246 for (p
= 1, i
= 1; i
< NCHARS
; i
++) {
248 if (strxfrm(dst
, src
, sizeof(dst
)) == 1 && pri
&&
249 pri
== (unsigned char)*dst
)
267 stopval
= *++s
->str
== '\\' ? backslash(s
) : (u_char
)*s
->str
++;
268 if (stopval
< (u_char
)s
->lastch
) {
272 s
->cnt
= stopval
- s
->lastch
+ 1;
283 if (s
->which
== STRING1
)
284 errx(1, "sequences only valid in string2");
287 s
->lastch
= backslash(s
);
289 s
->lastch
= *s
->str
++;
291 errx(1, "misplaced sequence asterisk");
295 s
->cnt
= backslash(s
);
302 if (isdigit((u_char
)*s
->str
)) {
303 s
->cnt
= strtol(s
->str
, &ep
, 0);
309 errx(1, "illegal sequence count");
313 s
->state
= s
->cnt
? SEQUENCE
: INFINITE
;
317 * Translate \??? into a character. Up to 3 octal digits, if no digits either
318 * an escape code or a literal character.
325 for (cnt
= val
= 0;;) {
326 ch
= (u_char
)*++s
->str
;
327 if (!isascii(ch
) || !isdigit(ch
))
329 val
= val
* 8 + ch
- '0';
340 case 'a': /* escape characters */
354 case '\0': /* \" -> \ */
357 default: /* \x" -> x */