xref: /illumos-gate/usr/src/cmd/tr/str.c (revision 8fb237d8)
1163bd69bSGarrett D'Amore /*
2163bd69bSGarrett D'Amore  * Copyright (c) 1991, 1993
3163bd69bSGarrett D'Amore  *	The Regents of the University of California.  All rights reserved.
4163bd69bSGarrett D'Amore  *
5163bd69bSGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
6163bd69bSGarrett D'Amore  * modification, are permitted provided that the following conditions
7163bd69bSGarrett D'Amore  * are met:
8163bd69bSGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
9163bd69bSGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
10163bd69bSGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
11163bd69bSGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
12163bd69bSGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
13163bd69bSGarrett D'Amore  * 3. All advertising materials mentioning features or use of this software
14163bd69bSGarrett D'Amore  *    must display the following acknowledgement:
15163bd69bSGarrett D'Amore  *	This product includes software developed by the University of
16163bd69bSGarrett D'Amore  *	California, Berkeley and its contributors.
17163bd69bSGarrett D'Amore  * 4. Neither the name of the University nor the names of its contributors
18163bd69bSGarrett D'Amore  *    may be used to endorse or promote products derived from this software
19163bd69bSGarrett D'Amore  *    without specific prior written permission.
20163bd69bSGarrett D'Amore  *
21163bd69bSGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22163bd69bSGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23163bd69bSGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24163bd69bSGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25163bd69bSGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26163bd69bSGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27163bd69bSGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28163bd69bSGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29163bd69bSGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30163bd69bSGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31163bd69bSGarrett D'Amore  * SUCH DAMAGE.
32163bd69bSGarrett D'Amore  */
33163bd69bSGarrett D'Amore 
34163bd69bSGarrett D'Amore #include <sys/types.h>
35163bd69bSGarrett D'Amore 
36163bd69bSGarrett D'Amore #include <ctype.h>
37163bd69bSGarrett D'Amore #include <err.h>
38163bd69bSGarrett D'Amore #include <errno.h>
39163bd69bSGarrett D'Amore #include <stddef.h>
40163bd69bSGarrett D'Amore #include <stdio.h>
41163bd69bSGarrett D'Amore #include <stdlib.h>
42163bd69bSGarrett D'Amore #include <string.h>
43163bd69bSGarrett D'Amore #include <strings.h>
44163bd69bSGarrett D'Amore #include <wchar.h>
45163bd69bSGarrett D'Amore #include <wctype.h>
46163bd69bSGarrett D'Amore 
47163bd69bSGarrett D'Amore #include "extern.h"
48163bd69bSGarrett D'Amore 
49163bd69bSGarrett D'Amore static int	backslash(STR *, int *);
50163bd69bSGarrett D'Amore static int	bracket(STR *);
51163bd69bSGarrett D'Amore static void	genclass(STR *);
52163bd69bSGarrett D'Amore static void	genequiv(STR *);
53163bd69bSGarrett D'Amore static int	genrange(STR *, int);
54163bd69bSGarrett D'Amore static void	genseq(STR *);
55163bd69bSGarrett D'Amore 
56163bd69bSGarrett D'Amore wint_t
next(s)57163bd69bSGarrett D'Amore next(s)
58163bd69bSGarrett D'Amore 	STR *s;
59163bd69bSGarrett D'Amore {
60163bd69bSGarrett D'Amore 	int is_octal;
61163bd69bSGarrett D'Amore 	wint_t ch;
62163bd69bSGarrett D'Amore 	wchar_t wch;
63163bd69bSGarrett D'Amore 	size_t clen;
64163bd69bSGarrett D'Amore 
65163bd69bSGarrett D'Amore 	switch (s->state) {
66163bd69bSGarrett D'Amore 	case EOS:
67163bd69bSGarrett D'Amore 		return (0);
68163bd69bSGarrett D'Amore 	case INFINITE:
69163bd69bSGarrett D'Amore 		return (1);
70163bd69bSGarrett D'Amore 	case NORMAL:
71163bd69bSGarrett D'Amore 		switch (*s->str) {
72163bd69bSGarrett D'Amore 		case '\0':
73163bd69bSGarrett D'Amore 			s->state = EOS;
74163bd69bSGarrett D'Amore 			return (0);
75163bd69bSGarrett D'Amore 		case '\\':
76163bd69bSGarrett D'Amore 			s->lastch = backslash(s, &is_octal);
77163bd69bSGarrett D'Amore 			break;
78163bd69bSGarrett D'Amore 		case '[':
79163bd69bSGarrett D'Amore 			if (bracket(s))
80163bd69bSGarrett D'Amore 				return (next(s));
81163bd69bSGarrett D'Amore 			/* FALLTHROUGH */
82163bd69bSGarrett D'Amore 		default:
83163bd69bSGarrett D'Amore 			clen = mbrtowc(&wch, s->str, MB_LEN_MAX, NULL);
84163bd69bSGarrett D'Amore 			if (clen == (size_t)-1 || clen == (size_t)-2 ||
85*8fb237d8SJoshua M. Clulow 			    clen == 0)
86*8fb237d8SJoshua M. Clulow 				errx(1, "illegal sequence");
87163bd69bSGarrett D'Amore 			is_octal = 0;
88163bd69bSGarrett D'Amore 			s->lastch = wch;
89163bd69bSGarrett D'Amore 			s->str += clen;
90163bd69bSGarrett D'Amore 			break;
91163bd69bSGarrett D'Amore 		}
92163bd69bSGarrett D'Amore 
93163bd69bSGarrett D'Amore 		/* We can start a range at any time. */
94163bd69bSGarrett D'Amore 		if (s->str[0] == '-' && genrange(s, is_octal))
95163bd69bSGarrett D'Amore 			return (next(s));
96163bd69bSGarrett D'Amore 		return (1);
97163bd69bSGarrett D'Amore 	case RANGE:
98163bd69bSGarrett D'Amore 		if (s->cnt-- == 0) {
99163bd69bSGarrett D'Amore 			s->state = NORMAL;
100163bd69bSGarrett D'Amore 			return (next(s));
101163bd69bSGarrett D'Amore 		}
102163bd69bSGarrett D'Amore 		++s->lastch;
103163bd69bSGarrett D'Amore 		return (1);
104163bd69bSGarrett D'Amore 	case SEQUENCE:
105163bd69bSGarrett D'Amore 		if (s->cnt-- == 0) {
106163bd69bSGarrett D'Amore 			s->state = NORMAL;
107163bd69bSGarrett D'Amore 			return (next(s));
108163bd69bSGarrett D'Amore 		}
109163bd69bSGarrett D'Amore 		return (1);
110163bd69bSGarrett D'Amore 	case CCLASS:
111163bd69bSGarrett D'Amore 	case CCLASS_UPPER:
112163bd69bSGarrett D'Amore 	case CCLASS_LOWER:
113163bd69bSGarrett D'Amore 		s->cnt++;
114163bd69bSGarrett D'Amore 		ch = nextwctype(s->lastch, s->cclass);
115163bd69bSGarrett D'Amore 		if (ch == -1) {
116163bd69bSGarrett D'Amore 			s->state = NORMAL;
117163bd69bSGarrett D'Amore 			return (next(s));
118163bd69bSGarrett D'Amore 		}
119163bd69bSGarrett D'Amore 		s->lastch = ch;
120163bd69bSGarrett D'Amore 		return (1);
121163bd69bSGarrett D'Amore 	case SET:
122163bd69bSGarrett D'Amore 		if ((ch = s->set[s->cnt++]) == OOBCH) {
123163bd69bSGarrett D'Amore 			s->state = NORMAL;
124163bd69bSGarrett D'Amore 			return (next(s));
125163bd69bSGarrett D'Amore 		}
126163bd69bSGarrett D'Amore 		s->lastch = ch;
127163bd69bSGarrett D'Amore 		return (1);
128163bd69bSGarrett D'Amore 	default:
129163bd69bSGarrett D'Amore 		return (0);
130163bd69bSGarrett D'Amore 	}
131163bd69bSGarrett D'Amore 	/* NOTREACHED */
132163bd69bSGarrett D'Amore }
133163bd69bSGarrett D'Amore 
134163bd69bSGarrett D'Amore static int
bracket(s)135163bd69bSGarrett D'Amore bracket(s)
136163bd69bSGarrett D'Amore 	STR *s;
137163bd69bSGarrett D'Amore {
138163bd69bSGarrett D'Amore 	char *p;
139163bd69bSGarrett D'Amore 
140163bd69bSGarrett D'Amore 	switch (s->str[1]) {
141163bd69bSGarrett D'Amore 	case ':':				/* "[:class:]" */
142163bd69bSGarrett D'Amore 		if ((p = strchr(s->str + 2, ']')) == NULL)
143163bd69bSGarrett D'Amore 			return (0);
144163bd69bSGarrett D'Amore 		if (*(p - 1) != ':' || p - s->str < 4)
145163bd69bSGarrett D'Amore 			goto repeat;
146163bd69bSGarrett D'Amore 		*(p - 1) = '\0';
147163bd69bSGarrett D'Amore 		s->str += 2;
148163bd69bSGarrett D'Amore 		genclass(s);
149163bd69bSGarrett D'Amore 		s->str = p + 1;
150163bd69bSGarrett D'Amore 		return (1);
151163bd69bSGarrett D'Amore 	case '=':				/* "[=equiv=]" */
15284cf253fSRichard Lowe 		if ((p = strchr(s->str + 3, ']')) == NULL)
153163bd69bSGarrett D'Amore 			return (0);
154163bd69bSGarrett D'Amore 		if (*(p - 1) != '=' || p - s->str < 4)
155163bd69bSGarrett D'Amore 			goto repeat;
156163bd69bSGarrett D'Amore 		s->str += 2;
157163bd69bSGarrett D'Amore 		genequiv(s);
158163bd69bSGarrett D'Amore 		return (1);
159163bd69bSGarrett D'Amore 	default:				/* "[\###*n]" or "[#*n]" */
160163bd69bSGarrett D'Amore 	repeat:
161163bd69bSGarrett D'Amore 		if ((p = strpbrk(s->str + 2, "*]")) == NULL)
162163bd69bSGarrett D'Amore 			return (0);
163163bd69bSGarrett D'Amore 		if (p[0] != '*' || index(p, ']') == NULL)
164163bd69bSGarrett D'Amore 			return (0);
165163bd69bSGarrett D'Amore 		s->str += 1;
166163bd69bSGarrett D'Amore 		genseq(s);
167163bd69bSGarrett D'Amore 		return (1);
168163bd69bSGarrett D'Amore 	}
169163bd69bSGarrett D'Amore 	/* NOTREACHED */
170163bd69bSGarrett D'Amore }
171163bd69bSGarrett D'Amore 
172163bd69bSGarrett D'Amore static void
genclass(s)173163bd69bSGarrett D'Amore genclass(s)
174163bd69bSGarrett D'Amore 	STR *s;
175163bd69bSGarrett D'Amore {
176163bd69bSGarrett D'Amore 
177163bd69bSGarrett D'Amore 	if ((s->cclass = wctype(s->str)) == 0)
178163bd69bSGarrett D'Amore 		errx(1, "unknown class %s", s->str);
179163bd69bSGarrett D'Amore 	s->cnt = 0;
180163bd69bSGarrett D'Amore 	s->lastch = -1;		/* incremented before check in next() */
181163bd69bSGarrett D'Amore 	if (strcmp(s->str, "upper") == 0)
182163bd69bSGarrett D'Amore 		s->state = CCLASS_UPPER;
183163bd69bSGarrett D'Amore 	else if (strcmp(s->str, "lower") == 0)
184163bd69bSGarrett D'Amore 		s->state = CCLASS_LOWER;
185163bd69bSGarrett D'Amore 	else
186163bd69bSGarrett D'Amore 		s->state = CCLASS;
187163bd69bSGarrett D'Amore }
188163bd69bSGarrett D'Amore 
189163bd69bSGarrett D'Amore static void
genequiv(s)190163bd69bSGarrett D'Amore genequiv(s)
191163bd69bSGarrett D'Amore 	STR *s;
192163bd69bSGarrett D'Amore {
193163bd69bSGarrett D'Amore 	int i, p, pri;
194163bd69bSGarrett D'Amore 	char src[2], dst[3];
195163bd69bSGarrett D'Amore 	size_t clen;
196163bd69bSGarrett D'Amore 	wchar_t wc;
197163bd69bSGarrett D'Amore 
198163bd69bSGarrett D'Amore 	if (*s->str == '\\') {
199163bd69bSGarrett D'Amore 		s->equiv[0] = backslash(s, NULL);
200163bd69bSGarrett D'Amore 		if (*s->str != '=')
201163bd69bSGarrett D'Amore 			errx(1, "misplaced equivalence equals sign");
202163bd69bSGarrett D'Amore 		s->str += 2;
203163bd69bSGarrett D'Amore 	} else {
204163bd69bSGarrett D'Amore 		clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
205163bd69bSGarrett D'Amore 		if (clen == (size_t)-1 || clen == (size_t)-2 || clen == 0) {
206163bd69bSGarrett D'Amore 			errno = EILSEQ;
207163bd69bSGarrett D'Amore 			err(1, NULL);
208163bd69bSGarrett D'Amore 		}
209163bd69bSGarrett D'Amore 		s->equiv[0] = wc;
210163bd69bSGarrett D'Amore 		if (s->str[clen] != '=')
211163bd69bSGarrett D'Amore 			errx(1, "misplaced equivalence equals sign");
212163bd69bSGarrett D'Amore 		s->str += clen + 2;
213163bd69bSGarrett D'Amore 	}
214163bd69bSGarrett D'Amore 
215163bd69bSGarrett D'Amore 	/*
216163bd69bSGarrett D'Amore 	 * Calculate the set of all characters in the same equivalence class
217163bd69bSGarrett D'Amore 	 * as the specified character (they will have the same primary
218163bd69bSGarrett D'Amore 	 * collation weights).
219163bd69bSGarrett D'Amore 	 * XXX Knows too much about how strxfrm() is implemented. Assumes
220163bd69bSGarrett D'Amore 	 * it fills the string with primary collation weight bytes. Only one-
221163bd69bSGarrett D'Amore 	 * to-one mappings are supported.
222163bd69bSGarrett D'Amore 	 * XXX Equivalence classes not supported in multibyte locales.
223163bd69bSGarrett D'Amore 	 */
224163bd69bSGarrett D'Amore 	src[0] = (char)s->equiv[0];
225163bd69bSGarrett D'Amore 	src[1] = '\0';
226163bd69bSGarrett D'Amore 	if (MB_CUR_MAX == 1 && strxfrm(dst, src, sizeof (dst)) == 1) {
227163bd69bSGarrett D'Amore 		pri = (unsigned char)*dst;
228163bd69bSGarrett D'Amore 		for (p = 1, i = 1; i < NCHARS_SB; i++) {
229163bd69bSGarrett D'Amore 			*src = i;
230163bd69bSGarrett D'Amore 			if (strxfrm(dst, src, sizeof (dst)) == 1 && pri &&
231163bd69bSGarrett D'Amore 			    pri == (unsigned char)*dst)
232163bd69bSGarrett D'Amore 				s->equiv[p++] = i;
233163bd69bSGarrett D'Amore 		}
234163bd69bSGarrett D'Amore 		s->equiv[p] = OOBCH;
235163bd69bSGarrett D'Amore 	}
236163bd69bSGarrett D'Amore 
237163bd69bSGarrett D'Amore 	s->cnt = 0;
238163bd69bSGarrett D'Amore 	s->state = SET;
239163bd69bSGarrett D'Amore 	s->set = s->equiv;
240163bd69bSGarrett D'Amore }
241163bd69bSGarrett D'Amore 
242163bd69bSGarrett D'Amore static int
genrange(STR * s,int was_octal)243163bd69bSGarrett D'Amore genrange(STR *s, int was_octal)
244163bd69bSGarrett D'Amore {
245163bd69bSGarrett D'Amore 	int stopval, octal;
246163bd69bSGarrett D'Amore 	char *savestart;
247163bd69bSGarrett D'Amore 	int n, cnt, *p;
248163bd69bSGarrett D'Amore 	size_t clen;
249163bd69bSGarrett D'Amore 	wchar_t wc;
250163bd69bSGarrett D'Amore 
251163bd69bSGarrett D'Amore 	octal = 0;
252163bd69bSGarrett D'Amore 	savestart = s->str;
253163bd69bSGarrett D'Amore 	if (*++s->str == '\\')
254163bd69bSGarrett D'Amore 		stopval = backslash(s, &octal);
255163bd69bSGarrett D'Amore 	else {
256163bd69bSGarrett D'Amore 		clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
257163bd69bSGarrett D'Amore 		if (clen == (size_t)-1 || clen == (size_t)-2) {
258163bd69bSGarrett D'Amore 			errno = EILSEQ;
259163bd69bSGarrett D'Amore 			err(1, NULL);
260163bd69bSGarrett D'Amore 		}
261163bd69bSGarrett D'Amore 		stopval = wc;
262163bd69bSGarrett D'Amore 		s->str += clen;
263163bd69bSGarrett D'Amore 	}
264163bd69bSGarrett D'Amore 	/*
265163bd69bSGarrett D'Amore 	 * XXX Characters are not ordered according to collating sequence in
266163bd69bSGarrett D'Amore 	 * multibyte locales.
267163bd69bSGarrett D'Amore 	 */
268163bd69bSGarrett D'Amore 	if (octal || was_octal || MB_CUR_MAX > 1) {
269163bd69bSGarrett D'Amore 		if (stopval < s->lastch) {
270163bd69bSGarrett D'Amore 			s->str = savestart;
271163bd69bSGarrett D'Amore 			return (0);
272163bd69bSGarrett D'Amore 		}
273163bd69bSGarrett D'Amore 		s->cnt = stopval - s->lastch + 1;
274163bd69bSGarrett D'Amore 		s->state = RANGE;
275163bd69bSGarrett D'Amore 		--s->lastch;
276163bd69bSGarrett D'Amore 		return (1);
277163bd69bSGarrett D'Amore 	}
278163bd69bSGarrett D'Amore 	if (charcoll((const void *)&stopval, (const void *)&(s->lastch)) < 0) {
279163bd69bSGarrett D'Amore 		s->str = savestart;
280163bd69bSGarrett D'Amore 		return (0);
281163bd69bSGarrett D'Amore 	}
282163bd69bSGarrett D'Amore 	p = malloc((NCHARS_SB + 1) * sizeof (int));
283f5b8ba47SGarrett D'Amore 	if ((s->set = (void *)p) == NULL)
284163bd69bSGarrett D'Amore 		err(1, "genrange() malloc");
285163bd69bSGarrett D'Amore 	for (cnt = 0; cnt < NCHARS_SB; cnt++)
286163bd69bSGarrett D'Amore 		if (charcoll((const void *)&cnt, (const void *)&(s->lastch)) >=
287163bd69bSGarrett D'Amore 		    0 &&
288163bd69bSGarrett D'Amore 		    charcoll((const void *)&cnt, (const void *)&stopval) <= 0)
289163bd69bSGarrett D'Amore 			*p++ = cnt;
290163bd69bSGarrett D'Amore 	*p = OOBCH;
291163bd69bSGarrett D'Amore 	n = (int *)p - (int *)s->set;
292163bd69bSGarrett D'Amore 
293163bd69bSGarrett D'Amore 	s->cnt = 0;
294163bd69bSGarrett D'Amore 	s->state = SET;
295163bd69bSGarrett D'Amore 	if (n > 1)
296163bd69bSGarrett D'Amore 		qsort(s->set, n, sizeof (*(s->set)), charcoll);
297163bd69bSGarrett D'Amore 	return (1);
298163bd69bSGarrett D'Amore }
299163bd69bSGarrett D'Amore 
300163bd69bSGarrett D'Amore static void
genseq(s)301163bd69bSGarrett D'Amore genseq(s)
302163bd69bSGarrett D'Amore 	STR *s;
303163bd69bSGarrett D'Amore {
304163bd69bSGarrett D'Amore 	char *ep;
305163bd69bSGarrett D'Amore 	wchar_t wc;
306163bd69bSGarrett D'Amore 	size_t clen;
307163bd69bSGarrett D'Amore 
308163bd69bSGarrett D'Amore 	if (s->which == STRING1)
309163bd69bSGarrett D'Amore 		errx(1, "sequences only valid in string2");
310163bd69bSGarrett D'Amore 
311163bd69bSGarrett D'Amore 	if (*s->str == '\\')
312163bd69bSGarrett D'Amore 		s->lastch = backslash(s, NULL);
313163bd69bSGarrett D'Amore 	else {
314163bd69bSGarrett D'Amore 		clen = mbrtowc(&wc, s->str, MB_LEN_MAX, NULL);
315163bd69bSGarrett D'Amore 		if (clen == (size_t)-1 || clen == (size_t)-2) {
316163bd69bSGarrett D'Amore 			errno = EILSEQ;
317163bd69bSGarrett D'Amore 			err(1, NULL);
318163bd69bSGarrett D'Amore 		}
319163bd69bSGarrett D'Amore 		s->lastch = wc;
320163bd69bSGarrett D'Amore 		s->str += clen;
321163bd69bSGarrett D'Amore 	}
322163bd69bSGarrett D'Amore 	if (*s->str != '*')
323163bd69bSGarrett D'Amore 		errx(1, "misplaced sequence asterisk");
324163bd69bSGarrett D'Amore 
325163bd69bSGarrett D'Amore 	switch (*++s->str) {
326163bd69bSGarrett D'Amore 	case '\\':
327163bd69bSGarrett D'Amore 		s->cnt = backslash(s, NULL);
328163bd69bSGarrett D'Amore 		break;
329163bd69bSGarrett D'Amore 	case ']':
330163bd69bSGarrett D'Amore 		s->cnt = 0;
331163bd69bSGarrett D'Amore 		++s->str;
332163bd69bSGarrett D'Amore 		break;
333163bd69bSGarrett D'Amore 	default:
334163bd69bSGarrett D'Amore 		if (isdigit((uchar_t)*s->str)) {
335163bd69bSGarrett D'Amore 			s->cnt = strtol(s->str, &ep, 0);
336163bd69bSGarrett D'Amore 			if (*ep == ']') {
337163bd69bSGarrett D'Amore 				s->str = ep + 1;
338163bd69bSGarrett D'Amore 				break;
339163bd69bSGarrett D'Amore 			}
340163bd69bSGarrett D'Amore 		}
341163bd69bSGarrett D'Amore 		errx(1, "illegal sequence count");
342163bd69bSGarrett D'Amore 		/* NOTREACHED */
343163bd69bSGarrett D'Amore 	}
344163bd69bSGarrett D'Amore 
345163bd69bSGarrett D'Amore 	s->state = s->cnt ? SEQUENCE : INFINITE;
346163bd69bSGarrett D'Amore }
347163bd69bSGarrett D'Amore 
348163bd69bSGarrett D'Amore /*
349163bd69bSGarrett D'Amore  * Translate \??? into a character.  Up to 3 octal digits, if no digits either
350163bd69bSGarrett D'Amore  * an escape code or a literal character.
351163bd69bSGarrett D'Amore  */
352163bd69bSGarrett D'Amore static int
backslash(STR * s,int * is_octal)353163bd69bSGarrett D'Amore backslash(STR *s, int *is_octal)
354163bd69bSGarrett D'Amore {
355163bd69bSGarrett D'Amore 	int ch, cnt, val;
356163bd69bSGarrett D'Amore 
357163bd69bSGarrett D'Amore 	if (is_octal != NULL)
358163bd69bSGarrett D'Amore 		*is_octal = 0;
359163bd69bSGarrett D'Amore 	for (cnt = val = 0; ; ) {
360163bd69bSGarrett D'Amore 		ch = (uchar_t)*++s->str;
361163bd69bSGarrett D'Amore 		if (!isdigit(ch) || ch > '7')
362163bd69bSGarrett D'Amore 			break;
363163bd69bSGarrett D'Amore 		val = val * 8 + ch - '0';
364163bd69bSGarrett D'Amore 		if (++cnt == 3) {
365163bd69bSGarrett D'Amore 			++s->str;
366163bd69bSGarrett D'Amore 			break;
367163bd69bSGarrett D'Amore 		}
368163bd69bSGarrett D'Amore 	}
369163bd69bSGarrett D'Amore 	if (cnt) {
370163bd69bSGarrett D'Amore 		if (is_octal != NULL)
371163bd69bSGarrett D'Amore 			*is_octal = 1;
372163bd69bSGarrett D'Amore 		return (val);
373163bd69bSGarrett D'Amore 	}
374163bd69bSGarrett D'Amore 	if (ch != '\0')
375163bd69bSGarrett D'Amore 		++s->str;
376163bd69bSGarrett D'Amore 	switch (ch) {
377163bd69bSGarrett D'Amore 	case 'a':			/* escape characters */
378163bd69bSGarrett D'Amore 		return ('\7');
379163bd69bSGarrett D'Amore 	case 'b':
380163bd69bSGarrett D'Amore 		return ('\b');
381163bd69bSGarrett D'Amore 	case 'f':
382163bd69bSGarrett D'Amore 		return ('\f');
383163bd69bSGarrett D'Amore 	case 'n':
384163bd69bSGarrett D'Amore 		return ('\n');
385163bd69bSGarrett D'Amore 	case 'r':
386163bd69bSGarrett D'Amore 		return ('\r');
387163bd69bSGarrett D'Amore 	case 't':
388163bd69bSGarrett D'Amore 		return ('\t');
389163bd69bSGarrett D'Amore 	case 'v':
390163bd69bSGarrett D'Amore 		return ('\13');
391163bd69bSGarrett D'Amore 	case '\0':			/*  \" -> \ */
392163bd69bSGarrett D'Amore 		s->state = EOS;
393163bd69bSGarrett D'Amore 		return ('\\');
394163bd69bSGarrett D'Amore 	default:			/* \x" -> x */
395163bd69bSGarrett D'Amore 		return (ch);
396163bd69bSGarrett D'Amore 	}
397163bd69bSGarrett D'Amore }
398