[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * cat -v implementation for busybox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (C) 2006 Rob Landley <rob@landley.net>
|
---|
| 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
| 10 | /* See "Cat -v considered harmful" at
|
---|
| 11 | * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
|
---|
| 12 |
|
---|
[3232] | 13 | //usage:#define catv_trivial_usage
|
---|
| 14 | //usage: "[-etv] [FILE]..."
|
---|
| 15 | //usage:#define catv_full_usage "\n\n"
|
---|
| 16 | //usage: "Display nonprinting characters as ^x or M-x\n"
|
---|
| 17 | //usage: "\n -e End each line with $"
|
---|
| 18 | //usage: "\n -t Show tabs as ^I"
|
---|
| 19 | //usage: "\n -v Don't use ^x or M-x escapes"
|
---|
| 20 |
|
---|
[1765] | 21 | #include "libbb.h"
|
---|
[821] | 22 |
|
---|
[2725] | 23 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 24 | int catv_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 25 | {
|
---|
[1765] | 26 | int retval = EXIT_SUCCESS;
|
---|
| 27 | int fd;
|
---|
| 28 | unsigned flags;
|
---|
[821] | 29 |
|
---|
[1765] | 30 | flags = getopt32(argv, "etv");
|
---|
| 31 | #define CATV_OPT_e (1<<0)
|
---|
| 32 | #define CATV_OPT_t (1<<1)
|
---|
| 33 | #define CATV_OPT_v (1<<2)
|
---|
| 34 | flags ^= CATV_OPT_v;
|
---|
| 35 | argv += optind;
|
---|
[821] | 36 |
|
---|
[1765] | 37 | /* Read from stdin if there's nothing else to do. */
|
---|
[2725] | 38 | if (!argv[0])
|
---|
| 39 | *--argv = (char*)"-";
|
---|
[821] | 40 | do {
|
---|
[2725] | 41 | fd = open_or_warn_stdin(*argv);
|
---|
[1765] | 42 | if (fd < 0) {
|
---|
| 43 | retval = EXIT_FAILURE;
|
---|
| 44 | continue;
|
---|
| 45 | }
|
---|
| 46 | for (;;) {
|
---|
[821] | 47 | int i, res;
|
---|
| 48 |
|
---|
[1765] | 49 | #define read_buf bb_common_bufsiz1
|
---|
| 50 | res = read(fd, read_buf, COMMON_BUFSIZE);
|
---|
| 51 | if (res < 0)
|
---|
| 52 | retval = EXIT_FAILURE;
|
---|
| 53 | if (res < 1)
|
---|
| 54 | break;
|
---|
| 55 | for (i = 0; i < res; i++) {
|
---|
| 56 | unsigned char c = read_buf[i];
|
---|
[821] | 57 |
|
---|
[1765] | 58 | if (c > 126 && (flags & CATV_OPT_v)) {
|
---|
[821] | 59 | if (c == 127) {
|
---|
| 60 | printf("^?");
|
---|
| 61 | continue;
|
---|
| 62 | }
|
---|
[1765] | 63 | printf("M-");
|
---|
| 64 | c -= 128;
|
---|
[821] | 65 | }
|
---|
| 66 | if (c < 32) {
|
---|
| 67 | if (c == 10) {
|
---|
[1765] | 68 | if (flags & CATV_OPT_e)
|
---|
[2725] | 69 | bb_putchar('$');
|
---|
[1765] | 70 | } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
|
---|
[821] | 71 | printf("^%c", c+'@');
|
---|
| 72 | continue;
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
[2725] | 75 | bb_putchar(c);
|
---|
[821] | 76 | }
|
---|
| 77 | }
|
---|
[1765] | 78 | if (ENABLE_FEATURE_CLEAN_UP && fd)
|
---|
| 79 | close(fd);
|
---|
[821] | 80 | } while (*++argv);
|
---|
| 81 |
|
---|
[1765] | 82 | fflush_stdout_and_exit(retval);
|
---|
[821] | 83 | }
|
---|