xref: /freebsd/usr.bin/tr/tr.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1988, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes  * are met:
109b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes  *    without specific prior written permission.
189b50d902SRodney W. Grimes  *
199b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes  * SUCH DAMAGE.
309b50d902SRodney W. Grimes  */
319b50d902SRodney W. Grimes 
329b50d902SRodney W. Grimes #include <sys/types.h>
33d36899d1SConrad Meyer #include <sys/capsicum.h>
34df3f5d9dSPeter Wemm 
35a4e3fc54SMariusz Zaborski #include <capsicum_helpers.h>
36821df508SXin LI #include <ctype.h>
37af647767SPhilippe Charnier #include <err.h>
38821df508SXin LI #include <limits.h>
3978732475SMark Murray #include <locale.h>
40f792cb87SAndrew Turner #include <stdint.h>
419b50d902SRodney W. Grimes #include <stdio.h>
429b50d902SRodney W. Grimes #include <stdlib.h>
439b50d902SRodney W. Grimes #include <string.h>
44df3f5d9dSPeter Wemm #include <unistd.h>
45821df508SXin LI #include <wchar.h>
46821df508SXin LI #include <wctype.h>
47df3f5d9dSPeter Wemm 
48ca99cfddSTim J. Robbins #include "cmap.h"
49ca99cfddSTim J. Robbins #include "cset.h"
509b50d902SRodney W. Grimes #include "extern.h"
519b50d902SRodney W. Grimes 
521dca6e4eSEd Schouten static STR s1 = { STRING1, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL };
531dca6e4eSEd Schouten static STR s2 = { STRING2, NORMAL, 0, OOBCH, 0, { 0, OOBCH }, NULL, NULL };
54dfac4f36STim J. Robbins 
55ca99cfddSTim J. Robbins static struct cset *setup(char *, STR *, int, int);
56*cccdaf50SAlfonso Gregory static void usage(void) __dead2;
579b50d902SRodney W. Grimes 
589b50d902SRodney W. Grimes int
main(int argc,char ** argv)59f4ac32deSDavid Malone main(int argc, char **argv)
609b50d902SRodney W. Grimes {
615b4fa425SAndrey A. Chernov 	static int carray[NCHARS_SB];
62ca99cfddSTim J. Robbins 	struct cmap *map;
63ca99cfddSTim J. Robbins 	struct cset *delete, *squeeze;
645b4fa425SAndrey A. Chernov 	int n, *p;
65dfac4f36STim J. Robbins 	int Cflag, cflag, dflag, sflag, isstring2;
666c97c3d1SStefan Farfeleder 	wint_t ch, cnt, lastch;
679b50d902SRodney W. Grimes 
6885f6c317STim J. Robbins 	(void)setlocale(LC_ALL, "");
69199482eaSAndrey A. Chernov 
70a4e3fc54SMariusz Zaborski 	if (caph_limit_stdio() == -1)
71a4e3fc54SMariusz Zaborski 		err(1, "unable to limit stdio");
72875cba63SConrad Meyer 
737672a014SMariusz Zaborski 	if (caph_enter() < 0)
74875cba63SConrad Meyer 		err(1, "unable to enter capability mode");
75875cba63SConrad Meyer 
76dfac4f36STim J. Robbins 	Cflag = cflag = dflag = sflag = 0;
77dfac4f36STim J. Robbins 	while ((ch = getopt(argc, argv, "Ccdsu")) != -1)
789b50d902SRodney W. Grimes 		switch((char)ch) {
79dfac4f36STim J. Robbins 		case 'C':
80dfac4f36STim J. Robbins 			Cflag = 1;
81dfac4f36STim J. Robbins 			cflag = 0;
82dfac4f36STim J. Robbins 			break;
839b50d902SRodney W. Grimes 		case 'c':
849b50d902SRodney W. Grimes 			cflag = 1;
85dfac4f36STim J. Robbins 			Cflag = 0;
869b50d902SRodney W. Grimes 			break;
879b50d902SRodney W. Grimes 		case 'd':
889b50d902SRodney W. Grimes 			dflag = 1;
899b50d902SRodney W. Grimes 			break;
909b50d902SRodney W. Grimes 		case 's':
919b50d902SRodney W. Grimes 			sflag = 1;
929b50d902SRodney W. Grimes 			break;
9369bd8767SWolfgang Helbig 		case 'u':
9469bd8767SWolfgang Helbig 			setbuf(stdout, (char *)NULL);
9569bd8767SWolfgang Helbig 			break;
969b50d902SRodney W. Grimes 		case '?':
979b50d902SRodney W. Grimes 		default:
989b50d902SRodney W. Grimes 			usage();
999b50d902SRodney W. Grimes 		}
1009b50d902SRodney W. Grimes 	argc -= optind;
1019b50d902SRodney W. Grimes 	argv += optind;
1029b50d902SRodney W. Grimes 
1039b50d902SRodney W. Grimes 	switch(argc) {
1049b50d902SRodney W. Grimes 	case 0:
1059b50d902SRodney W. Grimes 	default:
1069b50d902SRodney W. Grimes 		usage();
1079b50d902SRodney W. Grimes 		/* NOTREACHED */
1089b50d902SRodney W. Grimes 	case 1:
1099b50d902SRodney W. Grimes 		isstring2 = 0;
1109b50d902SRodney W. Grimes 		break;
1119b50d902SRodney W. Grimes 	case 2:
1129b50d902SRodney W. Grimes 		isstring2 = 1;
1139b50d902SRodney W. Grimes 		break;
1149b50d902SRodney W. Grimes 	}
1159b50d902SRodney W. Grimes 
1169b50d902SRodney W. Grimes 	/*
117dfac4f36STim J. Robbins 	 * tr -ds [-Cc] string1 string2
1189b50d902SRodney W. Grimes 	 * Delete all characters (or complemented characters) in string1.
1199b50d902SRodney W. Grimes 	 * Squeeze all characters in string2.
1209b50d902SRodney W. Grimes 	 */
1219b50d902SRodney W. Grimes 	if (dflag && sflag) {
1229b50d902SRodney W. Grimes 		if (!isstring2)
1239b50d902SRodney W. Grimes 			usage();
1249b50d902SRodney W. Grimes 
125ca99cfddSTim J. Robbins 		delete = setup(argv[0], &s1, cflag, Cflag);
126ca99cfddSTim J. Robbins 		squeeze = setup(argv[1], &s2, 0, 0);
1279b50d902SRodney W. Grimes 
128ca99cfddSTim J. Robbins 		for (lastch = OOBCH; (ch = getwchar()) != WEOF;)
129ca99cfddSTim J. Robbins 			if (!cset_in(delete, ch) &&
130ca99cfddSTim J. Robbins 			    (lastch != ch || !cset_in(squeeze, ch))) {
1319b50d902SRodney W. Grimes 				lastch = ch;
132ca99cfddSTim J. Robbins 				(void)putwchar(ch);
1339b50d902SRodney W. Grimes 			}
13494098353STim J. Robbins 		if (ferror(stdin))
13594098353STim J. Robbins 			err(1, NULL);
1369b50d902SRodney W. Grimes 		exit(0);
1379b50d902SRodney W. Grimes 	}
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 	/*
140dfac4f36STim J. Robbins 	 * tr -d [-Cc] string1
1419b50d902SRodney W. Grimes 	 * Delete all characters (or complemented characters) in string1.
1429b50d902SRodney W. Grimes 	 */
1439b50d902SRodney W. Grimes 	if (dflag) {
1449b50d902SRodney W. Grimes 		if (isstring2)
1459b50d902SRodney W. Grimes 			usage();
1469b50d902SRodney W. Grimes 
147ca99cfddSTim J. Robbins 		delete = setup(argv[0], &s1, cflag, Cflag);
1489b50d902SRodney W. Grimes 
149ca99cfddSTim J. Robbins 		while ((ch = getwchar()) != WEOF)
150ca99cfddSTim J. Robbins 			if (!cset_in(delete, ch))
151ca99cfddSTim J. Robbins 				(void)putwchar(ch);
15294098353STim J. Robbins 		if (ferror(stdin))
15394098353STim J. Robbins 			err(1, NULL);
1549b50d902SRodney W. Grimes 		exit(0);
1559b50d902SRodney W. Grimes 	}
1569b50d902SRodney W. Grimes 
1579b50d902SRodney W. Grimes 	/*
158dfac4f36STim J. Robbins 	 * tr -s [-Cc] string1
1599b50d902SRodney W. Grimes 	 * Squeeze all characters (or complemented characters) in string1.
1609b50d902SRodney W. Grimes 	 */
1619b50d902SRodney W. Grimes 	if (sflag && !isstring2) {
162ca99cfddSTim J. Robbins 		squeeze = setup(argv[0], &s1, cflag, Cflag);
1639b50d902SRodney W. Grimes 
164ca99cfddSTim J. Robbins 		for (lastch = OOBCH; (ch = getwchar()) != WEOF;)
165ca99cfddSTim J. Robbins 			if (lastch != ch || !cset_in(squeeze, ch)) {
1669b50d902SRodney W. Grimes 				lastch = ch;
167ca99cfddSTim J. Robbins 				(void)putwchar(ch);
1689b50d902SRodney W. Grimes 			}
16994098353STim J. Robbins 		if (ferror(stdin))
17094098353STim J. Robbins 			err(1, NULL);
1719b50d902SRodney W. Grimes 		exit(0);
1729b50d902SRodney W. Grimes 	}
1739b50d902SRodney W. Grimes 
1749b50d902SRodney W. Grimes 	/*
175dfac4f36STim J. Robbins 	 * tr [-Ccs] string1 string2
1769b50d902SRodney W. Grimes 	 * Replace all characters (or complemented characters) in string1 with
1779b50d902SRodney W. Grimes 	 * the character in the same position in string2.  If the -s option is
1789b50d902SRodney W. Grimes 	 * specified, squeeze all the characters in string2.
1799b50d902SRodney W. Grimes 	 */
1809b50d902SRodney W. Grimes 	if (!isstring2)
1819b50d902SRodney W. Grimes 		usage();
1829b50d902SRodney W. Grimes 
183ca99cfddSTim J. Robbins 	map = cmap_alloc();
184ca99cfddSTim J. Robbins 	if (map == NULL)
185ca99cfddSTim J. Robbins 		err(1, NULL);
186ca99cfddSTim J. Robbins 	squeeze = cset_alloc();
187ca99cfddSTim J. Robbins 	if (squeeze == NULL)
188ca99cfddSTim J. Robbins 		err(1, NULL);
189ca99cfddSTim J. Robbins 
1909b50d902SRodney W. Grimes 	s1.str = argv[0];
191ca99cfddSTim J. Robbins 
192ca99cfddSTim J. Robbins 	if (Cflag || cflag) {
193ca99cfddSTim J. Robbins 		cmap_default(map, OOBCH);
194e42eb683SAndrey A. Chernov 		if ((s2.str = strdup(argv[1])) == NULL)
195d7da7302SAndrey A. Chernov 			errx(1, "strdup(argv[1])");
196d7da7302SAndrey A. Chernov 	} else
197d7da7302SAndrey A. Chernov 		s2.str = argv[1];
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 	if (!next(&s2))
200af647767SPhilippe Charnier 		errx(1, "empty string2");
2019b50d902SRodney W. Grimes 
20200611f04SAndrey A. Chernov 	/*
20300611f04SAndrey A. Chernov 	 * For -s result will contain only those characters defined
20400611f04SAndrey A. Chernov 	 * as the second characters in each of the toupper or tolower
20500611f04SAndrey A. Chernov 	 * pairs.
20600611f04SAndrey A. Chernov 	 */
2079b50d902SRodney W. Grimes 
20800611f04SAndrey A. Chernov 	/* If string2 runs out of characters, use the last one specified. */
20900611f04SAndrey A. Chernov 	while (next(&s1)) {
21000611f04SAndrey A. Chernov 	again:
211ca99cfddSTim J. Robbins 		if (s1.state == CCLASS_LOWER &&
212ca99cfddSTim J. Robbins 		    s2.state == CCLASS_UPPER &&
21300611f04SAndrey A. Chernov 		    s1.cnt == 1 && s2.cnt == 1) {
21400611f04SAndrey A. Chernov 			do {
215ca99cfddSTim J. Robbins 				ch = towupper(s1.lastch);
216ca99cfddSTim J. Robbins 				cmap_add(map, s1.lastch, ch);
217ca99cfddSTim J. Robbins 				if (sflag && iswupper(ch))
218ca99cfddSTim J. Robbins 					cset_add(squeeze, ch);
21900611f04SAndrey A. Chernov 				if (!next(&s1))
22000611f04SAndrey A. Chernov 					goto endloop;
221ca99cfddSTim J. Robbins 			} while (s1.state == CCLASS_LOWER && s1.cnt > 1);
22200611f04SAndrey A. Chernov 			/* skip upper set */
22300611f04SAndrey A. Chernov 			do {
22400611f04SAndrey A. Chernov 				if (!next(&s2))
22500611f04SAndrey A. Chernov 					break;
226ca99cfddSTim J. Robbins 			} while (s2.state == CCLASS_UPPER && s2.cnt > 1);
22700611f04SAndrey A. Chernov 			goto again;
228ca99cfddSTim J. Robbins 		} else if (s1.state == CCLASS_UPPER &&
229ca99cfddSTim J. Robbins 			   s2.state == CCLASS_LOWER &&
23000611f04SAndrey A. Chernov 			   s1.cnt == 1 && s2.cnt == 1) {
23100611f04SAndrey A. Chernov 			do {
232ca99cfddSTim J. Robbins 				ch = towlower(s1.lastch);
233ca99cfddSTim J. Robbins 				cmap_add(map, s1.lastch, ch);
234ca99cfddSTim J. Robbins 				if (sflag && iswlower(ch))
235ca99cfddSTim J. Robbins 					cset_add(squeeze, ch);
23600611f04SAndrey A. Chernov 				if (!next(&s1))
23700611f04SAndrey A. Chernov 					goto endloop;
238ca99cfddSTim J. Robbins 			} while (s1.state == CCLASS_UPPER && s1.cnt > 1);
23900611f04SAndrey A. Chernov 			/* skip lower set */
24000611f04SAndrey A. Chernov 			do {
24100611f04SAndrey A. Chernov 				if (!next(&s2))
24200611f04SAndrey A. Chernov 					break;
243ca99cfddSTim J. Robbins 			} while (s2.state == CCLASS_LOWER && s2.cnt > 1);
24400611f04SAndrey A. Chernov 			goto again;
24500611f04SAndrey A. Chernov 		} else {
246ca99cfddSTim J. Robbins 			cmap_add(map, s1.lastch, s2.lastch);
24700611f04SAndrey A. Chernov 			if (sflag)
248ca99cfddSTim J. Robbins 				cset_add(squeeze, s2.lastch);
24900611f04SAndrey A. Chernov 		}
25000611f04SAndrey A. Chernov 		(void)next(&s2);
25100611f04SAndrey A. Chernov 	}
25200611f04SAndrey A. Chernov endloop:
2535b4fa425SAndrey A. Chernov 	if (cflag || (Cflag && MB_CUR_MAX > 1)) {
254ca99cfddSTim J. Robbins 		/*
255ca99cfddSTim J. Robbins 		 * This is somewhat tricky: since the character set is
256ca99cfddSTim J. Robbins 		 * potentially huge, we need to avoid allocating a map
257ca99cfddSTim J. Robbins 		 * entry for every character. Our strategy is to set the
258ca99cfddSTim J. Robbins 		 * default mapping to the last character of string #2
259ca99cfddSTim J. Robbins 		 * (= the one that gets automatically repeated), then to
260ca99cfddSTim J. Robbins 		 * add back identity mappings for characters that should
261ca99cfddSTim J. Robbins 		 * remain unchanged. We don't waste space on identity mappings
262ca99cfddSTim J. Robbins 		 * for non-characters with the -C option; those are simulated
263ca99cfddSTim J. Robbins 		 * in the I/O loop.
264ca99cfddSTim J. Robbins 		 */
265ca99cfddSTim J. Robbins 		s2.str = argv[1];
266ca99cfddSTim J. Robbins 		s2.state = NORMAL;
2678e0303b0SAndrey A. Chernov 		for (cnt = 0; cnt < WINT_MAX; cnt++) {
268ca99cfddSTim J. Robbins 			if (Cflag && !iswrune(cnt))
269ca99cfddSTim J. Robbins 				continue;
270ca99cfddSTim J. Robbins 			if (cmap_lookup(map, cnt) == OOBCH) {
271c4c8ae8bSAndrey A. Chernov 				if (next(&s2)) {
272ca99cfddSTim J. Robbins 					cmap_add(map, cnt, s2.lastch);
273ca99cfddSTim J. Robbins 					if (sflag)
274ca99cfddSTim J. Robbins 						cset_add(squeeze, s2.lastch);
275c4c8ae8bSAndrey A. Chernov 				}
276ca99cfddSTim J. Robbins 			} else
277ca99cfddSTim J. Robbins 				cmap_add(map, cnt, cnt);
278ca99cfddSTim J. Robbins 			if ((s2.state == EOS || s2.state == INFINITE) &&
279ca99cfddSTim J. Robbins 			    cnt >= cmap_max(map))
280ca99cfddSTim J. Robbins 				break;
281ca99cfddSTim J. Robbins 		}
282ca99cfddSTim J. Robbins 		cmap_default(map, s2.lastch);
2835b4fa425SAndrey A. Chernov 	} else if (Cflag) {
2845b4fa425SAndrey A. Chernov 		for (p = carray, cnt = 0; cnt < NCHARS_SB; cnt++) {
2855b4fa425SAndrey A. Chernov 			if (cmap_lookup(map, cnt) == OOBCH && iswrune(cnt))
2865b4fa425SAndrey A. Chernov 				*p++ = cnt;
2875b4fa425SAndrey A. Chernov 			else
2885b4fa425SAndrey A. Chernov 				cmap_add(map, cnt, cnt);
2895b4fa425SAndrey A. Chernov 		}
2905b4fa425SAndrey A. Chernov 		n = p - carray;
2915b4fa425SAndrey A. Chernov 		if (Cflag && n > 1)
2925b4fa425SAndrey A. Chernov 			(void)mergesort(carray, n, sizeof(*carray), charcoll);
2935b4fa425SAndrey A. Chernov 
2945b4fa425SAndrey A. Chernov 		s2.str = argv[1];
2955b4fa425SAndrey A. Chernov 		s2.state = NORMAL;
2965b4fa425SAndrey A. Chernov 		for (cnt = 0; cnt < n; cnt++) {
2975b4fa425SAndrey A. Chernov 			(void)next(&s2);
2985b4fa425SAndrey A. Chernov 			cmap_add(map, carray[cnt], s2.lastch);
2995b4fa425SAndrey A. Chernov 			/*
3005b4fa425SAndrey A. Chernov 			 * Chars taken from s2 can be different this time
3015b4fa425SAndrey A. Chernov 			 * due to lack of complex upper/lower processing,
3025b4fa425SAndrey A. Chernov 			 * so fill string2 again to not miss some.
3035b4fa425SAndrey A. Chernov 			 */
3045b4fa425SAndrey A. Chernov 			if (sflag)
3055b4fa425SAndrey A. Chernov 				cset_add(squeeze, s2.lastch);
3065b4fa425SAndrey A. Chernov 		}
307482711cfSTim J. Robbins 	}
3089b50d902SRodney W. Grimes 
309ca99cfddSTim J. Robbins 	cset_cache(squeeze);
310ca99cfddSTim J. Robbins 	cmap_cache(map);
311ca99cfddSTim J. Robbins 
3129b50d902SRodney W. Grimes 	if (sflag)
313ca99cfddSTim J. Robbins 		for (lastch = OOBCH; (ch = getwchar()) != WEOF;) {
314ca99cfddSTim J. Robbins 			if (!Cflag || iswrune(ch))
315ca99cfddSTim J. Robbins 				ch = cmap_lookup(map, ch);
316ca99cfddSTim J. Robbins 			if (lastch != ch || !cset_in(squeeze, ch)) {
3179b50d902SRodney W. Grimes 				lastch = ch;
318ca99cfddSTim J. Robbins 				(void)putwchar(ch);
3199b50d902SRodney W. Grimes 			}
3209b50d902SRodney W. Grimes 		}
3219b50d902SRodney W. Grimes 	else
322ca99cfddSTim J. Robbins 		while ((ch = getwchar()) != WEOF) {
323ca99cfddSTim J. Robbins 			if (!Cflag || iswrune(ch))
324ca99cfddSTim J. Robbins 				ch = cmap_lookup(map, ch);
325ca99cfddSTim J. Robbins 			(void)putwchar(ch);
326ca99cfddSTim J. Robbins 		}
32794098353STim J. Robbins 	if (ferror(stdin))
32894098353STim J. Robbins 		err(1, NULL);
3299b50d902SRodney W. Grimes 	exit (0);
3309b50d902SRodney W. Grimes }
3319b50d902SRodney W. Grimes 
332ca99cfddSTim J. Robbins static struct cset *
setup(char * arg,STR * str,int cflag,int Cflag)333ca99cfddSTim J. Robbins setup(char *arg, STR *str, int cflag, int Cflag)
3349b50d902SRodney W. Grimes {
335ca99cfddSTim J. Robbins 	struct cset *cs;
3369b50d902SRodney W. Grimes 
337ca99cfddSTim J. Robbins 	cs = cset_alloc();
338ca99cfddSTim J. Robbins 	if (cs == NULL)
339ca99cfddSTim J. Robbins 		err(1, NULL);
3409b50d902SRodney W. Grimes 	str->str = arg;
3419b50d902SRodney W. Grimes 	while (next(str))
342ca99cfddSTim J. Robbins 		cset_add(cs, str->lastch);
343ca99cfddSTim J. Robbins 	if (Cflag)
344ca99cfddSTim J. Robbins 		cset_addclass(cs, wctype("rune"), true);
345ca99cfddSTim J. Robbins 	if (cflag || Cflag)
346ca99cfddSTim J. Robbins 		cset_invert(cs);
347ca99cfddSTim J. Robbins 	cset_cache(cs);
348ca99cfddSTim J. Robbins 	return (cs);
3499b50d902SRodney W. Grimes }
3509b50d902SRodney W. Grimes 
3515b4fa425SAndrey A. Chernov int
charcoll(const void * a,const void * b)3525b4fa425SAndrey A. Chernov charcoll(const void *a, const void *b)
3535b4fa425SAndrey A. Chernov {
3545b4fa425SAndrey A. Chernov 	static char sa[2], sb[2];
3555b4fa425SAndrey A. Chernov 
3565b4fa425SAndrey A. Chernov 	sa[0] = *(const int *)a;
3575b4fa425SAndrey A. Chernov 	sb[0] = *(const int *)b;
3585b4fa425SAndrey A. Chernov 	return (strcoll(sa, sb));
3595b4fa425SAndrey A. Chernov }
3605b4fa425SAndrey A. Chernov 
3619b50d902SRodney W. Grimes static void
usage(void)362f4ac32deSDavid Malone usage(void)
3639b50d902SRodney W. Grimes {
364af647767SPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
365dfac4f36STim J. Robbins 		"usage: tr [-Ccsu] string1 string2",
366dfac4f36STim J. Robbins 		"       tr [-Ccu] -d string1",
367dfac4f36STim J. Robbins 		"       tr [-Ccu] -s string1",
368dfac4f36STim J. Robbins 		"       tr [-Ccu] -ds string1 string2");
3699b50d902SRodney W. Grimes 	exit(1);
3709b50d902SRodney W. Grimes }
371