blob: ff3139c80672ab3f1db7ae325b7588ccdeb6a735 [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 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8 */
9
10/* See "Cat -v considered harmful" at
11 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */
12
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000013#include "libbb.h"
Rob Landley8abbee42006-05-31 19:36:04 +000014
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000015int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000016int catv_main(int argc UNUSED_PARAM, char **argv)
Rob Landley8abbee42006-05-31 19:36:04 +000017{
Denis Vlasenko8e209c32007-08-06 12:28:24 +000018 int retval = EXIT_SUCCESS;
19 int fd;
Denis Vlasenko7039a662006-10-08 17:54:47 +000020 unsigned flags;
Rob Landley8abbee42006-05-31 19:36:04 +000021
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000022 flags = getopt32(argv, "etv");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000023#define CATV_OPT_e (1<<0)
24#define CATV_OPT_t (1<<1)
25#define CATV_OPT_v (1<<2)
26 flags ^= CATV_OPT_v;
Rob Landley8abbee42006-05-31 19:36:04 +000027 argv += optind;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000028
29 /* Read from stdin if there's nothing else to do. */
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000030 if (!argv[0])
31 *--argv = (char*)"-";
Rob Landley8abbee42006-05-31 19:36:04 +000032 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000033 fd = open_or_warn_stdin(*argv);
Denis Vlasenko16d58d72007-06-12 08:13:34 +000034 if (fd < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000035 retval = EXIT_FAILURE;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000036 continue;
37 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000038 for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000039 int i, res;
40
Denis Vlasenko74324c82007-06-04 10:16:52 +000041#define read_buf bb_common_bufsiz1
42 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000043 if (res < 0)
44 retval = EXIT_FAILURE;
45 if (res < 1)
46 break;
47 for (i = 0; i < res; i++) {
Denis Vlasenko8e209c32007-08-06 12:28:24 +000048 unsigned char c = read_buf[i];
Rob Landley8abbee42006-05-31 19:36:04 +000049
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000050 if (c > 126 && (flags & CATV_OPT_v)) {
Rob Landley8abbee42006-05-31 19:36:04 +000051 if (c == 127) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000052 printf("^?");
Rob Landley8abbee42006-05-31 19:36:04 +000053 continue;
Rob Landley8abbee42006-05-31 19:36:04 +000054 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000055 printf("M-");
56 c -= 128;
Rob Landley8abbee42006-05-31 19:36:04 +000057 }
58 if (c < 32) {
59 if (c == 10) {
Denis Vlasenko7039a662006-10-08 17:54:47 +000060 if (flags & CATV_OPT_e)
Denis Vlasenko4daad902007-09-27 10:20:47 +000061 bb_putchar('$');
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000062 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000063 printf("^%c", c+'@');
Rob Landley8abbee42006-05-31 19:36:04 +000064 continue;
65 }
66 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000067 bb_putchar(c);
Rob Landley8abbee42006-05-31 19:36:04 +000068 }
69 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000070 if (ENABLE_FEATURE_CLEAN_UP && fd)
71 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000072 } while (*++argv);
73
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000074 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000075}