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 static unsigned expand(const char *arg, char **buffer_p)
55 char *buffer = *buffer_p;
57 unsigned size = TR_BUFSIZ;
58 unsigned i; /* can't be unsigned char: must be able to hold 256 */
62 if (pos + ASCII > size) {
64 *buffer_p = buffer = xrealloc(buffer, size);
68 buffer[pos++] = bb_process_escape_sequence(&arg);
71 if (arg[1] == '-') { /* "0-9..." */
73 if (ac == '\0') { /* "0-": copy verbatim */
74 buffer[pos++] = *arg++; /* copy '0' */
75 continue; /* next iter will copy '-' and stop */
77 i = (unsigned char) *arg;
78 while (i <= ac) /* ok: i is unsigned _int_ */
80 arg += 3; /* skip 0-9 */
83 if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
87 i = (unsigned char) *arg++;
88 /* "[xyz...". i=x, arg points to y */
89 if (ENABLE_FEATURE_TR_CLASSES && i == ':') { /* [:class:] */
91 static const char classes[] ALIGN1 =
92 "alpha"CLO "alnum"CLO "digit"CLO
93 "lower"CLO "upper"CLO "space"CLO
94 "blank"CLO "punct"CLO "cntrl"CLO
97 CLASS_invalid = 0, /* we increment the retval */
114 /* xdigit needs 8, not 7 */
115 i = 7 + (arg[0] == 'x');
116 tmp = xstrndup(arg, i);
117 j = index_in_strings(classes, tmp) + 1;
120 if (j == CLASS_invalid)
124 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
125 for (i = '0'; i <= '9'; i++)
128 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
129 for (i = 'A'; i <= 'Z'; i++)
132 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
133 for (i = 'a'; i <= 'z'; i++)
136 if (j == CLASS_space || j == CLASS_blank) {
137 buffer[pos++] = '\t';
138 if (j == CLASS_space) {
139 buffer[pos++] = '\n';
140 buffer[pos++] = '\v';
141 buffer[pos++] = '\f';
142 buffer[pos++] = '\r';
146 if (j == CLASS_punct || j == CLASS_cntrl) {
147 for (i = '\0'; i < ASCII; i++) {
148 if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
149 || (j == CLASS_cntrl && iscntrl(i))
155 if (j == CLASS_xdigit) {
156 for (i = 'A'; i <= 'F'; i++) {
157 buffer[pos + 6] = i | 0x20;
164 /* "[xyz...", i=x, arg points to y */
165 if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
166 buffer[pos++] = *arg; /* copy CHAR */
167 if (!arg[0] || arg[1] != '=' || arg[2] != ']')
169 arg += 3; /* skip CHAR=] */
172 /* The rest of "[xyz..." cases is treated as normal
173 * string, "[" has no special meaning here:
174 * tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z",
175 * also try tr "[a-z]" "_A-Z+" and you'll see that
176 * [] is not special here.
179 arg -= 2; /* points to "[" in "[xyz..." */
181 buffer[pos++] = *arg++;
186 /* NB: buffer is guaranteed to be at least TR_BUFSIZE
187 * (which is >= ASCII) big.
189 static int complement(char *buffer, int buffer_len)
198 if (memchr(buffer, ch, buffer_len) == NULL)
203 memcpy(buffer, conv, len);
207 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
208 int tr_main(int argc UNUSED_PARAM, char **argv)
213 size_t in_index, out_index;
214 unsigned last = UCHAR_MAX + 1; /* not equal to any char */
215 unsigned char coded, c;
216 char *str1 = xmalloc(TR_BUFSIZ);
217 char *str2 = xmalloc(TR_BUFSIZ);
220 char *vector = xzalloc(ASCII * 3);
221 char *invec = vector + ASCII;
222 char *outvec = vector + ASCII * 2;
224 #define TR_OPT_complement (1 << 0)
225 #define TR_OPT_delete (1 << 1)
226 #define TR_OPT_squeeze_reps (1 << 2)
228 for (i = 0; i < ASCII; i++) {
230 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
233 opt_complementary = "-1";
234 flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
237 str1_length = expand(*argv++, &str1);
239 if (flags & TR_OPT_complement)
240 str1_length = complement(str1, str1_length);
242 if (argv[0][0] == '\0')
243 bb_error_msg_and_die("STRING2 cannot be empty");
244 str2_length = expand(*argv, &str2);
245 map(vector, str1, str1_length,
248 for (i = 0; i < str1_length; i++)
249 invec[(unsigned char)(str1[i])] = TRUE;
250 for (i = 0; i < str2_length; i++)
251 outvec[(unsigned char)(str2[i])] = TRUE;
255 /* In this loop, str1 space is reused as input buffer,
256 * str2 - as output one. */
258 /* If we're out of input, flush output and read more input. */
259 if ((ssize_t)in_index == read_chars) {
261 xwrite(STDOUT_FILENO, str2, out_index);
265 read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
266 if (read_chars <= 0) {
268 bb_perror_msg_and_die(bb_msg_read_error);
273 c = str1[in_index++];
274 if ((flags & TR_OPT_delete) && invec[c])
277 if ((flags & TR_OPT_squeeze_reps) && last == coded
278 && (invec[c] || outvec[coded])
282 str2[out_index++] = last = coded;