1 /* $NetBSD: str.c,v 1.12 2009/04/13 23:50:49 lukem 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/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
*);
64 switch (ch
= *s
->str
) {
69 s
->lastch
= backslash(s
);
81 /* We can start a range at any time. */
82 if (s
->str
[0] == '-' && genrange(s
))
99 if ((s
->lastch
= s
->set
[s
->cnt
++]) == OOBCH
) {
116 case ':': /* "[:class:]" */
117 if ((p
= strstr((char *) s
->str
+ 2, ":]")) == NULL
)
122 s
->str
= (unsigned char *) p
+ 2;
124 case '=': /* "[=equiv=]" */
125 if ((p
= strstr((char *) s
->str
+ 2, "=]")) == NULL
)
130 default: /* "[\###*n]" or "[#*n]" */
131 if ((p
= strpbrk((char *) s
->str
+ 2, "*]")) == NULL
)
133 if (p
[0] != '*' || strchr(p
, ']') == NULL
)
148 static CLASS classes
[] = {
149 { "alnum", isalnum
, NULL
, },
150 { "alpha", isalpha
, NULL
, },
151 { "blank", isblank
, NULL
, },
152 { "cntrl", iscntrl
, NULL
, },
153 { "digit", isdigit
, NULL
, },
154 { "graph", isgraph
, NULL
, },
155 { "lower", islower
, NULL
, },
156 { "print", isprint
, NULL
, },
157 { "punct", ispunct
, NULL
, },
158 { "space", isspace
, NULL
, },
159 { "upper", isupper
, NULL
, },
160 { "xdigit", isxdigit
, NULL
, },
167 int cnt
, (*func
) (int);
171 tmp
.name
= (char *) s
->str
;
172 if ((cp
= (CLASS
*)bsearch(&tmp
, classes
, sizeof(classes
) /
173 sizeof(CLASS
), sizeof(CLASS
), c_class
)) == NULL
) {
174 fprintf(stderr
, "tr: unknown class %s\n", s
->str
);
178 if ((cp
->set
= p
= malloc((NCHARS
+ 1) * sizeof(int))) == NULL
) {
182 memset(p
, 0, (NCHARS
+ 1) * sizeof(int));
183 for (cnt
= 0, func
= cp
->func
; cnt
< NCHARS
; ++cnt
)
197 return (strcmp(((const CLASS
*)a
)->name
, ((const CLASS
*)b
)->name
));
201 * English doesn't have any equivalence classes, so for now
202 * we just syntax check and grab the character.
208 if (*s
->str
== '\\') {
209 s
->equiv
[0] = backslash(s
);
210 if (*s
->str
!= '=') {
211 fprintf(stderr
, "tr: misplaced equivalence equals sign\n");
215 s
->equiv
[0] = s
->str
[0];
216 if (s
->str
[1] != '=') {
217 fprintf(stderr
, "tr: misplaced equivalence equals sign\n");
232 unsigned char *savestart
;
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 fprintf(stderr
, "tr: sequences only valid in string2\n");
258 s
->lastch
= backslash(s
);
260 s
->lastch
= *s
->str
++;
261 if (*s
->str
!= '*') {
262 fprintf(stderr
, "tr: misplaced sequence asterisk\n");
268 s
->cnt
= backslash(s
);
275 if (isdigit(*s
->str
)) {
276 s
->cnt
= strtol((char *) s
->str
, &ep
, 0);
278 s
->str
= (unsigned char *) ep
+ 1;
282 fprintf(stderr
, "tr: illegal sequence count\n");
287 s
->state
= s
->cnt
? SEQUENCE
: INFINITE
;
291 * Translate \??? into a character. Up to 3 octal digits, if no digits either
292 * an escape code or a literal character.
300 for (cnt
= val
= 0;;) {
302 if (!isascii(ch
) || !isdigit(ch
))
304 val
= val
* 8 + ch
- '0';
315 case 'a': /* escape characters */
329 case '\0': /* \" -> \ */
332 default: /* \x" -> x */