1 /* vi: set sw=4 ts=4: */
3 * Mini tr implementation for busybox
5 * Copyright (c) 1987,1997, Prentice Hall All rights reserved.
7 * The name of Prentice Hall may not be used to endorse or promote
8 * products derived from this software without specific prior
11 * Copyright (c) Michiel Huisjes
13 * This version of tr is adapted from Minix tr and was modified
14 * by Erik Andersen <andersen@codepoet.org> to be used in busybox.
16 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
18 /* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
22 //config: bool "tr (5.1 kb)"
25 //config: tr is used to squeeze, and/or delete characters from standard
26 //config: input, writing to standard output.
28 //config:config FEATURE_TR_CLASSES
29 //config: bool "Enable character classes (such as [:upper:])"
31 //config: depends on TR
33 //config: Enable character classes, enabling commands such as:
34 //config: tr [:upper:] [:lower:] to convert input into lowercase.
36 //config:config FEATURE_TR_EQUIV
37 //config: bool "Enable equivalence classes"
39 //config: depends on TR
41 //config: Enable equivalence classes, which essentially add the enclosed
42 //config: character to the current set. For instance, tr [=a=] xyz would
43 //config: replace all instances of 'a' with 'xyz'. This option is mainly
44 //config: useful for cases when no other way of expressing a character
45 //config: is possible.
47 //applet:IF_TR(APPLET(tr, BB_DIR_USR_BIN, BB_SUID_DROP))
49 //kbuild:lib-$(CONFIG_TR) += tr.o
51 //usage:#define tr_trivial_usage
52 //usage: "[-cds] STRING1 [STRING2]"
53 //usage:#define tr_full_usage "\n\n"
54 //usage: "Translate, squeeze, or delete characters from stdin, writing to stdout\n"
55 //usage: "\n -c Take complement of STRING1"
56 //usage: "\n -d Delete input characters coded STRING1"
57 //usage: "\n -s Squeeze multiple output characters of STRING2 into one character"
59 //usage:#define tr_example_usage
60 //usage: "$ echo \"gdkkn vnqkc\" | tr [a-y] [b-z]\n"
61 //usage: "hello world\n"
67 /* string buffer needs to be at least as big as the whole "alphabet".
68 * BUFSIZ == ASCII is ok, but we will realloc in expand
69 * even for smallest patterns, let's avoid that by using *2:
71 TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2,
74 static void map(char *pvector,
75 char *string1, unsigned string1_len,
76 char *string2, unsigned string2_len)
81 for (j = 0, i = 0; i < string1_len; i++) {
83 pvector[(unsigned char)(string1[i])] = last;
85 pvector[(unsigned char)(string1[i])] = last = string2[j++];
89 /* supported constructs:
90 * Ranges, e.g., 0-9 ==> 0123456789
91 * Escapes, e.g., \a ==> Control-G
92 * Character classes, e.g. [:upper:] ==> A...Z
93 * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
95 * [x*N] - repeat char x N times
96 * [x*] - repeat char x until it fills STRING2:
97 * # echo qwe123 | /usr/bin/tr 123456789 '[d]'
99 * # echo qwe123 | /usr/bin/tr 123456789 '[d*]'
102 static unsigned expand(char *arg, char **buffer_p)
104 char *buffer = *buffer_p;
106 unsigned size = TR_BUFSIZ;
107 unsigned i; /* can't be unsigned char: must be able to hold 256 */
111 if (pos + ASCII > size) {
113 *buffer_p = buffer = xrealloc(buffer, size);
119 ac = bb_process_escape_sequence(&z);
124 * fall through, there may be a range.
125 * If not, current char will be treated anyway.
128 if (arg[1] == '-') { /* "0-9..." */
130 if (ac == '\0') { /* "0-": copy verbatim */
131 buffer[pos++] = *arg++; /* copy '0' */
132 continue; /* next iter will copy '-' and stop */
134 i = (unsigned char) *arg;
135 arg += 3; /* skip 0-9 or 0-\ */
139 ac = bb_process_escape_sequence(&z);
142 while (i <= ac) /* ok: i is unsigned _int_ */
146 if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
150 i = (unsigned char) *arg++;
151 /* "[xyz...". i=x, arg points to y */
152 if (ENABLE_FEATURE_TR_CLASSES && i == ':') { /* [:class:] */
154 static const char classes[] ALIGN1 =
155 "alpha"CLO "alnum"CLO "digit"CLO
156 "lower"CLO "upper"CLO "space"CLO
157 "blank"CLO "punct"CLO "cntrl"CLO
160 CLASS_invalid = 0, /* we increment the retval */
177 /* xdigit needs 8, not 7 */
178 i = 7 + (arg[0] == 'x');
179 tmp = xstrndup(arg, i);
180 j = index_in_strings(classes, tmp) + 1;
183 if (j == CLASS_invalid)
187 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
188 for (i = '0'; i <= '9'; i++)
191 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
192 for (i = 'A'; i <= 'Z'; i++)
195 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
196 for (i = 'a'; i <= 'z'; i++)
199 if (j == CLASS_space || j == CLASS_blank) {
200 buffer[pos++] = '\t';
201 if (j == CLASS_space) {
202 buffer[pos++] = '\n';
203 buffer[pos++] = '\v';
204 buffer[pos++] = '\f';
205 buffer[pos++] = '\r';
209 if (j == CLASS_punct || j == CLASS_cntrl) {
210 for (i = '\0'; i < ASCII; i++) {
211 if ((j == CLASS_punct && isprint_asciionly(i) && !isalnum(i) && !isspace(i))
212 || (j == CLASS_cntrl && iscntrl(i))
218 if (j == CLASS_xdigit) {
219 for (i = 'A'; i <= 'F'; i++) {
220 buffer[pos + 6] = i | 0x20;
227 /* "[xyz...", i=x, arg points to y */
228 if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
229 buffer[pos++] = *arg; /* copy CHAR */
230 if (!arg[0] || arg[1] != '=' || arg[2] != ']')
232 arg += 3; /* skip CHAR=] */
235 /* The rest of "[xyz..." cases is treated as normal
236 * string, "[" has no special meaning here:
237 * tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z",
238 * also try tr "[a-z]" "_A-Z+" and you'll see that
239 * [] is not special here.
242 arg -= 2; /* points to "[" in "[xyz..." */
244 buffer[pos++] = *arg++;
249 /* NB: buffer is guaranteed to be at least TR_BUFSIZE
250 * (which is >= ASCII) big.
252 static int complement(char *buffer, int buffer_len)
261 if (memchr(buffer, ch, buffer_len) == NULL)
266 memcpy(buffer, conv, len);
270 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
271 int tr_main(int argc UNUSED_PARAM, char **argv)
276 size_t in_index, out_index;
277 unsigned last = UCHAR_MAX + 1; /* not equal to any char */
278 unsigned char coded, c;
279 char *str1 = xmalloc(TR_BUFSIZ);
280 char *str2 = xmalloc(TR_BUFSIZ);
283 char *vector = xzalloc(ASCII * 3);
284 char *invec = vector + ASCII;
285 char *outvec = vector + ASCII * 2;
287 #define TR_OPT_complement (3 << 0)
288 #define TR_OPT_delete (1 << 2)
289 #define TR_OPT_squeeze_reps (1 << 3)
291 for (i = 0; i < ASCII; i++) {
293 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
296 /* -C/-c difference is that -C complements "characters",
297 * and -c complements "values" (binary bytes I guess).
298 * In POSIX locale, these are the same.
301 /* '+': stop at first non-option */
302 opts = getopt32(argv, "^+" "Ccds" "\0" "-1");
305 str1_length = expand(*argv++, &str1);
307 if (opts & TR_OPT_complement)
308 str1_length = complement(str1, str1_length);
310 if (argv[0][0] == '\0')
311 bb_simple_error_msg_and_die("STRING2 cannot be empty");
312 str2_length = expand(*argv, &str2);
313 map(vector, str1, str1_length,
316 for (i = 0; i < str1_length; i++)
317 invec[(unsigned char)(str1[i])] = TRUE;
318 for (i = 0; i < str2_length; i++)
319 outvec[(unsigned char)(str2[i])] = TRUE;
323 /* In this loop, str1 space is reused as input buffer,
324 * str2 - as output one. */
326 /* If we're out of input, flush output and read more input. */
327 if ((ssize_t)in_index == read_chars) {
329 xwrite(STDOUT_FILENO, str2, out_index);
333 read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
334 if (read_chars <= 0) {
336 bb_simple_perror_msg_and_die(bb_msg_read_error);
341 c = str1[in_index++];
342 if ((opts & TR_OPT_delete) && invec[c])
345 if ((opts & TR_OPT_squeeze_reps) && last == coded
346 && (invec[c] || outvec[coded])
350 str2[out_index++] = last = coded;
353 if (ENABLE_FEATURE_CLEAN_UP) {