diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-15 16:41:55 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2009-03-15 16:41:55 +0000 |
commit | 9210a36495f90343989a7cd90b2c1949803c7460 (patch) | |
tree | c37f4fe3426aeeef03775669ba20a8b869abb84e | |
parent | fc58ba1298519712bf42e44ab71a916fb0dfba05 (diff) | |
download | busybox-w32-9210a36495f90343989a7cd90b2c1949803c7460.tar.gz busybox-w32-9210a36495f90343989a7cd90b2c1949803c7460.tar.bz2 busybox-w32-9210a36495f90343989a7cd90b2c1949803c7460.zip |
tr: support -C as synonym to -c
-rw-r--r-- | coreutils/tr.c | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/coreutils/tr.c b/coreutils/tr.c index bf6fa2996..d89b80bec 100644 --- a/coreutils/tr.c +++ b/coreutils/tr.c | |||
@@ -49,6 +49,14 @@ static void map(char *pvector, | |||
49 | * Escapes, e.g., \a ==> Control-G | 49 | * Escapes, e.g., \a ==> Control-G |
50 | * Character classes, e.g. [:upper:] ==> A...Z | 50 | * Character classes, e.g. [:upper:] ==> A...Z |
51 | * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?) | 51 | * Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?) |
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 | ||
52 | */ | 60 | */ |
53 | static unsigned expand(const char *arg, char **buffer_p) | 61 | static unsigned expand(const char *arg, char **buffer_p) |
54 | { | 62 | { |
@@ -208,7 +216,7 @@ int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
208 | int tr_main(int argc UNUSED_PARAM, char **argv) | 216 | int tr_main(int argc UNUSED_PARAM, char **argv) |
209 | { | 217 | { |
210 | int i; | 218 | int i; |
211 | smalluint flags; | 219 | smalluint opts; |
212 | ssize_t read_chars; | 220 | ssize_t read_chars; |
213 | size_t in_index, out_index; | 221 | size_t in_index, out_index; |
214 | unsigned last = UCHAR_MAX + 1; /* not equal to any char */ | 222 | unsigned last = UCHAR_MAX + 1; /* not equal to any char */ |
@@ -221,22 +229,27 @@ int tr_main(int argc UNUSED_PARAM, char **argv) | |||
221 | char *invec = vector + ASCII; | 229 | char *invec = vector + ASCII; |
222 | char *outvec = vector + ASCII * 2; | 230 | char *outvec = vector + ASCII * 2; |
223 | 231 | ||
224 | #define TR_OPT_complement (1 << 0) | 232 | #define TR_OPT_complement (3 << 0) |
225 | #define TR_OPT_delete (1 << 1) | 233 | #define TR_OPT_delete (1 << 2) |
226 | #define TR_OPT_squeeze_reps (1 << 2) | 234 | #define TR_OPT_squeeze_reps (1 << 3) |
227 | 235 | ||
228 | for (i = 0; i < ASCII; i++) { | 236 | for (i = 0; i < ASCII; i++) { |
229 | vector[i] = i; | 237 | vector[i] = i; |
230 | /*invec[i] = outvec[i] = FALSE; - done by xzalloc */ | 238 | /*invec[i] = outvec[i] = FALSE; - done by xzalloc */ |
231 | } | 239 | } |
232 | 240 | ||
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 | |||
233 | opt_complementary = "-1"; | 246 | opt_complementary = "-1"; |
234 | flags = getopt32(argv, "+cds"); /* '+': stop at first non-option */ | 247 | opts = getopt32(argv, "+Ccds"); /* '+': stop at first non-option */ |
235 | argv += optind; | 248 | argv += optind; |
236 | 249 | ||
237 | str1_length = expand(*argv++, &str1); | 250 | str1_length = expand(*argv++, &str1); |
238 | str2_length = 0; | 251 | str2_length = 0; |
239 | if (flags & TR_OPT_complement) | 252 | if (opts & TR_OPT_complement) |
240 | str1_length = complement(str1, str1_length); | 253 | str1_length = complement(str1, str1_length); |
241 | if (*argv) { | 254 | if (*argv) { |
242 | if (argv[0][0] == '\0') | 255 | if (argv[0][0] == '\0') |
@@ -271,10 +284,10 @@ int tr_main(int argc UNUSED_PARAM, char **argv) | |||
271 | in_index = 0; | 284 | in_index = 0; |
272 | } | 285 | } |
273 | c = str1[in_index++]; | 286 | c = str1[in_index++]; |
274 | if ((flags & TR_OPT_delete) && invec[c]) | 287 | if ((opts & TR_OPT_delete) && invec[c]) |
275 | continue; | 288 | continue; |
276 | coded = vector[c]; | 289 | coded = vector[c]; |
277 | if ((flags & TR_OPT_squeeze_reps) && last == coded | 290 | if ((opts & TR_OPT_squeeze_reps) && last == coded |
278 | && (invec[c] || outvec[coded]) | 291 | && (invec[c] || outvec[coded]) |
279 | ) { | 292 | ) { |
280 | continue; | 293 | continue; |