Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* See "Cat -v considered harmful" at |
| 11 | * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */ |
| 12 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | //config:config CATV |
| 14 | //config: bool "catv" |
| 15 | //config: default y |
| 16 | //config: help |
| 17 | //config: Display nonprinting characters as escape sequences (like some |
| 18 | //config: implementations' cat -v option). |
| 19 | |
| 20 | //applet:IF_CATV(APPLET(catv, BB_DIR_BIN, BB_SUID_DROP)) |
| 21 | |
| 22 | //kbuild:lib-$(CONFIG_CATV) += catv.o |
| 23 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 24 | //usage:#define catv_trivial_usage |
| 25 | //usage: "[-etv] [FILE]..." |
| 26 | //usage:#define catv_full_usage "\n\n" |
| 27 | //usage: "Display nonprinting characters as ^x or M-x\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 28 | //usage: "\n -e End each line with $" |
| 29 | //usage: "\n -t Show tabs as ^I" |
| 30 | //usage: "\n -v Don't use ^x or M-x escapes" |
| 31 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 32 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 33 | #include "common_bufsiz.h" |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 34 | |
Denys Vlasenko | 69a12fa | 2014-02-03 03:27:53 +0100 | [diff] [blame] | 35 | #define CATV_OPT_e (1<<0) |
| 36 | #define CATV_OPT_t (1<<1) |
| 37 | #define CATV_OPT_v (1<<2) |
| 38 | struct BUG_const_mismatch { |
| 39 | char BUG_const_mismatch[ |
| 40 | CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS |
| 41 | ? 1 : -1 |
| 42 | ]; |
| 43 | }; |
| 44 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 45 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 46 | int catv_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 47 | { |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 48 | int retval = EXIT_SUCCESS; |
| 49 | int fd; |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 50 | unsigned opts; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 51 | opts = getopt32(argv, "etv"); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 52 | argv += optind; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 53 | #if 0 /* These consts match, we can just pass "opts" to visible() */ |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 54 | if (opts & CATV_OPT_e) |
| 55 | flags |= VISIBLE_ENDLINE; |
| 56 | if (opts & CATV_OPT_t) |
| 57 | flags |= VISIBLE_SHOW_TABS; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 58 | #endif |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 59 | |
| 60 | /* Read from stdin if there's nothing else to do. */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 61 | if (!argv[0]) |
| 62 | *--argv = (char*)"-"; |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 63 | |
| 64 | #define read_buf bb_common_bufsiz1 |
| 65 | setup_common_bufsiz(); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 66 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 67 | fd = open_or_warn_stdin(*argv); |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 68 | if (fd < 0) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 69 | retval = EXIT_FAILURE; |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 70 | continue; |
| 71 | } |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 72 | for (;;) { |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 73 | int i, res; |
| 74 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 75 | res = read(fd, read_buf, COMMON_BUFSIZE); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 76 | if (res < 0) |
| 77 | retval = EXIT_FAILURE; |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 78 | if (res <= 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 79 | break; |
| 80 | for (i = 0; i < res; i++) { |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 81 | unsigned char c = read_buf[i]; |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 82 | if (opts & CATV_OPT_v) { |
| 83 | putchar(c); |
| 84 | } else { |
| 85 | char buf[sizeof("M-^c")]; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 86 | visible(c, buf, opts); |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 87 | fputs(buf, stdout); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 88 | } |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 91 | if (ENABLE_FEATURE_CLEAN_UP && fd) |
| 92 | close(fd); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 93 | } while (*++argv); |
| 94 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 95 | fflush_stdout_and_exit(retval); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 96 | } |