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 |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 19 | * TODO: graph, print |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 20 | */ |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 21 | |
| 22 | //kbuild:lib-$(CONFIG_TR) += tr.o |
| 23 | |
| 24 | //config:config TR |
| 25 | //config: bool "tr" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 26 | //config: default y |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 27 | //config: help |
| 28 | //config: tr is used to squeeze, and/or delete characters from standard |
| 29 | //config: input, writing to standard output. |
Denys Vlasenko | 729ce47 | 2010-06-05 21:29:41 +0200 | [diff] [blame] | 30 | //config: |
| 31 | //config:config FEATURE_TR_CLASSES |
| 32 | //config: bool "Enable character classes (such as [:upper:])" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 33 | //config: default y |
Denys Vlasenko | 729ce47 | 2010-06-05 21:29:41 +0200 | [diff] [blame] | 34 | //config: depends on TR |
| 35 | //config: help |
| 36 | //config: Enable character classes, enabling commands such as: |
| 37 | //config: tr [:upper:] [:lower:] to convert input into lowercase. |
| 38 | //config: |
| 39 | //config:config FEATURE_TR_EQUIV |
| 40 | //config: bool "Enable equivalence classes" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 41 | //config: default y |
Denys Vlasenko | 729ce47 | 2010-06-05 21:29:41 +0200 | [diff] [blame] | 42 | //config: depends on TR |
| 43 | //config: help |
| 44 | //config: Enable equivalence classes, which essentially add the enclosed |
| 45 | //config: character to the current set. For instance, tr [=a=] xyz would |
| 46 | //config: replace all instances of 'a' with 'xyz'. This option is mainly |
| 47 | //config: useful for cases when no other way of expressing a character |
| 48 | //config: is possible. |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 49 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 50 | #include "libbb.h" |
Erik Andersen | 330fd2b | 2000-05-19 05:35:19 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 52 | enum { |
| 53 | ASCII = 256, |
| 54 | /* string buffer needs to be at least as big as the whole "alphabet". |
| 55 | * BUFSIZ == ASCII is ok, but we will realloc in expand |
| 56 | * even for smallest patterns, let's avoid that by using *2: |
| 57 | */ |
| 58 | TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2, |
| 59 | }; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 61 | static void map(char *pvector, |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 62 | char *string1, unsigned string1_len, |
| 63 | char *string2, unsigned string2_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 64 | { |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 65 | char last = '0'; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 66 | unsigned i, j; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 67 | |
Eric Andersen | 00143ba | 2000-07-13 16:40:41 +0000 | [diff] [blame] | 68 | for (j = 0, i = 0; i < string1_len; i++) { |
| 69 | if (string2_len <= j) |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 70 | pvector[(unsigned char)(string1[i])] = last; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 71 | else |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 72 | pvector[(unsigned char)(string1[i])] = last = string2[j++]; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 76 | /* supported constructs: |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 77 | * Ranges, e.g., 0-9 ==> 0123456789 |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 78 | * Escapes, e.g., \a ==> Control-G |
| 79 | * Character classes, e.g. [:upper:] ==> A...Z |
| 80 | * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?) |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 81 | * not supported: |
| 82 | * \ooo-\ooo - octal ranges |
| 83 | * [x*N] - repeat char x N times |
| 84 | * [x*] - repeat char x until it fills STRING2: |
| 85 | * # echo qwe123 | /usr/bin/tr 123456789 '[d]' |
| 86 | * qwe[d] |
| 87 | * # echo qwe123 | /usr/bin/tr 123456789 '[d*]' |
| 88 | * qweddd |
Mark Whitley | 8b7a0d8 | 2001-05-24 21:31:09 +0000 | [diff] [blame] | 89 | */ |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 90 | static unsigned expand(const char *arg, char **buffer_p) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 91 | { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 92 | char *buffer = *buffer_p; |
| 93 | unsigned pos = 0; |
| 94 | unsigned size = TR_BUFSIZ; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 95 | 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] | 96 | unsigned char ac; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 97 | |
| 98 | while (*arg) { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 99 | if (pos + ASCII > size) { |
| 100 | size += ASCII; |
| 101 | *buffer_p = buffer = xrealloc(buffer, size); |
| 102 | } |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 103 | if (*arg == '\\') { |
| 104 | arg++; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 105 | buffer[pos++] = bb_process_escape_sequence(&arg); |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 106 | continue; |
| 107 | } |
| 108 | if (arg[1] == '-') { /* "0-9..." */ |
| 109 | ac = arg[2]; |
| 110 | if (ac == '\0') { /* "0-": copy verbatim */ |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 111 | buffer[pos++] = *arg++; /* copy '0' */ |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 112 | continue; /* next iter will copy '-' and stop */ |
| 113 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 114 | i = (unsigned char) *arg; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 115 | while (i <= ac) /* ok: i is unsigned _int_ */ |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 116 | buffer[pos++] = i++; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 117 | arg += 3; /* skip 0-9 */ |
| 118 | continue; |
| 119 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 120 | if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV) |
| 121 | && *arg == '[' |
| 122 | ) { |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 123 | arg++; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 124 | i = (unsigned char) *arg++; |
| 125 | /* "[xyz...". i=x, arg points to y */ |
| 126 | if (ENABLE_FEATURE_TR_CLASSES && i == ':') { /* [:class:] */ |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 127 | #define CLO ":]\0" |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 128 | static const char classes[] ALIGN1 = |
| 129 | "alpha"CLO "alnum"CLO "digit"CLO |
| 130 | "lower"CLO "upper"CLO "space"CLO |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 131 | "blank"CLO "punct"CLO "cntrl"CLO |
| 132 | "xdigit"CLO; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 133 | enum { |
| 134 | CLASS_invalid = 0, /* we increment the retval */ |
| 135 | CLASS_alpha = 1, |
| 136 | CLASS_alnum = 2, |
| 137 | CLASS_digit = 3, |
| 138 | CLASS_lower = 4, |
| 139 | CLASS_upper = 5, |
| 140 | CLASS_space = 6, |
| 141 | CLASS_blank = 7, |
| 142 | CLASS_punct = 8, |
| 143 | CLASS_cntrl = 9, |
| 144 | CLASS_xdigit = 10, |
| 145 | //CLASS_graph = 11, |
| 146 | //CLASS_print = 12, |
| 147 | }; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 148 | smalluint j; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 149 | char *tmp; |
| 150 | |
| 151 | /* xdigit needs 8, not 7 */ |
| 152 | i = 7 + (arg[0] == 'x'); |
| 153 | tmp = xstrndup(arg, i); |
| 154 | j = index_in_strings(classes, tmp) + 1; |
| 155 | free(tmp); |
| 156 | |
| 157 | if (j == CLASS_invalid) |
| 158 | goto skip_bracket; |
| 159 | |
| 160 | arg += i; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 161 | if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) { |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 162 | for (i = '0'; i <= '9'; i++) |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 163 | buffer[pos++] = i; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 164 | } |
| 165 | if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 166 | for (i = 'A'; i <= 'Z'; i++) |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 167 | buffer[pos++] = i; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 168 | } |
| 169 | if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) { |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 170 | for (i = 'a'; i <= 'z'; i++) |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 171 | buffer[pos++] = i; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 172 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 173 | if (j == CLASS_space || j == CLASS_blank) { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 174 | buffer[pos++] = '\t'; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 175 | if (j == CLASS_space) { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 176 | buffer[pos++] = '\n'; |
| 177 | buffer[pos++] = '\v'; |
| 178 | buffer[pos++] = '\f'; |
| 179 | buffer[pos++] = '\r'; |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 180 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 181 | buffer[pos++] = ' '; |
Rob Landley | 998dbee | 2006-04-19 22:22:06 +0000 | [diff] [blame] | 182 | } |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 183 | if (j == CLASS_punct || j == CLASS_cntrl) { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 184 | for (i = '\0'; i < ASCII; i++) { |
Denys Vlasenko | 8684cbb | 2009-11-18 11:34:43 +0100 | [diff] [blame] | 185 | if ((j == CLASS_punct && isprint_asciionly(i) && !isalnum(i) && !isspace(i)) |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 186 | || (j == CLASS_cntrl && iscntrl(i)) |
| 187 | ) { |
| 188 | buffer[pos++] = i; |
| 189 | } |
| 190 | } |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 191 | } |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 192 | if (j == CLASS_xdigit) { |
| 193 | for (i = 'A'; i <= 'F'; i++) { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 194 | buffer[pos + 6] = i | 0x20; |
| 195 | buffer[pos++] = i; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 196 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 197 | pos += 6; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 198 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 199 | continue; |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 201 | /* "[xyz...", i=x, arg points to y */ |
| 202 | if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */ |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 203 | buffer[pos++] = *arg; /* copy CHAR */ |
| 204 | if (!arg[0] || arg[1] != '=' || arg[2] != ']') |
Denis Vlasenko | 043d15d | 2008-07-28 23:49:42 +0000 | [diff] [blame] | 205 | bb_show_usage(); |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 206 | arg += 3; /* skip CHAR=] */ |
Rob Landley | f104814 | 2005-10-08 21:21:08 +0000 | [diff] [blame] | 207 | continue; |
| 208 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 209 | /* The rest of "[xyz..." cases is treated as normal |
| 210 | * string, "[" has no special meaning here: |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 211 | * tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z", |
| 212 | * also try tr "[a-z]" "_A-Z+" and you'll see that |
| 213 | * [] is not special here. |
| 214 | */ |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 215 | skip_bracket: |
| 216 | arg -= 2; /* points to "[" in "[xyz..." */ |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 217 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 218 | buffer[pos++] = *arg++; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 219 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 220 | return pos; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 223 | /* NB: buffer is guaranteed to be at least TR_BUFSIZE |
| 224 | * (which is >= ASCII) big. |
| 225 | */ |
Rob Landley | ab58d5c | 2006-06-30 19:04:09 +0000 | [diff] [blame] | 226 | static int complement(char *buffer, int buffer_len) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 227 | { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 228 | int len; |
| 229 | char conv[ASCII]; |
| 230 | unsigned char ch; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 231 | |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 232 | len = 0; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 233 | ch = '\0'; |
| 234 | while (1) { |
| 235 | if (memchr(buffer, ch, buffer_len) == NULL) |
| 236 | conv[len++] = ch; |
| 237 | if (++ch == '\0') |
| 238 | break; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 239 | } |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 240 | memcpy(buffer, conv, len); |
| 241 | return len; |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 244 | int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 245 | int tr_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 246 | { |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 247 | int i; |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 248 | smalluint opts; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 249 | ssize_t read_chars; |
| 250 | size_t in_index, out_index; |
Denis Vlasenko | 3d46167 | 2007-11-13 22:22:29 +0000 | [diff] [blame] | 251 | unsigned last = UCHAR_MAX + 1; /* not equal to any char */ |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 252 | unsigned char coded, c; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 253 | char *str1 = xmalloc(TR_BUFSIZ); |
| 254 | char *str2 = xmalloc(TR_BUFSIZ); |
| 255 | int str2_length; |
| 256 | int str1_length; |
| 257 | char *vector = xzalloc(ASCII * 3); |
| 258 | char *invec = vector + ASCII; |
| 259 | char *outvec = vector + ASCII * 2; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 260 | |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 261 | #define TR_OPT_complement (3 << 0) |
| 262 | #define TR_OPT_delete (1 << 2) |
| 263 | #define TR_OPT_squeeze_reps (1 << 3) |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 265 | for (i = 0; i < ASCII; i++) { |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 266 | vector[i] = i; |
Denis Vlasenko | 8d0a734 | 2007-11-13 22:23:57 +0000 | [diff] [blame] | 267 | /*invec[i] = outvec[i] = FALSE; - done by xzalloc */ |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 270 | /* -C/-c difference is that -C complements "characters", |
| 271 | * and -c complements "values" (binary bytes I guess). |
| 272 | * In POSIX locale, these are the same. |
| 273 | */ |
| 274 | |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 275 | opt_complementary = "-1"; |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 276 | opts = getopt32(argv, "+Ccds"); /* '+': stop at first non-option */ |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 277 | argv += optind; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 278 | |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 279 | str1_length = expand(*argv++, &str1); |
| 280 | str2_length = 0; |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 281 | if (opts & TR_OPT_complement) |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 282 | str1_length = complement(str1, str1_length); |
| 283 | if (*argv) { |
| 284 | if (argv[0][0] == '\0') |
| 285 | bb_error_msg_and_die("STRING2 cannot be empty"); |
| 286 | str2_length = expand(*argv, &str2); |
| 287 | map(vector, str1, str1_length, |
| 288 | str2, str2_length); |
Erik Andersen | 8f8d6d5 | 2000-05-01 22:30:37 +0000 | [diff] [blame] | 289 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 290 | for (i = 0; i < str1_length; i++) |
| 291 | invec[(unsigned char)(str1[i])] = TRUE; |
| 292 | for (i = 0; i < str2_length; i++) |
| 293 | outvec[(unsigned char)(str2[i])] = TRUE; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 294 | |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 295 | goto start_from; |
| 296 | |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 297 | /* In this loop, str1 space is reused as input buffer, |
| 298 | * str2 - as output one. */ |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 299 | for (;;) { |
| 300 | /* If we're out of input, flush output and read more input. */ |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 301 | if ((ssize_t)in_index == read_chars) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 302 | if (out_index) { |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 303 | xwrite(STDOUT_FILENO, str2, out_index); |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 304 | start_from: |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 305 | out_index = 0; |
| 306 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 307 | read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ); |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 308 | if (read_chars <= 0) { |
Denis Vlasenko | a89d50f | 2007-11-13 17:51:40 +0000 | [diff] [blame] | 309 | if (read_chars < 0) |
| 310 | bb_perror_msg_and_die(bb_msg_read_error); |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 311 | break; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 312 | } |
| 313 | in_index = 0; |
| 314 | } |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 315 | c = str1[in_index++]; |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 316 | if ((opts & TR_OPT_delete) && invec[c]) |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 317 | continue; |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 318 | coded = vector[c]; |
Denis Vlasenko | 9210a36 | 2009-03-15 16:41:55 +0000 | [diff] [blame] | 319 | if ((opts & TR_OPT_squeeze_reps) && last == coded |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 320 | && (invec[c] || outvec[coded]) |
| 321 | ) { |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 322 | continue; |
Denis Vlasenko | 491d6e3 | 2009-03-02 12:05:57 +0000 | [diff] [blame] | 323 | } |
| 324 | str2[out_index++] = last = coded; |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 325 | } |
Denis Vlasenko | 1bfcc8b | 2009-03-01 04:50:18 +0000 | [diff] [blame] | 326 | |
Bernhard Reutner-Fischer | eceecea | 2007-03-30 14:43:27 +0000 | [diff] [blame] | 327 | return EXIT_SUCCESS; |
Erik Andersen | fb002d0 | 2000-03-05 08:07:00 +0000 | [diff] [blame] | 328 | } |