Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 15 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 16 | int catv_main(int argc, char **argv) |
17 | { | ||||
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 18 | int retval = EXIT_SUCCESS; |
19 | int fd; | ||||
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 20 | unsigned flags; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 22 | flags = getopt32(argv, "etv"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 23 | #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 Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 27 | argv += optind; |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 28 | |
29 | /* Read from stdin if there's nothing else to do. */ | ||||
30 | fd = 0; | ||||
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 31 | if (!argv[0]) { |
32 | argv--; | ||||
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 33 | goto jump_in; |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 34 | } |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 35 | do { |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 36 | fd = open_or_warn(*argv, O_RDONLY); |
37 | if (fd < 0) { | ||||
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 38 | retval = EXIT_FAILURE; |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 39 | continue; |
40 | } | ||||
41 | jump_in: | ||||
42 | for (;;) { | ||||
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 43 | int i, res; |
44 | |||||
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 45 | #define read_buf bb_common_bufsiz1 |
46 | res = read(fd, read_buf, COMMON_BUFSIZE); | ||||
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 47 | if (res < 0) |
48 | retval = EXIT_FAILURE; | ||||
49 | if (res < 1) | ||||
50 | break; | ||||
51 | for (i = 0; i < res; i++) { | ||||
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 52 | unsigned char c = read_buf[i]; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 53 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 54 | if (c > 126 && (flags & CATV_OPT_v)) { |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 55 | if (c == 127) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 56 | printf("^?"); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 57 | continue; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 58 | } |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 59 | printf("M-"); |
60 | c -= 128; | ||||
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 61 | } |
62 | if (c < 32) { | ||||
63 | if (c == 10) { | ||||
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 64 | if (flags & CATV_OPT_e) |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 65 | bb_putchar('$'); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 66 | } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 67 | printf("^%c", c+'@'); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 68 | continue; |
69 | } | ||||
70 | } | ||||
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 71 | bb_putchar(c); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 72 | } |
73 | } | ||||
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 74 | if (ENABLE_FEATURE_CLEAN_UP && fd) |
75 | close(fd); | ||||
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 76 | } while (*++argv); |
77 | |||||
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 78 | fflush_stdout_and_exit(retval); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 79 | } |