blob: 6bb73ba63d5e8341113fafbddf5a7d0b8b13761b [file] [log] [blame]
Rob Landley8abbee42006-05-31 19:36:04 +00001/* 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 Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Rob Landley8abbee42006-05-31 19:36:04 +00008 */
9
10/* See "Cat -v considered harmful" at
11 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
12
Pere Orga34425382011-03-31 14:43:25 +020013//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 Orga34425382011-03-31 14:43:25 +020017//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 Vlasenkob6adbf12007-05-26 19:00:18 +000021#include "libbb.h"
Rob Landley8abbee42006-05-31 19:36:04 +000022
Denys Vlasenko69a12fa2014-02-03 03:27:53 +010023#define CATV_OPT_e (1<<0)
24#define CATV_OPT_t (1<<1)
25#define CATV_OPT_v (1<<2)
26struct 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 Vlasenko9b49a5e2007-10-11 10:05:36 +000033int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000034int catv_main(int argc UNUSED_PARAM, char **argv)
Rob Landley8abbee42006-05-31 19:36:04 +000035{
Denis Vlasenko8e209c32007-08-06 12:28:24 +000036 int retval = EXIT_SUCCESS;
37 int fd;
Bartosz Golaszewski79c618c2013-07-30 06:29:42 +020038 unsigned opts;
Denys Vlasenkobfa6ed12013-07-30 11:41:58 +020039 opts = getopt32(argv, "etv");
Rob Landley8abbee42006-05-31 19:36:04 +000040 argv += optind;
Denys Vlasenkobfa6ed12013-07-30 11:41:58 +020041#if 0 /* These consts match, we can just pass "opts" to visible() */
Bartosz Golaszewski79c618c2013-07-30 06:29:42 +020042 if (opts & CATV_OPT_e)
43 flags |= VISIBLE_ENDLINE;
44 if (opts & CATV_OPT_t)
45 flags |= VISIBLE_SHOW_TABS;
Denys Vlasenkobfa6ed12013-07-30 11:41:58 +020046#endif
Denis Vlasenko16d58d72007-06-12 08:13:34 +000047
48 /* Read from stdin if there's nothing else to do. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000049 if (!argv[0])
50 *--argv = (char*)"-";
Rob Landley8abbee42006-05-31 19:36:04 +000051 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000052 fd = open_or_warn_stdin(*argv);
Denis Vlasenko16d58d72007-06-12 08:13:34 +000053 if (fd < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000054 retval = EXIT_FAILURE;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000055 continue;
56 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000057 for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000058 int i, res;
59
Denis Vlasenko74324c82007-06-04 10:16:52 +000060#define read_buf bb_common_bufsiz1
61 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000062 if (res < 0)
63 retval = EXIT_FAILURE;
Bartosz Golaszewski79c618c2013-07-30 06:29:42 +020064 if (res <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000065 break;
66 for (i = 0; i < res; i++) {
Denis Vlasenko8e209c32007-08-06 12:28:24 +000067 unsigned char c = read_buf[i];
Bartosz Golaszewski79c618c2013-07-30 06:29:42 +020068 if (opts & CATV_OPT_v) {
69 putchar(c);
70 } else {
71 char buf[sizeof("M-^c")];
Denys Vlasenkobfa6ed12013-07-30 11:41:58 +020072 visible(c, buf, opts);
Bartosz Golaszewski79c618c2013-07-30 06:29:42 +020073 fputs(buf, stdout);
Rob Landley8abbee42006-05-31 19:36:04 +000074 }
Rob Landley8abbee42006-05-31 19:36:04 +000075 }
76 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000077 if (ENABLE_FEATURE_CLEAN_UP && fd)
78 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000079 } while (*++argv);
80
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000081 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000082}