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