123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #include "libbb.h"
- enum {
- ASCII = 256,
-
- TR_BUFSIZ = (BUFSIZ > ASCII*2) ? BUFSIZ : ASCII*2,
- };
- static void map(char *pvector,
- char *string1, unsigned string1_len,
- char *string2, unsigned string2_len)
- {
- char last = '0';
- unsigned i, j;
- for (j = 0, i = 0; i < string1_len; i++) {
- if (string2_len <= j)
- pvector[(unsigned char)(string1[i])] = last;
- else
- pvector[(unsigned char)(string1[i])] = last = string2[j++];
- }
- }
- static unsigned expand(char *arg, char **buffer_p)
- {
- char *buffer = *buffer_p;
- unsigned pos = 0;
- unsigned size = TR_BUFSIZ;
- unsigned i;
- unsigned char ac;
- while (*arg) {
- if (pos + ASCII > size) {
- size += ASCII;
- *buffer_p = buffer = xrealloc(buffer, size);
- }
- if (*arg == '\\') {
- const char *z;
- arg++;
- z = arg;
- ac = bb_process_escape_sequence(&z);
- arg = (char *)z;
- arg--;
- *arg = ac;
-
- }
- if (arg[1] == '-') {
- ac = arg[2];
- if (ac == '\0') {
- buffer[pos++] = *arg++;
- continue;
- }
- i = (unsigned char) *arg;
- arg += 3;
- if (ac == '\\') {
- const char *z;
- z = arg;
- ac = bb_process_escape_sequence(&z);
- arg = (char *)z;
- }
- while (i <= ac)
- buffer[pos++] = i++;
- continue;
- }
- if ((ENABLE_FEATURE_TR_CLASSES || ENABLE_FEATURE_TR_EQUIV)
- && *arg == '['
- ) {
- arg++;
- i = (unsigned char) *arg++;
-
- if (ENABLE_FEATURE_TR_CLASSES && i == ':') {
- #define CLO ":]\0"
- static const char classes[] ALIGN1 =
- "alpha"CLO "alnum"CLO "digit"CLO
- "lower"CLO "upper"CLO "space"CLO
- "blank"CLO "punct"CLO "cntrl"CLO
- "xdigit"CLO;
- enum {
- CLASS_invalid = 0,
- 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,
-
-
- };
- smalluint j;
- char *tmp;
-
- 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;
- if (j == CLASS_alnum || j == CLASS_digit || j == CLASS_xdigit) {
- for (i = '0'; i <= '9'; i++)
- buffer[pos++] = i;
- }
- if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_upper) {
- for (i = 'A'; i <= 'Z'; i++)
- buffer[pos++] = i;
- }
- if (j == CLASS_alpha || j == CLASS_alnum || j == CLASS_lower) {
- for (i = 'a'; i <= 'z'; i++)
- buffer[pos++] = i;
- }
- if (j == CLASS_space || j == CLASS_blank) {
- buffer[pos++] = '\t';
- if (j == CLASS_space) {
- buffer[pos++] = '\n';
- buffer[pos++] = '\v';
- buffer[pos++] = '\f';
- buffer[pos++] = '\r';
- }
- buffer[pos++] = ' ';
- }
- if (j == CLASS_punct || j == CLASS_cntrl) {
- for (i = '\0'; i < ASCII; i++) {
- if ((j == CLASS_punct && isprint_asciionly(i) && !isalnum(i) && !isspace(i))
- || (j == CLASS_cntrl && iscntrl(i))
- ) {
- buffer[pos++] = i;
- }
- }
- }
- if (j == CLASS_xdigit) {
- for (i = 'A'; i <= 'F'; i++) {
- buffer[pos + 6] = i | 0x20;
- buffer[pos++] = i;
- }
- pos += 6;
- }
- continue;
- }
-
- if (ENABLE_FEATURE_TR_EQUIV && i == '=') {
- buffer[pos++] = *arg;
- if (!arg[0] || arg[1] != '=' || arg[2] != ']')
- bb_show_usage();
- arg += 3;
- continue;
- }
-
- skip_bracket:
- arg -= 2;
- }
- buffer[pos++] = *arg++;
- }
- return pos;
- }
- static int complement(char *buffer, int buffer_len)
- {
- int len;
- char conv[ASCII];
- unsigned char ch;
- len = 0;
- ch = '\0';
- while (1) {
- if (memchr(buffer, ch, buffer_len) == NULL)
- conv[len++] = ch;
- if (++ch == '\0')
- break;
- }
- memcpy(buffer, conv, len);
- return len;
- }
- int tr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int tr_main(int argc UNUSED_PARAM, char **argv)
- {
- int i;
- smalluint opts;
- ssize_t read_chars;
- size_t in_index, out_index;
- unsigned last = UCHAR_MAX + 1;
- unsigned char coded, c;
- 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;
- #define TR_OPT_complement (3 << 0)
- #define TR_OPT_delete (1 << 2)
- #define TR_OPT_squeeze_reps (1 << 3)
- for (i = 0; i < ASCII; i++) {
- vector[i] = i;
-
- }
-
-
- opts = getopt32(argv, "^+" "Ccds" "\0" "-1:?2");
- argv += optind;
- str1_length = expand(*argv++, &str1);
- str2_length = 0;
- if (opts & TR_OPT_complement)
- str1_length = complement(str1, str1_length);
- if (*argv) {
- if (argv[0][0] == '\0')
- bb_simple_error_msg_and_die("STRING2 cannot be empty");
- str2_length = expand(*argv, &str2);
- map(vector, str1, str1_length,
- str2, str2_length);
- }
- 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;
- goto start_from;
-
- for (;;) {
-
- if ((ssize_t)in_index == read_chars) {
- if (out_index) {
- xwrite(STDOUT_FILENO, str2, out_index);
- start_from:
- out_index = 0;
- }
- read_chars = safe_read(STDIN_FILENO, str1, TR_BUFSIZ);
- if (read_chars <= 0) {
- if (read_chars < 0)
- bb_simple_perror_msg_and_die(bb_msg_read_error);
- break;
- }
- in_index = 0;
- }
- c = str1[in_index++];
- if ((opts & TR_OPT_delete) && invec[c])
- continue;
- coded = vector[c];
- if ((opts & TR_OPT_squeeze_reps) && last == coded
- && (invec[c] || outvec[coded])
- ) {
- continue;
- }
- str2[out_index++] = last = coded;
- }
- if (ENABLE_FEATURE_CLEAN_UP) {
- free(vector);
- free(str2);
- free(str1);
- }
- return EXIT_SUCCESS;
- }
|