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
19 * TODO: xdigit, graph, print
25 static void map(char *pvector,
26 unsigned char *string1, unsigned string1_len,
27 unsigned char *string2, unsigned string2_len)
32 for (j = 0, i = 0; i < string1_len; i++) {
34 pvector[string1[i]] = last;
36 pvector[string1[i]] = last = string2[j++];
40 /* supported constructs:
41 * Ranges, e.g., 0-9 ==> 0123456789
42 * Escapes, e.g., \a ==> Control-G
43 * Character classes, e.g. [:upper:] ==> A...Z
44 * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
46 static unsigned expand(const char *arg, char *buffer)
48 char *buffer_start = buffer;
49 unsigned i; /* can't be unsigned char: must be able to hold 256 */
55 *buffer++ = bb_process_escape_sequence(&arg);
58 if (arg[1] == '-') { /* "0-9..." */
60 if (ac == '\0') { /* "0-": copy verbatim */
61 *buffer++ = *arg++; /* copy '0' */
62 continue; /* next iter will copy '-' and stop */
65 while (i <= ac) /* ok: i is unsigned _int_ */
67 arg += 3; /* skip 0-9 */
70 if (*arg == '[') { /* "[xyz..." */
73 /* "[xyz...", i=x, arg points to y */
74 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
76 static const char classes[] ALIGN1 =
77 "alpha"CLO "alnum"CLO "digit"CLO
78 "lower"CLO "upper"CLO "space"CLO
79 "blank"CLO "punct"CLO "cntrl"CLO
81 #define CLASS_invalid 0 /* we increment the retval */
91 #define CLASS_xdigit 10
92 //#define CLASS_graph 11
93 //#define CLASS_print 12
96 /* xdigit needs 8, not 7 */
97 char *tmp = xstrndup(arg, 7 + (arg[0]=='x'));
98 j = index_in_strings(classes, tmp) + 1;
101 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
102 for (i = '0'; i <= '9'; i++)
105 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
106 for (i = 'A'; i <= 'Z'; i++)
109 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
110 for (i = 'a'; i <= 'z'; i++)
113 if (j == CLASS_space || j == CLASS_blank) {
115 if (j == CLASS_space) {
123 if (j == CLASS_punct || j == CLASS_cntrl) {
124 for (i = '\0'; i <= ASCII; i++)
125 if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
126 || (j == CLASS_cntrl && iscntrl(i)))
129 if (j == CLASS_xdigit) {
130 for (i = 'A'; i <= 'F'; i++) {
132 *buffer++ = i | 0x20;
135 if (j == CLASS_invalid) {
142 /* "[xyz...", i=x, arg points to y */
143 if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
144 *buffer++ = *arg; /* copy CHAR */
145 if (!*arg || arg[1] != '=' || arg[2] != ']')
147 arg += 3; /* skip CHAR=] */
150 /* The rest of [xyz... cases is treated as normal
151 * string, '[' has no special meaning here:
152 * tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z",
153 * also try tr "[a-z]" "_A-Z+" and you'll see that
154 * [] is not special here.
157 arg--; /* points to x */
162 return (buffer - buffer_start);
165 static int complement(char *buffer, int buffer_len)
168 char conv[ASCII + 2];
171 for (ch = '\0'; ch <= ASCII; ch++) {
172 for (j = 0; j < buffer_len; j++)
176 conv[len++] = (char) ch;
180 memcpy(buffer, conv, len);
184 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
185 int tr_main(int argc UNUSED_PARAM, char **argv)
190 size_t in_index, out_index;
191 unsigned last = UCHAR_MAX + 1; /* not equal to any char */
192 unsigned char coded, c;
193 unsigned char *output = xmalloc(BUFSIZ);
194 char *vector = xzalloc((ASCII+1) * 3);
195 char *invec = vector + (ASCII+1);
196 char *outvec = vector + (ASCII+1) * 2;
198 #define TR_OPT_complement (1 << 0)
199 #define TR_OPT_delete (1 << 1)
200 #define TR_OPT_squeeze_reps (1 << 2)
202 flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
205 for (i = 0; i <= ASCII; i++) {
207 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
210 #define tr_buf bb_common_bufsiz1
212 int output_length = 0;
215 input_length = expand(*argv++, tr_buf);
216 if (flags & TR_OPT_complement)
217 input_length = complement(tr_buf, input_length);
219 if (argv[0][0] == '\0')
220 bb_error_msg_and_die("STRING2 cannot be empty");
221 output_length = expand(*argv, (char *)output);
222 map(vector, (unsigned char *)tr_buf, input_length, output, output_length);
224 for (i = 0; i < input_length; i++)
225 invec[(unsigned char)tr_buf[i]] = TRUE;
226 for (i = 0; i < output_length; i++)
227 outvec[output[i]] = TRUE;
233 /* If we're out of input, flush output and read more input. */
234 if ((ssize_t)in_index == read_chars) {
236 xwrite(STDOUT_FILENO, (char *)output, out_index);
240 read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ);
241 if (read_chars <= 0) {
243 bb_perror_msg_and_die(bb_msg_read_error);
248 c = tr_buf[in_index++];
249 if ((flags & TR_OPT_delete) && invec[c])
252 if ((flags & TR_OPT_squeeze_reps) && last == coded
253 && (invec[c] || outvec[coded]))
255 output[out_index++] = last = coded;