blob: 214b4311a6e32efd1d6c67037a4d96a277305ea6 [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;
Denis Vlasenko7039a662006-10-08 17:54:47 +000028 unsigned flags;
Rob Landley8abbee42006-05-31 19:36:04 +000029
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000030 flags = getopt32(argv, "etv");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000031#define CATV_OPT_e (1<<0)
32#define CATV_OPT_t (1<<1)
33#define CATV_OPT_v (1<<2)
34 flags ^= CATV_OPT_v;
Rob Landley8abbee42006-05-31 19:36:04 +000035 argv += optind;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000036
37 /* Read from stdin if there's nothing else to do. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000038 if (!argv[0])
39 *--argv = (char*)"-";
Rob Landley8abbee42006-05-31 19:36:04 +000040 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000041 fd = open_or_warn_stdin(*argv);
Denis Vlasenko16d58d72007-06-12 08:13:34 +000042 if (fd < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000043 retval = EXIT_FAILURE;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000044 continue;
45 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000046 for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000047 int i, res;
48
Denis Vlasenko74324c82007-06-04 10:16:52 +000049#define read_buf bb_common_bufsiz1
50 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000051 if (res < 0)
52 retval = EXIT_FAILURE;
53 if (res < 1)
54 break;
55 for (i = 0; i < res; i++) {
Denis Vlasenko8e209c32007-08-06 12:28:24 +000056 unsigned char c = read_buf[i];
Rob Landley8abbee42006-05-31 19:36:04 +000057
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000058 if (c > 126 && (flags & CATV_OPT_v)) {
Rob Landley8abbee42006-05-31 19:36:04 +000059 if (c == 127) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000060 printf("^?");
Rob Landley8abbee42006-05-31 19:36:04 +000061 continue;
Rob Landley8abbee42006-05-31 19:36:04 +000062 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000063 printf("M-");
64 c -= 128;
Rob Landley8abbee42006-05-31 19:36:04 +000065 }
66 if (c < 32) {
67 if (c == 10) {
Denis Vlasenko7039a662006-10-08 17:54:47 +000068 if (flags & CATV_OPT_e)
Denis Vlasenko4daad902007-09-27 10:20:47 +000069 bb_putchar('$');
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000070 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000071 printf("^%c", c+'@');
Rob Landley8abbee42006-05-31 19:36:04 +000072 continue;
73 }
74 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000075 bb_putchar(c);
Rob Landley8abbee42006-05-31 19:36:04 +000076 }
77 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000078 if (ENABLE_FEATURE_CLEAN_UP && fd)
79 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000080 } while (*++argv);
81
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000082 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000083}