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.
23 /* some "globals" shared across this file */
24 static char com_fl, del_fl, sq_fl;
25 /* these last are pointers to static buffers declared in tr_main */
26 static unsigned char *poutput;
27 static unsigned char *pvector;
28 static unsigned char *pinvec, *poutvec;
30 static void convert(void)
32 int read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1;
35 // If we're out of input, flush output and read more input.
37 if (in_index == read_chars) {
39 if (write(1, (char *) poutput, out_index) != out_index)
40 bb_error_msg_and_die(bb_msg_write_error);
44 if ((read_chars = read(0, bb_common_bufsiz1, BUFSIZ)) <= 0) {
45 if (write(1, (char *) poutput, out_index) != out_index)
46 bb_error_msg(bb_msg_write_error);
51 c = bb_common_bufsiz1[in_index++];
53 if (del_fl && pinvec[c])
55 if (sq_fl && last == coded && (pinvec[c] || poutvec[coded]))
57 poutput[out_index++] = last = coded;
63 static void map(register unsigned char *string1, unsigned int string1_len,
64 register unsigned char *string2, unsigned int string2_len)
66 unsigned char last = '0';
69 for (j = 0, i = 0; i < string1_len; i++) {
71 pvector[string1[i]] = last;
73 pvector[string1[i]] = last = string2[j++];
77 /* supported constructs:
78 * Ranges, e.g., [0-9] ==> 0123456789
79 * Escapes, e.g., \a ==> Control-G
80 * Character classes, e.g. [:upper:] ==> A ... Z
82 static unsigned int expand(const char *arg, register unsigned char *buffer)
84 unsigned char *buffer_start = buffer;
90 *buffer++ = bb_process_escape_sequence(&arg);
91 } else if (*(arg+1) == '-') {
100 arg += 3; /* Skip the assumed a-z */
101 } else if (*arg == '[') {
104 if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
105 if (strncmp(arg, "alpha", 5) == 0) {
106 for (i = 'A'; i <= 'Z'; i++)
108 for (i = 'a'; i <= 'z'; i++)
111 else if (strncmp(arg, "alnum", 5) == 0) {
112 for (i = '0'; i <= '9'; i++)
114 for (i = 'A'; i <= 'Z'; i++)
116 for (i = 'a'; i <= 'z'; i++)
119 else if (strncmp(arg, "digit", 5) == 0)
120 for (i = '0'; i <= '9'; i++)
122 else if (strncmp(arg, "lower", 5) == 0)
123 for (i = 'a'; i <= 'z'; i++)
125 else if (strncmp(arg, "upper", 5) == 0)
126 for (i = 'A'; i <= 'Z'; i++)
128 else if (strncmp(arg, "space", 5) == 0) {
129 const char s[] = "\t\n\v\f\r ";
130 strcat((char*)buffer, s);
131 buffer += sizeof(s) - 1;
133 else if (strncmp(arg, "blank", 5) == 0) {
137 /* gcc gives a warning if braces aren't used here */
138 else if (strncmp(arg, "punct", 5) == 0) {
139 for (i = 0; i <= ASCII; i++)
140 if (isprint(i) && (!isalnum(i)) && (!isspace(i)))
143 else if (strncmp(arg, "cntrl", 5) == 0) {
144 for (i = 0; i <= ASCII; i++)
155 if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
157 /* skip the closing =] */
169 arg++; /* Skip the assumed ']' */
174 return (buffer - buffer_start);
177 static int complement(unsigned char *buffer, int buffer_len)
179 register short i, j, ix;
180 char conv[ASCII + 2];
183 for (i = 0; i <= ASCII; i++) {
184 for (j = 0; j < buffer_len; j++)
188 conv[ix++] = i & ASCII;
190 memcpy(buffer, conv, ix);
194 int tr_main(int argc, char **argv)
196 register unsigned char *ptr;
197 int output_length=0, input_length;
200 RESERVE_CONFIG_BUFFER(output, BUFSIZ);
201 RESERVE_CONFIG_BUFFER(vector, ASCII+1);
202 RESERVE_CONFIG_BUFFER(invec, ASCII+1);
203 RESERVE_CONFIG_BUFFER(outvec, ASCII+1);
205 /* ... but make them available globally */
206 poutput = (unsigned char*)output;
207 pvector = (unsigned char*)vector;
208 pinvec = (unsigned char*)invec;
209 poutvec = (unsigned char*)outvec;
211 if (argc > 1 && argv[idx][0] == '-') {
212 for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) {
229 for (i = 0; i <= ASCII; i++) {
231 invec[i] = outvec[i] = FALSE;
234 if (argv[idx] != NULL) {
235 input_length = expand(argv[idx++], bb_common_bufsiz1);
237 input_length = complement(bb_common_bufsiz1, input_length);
238 if (argv[idx] != NULL) {
239 if (*argv[idx] == '\0')
240 bb_error_msg_and_die("STRING2 cannot be empty");
241 output_length = expand(argv[idx], (unsigned char*)output);
242 map(bb_common_bufsiz1, input_length, (unsigned char*)output, output_length);
244 for (i = 0; i < input_length; i++)
245 invec[bb_common_bufsiz1[i]] = TRUE;
246 for (i = 0; i < output_length; i++)
247 outvec[(unsigned char)output[i]] = TRUE;