blob: 37c7ed2d1425020680c0d73ea42e6f3d32f1b74d [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"
17//usage: "\nOptions:"
18//usage: "\n -e End each line with $"
19//usage: "\n -t Show tabs as ^I"
20//usage: "\n -v Don't use ^x or M-x escapes"
21
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Rob Landley8abbee42006-05-31 19:36:04 +000023
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000024int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000025int catv_main(int argc UNUSED_PARAM, char **argv)
Rob Landley8abbee42006-05-31 19:36:04 +000026{
Denis Vlasenko8e209c32007-08-06 12:28:24 +000027 int retval = EXIT_SUCCESS;
28 int fd;
Denis Vlasenko7039a662006-10-08 17:54:47 +000029 unsigned flags;
Rob Landley8abbee42006-05-31 19:36:04 +000030
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000031 flags = getopt32(argv, "etv");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000032#define CATV_OPT_e (1<<0)
33#define CATV_OPT_t (1<<1)
34#define CATV_OPT_v (1<<2)
35 flags ^= CATV_OPT_v;
Rob Landley8abbee42006-05-31 19:36:04 +000036 argv += optind;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000037
38 /* Read from stdin if there's nothing else to do. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000039 if (!argv[0])
40 *--argv = (char*)"-";
Rob Landley8abbee42006-05-31 19:36:04 +000041 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000042 fd = open_or_warn_stdin(*argv);
Denis Vlasenko16d58d72007-06-12 08:13:34 +000043 if (fd < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000044 retval = EXIT_FAILURE;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000045 continue;
46 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000047 for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000048 int i, res;
49
Denis Vlasenko74324c82007-06-04 10:16:52 +000050#define read_buf bb_common_bufsiz1
51 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000052 if (res < 0)
53 retval = EXIT_FAILURE;
54 if (res < 1)
55 break;
56 for (i = 0; i < res; i++) {
Denis Vlasenko8e209c32007-08-06 12:28:24 +000057 unsigned char c = read_buf[i];
Rob Landley8abbee42006-05-31 19:36:04 +000058
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000059 if (c > 126 && (flags & CATV_OPT_v)) {
Rob Landley8abbee42006-05-31 19:36:04 +000060 if (c == 127) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000061 printf("^?");
Rob Landley8abbee42006-05-31 19:36:04 +000062 continue;
Rob Landley8abbee42006-05-31 19:36:04 +000063 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000064 printf("M-");
65 c -= 128;
Rob Landley8abbee42006-05-31 19:36:04 +000066 }
67 if (c < 32) {
68 if (c == 10) {
Denis Vlasenko7039a662006-10-08 17:54:47 +000069 if (flags & CATV_OPT_e)
Denis Vlasenko4daad902007-09-27 10:20:47 +000070 bb_putchar('$');
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000071 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000072 printf("^%c", c+'@');
Rob Landley8abbee42006-05-31 19:36:04 +000073 continue;
74 }
75 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000076 bb_putchar(c);
Rob Landley8abbee42006-05-31 19:36:04 +000077 }
78 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000079 if (ENABLE_FEATURE_CLEAN_UP && fd)
80 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000081 } while (*++argv);
82
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000083 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000084}