2000-03-05 13:37:00 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
2000-05-02 04:00:37 +05:30
|
|
|
* Mini tr implementation for busybox
|
2000-03-05 13:37:00 +05:30
|
|
|
*
|
2006-06-30 22:05:40 +05:30
|
|
|
** Copyright (c) 1987,1997, Prentice Hall All rights reserved.
|
|
|
|
*
|
|
|
|
* The name of Prentice Hall may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
2000-05-02 05:37:56 +05:30
|
|
|
* Copyright (c) Michiel Huisjes
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* This version of tr is adapted from Minix tr and was modified
|
2003-07-15 02:51:08 +05:30
|
|
|
* by Erik Andersen <andersen@codepoet.org> to be used in busybox.
|
2000-03-05 13:37:00 +05:30
|
|
|
*
|
2006-06-30 22:05:40 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2000-03-05 13:37:00 +05:30
|
|
|
*/
|
2007-03-30 20:13:27 +05:30
|
|
|
/* http://www.opengroup.org/onlinepubs/009695399/utilities/tr.html
|
2009-03-02 17:35:57 +05:30
|
|
|
* TODO: graph, print
|
2007-03-30 20:13:27 +05:30
|
|
|
*/
|
2010-06-04 23:29:49 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_TR) += tr.o
|
|
|
|
|
|
|
|
//config:config TR
|
|
|
|
//config: bool "tr"
|
2010-06-06 07:44:28 +05:30
|
|
|
//config: default y
|
2010-06-04 23:29:49 +05:30
|
|
|
//config: help
|
|
|
|
//config: tr is used to squeeze, and/or delete characters from standard
|
|
|
|
//config: input, writing to standard output.
|
2010-06-06 00:59:41 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_TR_CLASSES
|
|
|
|
//config: bool "Enable character classes (such as [:upper:])"
|
2010-06-06 07:44:28 +05:30
|
|
|
//config: default y
|
2010-06-06 00:59:41 +05:30
|
|
|
//config: depends on TR
|
|
|
|
//config: help
|
|
|
|
//config: Enable character classes, enabling commands such as:
|
|
|
|
//config: tr [:upper:] [:lower:] to convert input into lowercase.
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_TR_EQUIV
|
|
|
|
//config: bool "Enable equivalence classes"
|
2010-06-06 07:44:28 +05:30
|
|
|
//config: default y
|
2010-06-06 00:59:41 +05:30
|
|
|
//config: depends on TR
|
|
|
|
//config: help
|
|
|
|
//config: Enable equivalence classes, which essentially add the enclosed
|
|
|
|
//config: character to the current set. For instance, tr [=a=] xyz would
|
|
|
|
//config: replace all instances of 'a' with 'xyz'. This option is mainly
|
|
|
|
//config: useful for cases when no other way of expressing a character
|
|
|
|
//config: is possible.
|
2010-06-04 23:29:49 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2000-05-19 11:05:19 +05:30
|
|
|
|
2009-03-02 17:35:57 +05:30
|
|
|
enum {
|
|
|
|
ASCII = 256,
|
|
|
|
/* string buffer needs to be at least as big as the whole "alphabet".
|
|
|
|
* BUFSIZ == ASCII is ok, but we will realloc in expand
|
|
|
|
* even for smallest patterns, let's avoid that by using *2:
|
|
|
|
*/
|
|
|
|
TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2,
|
|
|
|
};
|
2000-03-05 13:37:00 +05:30
|
|
|
|
2007-06-04 15:46:52 +05:30
|
|
|
static void map(char *pvector,
|
2009-03-02 17:35:57 +05:30
|
|
|
char *string1, unsigned string1_len,
|
|
|
|
char *string2, unsigned string2_len)
|
2000-03-05 13:37:00 +05:30
|
|
|
{
|
2006-07-01 00:34:09 +05:30
|
|
|
char last = '0';
|
2009-03-01 10:20:18 +05:30
|
|
|
unsigned i, j;
|
2000-05-02 04:00:37 +05:30
|
|
|
|
2000-07-13 22:10:41 +05:30
|
|
|
for (j = 0, i = 0; i < string1_len; i++) {
|
|
|
|
if (string2_len <= j)
|
2009-03-02 17:35:57 +05:30
|
|
|
pvector[(unsigned char)(string1[i])] = last;
|
2000-05-02 04:00:37 +05:30
|
|
|
else
|
2009-03-02 17:35:57 +05:30
|
|
|
pvector[(unsigned char)(string1[i])] = last = string2[j++];
|
2000-05-02 04:00:37 +05:30
|
|
|
}
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|
|
|
|
|
2001-05-25 03:01:09 +05:30
|
|
|
/* supported constructs:
|
2007-11-14 03:52:29 +05:30
|
|
|
* Ranges, e.g., 0-9 ==> 0123456789
|
|
|
|
* Escapes, e.g., \a ==> Control-G
|
|
|
|
* Character classes, e.g. [:upper:] ==> A...Z
|
|
|
|
* Equiv classess, e.g. [=A=] ==> A (hmmmmmmm?)
|
2009-03-15 22:11:55 +05:30
|
|
|
* not supported:
|
|
|
|
* \ooo-\ooo - octal ranges
|
|
|
|
* [x*N] - repeat char x N times
|
|
|
|
* [x*] - repeat char x until it fills STRING2:
|
|
|
|
* # echo qwe123 | /usr/bin/tr 123456789 '[d]'
|
|
|
|
* qwe[d]
|
|
|
|
* # echo qwe123 | /usr/bin/tr 123456789 '[d*]'
|
|
|
|
* qweddd
|
2001-05-25 03:01:09 +05:30
|
|
|
*/
|
2009-03-02 17:35:57 +05:30
|
|
|
static unsigned expand(const char *arg, char **buffer_p)
|
2000-03-05 13:37:00 +05:30
|
|
|
{
|
2009-03-02 17:35:57 +05:30
|
|
|
char *buffer = *buffer_p;
|
|
|
|
unsigned pos = 0;
|
|
|
|
unsigned size = TR_BUFSIZ;
|
2007-11-14 03:52:29 +05:30
|
|
|
unsigned i; /* can't be unsigned char: must be able to hold 256 */
|
2007-03-30 20:13:27 +05:30
|
|
|
unsigned char ac;
|
2007-11-14 03:52:29 +05:30
|
|
|
|
|
|
|
while (*arg) {
|
2009-03-02 17:35:57 +05:30
|
|
|
if (pos + ASCII > size) {
|
|
|
|
size += ASCII;
|
|
|
|
*buffer_p = buffer = xrealloc(buffer, size);
|
|
|
|
}
|
2007-11-14 03:52:29 +05:30
|
|
|
if (*arg == '\\') {
|
|
|
|
arg++;
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = bb_process_escape_sequence(&arg);
|
2007-11-14 03:52:29 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (arg[1] == '-') { /* "0-9..." */
|
|
|
|
ac = arg[2];
|
|
|
|
if (ac == '\0') { /* "0-": copy verbatim */
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = *arg++; /* copy '0' */
|
2007-11-14 03:52:29 +05:30
|
|
|
continue; /* next iter will copy '-' and stop */
|
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
i = (unsigned char) *arg;
|
2007-11-14 03:52:29 +05:30
|
|
|
while (i <= ac) /* ok: i is unsigned _int_ */
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = i++;
|
2007-11-14 03:52:29 +05:30
|
|
|
arg += 3; /* skip 0-9 */
|
|
|
|
continue;
|
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
|
|
|
|
&& *arg == '['
|
|
|
|
) {
|
2007-11-14 03:52:29 +05:30
|
|
|
arg++;
|
2009-03-02 17:35:57 +05:30
|
|
|
i = (unsigned char) *arg++;
|
|
|
|
/* "[xyz...". i=x, arg points to y */
|
|
|
|
if (ENABLE_FEATURE_TR_CLASSES && i == ':') { /* [:class:] */
|
2007-07-24 21:24:42 +05:30
|
|
|
#define CLO ":]\0"
|
2007-11-14 03:52:29 +05:30
|
|
|
static const char classes[] ALIGN1 =
|
|
|
|
"alpha"CLO "alnum"CLO "digit"CLO
|
|
|
|
"lower"CLO "upper"CLO "space"CLO
|
2009-03-01 10:20:18 +05:30
|
|
|
"blank"CLO "punct"CLO "cntrl"CLO
|
|
|
|
"xdigit"CLO;
|
2009-03-02 17:35:57 +05:30
|
|
|
enum {
|
|
|
|
CLASS_invalid = 0, /* we increment the retval */
|
|
|
|
CLASS_alpha = 1,
|
|
|
|
CLASS_alnum = 2,
|
|
|
|
CLASS_digit = 3,
|
|
|
|
CLASS_lower = 4,
|
|
|
|
CLASS_upper = 5,
|
|
|
|
CLASS_space = 6,
|
|
|
|
CLASS_blank = 7,
|
|
|
|
CLASS_punct = 8,
|
|
|
|
CLASS_cntrl = 9,
|
|
|
|
CLASS_xdigit = 10,
|
|
|
|
//CLASS_graph = 11,
|
|
|
|
//CLASS_print = 12,
|
|
|
|
};
|
2007-03-30 20:13:27 +05:30
|
|
|
smalluint j;
|
2009-03-02 17:35:57 +05:30
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
/* xdigit needs 8, not 7 */
|
|
|
|
i = 7 + (arg[0] == 'x');
|
|
|
|
tmp = xstrndup(arg, i);
|
|
|
|
j = index_in_strings(classes, tmp) + 1;
|
|
|
|
free(tmp);
|
|
|
|
|
|
|
|
if (j == CLASS_invalid)
|
|
|
|
goto skip_bracket;
|
|
|
|
|
|
|
|
arg += i;
|
2009-03-01 10:20:18 +05:30
|
|
|
if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
|
2006-04-20 03:52:06 +05:30
|
|
|
for (i = '0'; i <= '9'; i++)
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = i;
|
2007-03-30 20:13:27 +05:30
|
|
|
}
|
|
|
|
if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
|
2005-10-09 02:51:08 +05:30
|
|
|
for (i = 'A'; i <= 'Z'; i++)
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = i;
|
2005-10-09 02:51:08 +05:30
|
|
|
}
|
2007-03-30 20:13:27 +05:30
|
|
|
if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
|
2005-10-09 02:51:08 +05:30
|
|
|
for (i = 'a'; i <= 'z'; i++)
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = i;
|
2006-04-20 03:52:06 +05:30
|
|
|
}
|
2007-03-30 20:13:27 +05:30
|
|
|
if (j == CLASS_space || j == CLASS_blank) {
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = '\t';
|
2007-03-30 20:13:27 +05:30
|
|
|
if (j == CLASS_space) {
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = '\n';
|
|
|
|
buffer[pos++] = '\v';
|
|
|
|
buffer[pos++] = '\f';
|
|
|
|
buffer[pos++] = '\r';
|
2007-03-30 20:13:27 +05:30
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = ' ';
|
2006-04-20 03:52:06 +05:30
|
|
|
}
|
2007-03-30 20:13:27 +05:30
|
|
|
if (j == CLASS_punct || j == CLASS_cntrl) {
|
2009-03-02 17:35:57 +05:30
|
|
|
for (i = '\0'; i < ASCII; i++) {
|
2009-11-18 16:04:43 +05:30
|
|
|
if ((j == CLASS_punct && isprint_asciionly(i) && !isalnum(i) && !isspace(i))
|
2009-03-02 17:35:57 +05:30
|
|
|
|| (j == CLASS_cntrl && iscntrl(i))
|
|
|
|
) {
|
|
|
|
buffer[pos++] = i;
|
|
|
|
}
|
|
|
|
}
|
2005-10-09 02:51:08 +05:30
|
|
|
}
|
2009-03-01 10:20:18 +05:30
|
|
|
if (j == CLASS_xdigit) {
|
|
|
|
for (i = 'A'; i <= 'F'; i++) {
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos + 6] = i | 0x20;
|
|
|
|
buffer[pos++] = i;
|
2009-03-01 10:20:18 +05:30
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
pos += 6;
|
2009-03-01 10:20:18 +05:30
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
continue;
|
2005-10-09 02:51:08 +05:30
|
|
|
}
|
2007-11-14 03:52:29 +05:30
|
|
|
/* "[xyz...", i=x, arg points to y */
|
|
|
|
if (ENABLE_FEATURE_TR_EQUIV && i == '=') { /* [=CHAR=] */
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = *arg; /* copy CHAR */
|
|
|
|
if (!arg[0] || arg[1] != '=' || arg[2] != ']')
|
2008-07-29 05:19:42 +05:30
|
|
|
bb_show_usage();
|
2007-11-14 03:52:29 +05:30
|
|
|
arg += 3; /* skip CHAR=] */
|
2005-10-09 02:51:08 +05:30
|
|
|
continue;
|
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
/* The rest of "[xyz..." cases is treated as normal
|
|
|
|
* string, "[" has no special meaning here:
|
2009-03-01 10:20:18 +05:30
|
|
|
* tr "[a-z]" "[A-Z]" can be written as tr "a-z" "A-Z",
|
|
|
|
* also try tr "[a-z]" "_A-Z+" and you'll see that
|
|
|
|
* [] is not special here.
|
|
|
|
*/
|
2009-03-02 17:35:57 +05:30
|
|
|
skip_bracket:
|
|
|
|
arg -= 2; /* points to "[" in "[xyz..." */
|
2007-11-14 03:52:29 +05:30
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
buffer[pos++] = *arg++;
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
return pos;
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|
|
|
|
|
2009-03-02 17:35:57 +05:30
|
|
|
/* NB: buffer is guaranteed to be at least TR_BUFSIZE
|
|
|
|
* (which is >= ASCII) big.
|
|
|
|
*/
|
2006-07-01 00:34:09 +05:30
|
|
|
static int complement(char *buffer, int buffer_len)
|
2000-03-05 13:37:00 +05:30
|
|
|
{
|
2009-03-02 17:35:57 +05:30
|
|
|
int len;
|
|
|
|
char conv[ASCII];
|
|
|
|
unsigned char ch;
|
2000-05-02 04:00:37 +05:30
|
|
|
|
2009-03-01 10:20:18 +05:30
|
|
|
len = 0;
|
2009-03-02 17:35:57 +05:30
|
|
|
ch = '\0';
|
|
|
|
while (1) {
|
|
|
|
if (memchr(buffer, ch, buffer_len) == NULL)
|
|
|
|
conv[len++] = ch;
|
|
|
|
if (++ch == '\0')
|
|
|
|
break;
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|
2009-03-01 10:20:18 +05:30
|
|
|
memcpy(buffer, conv, len);
|
|
|
|
return len;
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int tr_main(int argc UNUSED_PARAM, char **argv)
|
2000-03-05 13:37:00 +05:30
|
|
|
{
|
2000-07-14 12:19:52 +05:30
|
|
|
int i;
|
2009-03-15 22:11:55 +05:30
|
|
|
smalluint opts;
|
2009-03-01 10:20:18 +05:30
|
|
|
ssize_t read_chars;
|
|
|
|
size_t in_index, out_index;
|
2007-11-14 03:52:29 +05:30
|
|
|
unsigned last = UCHAR_MAX + 1; /* not equal to any char */
|
2007-12-24 19:39:19 +05:30
|
|
|
unsigned char coded, c;
|
2009-03-02 17:35:57 +05:30
|
|
|
char *str1 = xmalloc(TR_BUFSIZ);
|
|
|
|
char *str2 = xmalloc(TR_BUFSIZ);
|
|
|
|
int str2_length;
|
|
|
|
int str1_length;
|
|
|
|
char *vector = xzalloc(ASCII * 3);
|
|
|
|
char *invec = vector + ASCII;
|
|
|
|
char *outvec = vector + ASCII * 2;
|
2007-11-14 03:53:57 +05:30
|
|
|
|
2009-03-15 22:11:55 +05:30
|
|
|
#define TR_OPT_complement (3 << 0)
|
|
|
|
#define TR_OPT_delete (1 << 2)
|
|
|
|
#define TR_OPT_squeeze_reps (1 << 3)
|
2007-11-14 03:53:57 +05:30
|
|
|
|
2009-03-02 17:35:57 +05:30
|
|
|
for (i = 0; i < ASCII; i++) {
|
2000-05-02 04:00:37 +05:30
|
|
|
vector[i] = i;
|
2007-11-14 03:53:57 +05:30
|
|
|
/*invec[i] = outvec[i] = FALSE; - done by xzalloc */
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|
|
|
|
|
2009-03-15 22:11:55 +05:30
|
|
|
/* -C/-c difference is that -C complements "characters",
|
|
|
|
* and -c complements "values" (binary bytes I guess).
|
|
|
|
* In POSIX locale, these are the same.
|
|
|
|
*/
|
|
|
|
|
2009-03-02 17:35:57 +05:30
|
|
|
opt_complementary = "-1";
|
2009-03-15 22:11:55 +05:30
|
|
|
opts = getopt32(argv, "+Ccds"); /* '+': stop at first non-option */
|
2009-03-02 17:35:57 +05:30
|
|
|
argv += optind;
|
2009-03-01 10:20:18 +05:30
|
|
|
|
2009-03-02 17:35:57 +05:30
|
|
|
str1_length = expand(*argv++, &str1);
|
|
|
|
str2_length = 0;
|
2009-03-15 22:11:55 +05:30
|
|
|
if (opts & TR_OPT_complement)
|
2009-03-02 17:35:57 +05:30
|
|
|
str1_length = complement(str1, str1_length);
|
|
|
|
if (*argv) {
|
|
|
|
if (argv[0][0] == '\0')
|
|
|
|
bb_error_msg_and_die("STRING2 cannot be empty");
|
|
|
|
str2_length = expand(*argv, &str2);
|
|
|
|
map(vector, str1, str1_length,
|
|
|
|
str2, str2_length);
|
2000-05-02 04:00:37 +05:30
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
for (i = 0; i < str1_length; i++)
|
|
|
|
invec[(unsigned char)(str1[i])] = TRUE;
|
|
|
|
for (i = 0; i < str2_length; i++)
|
|
|
|
outvec[(unsigned char)(str2[i])] = TRUE;
|
2007-06-04 15:46:52 +05:30
|
|
|
|
2009-03-01 10:20:18 +05:30
|
|
|
goto start_from;
|
|
|
|
|
2009-03-02 17:35:57 +05:30
|
|
|
/* In this loop, str1 space is reused as input buffer,
|
|
|
|
* str2 - as output one. */
|
2007-06-04 15:46:52 +05:30
|
|
|
for (;;) {
|
|
|
|
/* If we're out of input, flush output and read more input. */
|
2008-05-13 07:57:31 +05:30
|
|
|
if ((ssize_t)in_index == read_chars) {
|
2007-06-04 15:46:52 +05:30
|
|
|
if (out_index) {
|
2009-03-02 17:35:57 +05:30
|
|
|
xwrite(STDOUT_FILENO, str2, out_index);
|
2009-03-01 10:20:18 +05:30
|
|
|
start_from:
|
2007-06-04 15:46:52 +05:30
|
|
|
out_index = 0;
|
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
|
2007-06-04 15:46:52 +05:30
|
|
|
if (read_chars <= 0) {
|
2007-11-13 23:21:40 +05:30
|
|
|
if (read_chars < 0)
|
|
|
|
bb_perror_msg_and_die(bb_msg_read_error);
|
2009-03-01 10:20:18 +05:30
|
|
|
break;
|
2007-06-04 15:46:52 +05:30
|
|
|
}
|
|
|
|
in_index = 0;
|
|
|
|
}
|
2009-03-02 17:35:57 +05:30
|
|
|
c = str1[in_index++];
|
2009-03-15 22:11:55 +05:30
|
|
|
if ((opts & TR_OPT_delete) && invec[c])
|
2007-06-04 15:46:52 +05:30
|
|
|
continue;
|
2009-03-01 10:20:18 +05:30
|
|
|
coded = vector[c];
|
2009-03-15 22:11:55 +05:30
|
|
|
if ((opts & TR_OPT_squeeze_reps) && last == coded
|
2009-03-02 17:35:57 +05:30
|
|
|
&& (invec[c] || outvec[coded])
|
|
|
|
) {
|
2007-06-04 15:46:52 +05:30
|
|
|
continue;
|
2009-03-02 17:35:57 +05:30
|
|
|
}
|
|
|
|
str2[out_index++] = last = coded;
|
2007-06-04 15:46:52 +05:30
|
|
|
}
|
2009-03-01 10:20:18 +05:30
|
|
|
|
2007-03-30 20:13:27 +05:30
|
|
|
return EXIT_SUCCESS;
|
2000-03-05 13:37:00 +05:30
|
|
|
}
|