xref: /original-bsd/usr.bin/tr/tr.c (revision 0f81f0ee)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1988, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)tr.c	8.2 (Berkeley) 05/04/95";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include "extern.h"
26 
27 static int string1[NCHARS] = {
28 	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,		/* ASCII */
29 	0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
30 	0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
31 	0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
32 	0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
33 	0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
34 	0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
35 	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
36 	0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
37 	0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
38 	0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
39 	0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
40 	0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
41 	0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
42 	0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
43 	0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f,
44 	0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
45 	0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
46 	0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
47 	0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
48 	0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
49 	0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
50 	0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
51 	0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
52 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
53 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
54 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
55 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
56 	0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
57 	0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
58 	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
59 	0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
60 }, string2[NCHARS];
61 
62 STR s1 = { STRING1, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
63 STR s2 = { STRING2, NORMAL, 0, OOBCH, { 0, OOBCH }, NULL, NULL };
64 
65 static void setup __P((int *, char *, STR *, int));
66 static void usage __P((void));
67 
68 int
69 main(argc, argv)
70 	int argc;
71 	char **argv;
72 {
73 	register int ch, cnt, lastch, *p;
74 	int cflag, dflag, sflag, isstring2;
75 
76 	cflag = dflag = sflag = 0;
77 	while ((ch = getopt(argc, argv, "cds")) != EOF)
78 		switch((char)ch) {
79 		case 'c':
80 			cflag = 1;
81 			break;
82 		case 'd':
83 			dflag = 1;
84 			break;
85 		case 's':
86 			sflag = 1;
87 			break;
88 		case '?':
89 		default:
90 			usage();
91 		}
92 	argc -= optind;
93 	argv += optind;
94 
95 	switch(argc) {
96 	case 0:
97 	default:
98 		usage();
99 		/* NOTREACHED */
100 	case 1:
101 		isstring2 = 0;
102 		break;
103 	case 2:
104 		isstring2 = 1;
105 		break;
106 	}
107 
108 	/*
109 	 * tr -ds [-c] string1 string2
110 	 * Delete all characters (or complemented characters) in string1.
111 	 * Squeeze all characters in string2.
112 	 */
113 	if (dflag && sflag) {
114 		if (!isstring2)
115 			usage();
116 
117 		setup(string1, argv[0], &s1, cflag);
118 		setup(string2, argv[1], &s2, 0);
119 
120 		for (lastch = OOBCH; (ch = getchar()) != EOF;)
121 			if (!string1[ch] && (!string2[ch] || lastch != ch)) {
122 				lastch = ch;
123 				(void)putchar(ch);
124 			}
125 		exit(0);
126 	}
127 
128 	/*
129 	 * tr -d [-c] string1
130 	 * Delete all characters (or complemented characters) in string1.
131 	 */
132 	if (dflag) {
133 		if (isstring2)
134 			usage();
135 
136 		setup(string1, argv[0], &s1, cflag);
137 
138 		while ((ch = getchar()) != EOF)
139 			if (!string1[ch])
140 				(void)putchar(ch);
141 		exit(0);
142 	}
143 
144 	/*
145 	 * tr -s [-c] string1
146 	 * Squeeze all characters (or complemented characters) in string1.
147 	 */
148 	if (sflag && !isstring2) {
149 		setup(string1, argv[0], &s1, cflag);
150 
151 		for (lastch = OOBCH; (ch = getchar()) != EOF;)
152 			if (!string1[ch] || lastch != ch) {
153 				lastch = ch;
154 				(void)putchar(ch);
155 			}
156 		exit(0);
157 	}
158 
159 	/*
160 	 * tr [-cs] string1 string2
161 	 * Replace all characters (or complemented characters) in string1 with
162 	 * the character in the same position in string2.  If the -s option is
163 	 * specified, squeeze all the characters in string2.
164 	 */
165 	if (!isstring2)
166 		usage();
167 
168 	s1.str = argv[0];
169 	s2.str = argv[1];
170 
171 	if (cflag)
172 		for (cnt = NCHARS, p = string1; cnt--;)
173 			*p++ = OOBCH;
174 
175 	if (!next(&s2))
176 		err("empty string2");
177 
178 	/* If string2 runs out of characters, use the last one specified. */
179 	if (sflag)
180 		while (next(&s1)) {
181 			string1[s1.lastch] = ch = s2.lastch;
182 			string2[ch] = 1;
183 			(void)next(&s2);
184 		}
185 	else
186 		while (next(&s1)) {
187 			string1[s1.lastch] = ch = s2.lastch;
188 			(void)next(&s2);
189 		}
190 
191 	if (cflag)
192 		for (cnt = 0, p = string1; cnt < NCHARS; ++p, ++cnt)
193 			*p = *p == OOBCH ? ch : cnt;
194 
195 	if (sflag)
196 		for (lastch = OOBCH; (ch = getchar()) != EOF;) {
197 			ch = string1[ch];
198 			if (!string2[ch] || lastch != ch) {
199 				lastch = ch;
200 				(void)putchar(ch);
201 			}
202 		}
203 	else
204 		while ((ch = getchar()) != EOF)
205 			(void)putchar(string1[ch]);
206 	exit (0);
207 }
208 
209 static void
210 setup(string, arg, str, cflag)
211 	int *string;
212 	char *arg;
213 	STR *str;
214 	int cflag;
215 {
216 	register int cnt, *p;
217 
218 	str->str = arg;
219 	bzero(string, NCHARS * sizeof(int));
220 	while (next(str))
221 		string[str->lastch] = 1;
222 	if (cflag)
223 		for (p = string, cnt = NCHARS; cnt--; ++p)
224 			*p = !*p;
225 }
226 
227 static void
228 usage()
229 {
230 	(void)fprintf(stderr, "usage: tr [-cs] string1 string2\n");
231 	(void)fprintf(stderr, "       tr [-c] -d string1\n");
232 	(void)fprintf(stderr, "       tr [-c] -s string1\n");
233 	(void)fprintf(stderr, "       tr [-c] -ds string1 string2\n");
234 	exit(1);
235 }
236 
237 #if __STDC__
238 #include <stdarg.h>
239 #else
240 #include <varargs.h>
241 #endif
242 
243 void
244 #if __STDC__
245 err(const char *fmt, ...)
246 #else
247 err(fmt, va_alist)
248 	char *fmt;
249         va_dcl
250 #endif
251 {
252 	va_list ap;
253 #if __STDC__
254 	va_start(ap, fmt);
255 #else
256 	va_start(ap);
257 #endif
258 	(void)fprintf(stderr, "tr: ");
259 	(void)vfprintf(stderr, fmt, ap);
260 	va_end(ap);
261 	(void)fprintf(stderr, "\n");
262 	exit(1);
263 	/* NOTREACHED */
264 }
265