Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * cat implementation for busybox |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 9 | //config:config CAT |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "cat (5.6 kb)" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 11 | //config: default y |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: cat is used to concatenate files and print them to the standard |
| 14 | //config: output. Enable this option if you wish to enable the 'cat' utility. |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 15 | //config: |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 16 | //config:config FEATURE_CATN |
| 17 | //config: bool "Enable -n and -b options" |
| 18 | //config: default y |
| 19 | //config: depends on CAT |
| 20 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 21 | //config: -n numbers all output lines while -b numbers nonempty output lines. |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 22 | //config: |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 23 | //config:config FEATURE_CATV |
| 24 | //config: bool "cat -v[etA]" |
| 25 | //config: default y |
| 26 | //config: depends on CAT |
| 27 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 28 | //config: Display nonprinting characters as escape sequences |
Denys Vlasenko | e4070cb | 2010-06-04 19:59:49 +0200 | [diff] [blame] | 29 | |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 30 | //applet:IF_CAT(APPLET(cat, BB_DIR_BIN, BB_SUID_DROP)) |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 31 | |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 32 | //kbuild:lib-$(CONFIG_CAT) += cat.o |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 33 | |
| 34 | /* BB_AUDIT SUSv3 compliant */ |
| 35 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */ |
| 36 | |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 37 | //usage:#if ENABLE_FEATURE_CATN || ENABLE_FEATURE_CATV |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 38 | //usage:#define cat_trivial_usage |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 39 | //usage: "[-" IF_FEATURE_CATN("nb") IF_FEATURE_CATV("vteA") "] [FILE]..." |
| 40 | //usage:#else |
| 41 | //usage:#define cat_trivial_usage |
| 42 | //usage: "[FILE]..." |
| 43 | //usage:#endif |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 44 | //usage:#define cat_full_usage "\n\n" |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 45 | //usage: "Print FILEs to stdout\n" |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 46 | //usage: IF_FEATURE_CATN( |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 47 | //usage: "\n -n Number output lines" |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 48 | //usage: "\n -b Number nonempty lines" |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 49 | //usage: ) |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 50 | //usage: IF_FEATURE_CATV( |
| 51 | //usage: "\n -v Show nonprinting characters as ^x or M-x" |
| 52 | //usage: "\n -t ...and tabs as ^I" |
| 53 | //usage: "\n -e ...and end lines with $" |
| 54 | //usage: "\n -A Same as -vte" |
| 55 | //usage: ) |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 56 | /* |
| 57 | Longopts not implemented yet: |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 58 | --number-nonblank number nonempty output lines, overrides -n |
| 59 | --number number all output lines |
| 60 | --show-nonprinting use ^ and M- notation, except for LFD and TAB |
| 61 | --show-all equivalent to -vet |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 62 | Not implemented yet: |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 63 | -E, --show-ends display $ at end of each line (-e sans -v) |
| 64 | -T, --show-tabs display TAB characters as ^I (-t sans -v) |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 65 | -s, --squeeze-blank suppress repeated empty output lines |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 66 | */ |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 67 | //usage: |
| 68 | //usage:#define cat_example_usage |
| 69 | //usage: "$ cat /proc/uptime\n" |
| 70 | //usage: "110716.72 17.67" |
| 71 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 72 | #include "libbb.h" |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 73 | #include "common_bufsiz.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 74 | |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 75 | #if ENABLE_FEATURE_CATV |
| 76 | /* |
| 77 | * cat -v implementation for busybox |
| 78 | * |
| 79 | * Copyright (C) 2006 Rob Landley <rob@landley.net> |
| 80 | * |
| 81 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
| 82 | */ |
| 83 | /* Rob had "cat -v" implemented as a separate applet, catv. |
| 84 | * See "cat -v considered harmful" at |
| 85 | * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz |
| 86 | * From USENIX Summer Conference Proceedings, 1983 |
| 87 | * """ |
| 88 | * The talk reviews reasons for UNIX's popularity and shows, using UCB cat |
| 89 | * as a primary example, how UNIX has grown fat. cat isn't for printing |
| 90 | * files with line numbers, it isn't for compressing multiple blank lines, |
| 91 | * it's not for looking at non-printing ASCII characters, it's for |
| 92 | * concatenating files. |
| 93 | * We are reminded that ls isn't the place for code to break a single column |
| 94 | * into multiple ones, and that mailnews shouldn't have its own more |
| 95 | * processing or joke encryption code. |
| 96 | * """ |
| 97 | * |
| 98 | * I agree with the argument. Unfortunately, this ship has sailed (1983...). |
| 99 | * There are dozens of Linux distros and each of them has "cat" which supports -v. |
| 100 | * It's unrealistic for us to "reeducate" them to use our, incompatible way |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 101 | * to achieve "cat -v" effect. The actual effect would be "users pissed off |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 102 | * by gratuitous incompatibility". |
| 103 | */ |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 104 | #define CAT_OPT_e (1<<0) |
| 105 | #define CAT_OPT_t (1<<1) |
| 106 | #define CAT_OPT_v (1<<2) |
| 107 | /* -A occupies bit (1<<3) */ |
| 108 | #define CAT_OPT_n ((1<<4) * ENABLE_FEATURE_CATN) |
| 109 | #define CAT_OPT_b ((1<<5) * ENABLE_FEATURE_CATN) |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 110 | static int catv(unsigned opts, char **argv) |
| 111 | { |
| 112 | int retval = EXIT_SUCCESS; |
| 113 | int fd; |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 114 | #if ENABLE_FEATURE_CATN |
| 115 | unsigned lineno = 0; |
| 116 | unsigned eol_char = (opts & (CAT_OPT_n|CAT_OPT_b)) ? '\n' : 0x100; |
| 117 | unsigned skip_num_on = (opts & CAT_OPT_b) ? '\n' : 0x100; |
| 118 | bool eol_seen = 1; |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 119 | #endif |
| 120 | |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 121 | BUILD_BUG_ON(CAT_OPT_e != VISIBLE_ENDLINE); |
| 122 | BUILD_BUG_ON(CAT_OPT_t != VISIBLE_SHOW_TABS); |
| 123 | #if 0 /* These consts match, we can just pass "opts" to visible() */ |
| 124 | if (opts & CAT_OPT_e) |
| 125 | flags |= VISIBLE_ENDLINE; |
| 126 | if (opts & CAT_OPT_t) |
| 127 | flags |= VISIBLE_SHOW_TABS; |
| 128 | #endif |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 129 | |
| 130 | #define read_buf bb_common_bufsiz1 |
| 131 | setup_common_bufsiz(); |
| 132 | do { |
| 133 | fd = open_or_warn_stdin(*argv); |
| 134 | if (fd < 0) { |
| 135 | retval = EXIT_FAILURE; |
| 136 | continue; |
| 137 | } |
| 138 | for (;;) { |
| 139 | int i, res; |
| 140 | |
| 141 | res = read(fd, read_buf, COMMON_BUFSIZE); |
| 142 | if (res < 0) |
| 143 | retval = EXIT_FAILURE; |
| 144 | if (res <= 0) |
| 145 | break; |
| 146 | for (i = 0; i < res; i++) { |
| 147 | unsigned char c = read_buf[i]; |
| 148 | char buf[sizeof("M-^c")]; |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 149 | #if ENABLE_FEATURE_CATN |
| 150 | if (eol_seen && c != skip_num_on) |
| 151 | printf("%6u ", ++lineno); |
| 152 | eol_seen = (c == eol_char); |
| 153 | #endif |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 154 | visible(c, buf, opts); |
| 155 | fputs(buf, stdout); |
| 156 | } |
| 157 | } |
| 158 | if (ENABLE_FEATURE_CLEAN_UP && fd) |
| 159 | close(fd); |
| 160 | } while (*++argv); |
| 161 | |
| 162 | fflush_stdout_and_exit(retval); |
| 163 | } |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 164 | #undef CAT_OPT_n |
| 165 | #undef CAT_OPT_b |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 166 | #endif |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 168 | int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 169 | int cat_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 170 | { |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 171 | unsigned opts; |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 172 | |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 173 | IF_FEATURE_CATV(opt_complementary = "Aetv"; /* -A == -vet */) |
| 174 | /* -u is ignored ("unbuffered") */ |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 175 | opts = getopt32(argv, IF_FEATURE_CATV("etvA") IF_FEATURE_CATN("nb") "u"); |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 176 | argv += optind; |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 177 | |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 178 | /* Read from stdin if there's nothing else to do. */ |
| 179 | if (!argv[0]) |
| 180 | *--argv = (char*)"-"; |
| 181 | |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 182 | #if ENABLE_FEATURE_CATV |
| 183 | if (opts & 7) |
| 184 | return catv(opts, argv); |
Denys Vlasenko | e31ca2e | 2017-04-12 17:17:29 +0200 | [diff] [blame] | 185 | opts >>= 4; |
| 186 | #endif |
| 187 | |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 188 | #if ENABLE_FEATURE_CATN |
| 189 | # define CAT_OPT_n (1<<0) |
| 190 | # define CAT_OPT_b (1<<1) |
| 191 | if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */ |
Denys Vlasenko | 75e90b1 | 2017-07-14 10:47:18 +0200 | [diff] [blame] | 192 | struct number_state ns; |
| 193 | |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 194 | ns.width = 6; |
| 195 | ns.start = 1; |
| 196 | ns.inc = 1; |
| 197 | ns.sep = "\t"; |
| 198 | ns.empty_str = "\n"; |
| 199 | ns.all = !(opts & CAT_OPT_b); /* -n without -b */ |
| 200 | ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */ |
| 201 | do { |
| 202 | print_numbered_lines(&ns, *argv); |
| 203 | } while (*++argv); |
| 204 | fflush_stdout_and_exit(EXIT_SUCCESS); |
| 205 | } |
| 206 | /*opts >>= 2;*/ |
| 207 | #endif |
Denys Vlasenko | d88f94a | 2017-04-05 18:17:17 +0200 | [diff] [blame] | 208 | |
Kang-Che Sung | a2bdc5c | 2017-07-14 09:56:13 +0200 | [diff] [blame] | 209 | return bb_cat(argv); |
Denis Vlasenko | bf66fbc | 2006-12-21 13:23:14 +0000 | [diff] [blame] | 210 | } |