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 | */ |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 18 | /* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html |
| 19 | * TODO: xdigit, graph, print |
| 20 | */ |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 22 | |
Eric Andersen | 22ecf04 | 2001-07-02 17:32:40 +0000 | [diff] [blame] | 23 | #define ASCII 0377 |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 24 | |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 25 | #define TR_OPT_complement (1<<0) |
| 26 | #define TR_OPT_delete (1<<1) |
| 27 | #define TR_OPT_squeeze_reps (1<<2) |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 28 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 29 | static void map(char *pvector, |
| 30 | unsigned char *string1, unsigned int string1_len, |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 31 | unsigned char *string2, unsigned int string2_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 32 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 33 | char last = '0'; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 34 | unsigned int i, j; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 36 | for (j = 0, i = 0; i < string1_len; i++) { |
| 37 | if (string2_len <= j) |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 38 | pvector[string1[i]] = last; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 39 | else |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 40 | pvector[string1[i]] = last = string2[j++]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 44 | /* supported constructs: |
| 45 | * Ranges, e.g., [0-9] ==> 0123456789 |
| 46 | * Escapes, e.g., \a ==> Control-G |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 47 | * Character classes, e.g. [:upper:] ==> A ... Z |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 48 | */ |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 49 | static unsigned int expand(const char *arg, char *buffer) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 50 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 51 | char *buffer_start = buffer; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 52 | unsigned i; /* XXX: FIXME: use unsigned char? */ |
| 53 | unsigned char ac; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 54 | #define CLO ":]" |
Denis Vlasenko | 240a1cf | 2007-04-08 16:07:02 +0000 | [diff] [blame] | 55 | static const char * const classes[] = { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 56 | "alpha"CLO, "alnum"CLO, "digit"CLO, "lower"CLO, "upper"CLO, "space"CLO, |
| 57 | "blank"CLO, "punct"CLO, "cntrl"CLO, NULL |
| 58 | }; |
| 59 | #define CLASS_invalid 0 /* we increment the retval */ |
| 60 | #define CLASS_alpha 1 |
| 61 | #define CLASS_alnum 2 |
| 62 | #define CLASS_digit 3 |
| 63 | #define CLASS_lower 4 |
| 64 | #define CLASS_upper 5 |
| 65 | #define CLASS_space 6 |
| 66 | #define CLASS_blank 7 |
| 67 | #define CLASS_punct 8 |
| 68 | #define CLASS_cntrl 9 |
| 69 | //#define CLASS_xdigit 10 |
| 70 | //#define CLASS_graph 11 |
| 71 | //#define CLASS_print 12 |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 72 | while (*arg) { |
| 73 | if (*arg == '\\') { |
| 74 | arg++; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | *buffer++ = bb_process_escape_sequence(&arg); |
Eric Andersen | 5a4a46a | 2001-07-09 21:32:29 +0000 | [diff] [blame] | 76 | } else if (*(arg+1) == '-') { |
| 77 | ac = *(arg+2); |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 78 | if (ac == 0) { |
Eric Andersen | 5a4a46a | 2001-07-09 21:32:29 +0000 | [diff] [blame] | 79 | *buffer++ = *arg++; |
| 80 | continue; |
| 81 | } |
| 82 | i = *arg; |
| 83 | while (i <= ac) |
| 84 | *buffer++ = i++; |
| 85 | arg += 3; /* Skip the assumed a-z */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 86 | } else if (*arg == '[') { |
| 87 | arg++; |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 88 | i = *arg++; |
| 89 | if (ENABLE_FEATURE_TR_CLASSES && i == ':') { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 90 | smalluint j; |
| 91 | { /* not really pretty.. */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 92 | char *tmp = xstrndup(arg, 7); // warning: xdigit needs 8, not 7 |
| 93 | j = index_in_str_array(classes, tmp) + 1; |
| 94 | free(tmp); |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 95 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 96 | if (j == CLASS_alnum || j == CLASS_digit) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 97 | for (i = '0'; i <= '9'; i++) |
| 98 | *buffer++ = i; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 99 | } |
| 100 | if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 101 | for (i = 'A'; i <= 'Z'; i++) |
| 102 | *buffer++ = i; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 103 | } |
| 104 | if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 105 | for (i = 'a'; i <= 'z'; i++) |
| 106 | *buffer++ = i; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 107 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 108 | if (j == CLASS_space || j == CLASS_blank) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 109 | *buffer++ = '\t'; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 110 | if (j == CLASS_space) { |
| 111 | *buffer++ = '\n'; |
| 112 | *buffer++ = '\v'; |
| 113 | *buffer++ = '\f'; |
| 114 | *buffer++ = '\r'; |
| 115 | } |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 116 | *buffer++ = ' '; |
| 117 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 118 | if (j == CLASS_punct || j == CLASS_cntrl) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 119 | for (i = 0; i <= ASCII; i++) |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 120 | if ((j == CLASS_punct && |
| 121 | isprint(i) && (!isalnum(i)) && (!isspace(i))) || |
| 122 | (j == CLASS_cntrl && iscntrl(i))) |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 123 | *buffer++ = i; |
| 124 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 125 | if (j == CLASS_invalid) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 126 | *buffer++ = '['; |
| 127 | *buffer++ = ':'; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | break; |
| 131 | } |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 132 | if (ENABLE_FEATURE_TR_EQUIV && i == '=') { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 133 | *buffer++ = *arg; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 134 | arg += 3; /* Skip the closing =] */ |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 135 | continue; |
| 136 | } |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 137 | if (*arg++ != '-') { |
| 138 | *buffer++ = '['; |
| 139 | arg -= 2; |
| 140 | continue; |
| 141 | } |
| 142 | ac = *arg++; |
| 143 | while (i <= ac) |
| 144 | *buffer++ = i++; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 145 | arg++; /* Skip the assumed ']' */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 146 | } else |
| 147 | *buffer++ = *arg++; |
| 148 | } |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 149 | return (buffer - buffer_start); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 152 | static int complement(char *buffer, int buffer_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 153 | { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 154 | int i, j, ix; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 155 | char conv[ASCII + 2]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 156 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 157 | ix = 0; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 158 | for (i = 0; i <= ASCII; i++) { |
| 159 | for (j = 0; j < buffer_len; j++) |
| 160 | if (buffer[j] == i) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 161 | break; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 162 | if (j == buffer_len) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 163 | conv[ix++] = i & ASCII; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 164 | } |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 165 | memcpy(buffer, conv, ix); |
| 166 | return ix; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 169 | int tr_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 170 | int tr_main(int argc, char **argv) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 171 | { |
"Robert P. J. Day" | 6822983 | 2006-07-01 13:08:46 +0000 | [diff] [blame] | 172 | unsigned char *ptr; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 173 | int output_length = 0, input_length; |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 174 | int idx = 1; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 175 | int i; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 176 | smalluint flags = 0; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 177 | size_t read_chars = 0, in_index = 0, out_index = 0, c, coded, last = -1; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 178 | RESERVE_CONFIG_UBUFFER(output, BUFSIZ); |
Rob Landley | 998f449 | 2006-04-10 16:40:47 +0000 | [diff] [blame] | 179 | RESERVE_CONFIG_BUFFER(vector, ASCII+1); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 180 | RESERVE_CONFIG_BUFFER(invec, ASCII+1); |
| 181 | RESERVE_CONFIG_BUFFER(outvec, ASCII+1); |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 182 | |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 183 | if (argc > 1 && argv[idx][0] == '-') { |
| 184 | for (ptr = (unsigned char *) &argv[idx][1]; *ptr; ptr++) { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 185 | if (*ptr == 'c') |
| 186 | flags |= TR_OPT_complement; |
| 187 | else if (*ptr == 'd') |
| 188 | flags |= TR_OPT_delete; |
| 189 | else if (*ptr == 's') |
| 190 | flags |= TR_OPT_squeeze_reps; |
| 191 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 192 | bb_show_usage(); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 193 | } |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 194 | idx++; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 195 | } |
| 196 | for (i = 0; i <= ASCII; i++) { |
| 197 | vector[i] = i; |
| 198 | invec[i] = outvec[i] = FALSE; |
| 199 | } |
| 200 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 201 | #define tr_buf bb_common_bufsiz1 |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 202 | if (argv[idx] != NULL) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 203 | input_length = expand(argv[idx++], tr_buf); |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 204 | if (flags & TR_OPT_complement) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 205 | input_length = complement(tr_buf, input_length); |
Eric Andersen | f6aa13d | 2001-03-23 17:08:21 +0000 | [diff] [blame] | 206 | if (argv[idx] != NULL) { |
| 207 | if (*argv[idx] == '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 208 | bb_error_msg_and_die("STRING2 cannot be empty"); |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 209 | output_length = expand(argv[idx], output); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 210 | map(vector, tr_buf, input_length, output, output_length); |
Eric Andersen | a03d86c | 2000-07-10 16:38:50 +0000 | [diff] [blame] | 211 | } |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 212 | for (i = 0; i < input_length; i++) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 213 | invec[(unsigned char)tr_buf[i]] = TRUE; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 214 | for (i = 0; i < output_length; i++) |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 215 | outvec[output[i]] = TRUE; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 216 | } |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 217 | |
| 218 | for (;;) { |
| 219 | /* If we're out of input, flush output and read more input. */ |
| 220 | if (in_index == read_chars) { |
| 221 | if (out_index) { |
| 222 | xwrite(STDOUT_FILENO, (char *)output, out_index); |
| 223 | out_index = 0; |
| 224 | } |
| 225 | read_chars = read(STDIN_FILENO, tr_buf, BUFSIZ); |
| 226 | if (read_chars <= 0) { |
| 227 | if (write(STDOUT_FILENO, (char *)output, out_index) != out_index) |
| 228 | bb_perror_msg(bb_msg_write_error); |
| 229 | exit(EXIT_SUCCESS); |
| 230 | } |
| 231 | in_index = 0; |
| 232 | } |
| 233 | c = tr_buf[in_index++]; |
| 234 | coded = vector[c]; |
| 235 | if ((flags & TR_OPT_delete) && invec[c]) |
| 236 | continue; |
| 237 | if ((flags & TR_OPT_squeeze_reps) && last == coded && |
| 238 | (invec[c] || outvec[coded])) |
| 239 | continue; |
| 240 | output[out_index++] = last = coded; |
| 241 | } |
| 242 | /* NOTREACHED */ |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 243 | return EXIT_SUCCESS; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 244 | } |