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.
21 // Even with -funsigned-char, gcc still complains about char as an array index.
23 #define GCC4_IS_STUPID int
27 /* some "globals" shared across this file */
28 static char com_fl, del_fl, sq_fl;
29 /* these last are pointers to static buffers declared in tr_main */
30 static char *poutput, *pvector, *pinvec, *poutvec;
32 static void convert(void)
34 int read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
37 // If we're out of input, flush output and read more input.
39 if (in_index == read_chars) {
41 if (write(1, (char *) poutput, out_index) != out_index)
42 bb_error_msg_and_die(bb_msg_write_error);
46 if ((read_chars = read(0, bb_common_bufsiz1, BUFSIZ)) <= 0) {
47 if (write(1, (char *) poutput, out_index) != out_index)
48 bb_error_msg(bb_msg_write_error);
53 c = bb_common_bufsiz1[in_index++];
55 if (del_fl && pinvec[c])
57 if (sq_fl && last == coded && (pinvec[c] || poutvec[coded]))
59 poutput[out_index++] = last = coded;
65 static void map(char *string1, unsigned int string1_len,
66 char *string2, unsigned int string2_len)
71 for (j = 0, i = 0; i < string1_len; i++) {
73 pvector[(GCC4_IS_STUPID)string1[i]] = last;
75 pvector[(GCC4_IS_STUPID)string1[i]] = last = string2[j++];
79 /* supported constructs:
80 * Ranges, e.g., [0-9] ==> 0123456789
81 * Escapes, e.g., \a ==> Control-G
82 * Character classes, e.g. [:upper:] ==> A ... Z
84 static unsigned int expand(const char *arg, char *buffer)
86 char *buffer_start = buffer;
92 *buffer++ = bb_process_escape_sequence(&arg);
93 } else if (*(arg+1) == '-') {
102 arg += 3; /* Skip the assumed a-z */
103 } else if (*arg == '[') {
106 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
107 if (strncmp(arg, "alpha", 5) == 0) {
108 for (i = 'A'; i <= 'Z'; i++)
110 for (i = 'a'; i <= 'z'; i++)
113 else if (strncmp(arg, "alnum", 5) == 0) {
114 for (i = '0'; i <= '9'; i++)
116 for (i = 'A'; i <= 'Z'; i++)
118 for (i = 'a'; i <= 'z'; i++)
121 else if (strncmp(arg, "digit", 5) == 0)
122 for (i = '0'; i <= '9'; i++)
124 else if (strncmp(arg, "lower", 5) == 0)
125 for (i = 'a'; i <= 'z'; i++)
127 else if (strncmp(arg, "upper", 5) == 0)
128 for (i = 'A'; i <= 'Z'; i++)
130 else if (strncmp(arg, "space", 5) == 0) {
131 const char s[] = "\t\n\v\f\r ";
132 strcat((char*)buffer, s);
133 buffer += sizeof(s) - 1;
135 else if (strncmp(arg, "blank", 5) == 0) {
139 /* gcc gives a warning if braces aren't used here */
140 else if (strncmp(arg, "punct", 5) == 0) {
141 for (i = 0; i <= ASCII; i++)
142 if (isprint(i) && (!isalnum(i)) && (!isspace(i)))
145 else if (strncmp(arg, "cntrl", 5) == 0) {
146 for (i = 0; i <= ASCII; i++)
157 if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
159 /* skip the closing =] */
171 arg++; /* Skip the assumed ']' */
176 return (buffer - buffer_start);
179 static int complement(char *buffer, int buffer_len)
182 char conv[ASCII + 2];
185 for (i = 0; i <= ASCII; i++) {
186 for (j = 0; j < buffer_len; j++)
190 conv[ix++] = i & ASCII;
192 memcpy(buffer, conv, ix);
196 int tr_main(int argc, char **argv);
197 int tr_main(int argc, char **argv)
200 int output_length=0, input_length;
203 RESERVE_CONFIG_BUFFER(output, BUFSIZ);
204 RESERVE_CONFIG_BUFFER(vector, ASCII+1);
205 RESERVE_CONFIG_BUFFER(invec, ASCII+1);
206 RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
208 /* ... but make them available globally */
214 if (argc > 1 && argv[idx][0] == '-') {
215 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
232 for (i = 0; i <= ASCII; i++) {
234 invec[i] = outvec[i] = FALSE;
237 if (argv[idx] != NULL) {
238 input_length = expand(argv[idx++], bb_common_bufsiz1);
240 input_length = complement(bb_common_bufsiz1, input_length);
241 if (argv[idx] != NULL) {
242 if (*argv[idx] == '\0')
243 bb_error_msg_and_die("STRING2 cannot be empty");
244 output_length = expand(argv[idx], output);
245 map(bb_common_bufsiz1, input_length, output, output_length);
247 for (i = 0; i < input_length; i++)
248 invec[(GCC4_IS_STUPID)bb_common_bufsiz1[i]] = TRUE;
249 for (i = 0; i < output_length; i++)
250 outvec[(GCC4_IS_STUPID)output[i]] = TRUE;