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