[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"
|
---|
[3621] | 22 | #include "common_bufsiz.h"
|
---|
[821] | 23 |
|
---|
[3621] | 24 | #define CATV_OPT_e (1<<0)
|
---|
| 25 | #define CATV_OPT_t (1<<1)
|
---|
| 26 | #define CATV_OPT_v (1<<2)
|
---|
| 27 | struct BUG_const_mismatch {
|
---|
| 28 | char BUG_const_mismatch[
|
---|
| 29 | CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS
|
---|
| 30 | ? 1 : -1
|
---|
| 31 | ];
|
---|
| 32 | };
|
---|
| 33 |
|
---|
[2725] | 34 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 35 | int catv_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 36 | {
|
---|
[1765] | 37 | int retval = EXIT_SUCCESS;
|
---|
| 38 | int fd;
|
---|
[3621] | 39 | unsigned opts;
|
---|
| 40 | opts = getopt32(argv, "etv");
|
---|
[1765] | 41 | argv += optind;
|
---|
[3621] | 42 | #if 0 /* These consts match, we can just pass "opts" to visible() */
|
---|
| 43 | if (opts & CATV_OPT_e)
|
---|
| 44 | flags |= VISIBLE_ENDLINE;
|
---|
| 45 | if (opts & CATV_OPT_t)
|
---|
| 46 | flags |= VISIBLE_SHOW_TABS;
|
---|
| 47 | #endif
|
---|
[821] | 48 |
|
---|
[1765] | 49 | /* Read from stdin if there's nothing else to do. */
|
---|
[2725] | 50 | if (!argv[0])
|
---|
| 51 | *--argv = (char*)"-";
|
---|
[3621] | 52 |
|
---|
| 53 | #define read_buf bb_common_bufsiz1
|
---|
| 54 | setup_common_bufsiz();
|
---|
[821] | 55 | do {
|
---|
[2725] | 56 | fd = open_or_warn_stdin(*argv);
|
---|
[1765] | 57 | if (fd < 0) {
|
---|
| 58 | retval = EXIT_FAILURE;
|
---|
| 59 | continue;
|
---|
| 60 | }
|
---|
| 61 | for (;;) {
|
---|
[821] | 62 | int i, res;
|
---|
| 63 |
|
---|
[1765] | 64 | res = read(fd, read_buf, COMMON_BUFSIZE);
|
---|
| 65 | if (res < 0)
|
---|
| 66 | retval = EXIT_FAILURE;
|
---|
[3621] | 67 | if (res <= 0)
|
---|
[1765] | 68 | break;
|
---|
| 69 | for (i = 0; i < res; i++) {
|
---|
| 70 | unsigned char c = read_buf[i];
|
---|
[3621] | 71 | if (opts & CATV_OPT_v) {
|
---|
| 72 | putchar(c);
|
---|
| 73 | } else {
|
---|
| 74 | char buf[sizeof("M-^c")];
|
---|
| 75 | visible(c, buf, opts);
|
---|
| 76 | fputs(buf, stdout);
|
---|
[821] | 77 | }
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[1765] | 80 | if (ENABLE_FEATURE_CLEAN_UP && fd)
|
---|
| 81 | close(fd);
|
---|
[821] | 82 | } while (*++argv);
|
---|
| 83 |
|
---|
[1765] | 84 | fflush_stdout_and_exit(retval);
|
---|
[821] | 85 | }
|
---|