tr.c revision 1.16 1 /* $NetBSD: tr.c,v 1.16 2013/08/11 00:34:09 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95";
41 #endif
42 __RCSID("$NetBSD: tr.c,v 1.16 2013/08/11 00:34:09 dholland Exp $");
43 #endif /* not lint */
44
45 #include <sys/types.h>
46
47 #include <err.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "extern.h"
54
55 static int string1[NCHARS], string2[NCHARS];
56
57 static void setup(int *, const char *, STR *, int);
58 __dead static void usage(void);
59
60 int
61 main(int argc, char **argv)
62 {
63 int ch, ch2, lastch;
64 int cflag, dflag, sflag, isstring2;
65 STR *s1, *s2;
66
67 cflag = dflag = sflag = 0;
68 while ((ch = getopt(argc, argv, "cds")) != -1)
69 switch (ch) {
70 case 'c':
71 cflag = 1;
72 break;
73 case 'd':
74 dflag = 1;
75 break;
76 case 's':
77 sflag = 1;
78 break;
79 case '?':
80 default:
81 usage();
82 }
83 argc -= optind;
84 argv += optind;
85
86 switch(argc) {
87 case 0:
88 default:
89 usage();
90 /* NOTREACHED */
91 case 1:
92 isstring2 = 0;
93 break;
94 case 2:
95 isstring2 = 1;
96 break;
97 }
98
99 s1 = str_create(1);
100 s2 = str_create(2);
101
102 /*
103 * tr -ds [-c] string1 string2
104 * Delete all characters (or complemented characters) in string1.
105 * Squeeze all characters in string2.
106 */
107 if (dflag && sflag) {
108 if (!isstring2)
109 usage();
110
111 setup(string1, argv[0], s1, cflag);
112 setup(string2, argv[1], s2, 0);
113
114 for (lastch = OOBCH; (ch = getchar()) != EOF;)
115 if (!string1[ch] && (!string2[ch] || lastch != ch)) {
116 lastch = ch;
117 (void)putchar(ch);
118 }
119 str_destroy(s1);
120 str_destroy(s2);
121 exit(0);
122 }
123
124 /*
125 * tr -d [-c] string1
126 * Delete all characters (or complemented characters) in string1.
127 */
128 if (dflag) {
129 if (isstring2)
130 usage();
131
132 setup(string1, argv[0], s1, cflag);
133
134 while ((ch = getchar()) != EOF)
135 if (!string1[ch])
136 (void)putchar(ch);
137 str_destroy(s1);
138 str_destroy(s2);
139 exit(0);
140 }
141
142 /*
143 * tr -s [-c] string1
144 * Squeeze all characters (or complemented characters) in string1.
145 */
146 if (sflag && !isstring2) {
147 setup(string1, argv[0], s1, cflag);
148
149 for (lastch = OOBCH; (ch = getchar()) != EOF;)
150 if (!string1[ch] || lastch != ch) {
151 lastch = ch;
152 (void)putchar(ch);
153 }
154 str_destroy(s1);
155 str_destroy(s2);
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 if (cflag) {
169 setup(string1, argv[0], s1, cflag);
170 ch = -1;
171 } else {
172 str_setstring(s1, argv[0]);
173 for (ch = 0; ch < NCHARS; ch++) {
174 string1[ch] = ch;
175 }
176 }
177 str_setstring(s2, argv[1]);
178
179 if (!next(s2, &ch2))
180 errx(1, "empty string2");
181
182 /* If string2 runs out of characters, use the last one specified. */
183 while (1) {
184 if (cflag) {
185 ch++;
186 while (ch < NCHARS && string1[ch] == 0) {
187 if (string1[ch] == 0) {
188 string1[ch] = ch;
189 }
190 ch++;
191 }
192 if (ch == NCHARS) {
193 break;
194 }
195 }
196 else {
197 if (!next(s1, &ch)) {
198 break;
199 }
200 }
201
202 string1[ch] = ch2;
203 if (sflag) {
204 string2[ch2] = 1;
205 }
206 (void)next(s2, &ch2);
207 }
208
209 if (sflag)
210 for (lastch = OOBCH; (ch = getchar()) != EOF;) {
211 ch = string1[ch];
212 if (!string2[ch] || lastch != ch) {
213 lastch = ch;
214 (void)putchar(ch);
215 }
216 }
217 else
218 while ((ch = getchar()) != EOF)
219 (void)putchar(string1[ch]);
220
221 str_destroy(s1);
222 str_destroy(s2);
223 exit (0);
224 }
225
226 static void
227 setup(int *string, const char *arg, STR *str, int cflag)
228 {
229 int cnt, *p;
230 int ch;
231
232 str_setstring(str, arg);
233 memset(string, 0, NCHARS * sizeof(int));
234 while (next(str, &ch))
235 string[ch] = 1;
236 if (cflag)
237 for (p = string, cnt = NCHARS; cnt--; ++p)
238 *p = !*p;
239 }
240
241 static void
242 usage(void)
243 {
244 (void)fprintf(stderr, "usage: tr [-cs] string1 string2\n");
245 (void)fprintf(stderr, " tr [-c] -d string1\n");
246 (void)fprintf(stderr, " tr [-c] -s string1\n");
247 (void)fprintf(stderr, " tr [-c] -ds string1 string2\n");
248 exit(1);
249 }
250