tr: support [:xdigit:], fix handling of ranges and [x]'s.
[oweals/busybox.git] / coreutils / tr.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini tr implementation for busybox
4  *
5  ** Copyright (c) 1987,1997, Prentice Hall   All rights reserved.
6  *
7  * The name of Prentice Hall may not be used to endorse or promote
8  * products derived from this software without specific prior
9  * written permission.
10  *
11  * Copyright (c) Michiel Huisjes
12  *
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.
15  *
16  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17  */
18 /* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
19  * TODO: xdigit, graph, print
20  */
21 #include "libbb.h"
22
23 #define ASCII 0377
24
25 static void map(char *pvector,
26                 unsigned char *string1, unsigned string1_len,
27                 unsigned char *string2, unsigned string2_len)
28 {
29         char last = '0';
30         unsigned i, j;
31
32         for (j = 0, i = 0; i < string1_len; i++) {
33                 if (string2_len <= j)
34                         pvector[string1[i]] = last;
35                 else
36                         pvector[string1[i]] = last = string2[j++];
37         }
38 }
39
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?)
45  */
46 static unsigned expand(const char *arg, char *buffer)
47 {
48         char *buffer_start = buffer;
49         unsigned i; /* can't be unsigned char: must be able to hold 256 */
50         unsigned char ac;
51
52         while (*arg) {
53                 if (*arg == '\\') {
54                         arg++;
55                         *buffer++ = bb_process_escape_sequence(&arg);
56                         continue;
57                 }
58                 if (arg[1] == '-') { /* "0-9..." */
59                         ac = arg[2];
60                         if (ac == '\0') { /* "0-": copy verbatim */
61                                 *buffer++ = *arg++; /* copy '0' */
62                                 continue; /* next iter will copy '-' and stop */
63                         }
64                         i = *arg;
65                         while (i <= ac) /* ok: i is unsigned _int_ */
66                                 *buffer++ = i++;
67                         arg += 3; /* skip 0-9 */
68                         continue;
69                 }
70                 if (*arg == '[') { /* "[xyz..." */
71                         arg++;
72                         i = *arg++;
73                         /* "[xyz...", i=x, arg points to y */
74                         if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
75 #define CLO ":]\0"
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
80                                         "xdigit"CLO;
81 #define CLASS_invalid 0 /* we increment the retval */
82 #define CLASS_alpha 1
83 #define CLASS_alnum 2
84 #define CLASS_digit 3
85 #define CLASS_lower 4
86 #define CLASS_upper 5
87 #define CLASS_space 6
88 #define CLASS_blank 7
89 #define CLASS_punct 8
90 #define CLASS_cntrl 9
91 #define CLASS_xdigit 10
92 //#define CLASS_graph 11
93 //#define CLASS_print 12
94                                 smalluint j;
95                                 {
96                                         /* xdigit needs 8, not 7 */
97                                         char *tmp = xstrndup(arg, 7 + (arg[0]=='x'));
98                                         j = index_in_strings(classes, tmp) + 1;
99                                         free(tmp);
100                                 }
101                                 if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
102                                         for (i = '0'; i <= '9'; i++)
103                                                 *buffer++ = i;
104                                 }
105                                 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
106                                         for (i = 'A'; i <= 'Z'; i++)
107                                                 *buffer++ = i;
108                                 }
109                                 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
110                                         for (i = 'a'; i <= 'z'; i++)
111                                                 *buffer++ = i;
112                                 }
113                                 if (j == CLASS_space || j == CLASS_blank) {
114                                         *buffer++ = '\t';
115                                         if (j == CLASS_space) {
116                                                 *buffer++ = '\n';
117                                                 *buffer++ = '\v';
118                                                 *buffer++ = '\f';
119                                                 *buffer++ = '\r';
120                                         }
121                                         *buffer++ = ' ';
122                                 }
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)))
127                                                         *buffer++ = i;
128                                 }
129                                 if (j == CLASS_xdigit) {
130                                         for (i = 'A'; i <= 'F'; i++) {
131                                                 *buffer++ = i;
132                                                 *buffer++ = i | 0x20;
133                                         }
134                                 }
135                                 if (j == CLASS_invalid) {
136                                         *buffer++ = '[';
137                                         *buffer++ = ':';
138                                         continue;
139                                 }
140                                 break;
141                         }
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] != ']')
146                                         bb_show_usage();
147                                 arg += 3;       /* skip CHAR=] */
148                                 continue;
149                         }
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.
155                          */
156                         *buffer++ = '[';
157                         arg--; /* points to x */
158                         continue;
159                 }
160                 *buffer++ = *arg++;
161         }
162         return (buffer - buffer_start);
163 }
164
165 static int complement(char *buffer, int buffer_len)
166 {
167         int ch, j, len;
168         char conv[ASCII + 2];
169
170         len = 0;
171         for (ch = '\0'; ch <= ASCII; ch++) {
172                 for (j = 0; j < buffer_len; j++)
173                         if (buffer[j] == ch)
174                                 goto next_ch;
175                 /* Didn't find it */
176                 conv[len++] = (char) ch;
177  next_ch:
178                 continue;
179         }
180         memcpy(buffer, conv, len);
181         return len;
182 }
183
184 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
185 int tr_main(int argc UNUSED_PARAM, char **argv)
186 {
187         int i;
188         smalluint flags;
189         ssize_t read_chars;
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;
197
198 #define TR_OPT_complement       (1 << 0)
199 #define TR_OPT_delete           (1 << 1)
200 #define TR_OPT_squeeze_reps     (1 << 2)
201
202         flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
203         argv += optind;
204
205         for (i = 0; i <= ASCII; i++) {
206                 vector[i] = i;
207                 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
208         }
209
210 #define tr_buf bb_common_bufsiz1
211         if (*argv != NULL) {
212                 int output_length = 0;
213                 int input_length;
214
215                 input_length = expand(*argv++, tr_buf);
216                 if (flags & TR_OPT_complement)
217                         input_length = complement(tr_buf, input_length);
218                 if (*argv) {
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);
223                 }
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;
228         }
229
230         goto start_from;
231
232         for (;;) {
233                 /* If we're out of input, flush output and read more input. */
234                 if ((ssize_t)in_index == read_chars) {
235                         if (out_index) {
236                                 xwrite(STDOUT_FILENO, (char *)output, out_index);
237  start_from:
238                                 out_index = 0;
239                         }
240                         read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ);
241                         if (read_chars <= 0) {
242                                 if (read_chars < 0)
243                                         bb_perror_msg_and_die(bb_msg_read_error);
244                                 break;
245                         }
246                         in_index = 0;
247                 }
248                 c = tr_buf[in_index++];
249                 if ((flags & TR_OPT_delete) && invec[c])
250                         continue;
251                 coded = vector[c];
252                 if ((flags & TR_OPT_squeeze_reps) && last == coded
253                  && (invec[c] || outvec[coded]))
254                         continue;
255                 output[out_index++] = last = coded;
256         }
257
258         return EXIT_SUCCESS;
259 }