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 tarball for details.
18 /* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
25 /* string buffer needs to be at least as big as the whole "alphabet".
26 * BUFSIZ == ASCII is ok, but we will realloc in expand
27 * even for smallest patterns, let's avoid that by using *2:
29 TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2,
32 static void map(char *pvector,
33 char *string1, unsigned string1_len,
34 char *string2, unsigned string2_len)
39 for (j = 0, i = 0; i < string1_len; i++) {
41 pvector[(unsigned char)(string1[i])] = last;
43 pvector[(unsigned char)(string1[i])] = last = string2[j++];
47 /* supported constructs:
48 * Ranges, e.g., 0-9 ==> 0123456789
49 * Escapes, e.g., \a ==> Control-G
50 * Character classes, e.g. [:upper:] ==> A...Z
51 * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
53 * \ooo-\ooo - octal ranges
54 * [x*N] - repeat char x N times
55 * [x*] - repeat char x until it fills STRING2:
56 * # echo qwe123 | /usr/bin/tr 123456789 '[d]'
58 * # echo qwe123 | /usr/bin/tr 123456789 '[d*]'
61 static unsigned expand(const char *arg, char **buffer_p)
63 char *buffer = *buffer_p;
65 unsigned size = TR_BUFSIZ;
66 unsigned i; /* can't be unsigned char: must be able to hold 256 */
70 if (pos + ASCII > size) {
72 *buffer_p = buffer = xrealloc(buffer, size);
76 buffer[pos++] = bb_process_escape_sequence(&arg);
79 if (arg[1] == '-') { /* "0-9..." */
81 if (ac == '\0') { /* "0-": copy verbatim */
82 buffer[pos++] = *arg++; /* copy '0' */
83 continue; /* next iter will copy '-' and stop */
85 i = (unsigned char) *arg;
86 while (i <= ac) /* ok: i is unsigned _int_ */
88 arg += 3; /* skip 0-9 */
91 if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
95 i = (unsigned char) *arg++;
96 /* "[xyz...". i=x, arg points to y */
97 if (ENABLE_FEATURE_TR_CLASSES && i == ':') { /* [:class:] */
99 static const char classes[] ALIGN1 =
100 "alpha"CLO "alnum"CLO "digit"CLO
101 "lower"CLO "upper"CLO "space"CLO
102 "blank"CLO "punct"CLO "cntrl"CLO
105 CLASS_invalid = 0, /* we increment the retval */
122 /* xdigit needs 8, not 7 */
123 i = 7 + (arg[0] == 'x');
124 tmp = xstrndup(arg, i);
125 j = index_in_strings(classes, tmp) + 1;
128 if (j == CLASS_invalid)
132 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
133 for (i = '0'; i <= '9'; i++)
136 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
137 for (i = 'A'; i <= 'Z'; i++)
140 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
141 for (i = 'a'; i <= 'z'; i++)
144 if (j == CLASS_space || j == CLASS_blank) {
145 buffer[pos++] = '\t';
146 if (j == CLASS_space) {
147 buffer[pos++] = '\n';
148 buffer[pos++] = '\v';
149 buffer[pos++] = '\f';
150 buffer[pos++] = '\r';
154 if (j == CLASS_punct || j == CLASS_cntrl) {
155 for (i = '\0'; i < ASCII; i++) {
156 if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
157 || (j == CLASS_cntrl && iscntrl(i))
163 if (j == CLASS_xdigit) {
164 for (i = 'A'; i <= 'F'; i++) {
165 buffer[pos + 6] = i | 0x20;
172 /* "[xyz...", i=x, arg points to y */
173 if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
174 buffer[pos++] = *arg; /* copy CHAR */
175 if (!arg[0] || arg[1] != '=' || arg[2] != ']')
177 arg += 3; /* skip CHAR=] */
180 /* The rest of "[xyz..." cases is treated as normal
181 * string, "[" has no special meaning here:
182 * tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z",
183 * also try tr "[a-z]" "_A-Z+" and you'll see that
184 * [] is not special here.
187 arg -= 2; /* points to "[" in "[xyz..." */
189 buffer[pos++] = *arg++;
194 /* NB: buffer is guaranteed to be at least TR_BUFSIZE
195 * (which is >= ASCII) big.
197 static int complement(char *buffer, int buffer_len)
206 if (memchr(buffer, ch, buffer_len) == NULL)
211 memcpy(buffer, conv, len);
215 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
216 int tr_main(int argc UNUSED_PARAM, char **argv)
221 size_t in_index, out_index;
222 unsigned last = UCHAR_MAX + 1; /* not equal to any char */
223 unsigned char coded, c;
224 char *str1 = xmalloc(TR_BUFSIZ);
225 char *str2 = xmalloc(TR_BUFSIZ);
228 char *vector = xzalloc(ASCII * 3);
229 char *invec = vector + ASCII;
230 char *outvec = vector + ASCII * 2;
232 #define TR_OPT_complement (3 << 0)
233 #define TR_OPT_delete (1 << 2)
234 #define TR_OPT_squeeze_reps (1 << 3)
236 for (i = 0; i < ASCII; i++) {
238 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
241 /* -C/-c difference is that -C complements "characters",
242 * and -c complements "values" (binary bytes I guess).
243 * In POSIX locale, these are the same.
246 opt_complementary = "-1";
247 opts = getopt32(argv, "+Ccds"); /* '+': stop at first non-option */
250 str1_length = expand(*argv++, &str1);
252 if (opts & TR_OPT_complement)
253 str1_length = complement(str1, str1_length);
255 if (argv[0][0] == '\0')
256 bb_error_msg_and_die("STRING2 cannot be empty");
257 str2_length = expand(*argv, &str2);
258 map(vector, str1, str1_length,
261 for (i = 0; i < str1_length; i++)
262 invec[(unsigned char)(str1[i])] = TRUE;
263 for (i = 0; i < str2_length; i++)
264 outvec[(unsigned char)(str2[i])] = TRUE;
268 /* In this loop, str1 space is reused as input buffer,
269 * str2 - as output one. */
271 /* If we're out of input, flush output and read more input. */
272 if ((ssize_t)in_index == read_chars) {
274 xwrite(STDOUT_FILENO, str2, out_index);
278 read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
279 if (read_chars <= 0) {
281 bb_perror_msg_and_die(bb_msg_read_error);
286 c = str1[in_index++];
287 if ((opts & TR_OPT_delete) && invec[c])
290 if ((opts & TR_OPT_squeeze_reps) && last == coded
291 && (invec[c] || outvec[coded])
295 str2[out_index++] = last = coded;