xref: /freebsd/usr.bin/tr/str.c (revision af647767ed8f2ec38251e8185ef0b6adb35529e6)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
20  *
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
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)str.c	8.2 (Berkeley) 4/28/95";
37 #endif
38 static const char rcsid[] =
39 	"$Id$";
40 #endif /* not lint */
41 
42 #include <sys/cdefs.h>
43 #include <sys/types.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "extern.h"
53 
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 *));
61 
62 int
63 next(s)
64 	register STR *s;
65 {
66 	register int ch;
67 
68 	switch (s->state) {
69 	case EOS:
70 		return (0);
71 	case INFINITE:
72 		return (1);
73 	case NORMAL:
74 		switch (ch = (u_char)*s->str) {
75 		case '\0':
76 			s->state = EOS;
77 			return (0);
78 		case '\\':
79 			s->lastch = backslash(s);
80 			break;
81 		case '[':
82 			if (bracket(s))
83 				return (next(s));
84 			/* FALLTHROUGH */
85 		default:
86 			++s->str;
87 			s->lastch = ch;
88 			break;
89 		}
90 
91 		/* We can start a range at any time. */
92 		if (s->str[0] == '-' && genrange(s))
93 			return (next(s));
94 		return (1);
95 	case RANGE:
96 		if (s->cnt-- == 0) {
97 			s->state = NORMAL;
98 			return (next(s));
99 		}
100 		++s->lastch;
101 		return (1);
102 	case SEQUENCE:
103 		if (s->cnt-- == 0) {
104 			s->state = NORMAL;
105 			return (next(s));
106 		}
107 		return (1);
108 	case SET:
109 		if ((s->lastch = s->set[s->cnt++]) == OOBCH) {
110 			s->state = NORMAL;
111 			return (next(s));
112 		}
113 		return (1);
114 	}
115 	/* NOTREACHED */
116 }
117 
118 static int
119 bracket(s)
120 	register STR *s;
121 {
122 	register char *p;
123 
124 	switch (s->str[1]) {
125 	case ':':				/* "[:class:]" */
126 		if ((p = strstr(s->str + 2, ":]")) == NULL)
127 			return (0);
128 		*p = '\0';
129 		s->str += 2;
130 		genclass(s);
131 		s->str = p + 2;
132 		return (1);
133 	case '=':				/* "[=equiv=]" */
134 		if ((p = strstr(s->str + 2, "=]")) == NULL)
135 			return (0);
136 		s->str += 2;
137 		genequiv(s);
138 		return (1);
139 	default:				/* "[\###*n]" or "[#*n]" */
140 		if ((p = strpbrk(s->str + 2, "*]")) == NULL)
141 			return (0);
142 		if (p[0] != '*' || index(p, ']') == NULL)
143 			return (0);
144 		s->str += 1;
145 		genseq(s);
146 		return (1);
147 	}
148 	/* NOTREACHED */
149 }
150 
151 typedef struct {
152 	char *name;
153 	int (*func) __P((int));
154 	int *set;
155 } CLASS;
156 
157 static CLASS classes[] = {
158 #undef isalnum
159 	{ "alnum",  isalnum,  },
160 #undef isalpha
161 	{ "alpha",  isalpha,  },
162 #undef isblank
163 	{ "blank",  isblank,  },
164 #undef iscntrl
165 	{ "cntrl",  iscntrl,  },
166 #undef isdigit
167 	{ "digit",  isdigit,  },
168 #undef isgraph
169 	{ "graph",  isgraph,  },
170 #undef islower
171 	{ "lower",  islower,  },
172 #undef isprint
173 	{ "print",  isprint,  },
174 #undef ispunct
175 	{ "punct",  ispunct,  },
176 #undef isspace
177 	{ "space",  isspace,  },
178 #undef isupper
179 	{ "upper",  isupper,  },
180 #undef isxdigit
181 	{ "xdigit", isxdigit, },
182 };
183 
184 static void
185 genclass(s)
186 	STR *s;
187 {
188 	register int cnt, (*func) __P((int));
189 	CLASS *cp, tmp;
190 	int *p;
191 
192 	tmp.name = s->str;
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);
196 
197 	if ((cp->set = p = malloc((NCHARS + 1) * sizeof(int))) == NULL)
198 		errx(1, "malloc");
199 	bzero(p, (NCHARS + 1) * sizeof(int));
200 	for (cnt = 0, func = cp->func; cnt < NCHARS; ++cnt)
201 		if ((func)(cnt))
202 			*p++ = cnt;
203 	*p = OOBCH;
204 
205 	s->cnt = 0;
206 	s->state = SET;
207 	s->set = cp->set;
208 }
209 
210 static int
211 c_class(a, b)
212 	const void *a, *b;
213 {
214 	return (strcmp(((CLASS *)a)->name, ((CLASS *)b)->name));
215 }
216 
217 /*
218  * English doesn't have any equivalence classes, so for now
219  * we just syntax check and grab the character.
220  */
221 static void
222 genequiv(s)
223 	STR *s;
224 {
225 	if (*s->str == '\\') {
226 		s->equiv[0] = backslash(s);
227 		if (*s->str != '=')
228 			errx(1, "misplaced equivalence equals sign");
229 	} else {
230 		s->equiv[0] = s->str[0];
231 		if (s->str[1] != '=')
232 			errx(1, "misplaced equivalence equals sign");
233 	}
234 	s->str += 2;
235 	s->cnt = 0;
236 	s->state = SET;
237 	s->set = s->equiv;
238 }
239 
240 static int
241 genrange(s)
242 	STR *s;
243 {
244 	int stopval;
245 	char *savestart;
246 
247 	savestart = s->str;
248 	stopval = *++s->str == '\\' ? backslash(s) : (u_char)*s->str++;
249 	if (stopval < (u_char)s->lastch) {
250 		s->str = savestart;
251 		return (0);
252 	}
253 	s->cnt = stopval - s->lastch + 1;
254 	s->state = RANGE;
255 	--s->lastch;
256 	return (1);
257 }
258 
259 static void
260 genseq(s)
261 	STR *s;
262 {
263 	char *ep;
264 
265 	if (s->which == STRING1)
266 		errx(1, "sequences only valid in string2");
267 
268 	if (*s->str == '\\')
269 		s->lastch = backslash(s);
270 	else
271 		s->lastch = *s->str++;
272 	if (*s->str != '*')
273 		errx(1, "misplaced sequence asterisk");
274 
275 	switch (*++s->str) {
276 	case '\\':
277 		s->cnt = backslash(s);
278 		break;
279 	case ']':
280 		s->cnt = 0;
281 		++s->str;
282 		break;
283 	default:
284 		if (isdigit((u_char)*s->str)) {
285 			s->cnt = strtol(s->str, &ep, 0);
286 			if (*ep == ']') {
287 				s->str = ep + 1;
288 				break;
289 			}
290 		}
291 		errx(1, "illegal sequence count");
292 		/* NOTREACHED */
293 	}
294 
295 	s->state = s->cnt ? SEQUENCE : INFINITE;
296 }
297 
298 /*
299  * Translate \??? into a character.  Up to 3 octal digits, if no digits either
300  * an escape code or a literal character.
301  */
302 static int
303 backslash(s)
304 	register STR *s;
305 {
306 	register int ch, cnt, val;
307 
308 	for (cnt = val = 0;;) {
309 		ch = (u_char)*++s->str;
310 		if (!isascii(ch) || !isdigit(ch))
311 			break;
312 		val = val * 8 + ch - '0';
313 		if (++cnt == 3) {
314 			++s->str;
315 			break;
316 		}
317 	}
318 	if (cnt)
319 		return (val);
320 	if (ch != '\0')
321 		++s->str;
322 	switch (ch) {
323 		case 'a':			/* escape characters */
324 			return ('\7');
325 		case 'b':
326 			return ('\b');
327 		case 'f':
328 			return ('\f');
329 		case 'n':
330 			return ('\n');
331 		case 'r':
332 			return ('\r');
333 		case 't':
334 			return ('\t');
335 		case 'v':
336 			return ('\13');
337 		case '\0':			/*  \" -> \ */
338 			s->state = EOS;
339 			return ('\\');
340 		default:			/* \x" -> x */
341 			return (ch);
342 	}
343 }
344