10 |
/* See "Cat -v considered harmful" at |
/* See "Cat -v considered harmful" at |
11 |
* http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */ |
* http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */ |
12 |
|
|
13 |
#include "busybox.h" |
#include "libbb.h" |
14 |
|
|
15 |
int catv_main(int argc, char **argv) |
int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
16 |
|
int catv_main(int argc UNUSED_PARAM, char **argv) |
17 |
{ |
{ |
18 |
int retval = EXIT_SUCCESS, fd; |
int retval = EXIT_SUCCESS; |
19 |
|
int fd; |
20 |
unsigned flags; |
unsigned flags; |
21 |
|
|
22 |
flags = getopt32(argc, argv, "etv"); |
flags = getopt32(argv, "etv"); |
23 |
#define CATV_OPT_e (1<<0) |
#define CATV_OPT_e (1<<0) |
24 |
#define CATV_OPT_t (1<<1) |
#define CATV_OPT_t (1<<1) |
25 |
#define CATV_OPT_v (1<<2) |
#define CATV_OPT_v (1<<2) |
26 |
flags ^= CATV_OPT_v; |
flags ^= CATV_OPT_v; |
|
|
|
27 |
argv += optind; |
argv += optind; |
28 |
|
|
29 |
|
/* Read from stdin if there's nothing else to do. */ |
30 |
|
if (!argv[0]) |
31 |
|
*--argv = (char*)"-"; |
32 |
do { |
do { |
33 |
/* Read from stdin if there's nothing else to do. */ |
fd = open_or_warn_stdin(*argv); |
34 |
fd = 0; |
if (fd < 0) { |
|
if (*argv && 0 > (fd = xopen(*argv, O_RDONLY))) |
|
35 |
retval = EXIT_FAILURE; |
retval = EXIT_FAILURE; |
36 |
else for (;;) { |
continue; |
37 |
|
} |
38 |
|
for (;;) { |
39 |
int i, res; |
int i, res; |
40 |
|
|
41 |
res = read(fd, bb_common_bufsiz1, sizeof(bb_common_bufsiz1)); |
#define read_buf bb_common_bufsiz1 |
42 |
|
res = read(fd, read_buf, COMMON_BUFSIZE); |
43 |
if (res < 0) |
if (res < 0) |
44 |
retval = EXIT_FAILURE; |
retval = EXIT_FAILURE; |
45 |
if (res < 1) |
if (res < 1) |
46 |
break; |
break; |
47 |
for (i = 0; i < res; i++) { |
for (i = 0; i < res; i++) { |
48 |
char c = bb_common_bufsiz1[i]; |
unsigned char c = read_buf[i]; |
49 |
|
|
50 |
if (c > 126 && (flags & CATV_OPT_v)) { |
if (c > 126 && (flags & CATV_OPT_v)) { |
51 |
if (c == 127) { |
if (c == 127) { |
52 |
printf("^?"); |
printf("^?"); |
53 |
continue; |
continue; |
|
} else { |
|
|
printf("M-"); |
|
|
c -= 128; |
|
54 |
} |
} |
55 |
|
printf("M-"); |
56 |
|
c -= 128; |
57 |
} |
} |
58 |
if (c < 32) { |
if (c < 32) { |
59 |
if (c == 10) { |
if (c == 10) { |
60 |
if (flags & CATV_OPT_e) |
if (flags & CATV_OPT_e) |
61 |
putchar('$'); |
bb_putchar('$'); |
62 |
} else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { |
} else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { |
63 |
printf("^%c", c+'@'); |
printf("^%c", c+'@'); |
64 |
continue; |
continue; |
65 |
} |
} |
66 |
} |
} |
67 |
putchar(c); |
bb_putchar(c); |
68 |
} |
} |
69 |
} |
} |
70 |
if (ENABLE_FEATURE_CLEAN_UP && fd) |
if (ENABLE_FEATURE_CLEAN_UP && fd) |