123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- #include <stdio.h>
- #include <string.h>
- #include <limits.h>
- #include <unistd.h>
- #define WANT_HEX_ESCAPES 1
- #undef _tolower
- #define _tolower(X) ((X)|((char) 0x20))
- static char bb_process_escape_sequence(const char **ptr)
- {
- static const char charmap[] = {
- 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
- '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
- const char *p;
- const char *q;
- unsigned int num_digits;
- unsigned int r;
- unsigned int n;
- unsigned int d;
- unsigned int base;
- num_digits = n = 0;
- base = 8;
- q = *ptr;
- #ifdef WANT_HEX_ESCAPES
- if (*q == 'x') {
- ++q;
- base = 16;
- ++num_digits;
- }
- #endif
- do {
- d = (unsigned char)(*q) - '0';
- #ifdef WANT_HEX_ESCAPES
- if (d >= 10) {
- d = (unsigned char)(_tolower(*q)) - 'a' + 10;
- }
- #endif
- if (d >= base) {
- #ifdef WANT_HEX_ESCAPES
- if ((base == 16) && (!--num_digits)) {
- --q;
- }
- #endif
- break;
- }
- r = n * base + d;
- if (r > UCHAR_MAX) {
- break;
- }
- n = r;
- ++q;
- } while (++num_digits < 3);
- if (num_digits == 0) {
- p = charmap;
- do {
- if (*p == *q) {
- q++;
- break;
- }
- } while (*++p);
- n = *(p + (sizeof(charmap)/2));
- }
- *ptr = q;
- return (char) n;
- }
- int main(int argc, char **argv)
- {
- const char *arg;
- const char *p;
- char nflag = 1;
- char eflag = 0;
-
- if (dup2(1, 1) != 1)
- return -1;
- while (1) {
- arg = *++argv;
- if (!arg)
- goto newline_ret;
- if (*arg != '-')
- break;
-
- p = arg + 1;
- if (!*p)
- goto just_echo;
- do {
- if (!strrchr("neE", *p))
- goto just_echo;
- } while (*++p);
-
- p = arg + 1;
- do {
- if (*p == 'n')
- nflag = 0;
- if (*p == 'e')
- eflag = '\\';
- } while (*++p);
- }
- just_echo:
- while (1) {
-
- int c;
- if (!eflag) {
-
- fputs(arg, stdout);
- } else
- while ((c = *arg++) != '\0') {
- if (c == eflag) {
-
- if (*arg == 'c') {
-
- goto ret;
- }
-
- if (*arg == '0') {
- if ((unsigned char)(arg[1] - '0') < 8) {
-
- arg++;
- }
- }
-
- {
-
- const char *z = arg;
- c = bb_process_escape_sequence(&z);
- arg = z;
- }
- }
- putchar(c);
- }
- arg = *++argv;
- if (!arg)
- break;
- putchar(' ');
- }
- newline_ret:
- if (nflag) {
- putchar('\n');
- }
- ret:
- return fflush(NULL);
- }
|