xref: /illumos-gate/usr/src/cmd/tr/tr.c (revision ef150c2b)
1163bd69bSGarrett D'Amore /*
2163bd69bSGarrett D'Amore  * Copyright (c) 1988, 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 <limits.h>
39163bd69bSGarrett D'Amore #include <locale.h>
40163bd69bSGarrett D'Amore #include <stdio.h>
41163bd69bSGarrett D'Amore #include <stdlib.h>
42163bd69bSGarrett D'Amore #include <string.h>
43163bd69bSGarrett D'Amore #include <unistd.h>
44163bd69bSGarrett D'Amore #include <wchar.h>
45163bd69bSGarrett D'Amore #include <wctype.h>
46163bd69bSGarrett D'Amore 
47163bd69bSGarrett D'Amore #include "cmap.h"
48163bd69bSGarrett D'Amore #include "cset.h"
49163bd69bSGarrett D'Amore #include "extern.h"
50163bd69bSGarrett D'Amore 
51163bd69bSGarrett D'Amore STR s1 = { STRING1, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL };
52163bd69bSGarrett D'Amore STR s2 = { STRING2, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL };
53163bd69bSGarrett D'Amore 
54163bd69bSGarrett D'Amore static struct cset *setup(char *, STR *, int, int);
552c668975SToomas Soome static void usage(void) __NORETURN;
56163bd69bSGarrett D'Amore 
57163bd69bSGarrett D'Amore static wint_t
cmap_lookup(struct cmap * cm,wint_t from)58163bd69bSGarrett D'Amore cmap_lookup(struct cmap *cm, wint_t from)
59163bd69bSGarrett D'Amore {
60163bd69bSGarrett D'Amore 
61163bd69bSGarrett D'Amore         if (from < CM_CACHE_SIZE && cm->cm_havecache)
62163bd69bSGarrett D'Amore 		return (cm->cm_cache[from]);
63163bd69bSGarrett D'Amore         return (cmap_lookup_hard(cm, from));
64163bd69bSGarrett D'Amore }
65163bd69bSGarrett D'Amore 
66163bd69bSGarrett D'Amore static wint_t
cmap_max(struct cmap * cm)67163bd69bSGarrett D'Amore cmap_max(struct cmap *cm)
68163bd69bSGarrett D'Amore {
69163bd69bSGarrett D'Amore 	return (cm->cm_max);
70163bd69bSGarrett D'Amore }
71163bd69bSGarrett D'Amore 
72163bd69bSGarrett D'Amore static inline bool
cset_in(struct cset * cs,wchar_t ch)73163bd69bSGarrett D'Amore cset_in(struct cset *cs, wchar_t ch)
74163bd69bSGarrett D'Amore {
75163bd69bSGarrett D'Amore 
76163bd69bSGarrett D'Amore  	if (ch < CS_CACHE_SIZE && cs->cs_havecache)
77163bd69bSGarrett D'Amore 		return (cs->cs_cache[ch]);
78163bd69bSGarrett D'Amore 	return (cset_in_hard(cs, ch));
79163bd69bSGarrett D'Amore }
80163bd69bSGarrett D'Amore 
81163bd69bSGarrett D'Amore int
main(int argc,char ** argv)82163bd69bSGarrett D'Amore main(int argc, char **argv)
83163bd69bSGarrett D'Amore {
84163bd69bSGarrett D'Amore 	static int carray[NCHARS_SB];
85163bd69bSGarrett D'Amore 	struct cmap *map;
86163bd69bSGarrett D'Amore 	struct cset *delete, *squeeze;
87163bd69bSGarrett D'Amore 	int n, *p;
88163bd69bSGarrett D'Amore 	int Cflag, cflag, dflag, sflag, isstring2;
89163bd69bSGarrett D'Amore 	wint_t ch, cnt, lastch;
90*ef150c2bSRichard Lowe 	int c;
91163bd69bSGarrett D'Amore 
92163bd69bSGarrett D'Amore 	(void) setlocale(LC_ALL, "");
93163bd69bSGarrett D'Amore 
94163bd69bSGarrett D'Amore 	Cflag = cflag = dflag = sflag = 0;
95*ef150c2bSRichard Lowe 	while ((c = getopt(argc, argv, "Ccdsu")) != -1)
96*ef150c2bSRichard Lowe 		switch (c) {
97163bd69bSGarrett D'Amore 		case 'C':
98163bd69bSGarrett D'Amore 			Cflag = 1;
99163bd69bSGarrett D'Amore 			cflag = 0;
100163bd69bSGarrett D'Amore 			break;
101163bd69bSGarrett D'Amore 		case 'c':
102163bd69bSGarrett D'Amore 			cflag = 1;
103163bd69bSGarrett D'Amore 			Cflag = 0;
104163bd69bSGarrett D'Amore 			break;
105163bd69bSGarrett D'Amore 		case 'd':
106163bd69bSGarrett D'Amore 			dflag = 1;
107163bd69bSGarrett D'Amore 			break;
108163bd69bSGarrett D'Amore 		case 's':
109163bd69bSGarrett D'Amore 			sflag = 1;
110163bd69bSGarrett D'Amore 			break;
111163bd69bSGarrett D'Amore 		case 'u':
112163bd69bSGarrett D'Amore 			setbuf(stdout, (char *)NULL);
113163bd69bSGarrett D'Amore 			break;
114163bd69bSGarrett D'Amore 		case '?':
115163bd69bSGarrett D'Amore 		default:
116163bd69bSGarrett D'Amore 			usage();
117163bd69bSGarrett D'Amore 		}
118163bd69bSGarrett D'Amore 	argc -= optind;
119163bd69bSGarrett D'Amore 	argv += optind;
120163bd69bSGarrett D'Amore 
121163bd69bSGarrett D'Amore 	switch (argc) {
122163bd69bSGarrett D'Amore 	case 0:
123163bd69bSGarrett D'Amore 	default:
124163bd69bSGarrett D'Amore 		usage();
125163bd69bSGarrett D'Amore 		/* NOTREACHED */
126163bd69bSGarrett D'Amore 	case 1:
127163bd69bSGarrett D'Amore 		isstring2 = 0;
128163bd69bSGarrett D'Amore 		break;
129163bd69bSGarrett D'Amore 	case 2:
130163bd69bSGarrett D'Amore 		isstring2 = 1;
131163bd69bSGarrett D'Amore 		break;
132163bd69bSGarrett D'Amore 	}
133163bd69bSGarrett D'Amore 
134163bd69bSGarrett D'Amore 	/*
135163bd69bSGarrett D'Amore 	 * tr -ds [-Cc] string1 string2
136163bd69bSGarrett D'Amore 	 * Delete all characters (or complemented characters) in string1.
137163bd69bSGarrett D'Amore 	 * Squeeze all characters in string2.
138163bd69bSGarrett D'Amore 	 */
139163bd69bSGarrett D'Amore 	if (dflag && sflag) {
140163bd69bSGarrett D'Amore 		if (!isstring2)
141163bd69bSGarrett D'Amore 			usage();
142163bd69bSGarrett D'Amore 
143163bd69bSGarrett D'Amore 		delete = setup(argv[0], &s1, cflag, Cflag);
144163bd69bSGarrett D'Amore 		squeeze = setup(argv[1], &s2, 0, 0);
145163bd69bSGarrett D'Amore 
146163bd69bSGarrett D'Amore 		for (lastch = OOBCH; (ch = getwchar()) != WEOF; )
147163bd69bSGarrett D'Amore 			if (!cset_in(delete, ch) &&
148163bd69bSGarrett D'Amore 			    (lastch != ch || !cset_in(squeeze, ch))) {
149163bd69bSGarrett D'Amore 				lastch = ch;
150163bd69bSGarrett D'Amore 				(void) putwchar(ch);
151163bd69bSGarrett D'Amore 			}
152163bd69bSGarrett D'Amore 		if (ferror(stdin))
153163bd69bSGarrett D'Amore 			err(1, NULL);
154163bd69bSGarrett D'Amore 		exit(0);
155163bd69bSGarrett D'Amore 	}
156163bd69bSGarrett D'Amore 
157163bd69bSGarrett D'Amore 	/*
158163bd69bSGarrett D'Amore 	 * tr -d [-Cc] string1
159163bd69bSGarrett D'Amore 	 * Delete all characters (or complemented characters) in string1.
160163bd69bSGarrett D'Amore 	 */
161163bd69bSGarrett D'Amore 	if (dflag) {
162163bd69bSGarrett D'Amore 		if (isstring2)
163163bd69bSGarrett D'Amore 			usage();
164163bd69bSGarrett D'Amore 
165163bd69bSGarrett D'Amore 		delete = setup(argv[0], &s1, cflag, Cflag);
166163bd69bSGarrett D'Amore 
167163bd69bSGarrett D'Amore 		while ((ch = getwchar()) != WEOF)
168163bd69bSGarrett D'Amore 			if (!cset_in(delete, ch))
169163bd69bSGarrett D'Amore 				(void) putwchar(ch);
170163bd69bSGarrett D'Amore 		if (ferror(stdin))
171163bd69bSGarrett D'Amore 			err(1, NULL);
172163bd69bSGarrett D'Amore 		exit(0);
173163bd69bSGarrett D'Amore 	}
174163bd69bSGarrett D'Amore 
175163bd69bSGarrett D'Amore 	/*
176163bd69bSGarrett D'Amore 	 * tr -s [-Cc] string1
177163bd69bSGarrett D'Amore 	 * Squeeze all characters (or complemented characters) in string1.
178163bd69bSGarrett D'Amore 	 */
179163bd69bSGarrett D'Amore 	if (sflag && !isstring2) {
180163bd69bSGarrett D'Amore 		squeeze = setup(argv[0], &s1, cflag, Cflag);
181163bd69bSGarrett D'Amore 
182163bd69bSGarrett D'Amore 		for (lastch = OOBCH; (ch = getwchar()) != WEOF; )
183163bd69bSGarrett D'Amore 			if (lastch != ch || !cset_in(squeeze, ch)) {
184163bd69bSGarrett D'Amore 				lastch = ch;
185163bd69bSGarrett D'Amore 				(void) putwchar(ch);
186163bd69bSGarrett D'Amore 			}
187163bd69bSGarrett D'Amore 		if (ferror(stdin))
188163bd69bSGarrett D'Amore 			err(1, NULL);
189163bd69bSGarrett D'Amore 		exit(0);
190163bd69bSGarrett D'Amore 	}
191163bd69bSGarrett D'Amore 
192163bd69bSGarrett D'Amore 	/*
193163bd69bSGarrett D'Amore 	 * tr [-Ccs] string1 string2
194163bd69bSGarrett D'Amore 	 * Replace all characters (or complemented characters) in string1 with
195163bd69bSGarrett D'Amore 	 * the character in the same position in string2.  If the -s option is
196163bd69bSGarrett D'Amore 	 * specified, squeeze all the characters in string2.
197163bd69bSGarrett D'Amore 	 */
198163bd69bSGarrett D'Amore 	if (!isstring2)
199163bd69bSGarrett D'Amore 		usage();
200163bd69bSGarrett D'Amore 
201163bd69bSGarrett D'Amore 	map = cmap_alloc();
202163bd69bSGarrett D'Amore 	if (map == NULL)
203163bd69bSGarrett D'Amore 		err(1, NULL);
204163bd69bSGarrett D'Amore 	squeeze = cset_alloc();
205163bd69bSGarrett D'Amore 	if (squeeze == NULL)
206163bd69bSGarrett D'Amore 		err(1, NULL);
207163bd69bSGarrett D'Amore 
208163bd69bSGarrett D'Amore 	s1.str = argv[0];
209163bd69bSGarrett D'Amore 
210163bd69bSGarrett D'Amore 	if (Cflag || cflag) {
211163bd69bSGarrett D'Amore 		(void) cmap_default(map, OOBCH);
212163bd69bSGarrett D'Amore 		if ((s2.str = strdup(argv[1])) == NULL)
213163bd69bSGarrett D'Amore 			errx(1, "strdup(argv[1])");
214163bd69bSGarrett D'Amore 	} else
215163bd69bSGarrett D'Amore 		s2.str = argv[1];
216163bd69bSGarrett D'Amore 
217163bd69bSGarrett D'Amore 	if (!next(&s2))
218163bd69bSGarrett D'Amore 		errx(1, "empty string2");
219163bd69bSGarrett D'Amore 
220163bd69bSGarrett D'Amore 	/*
221163bd69bSGarrett D'Amore 	 * For -s result will contain only those characters defined
222163bd69bSGarrett D'Amore 	 * as the second characters in each of the toupper or tolower
223163bd69bSGarrett D'Amore 	 * pairs.
224163bd69bSGarrett D'Amore 	 */
225163bd69bSGarrett D'Amore 
226163bd69bSGarrett D'Amore 	/* If string2 runs out of characters, use the last one specified. */
227163bd69bSGarrett D'Amore 	while (next(&s1)) {
228163bd69bSGarrett D'Amore 	again:
229163bd69bSGarrett D'Amore 		if (s1.state == CCLASS_LOWER &&
230163bd69bSGarrett D'Amore 		    s2.state == CCLASS_UPPER &&
231163bd69bSGarrett D'Amore 		    s1.cnt == 1 && s2.cnt == 1) {
232163bd69bSGarrett D'Amore 			do {
233163bd69bSGarrett D'Amore 				ch = towupper(s1.lastch);
234163bd69bSGarrett D'Amore 				(void) cmap_add(map, s1.lastch, ch);
235163bd69bSGarrett D'Amore 				if (sflag && iswupper(ch))
236163bd69bSGarrett D'Amore 					(void) cset_add(squeeze, ch);
237163bd69bSGarrett D'Amore 				if (!next(&s1))
238163bd69bSGarrett D'Amore 					goto endloop;
239163bd69bSGarrett D'Amore 			} while (s1.state == CCLASS_LOWER && s1.cnt > 1);
240163bd69bSGarrett D'Amore 			/* skip upper set */
241163bd69bSGarrett D'Amore 			do {
242163bd69bSGarrett D'Amore 				if (!next(&s2))
243163bd69bSGarrett D'Amore 					break;
244163bd69bSGarrett D'Amore 			} while (s2.state == CCLASS_UPPER && s2.cnt > 1);
245163bd69bSGarrett D'Amore 			goto again;
246163bd69bSGarrett D'Amore 		} else if (s1.state == CCLASS_UPPER &&
247163bd69bSGarrett D'Amore 		    s2.state == CCLASS_LOWER &&
248163bd69bSGarrett D'Amore 		    s1.cnt == 1 && s2.cnt == 1) {
249163bd69bSGarrett D'Amore 			do {
250163bd69bSGarrett D'Amore 				ch = towlower(s1.lastch);
251163bd69bSGarrett D'Amore 				(void) cmap_add(map, s1.lastch, ch);
252163bd69bSGarrett D'Amore 				if (sflag && iswlower(ch))
253163bd69bSGarrett D'Amore 					(void) cset_add(squeeze, ch);
254163bd69bSGarrett D'Amore 				if (!next(&s1))
255163bd69bSGarrett D'Amore 					goto endloop;
256163bd69bSGarrett D'Amore 			} while (s1.state == CCLASS_UPPER && s1.cnt > 1);
257163bd69bSGarrett D'Amore 			/* skip lower set */
258163bd69bSGarrett D'Amore 			do {
259163bd69bSGarrett D'Amore 				if (!next(&s2))
260163bd69bSGarrett D'Amore 					break;
261163bd69bSGarrett D'Amore 			} while (s2.state == CCLASS_LOWER && s2.cnt > 1);
262163bd69bSGarrett D'Amore 			goto again;
263163bd69bSGarrett D'Amore 		} else {
264163bd69bSGarrett D'Amore 			(void) cmap_add(map, s1.lastch, s2.lastch);
265163bd69bSGarrett D'Amore 			if (sflag)
266163bd69bSGarrett D'Amore 				(void) cset_add(squeeze, s2.lastch);
267163bd69bSGarrett D'Amore 		}
268163bd69bSGarrett D'Amore 		(void) next(&s2);
269163bd69bSGarrett D'Amore 	}
270163bd69bSGarrett D'Amore endloop:
271163bd69bSGarrett D'Amore 	if (cflag || (Cflag && MB_CUR_MAX > 1)) {
272163bd69bSGarrett D'Amore 		/*
273163bd69bSGarrett D'Amore 		 * This is somewhat tricky: since the character set is
274163bd69bSGarrett D'Amore 		 * potentially huge, we need to avoid allocating a map
275163bd69bSGarrett D'Amore 		 * entry for every character. Our strategy is to set the
276163bd69bSGarrett D'Amore 		 * default mapping to the last character of string #2
277163bd69bSGarrett D'Amore 		 * (= the one that gets automatically repeated), then to
278163bd69bSGarrett D'Amore 		 * add back identity mappings for characters that should
279163bd69bSGarrett D'Amore 		 * remain unchanged. We don't waste space on identity mappings
280163bd69bSGarrett D'Amore 		 * for non-characters with the -C option; those are simulated
281163bd69bSGarrett D'Amore 		 * in the I/O loop.
282163bd69bSGarrett D'Amore 		 */
283163bd69bSGarrett D'Amore 		s2.str = argv[1];
284163bd69bSGarrett D'Amore 		s2.state = NORMAL;
285163bd69bSGarrett D'Amore 		for (cnt = 0; cnt < WCHAR_MAX; cnt++) {
286163bd69bSGarrett D'Amore 			if (Cflag && !iswrune(cnt))
287163bd69bSGarrett D'Amore 				continue;
288163bd69bSGarrett D'Amore 			if (cmap_lookup(map, cnt) == OOBCH) {
289163bd69bSGarrett D'Amore 				if (next(&s2))
290163bd69bSGarrett D'Amore 					(void) cmap_add(map, cnt, s2.lastch);
291163bd69bSGarrett D'Amore 				if (sflag)
292163bd69bSGarrett D'Amore 					(void) cset_add(squeeze, s2.lastch);
293163bd69bSGarrett D'Amore 			} else
294163bd69bSGarrett D'Amore 				(void) cmap_add(map, cnt, cnt);
295163bd69bSGarrett D'Amore 			if ((s2.state == EOS || s2.state == INFINITE) &&
296163bd69bSGarrett D'Amore 			    cnt >= cmap_max(map))
297163bd69bSGarrett D'Amore 				break;
298163bd69bSGarrett D'Amore 		}
299163bd69bSGarrett D'Amore 		(void) cmap_default(map, s2.lastch);
300163bd69bSGarrett D'Amore 	} else if (Cflag) {
301163bd69bSGarrett D'Amore 		for (p = carray, cnt = 0; cnt < NCHARS_SB; cnt++) {
302163bd69bSGarrett D'Amore 			if (cmap_lookup(map, cnt) == OOBCH && iswrune(cnt))
303163bd69bSGarrett D'Amore 				*p++ = cnt;
304163bd69bSGarrett D'Amore 			else
305163bd69bSGarrett D'Amore 				(void) cmap_add(map, cnt, cnt);
306163bd69bSGarrett D'Amore 		}
307163bd69bSGarrett D'Amore 		n = p - carray;
308163bd69bSGarrett D'Amore 		if (Cflag && n > 1)
309163bd69bSGarrett D'Amore 			(void) qsort(carray, n, sizeof (*carray), charcoll);
310163bd69bSGarrett D'Amore 
311163bd69bSGarrett D'Amore 		s2.str = argv[1];
312163bd69bSGarrett D'Amore 		s2.state = NORMAL;
313163bd69bSGarrett D'Amore 		for (cnt = 0; cnt < n; cnt++) {
314163bd69bSGarrett D'Amore 			(void) next(&s2);
315163bd69bSGarrett D'Amore 			(void) cmap_add(map, carray[cnt], s2.lastch);
316163bd69bSGarrett D'Amore 			/*
317163bd69bSGarrett D'Amore 			 * Chars taken from s2 can be different this time
318163bd69bSGarrett D'Amore 			 * due to lack of complex upper/lower processing,
319163bd69bSGarrett D'Amore 			 * so fill string2 again to not miss some.
320163bd69bSGarrett D'Amore 			 */
321163bd69bSGarrett D'Amore 			if (sflag)
322163bd69bSGarrett D'Amore 				(void) cset_add(squeeze, s2.lastch);
323163bd69bSGarrett D'Amore 		}
324163bd69bSGarrett D'Amore 	}
325163bd69bSGarrett D'Amore 
326163bd69bSGarrett D'Amore 	cset_cache(squeeze);
327163bd69bSGarrett D'Amore 	cmap_cache(map);
328163bd69bSGarrett D'Amore 
329163bd69bSGarrett D'Amore 	if (sflag)
330163bd69bSGarrett D'Amore 		for (lastch = OOBCH; (ch = getwchar()) != WEOF; ) {
331163bd69bSGarrett D'Amore 			if (!Cflag || iswrune(ch))
332163bd69bSGarrett D'Amore 				ch = cmap_lookup(map, ch);
333163bd69bSGarrett D'Amore 			if (lastch != ch || !cset_in(squeeze, ch)) {
334163bd69bSGarrett D'Amore 				lastch = ch;
335163bd69bSGarrett D'Amore 				(void) putwchar(ch);
336163bd69bSGarrett D'Amore 			}
337163bd69bSGarrett D'Amore 		}
338163bd69bSGarrett D'Amore 	else
339163bd69bSGarrett D'Amore 		while ((ch = getwchar()) != WEOF) {
340163bd69bSGarrett D'Amore 			if (!Cflag || iswrune(ch))
341163bd69bSGarrett D'Amore 				ch = cmap_lookup(map, ch);
342163bd69bSGarrett D'Amore 			(void) putwchar(ch);
343163bd69bSGarrett D'Amore 		}
344163bd69bSGarrett D'Amore 	if (ferror(stdin))
345163bd69bSGarrett D'Amore 		err(1, NULL);
346163bd69bSGarrett D'Amore 	exit(0);
347163bd69bSGarrett D'Amore }
348163bd69bSGarrett D'Amore 
349163bd69bSGarrett D'Amore static struct cset *
setup(char * arg,STR * str,int cflag,int Cflag)350163bd69bSGarrett D'Amore setup(char *arg, STR *str, int cflag, int Cflag)
351163bd69bSGarrett D'Amore {
352163bd69bSGarrett D'Amore 	struct cset *cs;
353163bd69bSGarrett D'Amore 
354163bd69bSGarrett D'Amore 	cs = cset_alloc();
355163bd69bSGarrett D'Amore 	if (cs == NULL)
356163bd69bSGarrett D'Amore 		err(1, NULL);
357163bd69bSGarrett D'Amore 	str->str = arg;
358163bd69bSGarrett D'Amore 	while (next(str))
359163bd69bSGarrett D'Amore 		(void) cset_add(cs, str->lastch);
360163bd69bSGarrett D'Amore 	if (Cflag)
361163bd69bSGarrett D'Amore 		(void) cset_addclass(cs, wctype("rune"), true);
362163bd69bSGarrett D'Amore 	if (cflag || Cflag)
363163bd69bSGarrett D'Amore 		cset_invert(cs);
364163bd69bSGarrett D'Amore 	cset_cache(cs);
365163bd69bSGarrett D'Amore 	return (cs);
366163bd69bSGarrett D'Amore }
367163bd69bSGarrett D'Amore 
368163bd69bSGarrett D'Amore int
charcoll(const void * a,const void * b)369163bd69bSGarrett D'Amore charcoll(const void *a, const void *b)
370163bd69bSGarrett D'Amore {
371163bd69bSGarrett D'Amore 	static char sa[2], sb[2];
372163bd69bSGarrett D'Amore 
373163bd69bSGarrett D'Amore 	sa[0] = *(const int *)a;
374163bd69bSGarrett D'Amore 	sb[0] = *(const int *)b;
375163bd69bSGarrett D'Amore 	return (strcoll(sa, sb));
376163bd69bSGarrett D'Amore }
377163bd69bSGarrett D'Amore 
378163bd69bSGarrett D'Amore static void
usage(void)379163bd69bSGarrett D'Amore usage(void)
380163bd69bSGarrett D'Amore {
381163bd69bSGarrett D'Amore 	(void) fprintf(stderr, "%s\n%s\n%s\n%s\n",
382163bd69bSGarrett D'Amore 	    "usage: tr [-Ccsu] string1 string2",
383163bd69bSGarrett D'Amore 	    "       tr [-Ccu] -d string1",
384163bd69bSGarrett D'Amore 	    "       tr [-Ccu] -s string1",
385163bd69bSGarrett D'Amore 	    "       tr [-Ccu] -ds string1 string2");
386163bd69bSGarrett D'Amore 	exit(1);
387163bd69bSGarrett D'Amore }
388