Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 3 | * Mini tr implementation for busybox |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 4 | * |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 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 | * |
Erik Andersen | 5afc864 | 2000-05-02 00:07:56 +0000 | [diff] [blame] | 11 | * Copyright (c) Michiel Huisjes |
| 12 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * This version of tr is adapted from Minix tr and was modified |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 14 | * by Erik Andersen <andersen@codepoet.org> to be used in busybox. |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 15 | * |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 16 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 19 | #include "busybox.h" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 20 | |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 21 | // Even with -funsigned-char, gcc still complains about char as an array index. |
| 22 | |
| 23 | #define GCC4_IS_STUPID int |
| 24 | |
Eric Andersen | 22ecf04 | 2001-07-02 17:32:40 +0000 | [diff] [blame] | 25 | #define ASCII 0377 |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 26 | |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 27 | /* some "globals" shared across this file */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 28 | static char com_fl, del_fl, sq_fl; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 29 | /* these last are pointers to static buffers declared in tr_main */ |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 30 | static char *poutput, *pvector, *pinvec, *poutvec; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 31 | |
Eric Andersen | eaecbf3 | 2001-10-31 10:41:31 +0000 | [diff] [blame] | 32 | static void convert(void) |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 33 | { |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 34 | int read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 35 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 36 | for (;;) { |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 37 | // If we're out of input, flush output and read more input. |
| 38 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 39 | if (in_index == read_chars) { |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 40 | if (out_index) { |
| 41 | if (write(1, (char *) poutput, out_index) != out_index) |
| 42 | bb_error_msg_and_die(bb_msg_write_error); |
| 43 | out_index = 0; |
| 44 | } |
| 45 | |
| 46 | if ((read_chars = read(0, bb_common_bufsiz1, BUFSIZ)) <= 0) { |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 47 | if (write(1, (char *) poutput, out_index) != out_index) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | bb_error_msg(bb_msg_write_error); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 49 | exit(0); |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 50 | } |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 51 | in_index = 0; |
| 52 | } |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 53 | c = bb_common_bufsiz1[in_index++]; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 54 | coded = pvector[c]; |
| 55 | if (del_fl && pinvec[c]) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 56 | continue; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 57 | if (sq_fl && last == coded && (pinvec[c] || poutvec[coded])) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 58 | continue; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 59 | poutput[out_index++] = last = coded; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 62 | /* NOTREACHED */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 65 | static void map(char *string1, unsigned int string1_len, |
| 66 | char *string2, unsigned int string2_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 67 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 68 | char last = '0'; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 69 | unsigned int i, j; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 70 | |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 71 | for (j = 0, i = 0; i < string1_len; i++) { |
| 72 | if (string2_len <= j) |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 73 | pvector[(GCC4_IS_STUPID)string1[i]] = last; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 74 | else |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 75 | pvector[(GCC4_IS_STUPID)string1[i]] = last = string2[j++]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 79 | /* supported constructs: |
| 80 | * Ranges, e.g., [0-9] ==> 0123456789 |
| 81 | * Escapes, e.g., \a ==> Control-G |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 82 | * Character classes, e.g. [:upper:] ==> A ... Z |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 83 | */ |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 84 | static unsigned int expand(const char *arg, char *buffer) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 85 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 86 | char *buffer_start = buffer; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 87 | int i, ac; |
| 88 | |
| 89 | while (*arg) { |
| 90 | if (*arg == '\\') { |
| 91 | arg++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | *buffer++ = bb_process_escape_sequence(&arg); |
Eric Andersen | 5a4a46a | 2001-07-09 21:32:29 +0000 | [diff] [blame] | 93 | } else if (*(arg+1) == '-') { |
| 94 | ac = *(arg+2); |
| 95 | if(ac == 0) { |
| 96 | *buffer++ = *arg++; |
| 97 | continue; |
| 98 | } |
| 99 | i = *arg; |
| 100 | while (i <= ac) |
| 101 | *buffer++ = i++; |
| 102 | arg += 3; /* Skip the assumed a-z */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 103 | } else if (*arg == '[') { |
| 104 | arg++; |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 105 | i = *arg++; |
| 106 | if (ENABLE_FEATURE_TR_CLASSES && i == ':') { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 107 | if (strncmp(arg, "alpha", 5) == 0) { |
| 108 | for (i = 'A'; i <= 'Z'; i++) |
| 109 | *buffer++ = i; |
| 110 | for (i = 'a'; i <= 'z'; i++) |
| 111 | *buffer++ = i; |
| 112 | } |
| 113 | else if (strncmp(arg, "alnum", 5) == 0) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 114 | for (i = '0'; i <= '9'; i++) |
| 115 | *buffer++ = i; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 116 | for (i = 'A'; i <= 'Z'; i++) |
| 117 | *buffer++ = i; |
| 118 | for (i = 'a'; i <= 'z'; i++) |
| 119 | *buffer++ = i; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 120 | } |
| 121 | else if (strncmp(arg, "digit", 5) == 0) |
| 122 | for (i = '0'; i <= '9'; i++) |
| 123 | *buffer++ = i; |
| 124 | else if (strncmp(arg, "lower", 5) == 0) |
| 125 | for (i = 'a'; i <= 'z'; i++) |
| 126 | *buffer++ = i; |
| 127 | else if (strncmp(arg, "upper", 5) == 0) |
| 128 | for (i = 'A'; i <= 'Z'; i++) |
| 129 | *buffer++ = i; |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 130 | else if (strncmp(arg, "space", 5) == 0) { |
Denis Vlasenko | 9cac521 | 2006-09-09 12:24:19 +0000 | [diff] [blame] | 131 | const char s[] = "\t\n\v\f\r "; |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 132 | strcat((char*)buffer, s); |
| 133 | buffer += sizeof(s) - 1; |
| 134 | } |
| 135 | else if (strncmp(arg, "blank", 5) == 0) { |
| 136 | *buffer++ = '\t'; |
| 137 | *buffer++ = ' '; |
| 138 | } |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 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))) |
| 143 | *buffer++ = i; |
| 144 | } |
| 145 | else if (strncmp(arg, "cntrl", 5) == 0) { |
| 146 | for (i = 0; i <= ASCII; i++) |
| 147 | if (iscntrl(i)) |
| 148 | *buffer++ = i; |
| 149 | } |
| 150 | else { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 151 | *buffer++ = '['; |
| 152 | *buffer++ = ':'; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 153 | continue; |
| 154 | } |
| 155 | break; |
| 156 | } |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 157 | if (ENABLE_FEATURE_TR_EQUIV && i == '=') { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 158 | *buffer++ = *arg; |
| 159 | /* skip the closing =] */ |
| 160 | arg += 3; |
| 161 | continue; |
| 162 | } |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 163 | if (*arg++ != '-') { |
| 164 | *buffer++ = '['; |
| 165 | arg -= 2; |
| 166 | continue; |
| 167 | } |
| 168 | ac = *arg++; |
| 169 | while (i <= ac) |
| 170 | *buffer++ = i++; |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 171 | arg++; /* Skip the assumed ']' */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 172 | } else |
| 173 | *buffer++ = *arg++; |
| 174 | } |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 175 | |
| 176 | return (buffer - buffer_start); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 179 | static int complement(char *buffer, int buffer_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 180 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 181 | short i, j, ix; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 182 | char conv[ASCII + 2]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 183 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 184 | ix = 0; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 185 | for (i = 0; i <= ASCII; i++) { |
| 186 | for (j = 0; j < buffer_len; j++) |
| 187 | if (buffer[j] == i) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 188 | break; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 189 | if (j == buffer_len) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 190 | conv[ix++] = i & ASCII; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 191 | } |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 192 | memcpy(buffer, conv, ix); |
| 193 | return ix; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 196 | int tr_main(int argc, char **argv) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 197 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 198 | unsigned char *ptr; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 199 | int output_length=0, input_length; |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 200 | int idx = 1; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 201 | int i; |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 202 | RESERVE_CONFIG_BUFFER(output, BUFSIZ); |
Rob Landley | 998f449 | 2006-04-10 16:40:47 +0000 | [diff] [blame] | 203 | RESERVE_CONFIG_BUFFER(vector, ASCII+1); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 204 | RESERVE_CONFIG_BUFFER(invec, ASCII+1); |
| 205 | RESERVE_CONFIG_BUFFER(outvec, ASCII+1); |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 206 | |
| 207 | /* ... but make them available globally */ |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 208 | poutput = output; |
| 209 | pvector = vector; |
| 210 | pinvec = invec; |
| 211 | poutvec = outvec; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 212 | |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 213 | if (argc > 1 && argv[idx][0] == '-') { |
| 214 | for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) { |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 215 | switch (*ptr) { |
| 216 | case 'c': |
| 217 | com_fl = TRUE; |
| 218 | break; |
| 219 | case 'd': |
| 220 | del_fl = TRUE; |
| 221 | break; |
| 222 | case 's': |
| 223 | sq_fl = TRUE; |
| 224 | break; |
| 225 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | bb_show_usage(); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 229 | idx++; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 230 | } |
| 231 | for (i = 0; i <= ASCII; i++) { |
| 232 | vector[i] = i; |
| 233 | invec[i] = outvec[i] = FALSE; |
| 234 | } |
| 235 | |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 236 | if (argv[idx] != NULL) { |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 237 | input_length = expand(argv[idx++], bb_common_bufsiz1); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 238 | if (com_fl) |
Rob Landley | cd54528 | 2006-06-30 16:35:40 +0000 | [diff] [blame] | 239 | input_length = complement(bb_common_bufsiz1, input_length); |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 240 | if (argv[idx] != NULL) { |
| 241 | if (*argv[idx] == '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 242 | bb_error_msg_and_die("STRING2 cannot be empty"); |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 243 | output_length = expand(argv[idx], output); |
| 244 | map(bb_common_bufsiz1, input_length, output, output_length); |
Eric Andersen | a03d86c | 2000-07-10 16:38:50 +0000 | [diff] [blame] | 245 | } |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 246 | for (i = 0; i < input_length; i++) |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 247 | invec[(GCC4_IS_STUPID)bb_common_bufsiz1[i]] = TRUE; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 248 | for (i = 0; i < output_length; i++) |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 249 | outvec[(GCC4_IS_STUPID)output[i]] = TRUE; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 250 | } |
| 251 | convert(); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 252 | return 0; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 253 | } |