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 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 22 | |
Denys Vlasenko | 69a12fa | 2014-02-03 03:27:53 +0100 | [diff] [blame] | 23 | #define CATV_OPT_e (1<<0) |
| 24 | #define CATV_OPT_t (1<<1) |
| 25 | #define CATV_OPT_v (1<<2) |
| 26 | struct BUG_const_mismatch { |
| 27 | char BUG_const_mismatch[ |
| 28 | CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS |
| 29 | ? 1 : -1 |
| 30 | ]; |
| 31 | }; |
| 32 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 33 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 34 | int catv_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 35 | { |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 36 | int retval = EXIT_SUCCESS; |
| 37 | int fd; |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 38 | unsigned opts; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 39 | opts = getopt32(argv, "etv"); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 40 | argv += optind; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 41 | #if 0 /* These consts match, we can just pass "opts" to visible() */ |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 42 | if (opts & CATV_OPT_e) |
| 43 | flags |= VISIBLE_ENDLINE; |
| 44 | if (opts & CATV_OPT_t) |
| 45 | flags |= VISIBLE_SHOW_TABS; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 46 | #endif |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 47 | |
| 48 | /* Read from stdin if there's nothing else to do. */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 49 | if (!argv[0]) |
| 50 | *--argv = (char*)"-"; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 51 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 52 | fd = open_or_warn_stdin(*argv); |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 53 | if (fd < 0) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 54 | retval = EXIT_FAILURE; |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 55 | continue; |
| 56 | } |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 57 | for (;;) { |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 58 | int i, res; |
| 59 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 60 | #define read_buf bb_common_bufsiz1 |
| 61 | res = read(fd, read_buf, COMMON_BUFSIZE); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 62 | if (res < 0) |
| 63 | retval = EXIT_FAILURE; |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 64 | if (res <= 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 65 | break; |
| 66 | for (i = 0; i < res; i++) { |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 67 | unsigned char c = read_buf[i]; |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 68 | if (opts & CATV_OPT_v) { |
| 69 | putchar(c); |
| 70 | } else { |
| 71 | char buf[sizeof("M-^c")]; |
Denys Vlasenko | bfa6ed1 | 2013-07-30 11:41:58 +0200 | [diff] [blame] | 72 | visible(c, buf, opts); |
Bartosz Golaszewski | 79c618c | 2013-07-30 06:29:42 +0200 | [diff] [blame] | 73 | fputs(buf, stdout); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 74 | } |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 77 | if (ENABLE_FEATURE_CLEAN_UP && fd) |
| 78 | close(fd); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 79 | } while (*++argv); |
| 80 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 81 | fflush_stdout_and_exit(retval); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 82 | } |