xref: /freebsd/usr.bin/tr/str.c (revision f0adf7f5cdd241db2f2c817683191a6ef64a4e95)
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 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char sccsid[] = "@(#)str.c	8.2 (Berkeley) 4/28/95";
40 #endif
41 
42 #include <sys/cdefs.h>
43 #include <sys/types.h>
44 
45 #include <ctype.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <stddef.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <wchar.h>
53 #include <wctype.h>
54 
55 #include "extern.h"
56 
57 static int      backslash(STR *, int *);
58 static int	bracket(STR *);
59 static void	genclass(STR *);
60 static void	genequiv(STR *);
61 static int      genrange(STR *, int);
62 static void	genseq(STR *);
63 
64 wint_t
65 next(s)
66 	STR *s;
67 {
68 	int is_octal;
69 	wint_t ch;
70 	wchar_t wch;
71 	size_t clen;
72 
73 	switch (s->state) {
74 	case EOS:
75 		return (0);
76 	case INFINITE:
77 		return (1);
78 	case NORMAL:
79 		switch (*s->str) {
80 		case '\0':
81 			s->state = EOS;
82 			return (0);
83 		case '\\':
84 			s->lastch = backslash(s, &is_octal);
85 			break;
86 		case '[':
87 			if (bracket(s))
88 				return (next(s));
89 			/* FALLTHROUGH */
90 		default:
91 			clen = mbrtowc(&wch, s->str, MB_LEN_MAX, NULL);
92 			if (clen == (size_t)-1 || clen == (size_t)-2 ||
93 			    clen == 0)
94 				errc(1, EILSEQ, NULL);
95 			is_octal = 0;
96 			s->lastch = wch;
97 			s->str += clen;
98 			break;
99 		}
100 
101 		/* We can start a range at any time. */
102 		if (s->str[0] == '-' && genrange(s, is_octal))
103 			return (next(s));
104 		return (1);
105 	case RANGE:
106 		if (s->cnt-- == 0) {
107 			s->state = NORMAL;
108 			return (next(s));
109 		}
110 		++s->lastch;
111 		return (1);
112 	case SEQUENCE:
113 		if (s->cnt-- == 0) {
114 			s->state = NORMAL;
115 			return (next(s));
116 		}
117 		return (1);
118 	case CCLASS:
119 	case CCLASS_UPPER:
120 	case CCLASS_LOWER:
121 		s->cnt++;
122 		ch = nextwctype(s->lastch, s->cclass);
123 		if (ch == -1) {
124 			s->state = NORMAL;
125 			return (next(s));
126 		}
127 		s->lastch = ch;
128 		return (1);
129 	case SET:
130 		if ((ch = s->set[s->cnt++]) == OOBCH) {
131 			s->state = NORMAL;
132 			return (next(s));
133 		}
134 		s->lastch = ch;
135 		return (1);
136 	default:
137 		return (0);
138 	}
139 	/* NOTREACHED */
140 }
141 
142 static int
143 bracket(s)
144 	STR *s;
145 {
146 	char *p;
147 
148 	switch (s->str[1]) {
149 	case ':':				/* "[:class:]" */
150 		if ((p = strchr(s->str + 2, ']')) == NULL)
151 			return (0);
152 		if (*(p - 1) != ':' || p - s->str < 4)
153 			goto repeat;
154 		*(p - 1) = '\0';
155 		s->str += 2;
156 		genclass(s);
157 		s->str = p + 1;
158 		return (1);
159 	case '=':				/* "[=equiv=]" */
160 		if ((p = strchr(s->str + 2, ']')) == NULL)
161 			return (0);
162 		if (*(p - 1) != '=' || p - s->str < 4)
163 			goto repeat;
164 		s->str += 2;
165 		genequiv(s);
166 		return (1);
167 	default:				/* "[\###*n]" or "[#*n]" */
168 	repeat:
169 		if ((p = strpbrk(s->str + 2, "*]")) == NULL)
170 			return (0);
171 		if (p[0] != '*' || index(p, ']') == NULL)
172 			return (0);
173 		s->str += 1;
174 		genseq(s);
175 		return (1);
176 	}
177 	/* NOTREACHED */
178 }
179 
180 static void
181 genclass(s)
182 	STR *s;
183 {
184 
185 	if ((s->cclass = wctype(s->str)) == 0)
186 		errx(1, "unknown class %s", s->str);
187 	s->cnt = 0;
188 	s->lastch = -1;		/* incremented before check in next() */
189 	if (strcmp(s->str, "upper") == 0)
190 		s->state = CCLASS_UPPER;
191 	else if (strcmp(s->str, "lower") == 0)
192 		s->state = CCLASS_LOWER;
193 	else
194 		s->state = CCLASS;
195 }
196 
197 static void
198 genequiv(s)
199 	STR *s;
200 {
201 	int i, p, pri;
202 	char src[2], dst[3];
203 	size_t clen;
204 	wchar_t wc;
205 
206 	if (*s->str == '\\') {
207 		s->equiv[0] = backslash(s, NULL);
208 		if (*s->str != '=')
209 			errx(1, "misplaced equivalence equals sign");
210 		s->str += 2;
211 	} else {
212 		clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
213 		if (clen == (size_t)-1 || clen == (size_t)-2 || clen == 0)
214 			errc(1, EILSEQ, NULL);
215 		s->equiv[0] = wc;
216 		if (s->str[clen] != '=')
217 			errx(1, "misplaced equivalence equals sign");
218 		s->str += clen + 2;
219 	}
220 
221 	/*
222 	 * Calculate the set of all characters in the same equivalence class
223 	 * as the specified character (they will have the same primary
224 	 * collation weights).
225 	 * XXX Knows too much about how strxfrm() is implemented. Assumes
226 	 * it fills the string with primary collation weight bytes. Only one-
227 	 * to-one mappings are supported.
228 	 * XXX Equivalence classes not supported in multibyte locales.
229 	 */
230 	src[0] = (char)s->equiv[0];
231 	src[1] = '\0';
232 	if (MB_CUR_MAX == 1 && strxfrm(dst, src, sizeof(dst)) == 1) {
233 		pri = (unsigned char)*dst;
234 		for (p = 1, i = 1; i < NCHARS_SB; i++) {
235 			*src = i;
236 			if (strxfrm(dst, src, sizeof(dst)) == 1 && pri &&
237 			    pri == (unsigned char)*dst)
238 				s->equiv[p++] = i;
239 		}
240 		s->equiv[p] = OOBCH;
241 	}
242 
243 	s->cnt = 0;
244 	s->state = SET;
245 	s->set = s->equiv;
246 }
247 
248 static int
249 genrange(STR *s, int was_octal)
250 {
251 	int stopval, octal;
252 	char *savestart;
253 	int n, cnt, *p;
254 	size_t clen;
255 	wchar_t wc;
256 
257 	octal = 0;
258 	savestart = s->str;
259 	if (*++s->str == '\\')
260 		stopval = backslash(s, &octal);
261 	else {
262 		clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
263 		if (clen == (size_t)-1 || clen == (size_t)-2)
264 			errc(1, EILSEQ, NULL);
265 		stopval = wc;
266 		s->str += clen;
267 	}
268 	/*
269 	 * XXX Characters are not ordered according to collating sequence in
270 	 * multibyte locales.
271 	 */
272 	if (octal || was_octal || MB_CUR_MAX > 1) {
273 		if (stopval < s->lastch) {
274 			s->str = savestart;
275 			return (0);
276 		}
277 		s->cnt = stopval - s->lastch + 1;
278 		s->state = RANGE;
279 		--s->lastch;
280 		return (1);
281 	}
282 	if (charcoll((const void *)&stopval, (const void *)&(s->lastch)) < 0) {
283 		s->str = savestart;
284 		return (0);
285 	}
286 	if ((s->set = p = malloc((NCHARS_SB + 1) * sizeof(int))) == NULL)
287 		err(1, "genrange() malloc");
288 	for (cnt = 0; cnt < NCHARS_SB; cnt++)
289 		if (charcoll((const void *)&cnt, (const void *)&(s->lastch)) >= 0 &&
290 		    charcoll((const void *)&cnt, (const void *)&stopval) <= 0)
291 			*p++ = cnt;
292 	*p = OOBCH;
293 	n = p - s->set;
294 
295 	s->cnt = 0;
296 	s->state = SET;
297 	if (n > 1)
298 		mergesort(s->set, n, sizeof(*(s->set)), charcoll);
299 	return (1);
300 }
301 
302 static void
303 genseq(s)
304 	STR *s;
305 {
306 	char *ep;
307 	wchar_t wc;
308 	size_t clen;
309 
310 	if (s->which == STRING1)
311 		errx(1, "sequences only valid in string2");
312 
313 	if (*s->str == '\\')
314 		s->lastch = backslash(s, NULL);
315 	else {
316 		clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
317 		if (clen == (size_t)-1 || clen == (size_t)-2)
318 			errc(1, EILSEQ, NULL);
319 		s->lastch = wc;
320 		s->str += clen;
321 	}
322 	if (*s->str != '*')
323 		errx(1, "misplaced sequence asterisk");
324 
325 	switch (*++s->str) {
326 	case '\\':
327 		s->cnt = backslash(s, NULL);
328 		break;
329 	case ']':
330 		s->cnt = 0;
331 		++s->str;
332 		break;
333 	default:
334 		if (isdigit((u_char)*s->str)) {
335 			s->cnt = strtol(s->str, &ep, 0);
336 			if (*ep == ']') {
337 				s->str = ep + 1;
338 				break;
339 			}
340 		}
341 		errx(1, "illegal sequence count");
342 		/* NOTREACHED */
343 	}
344 
345 	s->state = s->cnt ? SEQUENCE : INFINITE;
346 }
347 
348 /*
349  * Translate \??? into a character.  Up to 3 octal digits, if no digits either
350  * an escape code or a literal character.
351  */
352 static int
353 backslash(STR *s, int *is_octal)
354 {
355 	int ch, cnt, val;
356 
357 	if (is_octal != NULL)
358 		*is_octal = 0;
359 	for (cnt = val = 0;;) {
360 		ch = (u_char)*++s->str;
361 		if (!isdigit(ch))
362 			break;
363 		val = val * 8 + ch - '0';
364 		if (++cnt == 3) {
365 			++s->str;
366 			break;
367 		}
368 	}
369 	if (cnt) {
370 		if (is_octal != NULL)
371 			*is_octal = 1;
372 		return (val);
373 	}
374 	if (ch != '\0')
375 		++s->str;
376 	switch (ch) {
377 		case 'a':			/* escape characters */
378 			return ('\7');
379 		case 'b':
380 			return ('\b');
381 		case 'f':
382 			return ('\f');
383 		case 'n':
384 			return ('\n');
385 		case 'r':
386 			return ('\r');
387 		case 't':
388 			return ('\t');
389 		case 'v':
390 			return ('\13');
391 		case '\0':			/*  \" -> \ */
392 			s->state = EOS;
393 			return ('\\');
394 		default:			/* \x" -> x */
395 			return (ch);
396 	}
397 }
398