cat: allow compiling out -n and -b
[oweals/busybox.git] / coreutils / cat.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * cat implementation for busybox
4  *
5  * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
6  *
7  * Licensed under GPLv2, see file LICENSE in this source tree.
8  */
9 //config:config CAT
10 //config:       bool "cat"
11 //config:       default y
12 //config:       help
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.
15 //config:
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
21 //config:         -n numbers all output lines while -b numbers nonempty output lines.
22 //config:
23 //config:config FEATURE_CATV
24 //config:       bool "cat -v[etA]"
25 //config:       default y
26 //config:       depends on CAT
27 //config:       help
28 //config:         Display nonprinting characters as escape sequences
29
30 //applet:IF_CAT(APPLET(cat, BB_DIR_BIN, BB_SUID_DROP))
31
32 //kbuild:lib-$(CONFIG_CAT) += cat.o
33
34 /* BB_AUDIT SUSv3 compliant */
35 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
36
37 //usage:#if ENABLE_FEATURE_CATN || ENABLE_FEATURE_CATV
38 //usage:#define cat_trivial_usage
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
44 //usage:#define cat_full_usage "\n\n"
45 //usage:       "Print FILEs to stdout\n"
46 //usage:        IF_FEATURE_CATN(
47 //usage:     "\n        -n      Number output lines"
48 //usage:     "\n        -b      Number nonempty lines"
49 //usage:        )
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:        )
56 /*
57   Longopts not implemented yet:
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
62   Not implemented yet:
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)
65   -s, --squeeze-blank      suppress repeated empty output lines
66 */
67 //usage:
68 //usage:#define cat_example_usage
69 //usage:       "$ cat /proc/uptime\n"
70 //usage:       "110716.72 17.67"
71
72 #include "libbb.h"
73 #include "common_bufsiz.h"
74
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
101  * to achieve "cat -v" effect. The actual effect would be "users pissed off
102  * by gratuitous incompatibility".
103  */
104 #define CATV_OPT_e (1<<0)
105 #define CATV_OPT_t (1<<1)
106 #define CATV_OPT_v (1<<2)
107 static int catv(unsigned opts, char **argv)
108 {
109         int retval = EXIT_SUCCESS;
110         int fd;
111
112         BUILD_BUG_ON(CATV_OPT_e != VISIBLE_ENDLINE);
113         BUILD_BUG_ON(CATV_OPT_t != VISIBLE_SHOW_TABS);
114 #if 0 /* These consts match, we can just pass "opts" to visible() */
115         if (opts & CATV_OPT_e)
116                 flags |= VISIBLE_ENDLINE;
117         if (opts & CATV_OPT_t)
118                 flags |= VISIBLE_SHOW_TABS;
119 #endif
120
121         /* Read from stdin if there's nothing else to do. */
122         if (!argv[0])
123                 *--argv = (char*)"-";
124
125 #define read_buf bb_common_bufsiz1
126         setup_common_bufsiz();
127         do {
128                 fd = open_or_warn_stdin(*argv);
129                 if (fd < 0) {
130                         retval = EXIT_FAILURE;
131                         continue;
132                 }
133                 for (;;) {
134                         int i, res;
135
136                         res = read(fd, read_buf, COMMON_BUFSIZE);
137                         if (res < 0)
138                                 retval = EXIT_FAILURE;
139                         if (res <= 0)
140                                 break;
141                         for (i = 0; i < res; i++) {
142                                 unsigned char c = read_buf[i];
143                                 char buf[sizeof("M-^c")];
144                                 visible(c, buf, opts);
145                                 fputs(buf, stdout);
146                         }
147                 }
148                 if (ENABLE_FEATURE_CLEAN_UP && fd)
149                         close(fd);
150         } while (*++argv);
151
152         fflush_stdout_and_exit(retval);
153 }
154 #endif
155
156 int cat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
157 int cat_main(int argc UNUSED_PARAM, char **argv)
158 {
159         struct number_state ns;
160         unsigned opts;
161
162         IF_FEATURE_CATV(opt_complementary = "Aetv"; /* -A == -vet */)
163         /* -u is ignored ("unbuffered") */
164         opts = getopt32(argv, IF_FEATURE_CATV("etvA") IF_FEATURE_CATN("nb") "u");
165         argv += optind;
166
167 #if ENABLE_FEATURE_CATV
168         if (opts & 7)
169                 return catv(opts, argv);
170 //BUG: -v,-e,-t,-A ignore -nb
171         opts >>= 4;
172 #endif
173
174 #if ENABLE_FEATURE_CATN
175 # define CAT_OPT_n (1<<0)
176 # define CAT_OPT_b (1<<1)
177         if (opts & (CAT_OPT_n|CAT_OPT_b)) { /* -n or -b */
178                 if (!*argv)
179                         *--argv = (char*)"-";
180                 ns.width = 6;
181                 ns.start = 1;
182                 ns.inc = 1;
183                 ns.sep = "\t";
184                 ns.empty_str = "\n";
185                 ns.all = !(opts & CAT_OPT_b); /* -n without -b */
186                 ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */
187                 do {
188                         print_numbered_lines(&ns, *argv);
189                 } while (*++argv);
190                 fflush_stdout_and_exit(EXIT_SUCCESS);
191         }
192         /*opts >>= 2;*/
193 #endif
194
195         return bb_cat(argv);
196 }