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 int string1_len,
27 unsigned char *string2, unsigned int 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 * Ranges, e.g., [0-9] ==> 0123456789
43 * Escapes, e.g., \a ==> Control-G
44 * Character classes, e.g. [:upper:] ==> A...Z
45 * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
47 static unsigned int expand(const char *arg, char *buffer)
49 char *buffer_start = buffer;
50 unsigned i; /* can't be unsigned char: must be able to hold 256 */
56 *buffer++ = bb_process_escape_sequence(&arg);
59 if (arg[1] == '-') { /* "0-9..." */
61 if (ac == '\0') { /* "0-": copy verbatim */
62 *buffer++ = *arg++; /* copy '0' */
63 continue; /* next iter will copy '-' and stop */
66 while (i <= ac) /* ok: i is unsigned _int_ */
68 arg += 3; /* skip 0-9 */
71 if (*arg == '[') { /* "[xyz..." */
74 /* "[xyz...", i=x, arg points to y */
75 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
77 static const char classes[] ALIGN1 =
78 "alpha"CLO "alnum"CLO "digit"CLO
79 "lower"CLO "upper"CLO "space"CLO
80 "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
95 { /* not really pretty.. */
96 char *tmp = xstrndup(arg, 7); // warning: xdigit would need 8, not 7
97 j = index_in_strings(classes, tmp) + 1;
100 if (j == CLASS_alnum || j == CLASS_digit) {
101 for (i = '0'; i <= '9'; i++)
104 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
105 for (i = 'A'; i <= 'Z'; i++)
108 if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
109 for (i = 'a'; i <= 'z'; i++)
112 if (j == CLASS_space || j == CLASS_blank) {
114 if (j == CLASS_space) {
122 if (j == CLASS_punct || j == CLASS_cntrl) {
123 for (i = '\0'; i <= ASCII; i++)
124 if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i))
125 || (j == CLASS_cntrl && iscntrl(i)))
128 if (j == CLASS_invalid) {
135 /* "[xyz...", i=x, arg points to y */
136 if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
137 *buffer++ = *arg; /* copy CHAR */
138 if (!*arg || arg[1] != '=' || arg[2] != ']')
140 arg += 3; /* skip CHAR=] */
143 if (i == '\0' || *arg != '-') { /* not [x-...] - copy verbatim */
145 arg--; /* points to x */
146 continue; /* copy all, including eventual ']' */
153 arg++; /* skip the assumed ']' */
158 return (buffer - buffer_start);
161 static int complement(char *buffer, int buffer_len)
164 char conv[ASCII + 2];
167 for (i = '\0'; i <= ASCII; i++) {
168 for (j = 0; j < buffer_len; j++)
172 conv[ix++] = i & ASCII;
174 memcpy(buffer, conv, ix);
178 int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
179 int tr_main(int argc UNUSED_PARAM, char **argv)
181 int output_length = 0, input_length;
184 ssize_t read_chars = 0;
185 size_t in_index = 0, out_index = 0;
186 unsigned last = UCHAR_MAX + 1; /* not equal to any char */
187 unsigned char coded, c;
188 unsigned char *output = xmalloc(BUFSIZ);
189 char *vector = xzalloc((ASCII+1) * 3);
190 char *invec = vector + (ASCII+1);
191 char *outvec = vector + (ASCII+1) * 2;
193 #define TR_OPT_complement (1 << 0)
194 #define TR_OPT_delete (1 << 1)
195 #define TR_OPT_squeeze_reps (1 << 2)
197 flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */
200 for (i = 0; i <= ASCII; i++) {
202 /*invec[i] = outvec[i] = FALSE; - done by xzalloc */
205 #define tr_buf bb_common_bufsiz1
207 input_length = expand(*argv++, tr_buf);
208 if (flags & TR_OPT_complement)
209 input_length = complement(tr_buf, input_length);
211 if (argv[0][0] == '\0')
212 bb_error_msg_and_die("STRING2 cannot be empty");
213 output_length = expand(*argv, (char *)output);
214 map(vector, (unsigned char *)tr_buf, input_length, output, output_length);
216 for (i = 0; i < input_length; i++)
217 invec[(unsigned char)tr_buf[i]] = TRUE;
218 for (i = 0; i < output_length; i++)
219 outvec[output[i]] = TRUE;
223 /* If we're out of input, flush output and read more input. */
224 if ((ssize_t)in_index == read_chars) {
226 xwrite(STDOUT_FILENO, (char *)output, out_index);
229 read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ);
230 if (read_chars <= 0) {
232 bb_perror_msg_and_die(bb_msg_read_error);
237 c = tr_buf[in_index++];
239 if ((flags & TR_OPT_delete) && invec[c])
241 if ((flags & TR_OPT_squeeze_reps) && last == coded
242 && (invec[c] || outvec[coded]))
244 output[out_index++] = last = coded;