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 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 25 | static void map(char *pvector, |
| 26 | unsigned char *string1, unsigned int string1_len, |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 27 | unsigned char *string2, unsigned int string2_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 28 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 29 | char last = '0'; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 30 | unsigned int i, j; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 31 | |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 32 | for (j = 0, i = 0; i < string1_len; i++) { |
| 33 | if (string2_len <= j) |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 34 | pvector[string1[i]] = last; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 35 | else |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 36 | pvector[string1[i]] = last = string2[j++]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 40 | /* supported constructs: |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 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?) |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 46 | */ |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 47 | static unsigned int expand(const char *arg, char *buffer) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 48 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 49 | char *buffer_start = buffer; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 50 | unsigned i; /* can't be unsigned char: must be able to hold 256 */ |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 51 | unsigned char ac; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 52 | |
| 53 | while (*arg) { |
| 54 | if (*arg == '\\') { |
| 55 | arg++; |
| 56 | *buffer++ = bb_process_escape_sequence(&arg); |
| 57 | continue; |
| 58 | } |
| 59 | if (arg[1] == '-') { /* "0-9..." */ |
| 60 | ac = arg[2]; |
| 61 | if (ac == '\0') { /* "0-": copy verbatim */ |
| 62 | *buffer++ = *arg++; /* copy '0' */ |
| 63 | continue; /* next iter will copy '-' and stop */ |
| 64 | } |
| 65 | i = *arg; |
| 66 | while (i <= ac) /* ok: i is unsigned _int_ */ |
| 67 | *buffer++ = i++; |
| 68 | arg += 3; /* skip 0-9 */ |
| 69 | continue; |
| 70 | } |
| 71 | if (*arg == '[') { /* "[xyz..." */ |
| 72 | arg++; |
| 73 | i = *arg++; |
| 74 | /* "[xyz...", i=x, arg points to y */ |
| 75 | if (ENABLE_FEATURE_TR_CLASSES && i == ':') { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 76 | #define CLO ":]\0" |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 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; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 81 | #define CLASS_invalid 0 /* we increment the retval */ |
| 82 | #define CLASS_alpha 1 |
| 83 | #define CLASS_alnum 2 |
| 84 | #define CLASS_digit 3 |
| 85 | #define CLASS_lower 4 |
| 86 | #define CLASS_upper 5 |
| 87 | #define CLASS_space 6 |
| 88 | #define CLASS_blank 7 |
| 89 | #define CLASS_punct 8 |
| 90 | #define CLASS_cntrl 9 |
| 91 | //#define CLASS_xdigit 10 |
| 92 | //#define CLASS_graph 11 |
| 93 | //#define CLASS_print 12 |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 94 | smalluint j; |
| 95 | { /* not really pretty.. */ |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 96 | char *tmp = xstrndup(arg, 7); // warning: xdigit would need 8, not 7 |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 97 | j = index_in_strings(classes, tmp) + 1; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 98 | free(tmp); |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 99 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 100 | if (j == CLASS_alnum || j == CLASS_digit) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 101 | for (i = '0'; i <= '9'; 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_upper) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 105 | for (i = 'A'; i <= 'Z'; i++) |
| 106 | *buffer++ = i; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 107 | } |
| 108 | if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 109 | for (i = 'a'; i <= 'z'; i++) |
| 110 | *buffer++ = i; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 111 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 112 | if (j == CLASS_space || j == CLASS_blank) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 113 | *buffer++ = '\t'; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 114 | if (j == CLASS_space) { |
| 115 | *buffer++ = '\n'; |
| 116 | *buffer++ = '\v'; |
| 117 | *buffer++ = '\f'; |
| 118 | *buffer++ = '\r'; |
| 119 | } |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 120 | *buffer++ = ' '; |
| 121 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 122 | if (j == CLASS_punct || j == CLASS_cntrl) { |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 123 | for (i = '\0'; i <= ASCII; i++) |
| 124 | if ((j == CLASS_punct && isprint(i) && !isalnum(i) && !isspace(i)) |
| 125 | || (j == CLASS_cntrl && iscntrl(i))) |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 126 | *buffer++ = i; |
| 127 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 128 | if (j == CLASS_invalid) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 129 | *buffer++ = '['; |
| 130 | *buffer++ = ':'; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 131 | continue; |
| 132 | } |
| 133 | break; |
| 134 | } |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 135 | /* "[xyz...", i=x, arg points to y */ |
| 136 | if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */ |
| 137 | *buffer++ = *arg; /* copy CHAR */ |
| 138 | arg += 3; /* skip CHAR=] */ |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 139 | continue; |
| 140 | } |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 141 | if (*arg != '-') { /* not [x-...] - copy verbatim */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 142 | *buffer++ = '['; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 143 | arg--; /* points to x */ |
| 144 | continue; /* copy all, including eventual ']' */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 145 | } |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 146 | /* [x-y...] */ |
| 147 | arg++; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 148 | ac = *arg++; |
| 149 | while (i <= ac) |
| 150 | *buffer++ = i++; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 151 | arg++; /* skip the assumed ']' */ |
| 152 | continue; |
| 153 | } |
| 154 | *buffer++ = *arg++; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 155 | } |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 156 | return (buffer - buffer_start); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 159 | static int complement(char *buffer, int buffer_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 160 | { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 161 | int i, j, ix; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 162 | char conv[ASCII + 2]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 163 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 164 | ix = 0; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 165 | for (i = '\0'; i <= ASCII; i++) { |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 166 | for (j = 0; j < buffer_len; j++) |
| 167 | if (buffer[j] == i) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 168 | break; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 169 | if (j == buffer_len) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 170 | conv[ix++] = i & ASCII; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 171 | } |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 172 | memcpy(buffer, conv, ix); |
| 173 | return ix; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 176 | int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 177 | int tr_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 178 | { |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 179 | int output_length = 0, input_length; |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 180 | int i; |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 181 | smalluint flags; |
Denis Vlasenko | a89d50f | 2007-11-13 17:51:40 +0000 | [diff] [blame] | 182 | ssize_t read_chars = 0; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 183 | size_t in_index = 0, out_index = 0; |
| 184 | unsigned last = UCHAR_MAX + 1; /* not equal to any char */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 185 | unsigned char coded, c; |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 186 | unsigned char *output = xmalloc(BUFSIZ); |
| 187 | char *vector = xzalloc((ASCII+1) * 3); |
| 188 | char *invec = vector + (ASCII+1); |
| 189 | char *outvec = vector + (ASCII+1) * 2; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 191 | #define TR_OPT_complement (1 << 0) |
| 192 | #define TR_OPT_delete (1 << 1) |
| 193 | #define TR_OPT_squeeze_reps (1 << 2) |
| 194 | |
| 195 | flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */ |
| 196 | argv += optind; |
| 197 | |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 198 | for (i = 0; i <= ASCII; i++) { |
| 199 | vector[i] = i; |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 200 | /*invec[i] = outvec[i] = FALSE; - done by xzalloc */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 203 | #define tr_buf bb_common_bufsiz1 |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 204 | if (*argv != NULL) { |
| 205 | input_length = expand(*argv++, tr_buf); |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 206 | if (flags & TR_OPT_complement) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 207 | input_length = complement(tr_buf, input_length); |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 208 | if (*argv) { |
| 209 | if (argv[0][0] == '\0') |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 210 | bb_error_msg_and_die("STRING2 cannot be empty"); |
Denis Vlasenko | 023dc67 | 2008-05-09 18:07:15 +0000 | [diff] [blame] | 211 | output_length = expand(*argv, (char *)output); |
| 212 | map(vector, (unsigned char *)tr_buf, input_length, output, output_length); |
Eric Andersen | a03d86c | 2000-07-10 16:38:50 +0000 | [diff] [blame] | 213 | } |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 214 | for (i = 0; i < input_length; i++) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 215 | invec[(unsigned char)tr_buf[i]] = TRUE; |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 216 | for (i = 0; i < output_length; i++) |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 217 | outvec[output[i]] = TRUE; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 218 | } |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 219 | |
| 220 | for (;;) { |
| 221 | /* If we're out of input, flush output and read more input. */ |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 222 | if ((ssize_t)in_index == read_chars) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 223 | if (out_index) { |
| 224 | xwrite(STDOUT_FILENO, (char *)output, out_index); |
| 225 | out_index = 0; |
| 226 | } |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 227 | read_chars = safe_read(STDIN_FILENO, tr_buf, BUFSIZ); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 228 | if (read_chars <= 0) { |
Denis Vlasenko | a89d50f | 2007-11-13 17:51:40 +0000 | [diff] [blame] | 229 | if (read_chars < 0) |
| 230 | bb_perror_msg_and_die(bb_msg_read_error); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 231 | exit(EXIT_SUCCESS); |
| 232 | } |
| 233 | in_index = 0; |
| 234 | } |
| 235 | c = tr_buf[in_index++]; |
| 236 | coded = vector[c]; |
| 237 | if ((flags & TR_OPT_delete) && invec[c]) |
| 238 | continue; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 239 | if ((flags & TR_OPT_squeeze_reps) && last == coded |
| 240 | && (invec[c] || outvec[coded])) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 241 | continue; |
| 242 | output[out_index++] = last = coded; |
| 243 | } |
| 244 | /* NOTREACHED */ |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 245 | return EXIT_SUCCESS; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 246 | } |