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