blob: 5f5f1bf8681f99ce1c8f1a018d8aeb60660aaf01 [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;
Rob Landley8abbee42006-05-31 19:36:04 +000016int catv_main(int argc, char **argv)
17{
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. */
30 fd = 0;
Denis Vlasenko8e209c32007-08-06 12:28:24 +000031 if (!argv[0]) {
32 argv--;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000033 goto jump_in;
Denis Vlasenko8e209c32007-08-06 12:28:24 +000034 }
Rob Landley8abbee42006-05-31 19:36:04 +000035 do {
Denis Vlasenko16d58d72007-06-12 08:13:34 +000036 fd = open_or_warn(*argv, O_RDONLY);
37 if (fd < 0) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000038 retval = EXIT_FAILURE;
Denis Vlasenko16d58d72007-06-12 08:13:34 +000039 continue;
40 }
41 jump_in:
42 for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000043 int i, res;
44
Denis Vlasenko74324c82007-06-04 10:16:52 +000045#define read_buf bb_common_bufsiz1
46 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000047 if (res < 0)
48 retval = EXIT_FAILURE;
49 if (res < 1)
50 break;
51 for (i = 0; i < res; i++) {
Denis Vlasenko8e209c32007-08-06 12:28:24 +000052 unsigned char c = read_buf[i];
Rob Landley8abbee42006-05-31 19:36:04 +000053
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000054 if (c > 126 && (flags & CATV_OPT_v)) {
Rob Landley8abbee42006-05-31 19:36:04 +000055 if (c == 127) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000056 printf("^?");
Rob Landley8abbee42006-05-31 19:36:04 +000057 continue;
Rob Landley8abbee42006-05-31 19:36:04 +000058 }
Denis Vlasenko16d58d72007-06-12 08:13:34 +000059 printf("M-");
60 c -= 128;
Rob Landley8abbee42006-05-31 19:36:04 +000061 }
62 if (c < 32) {
63 if (c == 10) {
Denis Vlasenko7039a662006-10-08 17:54:47 +000064 if (flags & CATV_OPT_e)
Denis Vlasenko4daad902007-09-27 10:20:47 +000065 bb_putchar('$');
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000066 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000067 printf("^%c", c+'@');
Rob Landley8abbee42006-05-31 19:36:04 +000068 continue;
69 }
70 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000071 bb_putchar(c);
Rob Landley8abbee42006-05-31 19:36:04 +000072 }
73 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000074 if (ENABLE_FEATURE_CLEAN_UP && fd)
75 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000076 } while (*++argv);
77
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000078 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000079}