blob: e3499c59787947c56f64522312d1f46fcce55190 [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
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000023int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000024int catv_main(int argc UNUSED_PARAM, char **argv)
Rob Landley8abbee42006-05-31 19:36:04 +000025{
Denis Vlasenko8e209c32007-08-06 12:28:24 +000026 int retval = EXIT_SUCCESS;
27 int fd;
maxwen27116ba2015-08-14 21:41:28 +020028 unsigned opts;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000029#define CATV_OPT_e (1<<0)
30#define CATV_OPT_t (1<<1)
31#define CATV_OPT_v (1<<2)
maxwen27116ba2015-08-14 21:41:28 +020032 typedef char BUG_const_mismatch[
33 CATV_OPT_e == VISIBLE_ENDLINE && CATV_OPT_t == VISIBLE_SHOW_TABS
34 ? 1 : -1
35 ];
36
37 opts = getopt32(argv, "etv");
Rob Landley8abbee42006-05-31 19:36:04 +000038 argv += optind;
maxwen27116ba2015-08-14 21:41:28 +020039#if 0 /* These consts match, we can just pass "opts" to visible() */
40 if (opts & CATV_OPT_e)
41 flags |= VISIBLE_ENDLINE;
42 if (opts & CATV_OPT_t)
43 flags |= VISIBLE_SHOW_TABS;
44#endif
Denis Vlasenko16d58d72007-06-12 08:13:34 +000045
46 /* Read from stdin if there's nothing else to do. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000047 if (!argv[0])
48 *--argv = (char*)"-";
Rob Landley8abbee42006-05-31 19:36:04 +000049 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000050 fd = open_or_warn_stdin(*argv);
Denis Vlasenko16d58d72007-06-12 08:13:34 +000051 if (fd < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000052 retval = EXIT_FAILURE;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000053 continue;
54 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000055 for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000056 int i, res;
57
Denis Vlasenko74324c82007-06-04 10:16:52 +000058#define read_buf bb_common_bufsiz1
59 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000060 if (res < 0)
61 retval = EXIT_FAILURE;
maxwen27116ba2015-08-14 21:41:28 +020062 if (res <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000063 break;
64 for (i = 0; i < res; i++) {
Denis Vlasenko8e209c32007-08-06 12:28:24 +000065 unsigned char c = read_buf[i];
maxwen27116ba2015-08-14 21:41:28 +020066 if (opts & CATV_OPT_v) {
67 putchar(c);
68 } else {
69 char buf[sizeof("M-^c")];
70 visible(c, buf, opts);
71 fputs(buf, stdout);
Rob Landley8abbee42006-05-31 19:36:04 +000072 }
Rob Landley8abbee42006-05-31 19:36:04 +000073 }
74 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000075 if (ENABLE_FEATURE_CLEAN_UP && fd)
76 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000077 } while (*++argv);
78
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000079 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000080}