summaryrefslogtreecommitdiff
path: root/usr.bin/tr/tr.c
blob: 8221880a7f3720dc4a6e9871c1c6e31788f68f37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*	$NetBSD: tr.c,v 1.20 2013/08/11 01:54:35 dholland Exp $	*/

/*
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
 The Regents of the University of California.  All rights reserved.");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)tr.c	8.2 (Berkeley) 5/4/95";
#endif
__RCSID("$NetBSD: tr.c,v 1.20 2013/08/11 01:54:35 dholland Exp $");
#endif /* not lint */

#include <sys/types.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "extern.h"

static int string1[NCHARS], string2[NCHARS];

static void setup(int *, const char *, int, int);
__dead static void usage(void);

int
main(int argc, char **argv)
{
	int ch, ch2, lastch;
	int cflag, dflag, sflag, isstring2;
	STR *s1, *s2;

	cflag = dflag = sflag = 0;
	while ((ch = getopt(argc, argv, "cds")) != -1)
		switch (ch) {
		case 'c':
			cflag = 1;
			break;
		case 'd':
			dflag = 1;
			break;
		case 's':
			sflag = 1;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;

	switch(argc) {
	case 0:
	default:
		usage();
		/* NOTREACHED */
	case 1:
		isstring2 = 0;
		break;
	case 2:
		isstring2 = 1;
		break;
	}

	/*
	 * tr -ds [-c] string1 string2
	 * Delete all characters (or complemented characters) in string1.
	 * Squeeze all characters in string2.
	 */
	if (dflag && sflag) {
		if (!isstring2)
			usage();

		setup(string1, argv[0], 1, cflag);
		setup(string2, argv[1], 2, 0);

		for (lastch = OOBCH; (ch = getchar()) != EOF; )
			if (!string1[ch] && (!string2[ch] || lastch != ch)) {
				lastch = ch;
				(void)putchar(ch);
			}
		exit(0);
	}

	/*
	 * tr -d [-c] string1
	 * Delete all characters (or complemented characters) in string1.
	 */
	if (dflag) {
		if (isstring2)
			usage();

		setup(string1, argv[0], 1, cflag);

		while ((ch = getchar()) != EOF)
			if (!string1[ch])
				(void)putchar(ch);
		exit(0);
	}

	/*
	 * tr -s [-c] string1
	 * Squeeze all characters (or complemented characters) in string1.
	 */
	if (sflag && !isstring2) {
		setup(string1, argv[0], 1, cflag);

		for (lastch = OOBCH; (ch = getchar()) != EOF;)
			if (!string1[ch] || lastch != ch) {
				lastch = ch;
				(void)putchar(ch);
			}
		exit(0);
	}

	/*
	 * tr [-cs] string1 string2
	 * Replace all characters (or complemented characters) in string1 with
	 * the character in the same position in string2.  If the -s option is
	 * specified, squeeze all the characters in string2.
	 */
	if (!isstring2)
		usage();

	/*
	 * The first and second strings need to be matched up. This
	 * means that if we are doing -c, we need to scan the first
	 * string in advance, complement it, and match *that* against
	 * the second string; otherwise we need to scan them together.
	 */

	if (cflag) {
		/*
		 * Scan string 1 and complement it. After this,
		 * string1[] contains 0 for chars to leave alone and 1
		 * for chars to translate.
		 */
		setup(string1, argv[0], 1, cflag);
		s1 = NULL; /* for safety */
		/* we will use ch to iterate over string1, so start it */
		ch = -1;
	} else {
		/* Create the scanner for string 1. */
		s1 = str_create(1, argv[0]);
		for (ch = 0; ch < NCHARS; ch++) {
			string1[ch] = ch;
		}
	}
	/* Create the scanner for string 2. */
	s2 = str_create(2, argv[1]);

	/* Read the first char of string 2 first to make sure there is one. */
	if (!next(s2, &ch2))
		errx(1, "empty string2");

	/*
	 * Loop over the chars from string 1. After this loop string1[]
	 * is a mapping from input to output chars.
	 */
	while (1) {
		if (cflag) {
			/*
			 * Try each character in order. For characters we
			 * skip over because we aren't translating them,
			 * set the translation to the identity.
			 */
			ch++;
			while (ch < NCHARS && string1[ch] == 0) {
				if (string1[ch] == 0) {
					string1[ch] = ch;
				}
				ch++;
			}
			if (ch == NCHARS) {
				break;
			}
		}
		else {
			/* Get the next character from string 1. */
			if (!next(s1, &ch)) {
				break;
			}
		}

		/* Set the translation to the character from string 2. */
		string1[ch] = ch2;

		/* Note the characters to squeeze in string2[]. */
		if (sflag) {
			string2[ch2] = 1;
		}

		/*
		 * Get the next character from string 2. If it runs
		 * out, this will keep returning the last character
		 * over and over again.
		 */
		(void)next(s2, &ch2);
	}

	/*
	 * Now do it.
	 */

	if (sflag)
		for (lastch = OOBCH; (ch = getchar()) != EOF;) {
			ch = string1[ch];
			if (!string2[ch] || lastch != ch) {
				lastch = ch;
				(void)putchar(ch);
			}
		}
	else
		while ((ch = getchar()) != EOF)
			(void)putchar(string1[ch]);

	/* Clean up and exit. */
	if (s1 != NULL) {
		str_destroy(s1);
	}
	str_destroy(s2);
	exit (0);
}

static void
setup(int *string, const char *arg, int whichstring, int cflag)
{
	int cnt, *p;
	int ch;
	STR *str;

	str = str_create(whichstring, arg);
	while (next(str, &ch))
		string[ch] = 1;
	if (cflag)
		for (p = string, cnt = NCHARS; cnt--; ++p)
			*p = !*p;
	str_destroy(str);
}

static void
usage(void)
{
	(void)fprintf(stderr, "usage: tr [-cs] string1 string2\n");
	(void)fprintf(stderr, "       tr [-c] -d string1\n");
	(void)fprintf(stderr, "       tr [-c] -s string1\n");
	(void)fprintf(stderr, "       tr [-c] -ds string1 string2\n");
	exit(1);
}