blob: cc61233e18bd5ee69ede128bf8dc479731e569e4 [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 Vlasenko06af2162007-02-03 17:28:39 +000015int catv_main(int argc, char **argv);
Rob Landley8abbee42006-05-31 19:36:04 +000016int catv_main(int argc, char **argv)
17{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000018 int retval = EXIT_SUCCESS, fd;
Denis Vlasenko7039a662006-10-08 17:54:47 +000019 unsigned flags;
Rob Landley8abbee42006-05-31 19:36:04 +000020
Denis Vlasenko67b23e62006-10-03 21:00:06 +000021 flags = getopt32(argc, argv, "etv");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000022#define CATV_OPT_e (1<<0)
23#define CATV_OPT_t (1<<1)
24#define CATV_OPT_v (1<<2)
25 flags ^= CATV_OPT_v;
Rob Landley8abbee42006-05-31 19:36:04 +000026
27 argv += optind;
28 do {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000029 /* Read from stdin if there's nothing else to do. */
Rob Landley8abbee42006-05-31 19:36:04 +000030 fd = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000031 if (*argv && 0 > (fd = xopen(*argv, O_RDONLY)))
32 retval = EXIT_FAILURE;
33 else for (;;) {
Rob Landley8abbee42006-05-31 19:36:04 +000034 int i, res;
35
Denis Vlasenko74324c82007-06-04 10:16:52 +000036#define read_buf bb_common_bufsiz1
37 res = read(fd, read_buf, COMMON_BUFSIZE);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000038 if (res < 0)
39 retval = EXIT_FAILURE;
40 if (res < 1)
41 break;
42 for (i = 0; i < res; i++) {
Denis Vlasenko74324c82007-06-04 10:16:52 +000043 char c = read_buf[i];
Rob Landley8abbee42006-05-31 19:36:04 +000044
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000045 if (c > 126 && (flags & CATV_OPT_v)) {
Rob Landley8abbee42006-05-31 19:36:04 +000046 if (c == 127) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000047 printf("^?");
Rob Landley8abbee42006-05-31 19:36:04 +000048 continue;
49 } else {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000050 printf("M-");
Rob Landley8abbee42006-05-31 19:36:04 +000051 c -= 128;
52 }
53 }
54 if (c < 32) {
55 if (c == 10) {
Denis Vlasenko7039a662006-10-08 17:54:47 +000056 if (flags & CATV_OPT_e)
57 putchar('$');
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000058 } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000059 printf("^%c", c+'@');
Rob Landley8abbee42006-05-31 19:36:04 +000060 continue;
61 }
62 }
63 putchar(c);
64 }
65 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000066 if (ENABLE_FEATURE_CLEAN_UP && fd)
67 close(fd);
Rob Landley8abbee42006-05-31 19:36:04 +000068 } while (*++argv);
69
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000070 fflush_stdout_and_exit(retval);
Rob Landley8abbee42006-05-31 19:36:04 +000071}