1 /* $NetBSD: tr.c,v 1.20 2013/08/11 01:54:35 dholland Exp $ */
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)tr.c 8.2 (Berkeley) 5/4/95";
42 __RCSID("$NetBSD: tr.c,v 1.20 2013/08/11 01:54:35 dholland Exp $");
45 #include <sys/types.h>
55 static int string1
[NCHARS
], string2
[NCHARS
];
57 static void setup(int *, const char *, int, int);
58 __dead
static void usage(void);
61 main(int argc
, char **argv
)
64 int cflag
, dflag
, sflag
, isstring2
;
67 cflag
= dflag
= sflag
= 0;
68 while ((ch
= getopt(argc
, argv
, "cds")) != -1)
100 * tr -ds [-c] string1 string2
101 * Delete all characters (or complemented characters) in string1.
102 * Squeeze all characters in string2.
104 if (dflag
&& sflag
) {
108 setup(string1
, argv
[0], 1, cflag
);
109 setup(string2
, argv
[1], 2, 0);
111 for (lastch
= OOBCH
; (ch
= getchar()) != EOF
; )
112 if (!string1
[ch
] && (!string2
[ch
] || lastch
!= ch
)) {
121 * Delete all characters (or complemented characters) in string1.
127 setup(string1
, argv
[0], 1, cflag
);
129 while ((ch
= getchar()) != EOF
)
137 * Squeeze all characters (or complemented characters) in string1.
139 if (sflag
&& !isstring2
) {
140 setup(string1
, argv
[0], 1, cflag
);
142 for (lastch
= OOBCH
; (ch
= getchar()) != EOF
;)
143 if (!string1
[ch
] || lastch
!= ch
) {
151 * tr [-cs] string1 string2
152 * Replace all characters (or complemented characters) in string1 with
153 * the character in the same position in string2. If the -s option is
154 * specified, squeeze all the characters in string2.
160 * The first and second strings need to be matched up. This
161 * means that if we are doing -c, we need to scan the first
162 * string in advance, complement it, and match *that* against
163 * the second string; otherwise we need to scan them together.
168 * Scan string 1 and complement it. After this,
169 * string1[] contains 0 for chars to leave alone and 1
170 * for chars to translate.
172 setup(string1
, argv
[0], 1, cflag
);
173 s1
= NULL
; /* for safety */
174 /* we will use ch to iterate over string1, so start it */
177 /* Create the scanner for string 1. */
178 s1
= str_create(1, argv
[0]);
179 for (ch
= 0; ch
< NCHARS
; ch
++) {
183 /* Create the scanner for string 2. */
184 s2
= str_create(2, argv
[1]);
186 /* Read the first char of string 2 first to make sure there is one. */
188 errx(1, "empty string2");
191 * Loop over the chars from string 1. After this loop string1[]
192 * is a mapping from input to output chars.
197 * Try each character in order. For characters we
198 * skip over because we aren't translating them,
199 * set the translation to the identity.
202 while (ch
< NCHARS
&& string1
[ch
] == 0) {
203 if (string1
[ch
] == 0) {
213 /* Get the next character from string 1. */
214 if (!next(s1
, &ch
)) {
219 /* Set the translation to the character from string 2. */
222 /* Note the characters to squeeze in string2[]. */
228 * Get the next character from string 2. If it runs
229 * out, this will keep returning the last character
230 * over and over again.
232 (void)next(s2
, &ch2
);
240 for (lastch
= OOBCH
; (ch
= getchar()) != EOF
;) {
242 if (!string2
[ch
] || lastch
!= ch
) {
248 while ((ch
= getchar()) != EOF
)
249 (void)putchar(string1
[ch
]);
251 /* Clean up and exit. */
260 setup(int *string
, const char *arg
, int whichstring
, int cflag
)
266 str
= str_create(whichstring
, arg
);
267 while (next(str
, &ch
))
270 for (p
= string
, cnt
= NCHARS
; cnt
--; ++p
)
278 (void)fprintf(stderr
, "usage: tr [-cs] string1 string2\n");
279 (void)fprintf(stderr
, " tr [-c] -d string1\n");
280 (void)fprintf(stderr
, " tr [-c] -s string1\n");
281 (void)fprintf(stderr
, " tr [-c] -ds string1 string2\n");