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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* See "Cat -v considered harmful" at |
| 11 | * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz */ |
| 12 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame^] | 13 | //usage:#define catv_trivial_usage |
| 14 | //usage: "[-etv] [FILE]..." |
| 15 | //usage:#define catv_full_usage "\n\n" |
| 16 | //usage: "Display nonprinting characters as ^x or M-x\n" |
| 17 | //usage: "\nOptions:" |
| 18 | //usage: "\n -e End each line with $" |
| 19 | //usage: "\n -t Show tabs as ^I" |
| 20 | //usage: "\n -v Don't use ^x or M-x escapes" |
| 21 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 22 | #include "libbb.h" |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 24 | int catv_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 25 | int catv_main(int argc UNUSED_PARAM, char **argv) |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 26 | { |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 27 | int retval = EXIT_SUCCESS; |
| 28 | int fd; |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 29 | unsigned flags; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 31 | flags = getopt32(argv, "etv"); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 32 | #define CATV_OPT_e (1<<0) |
| 33 | #define CATV_OPT_t (1<<1) |
| 34 | #define CATV_OPT_v (1<<2) |
| 35 | flags ^= CATV_OPT_v; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 36 | argv += optind; |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 37 | |
| 38 | /* Read from stdin if there's nothing else to do. */ |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 39 | if (!argv[0]) |
| 40 | *--argv = (char*)"-"; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 41 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 42 | fd = open_or_warn_stdin(*argv); |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 43 | if (fd < 0) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 44 | retval = EXIT_FAILURE; |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 45 | continue; |
| 46 | } |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 47 | for (;;) { |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 48 | int i, res; |
| 49 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 50 | #define read_buf bb_common_bufsiz1 |
| 51 | res = read(fd, read_buf, COMMON_BUFSIZE); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 52 | if (res < 0) |
| 53 | retval = EXIT_FAILURE; |
| 54 | if (res < 1) |
| 55 | break; |
| 56 | for (i = 0; i < res; i++) { |
Denis Vlasenko | 8e209c3 | 2007-08-06 12:28:24 +0000 | [diff] [blame] | 57 | unsigned char c = read_buf[i]; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 58 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 59 | if (c > 126 && (flags & CATV_OPT_v)) { |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 60 | if (c == 127) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 61 | printf("^?"); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 62 | continue; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 63 | } |
Denis Vlasenko | 16d58d7 | 2007-06-12 08:13:34 +0000 | [diff] [blame] | 64 | printf("M-"); |
| 65 | c -= 128; |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 66 | } |
| 67 | if (c < 32) { |
| 68 | if (c == 10) { |
Denis Vlasenko | 7039a66 | 2006-10-08 17:54:47 +0000 | [diff] [blame] | 69 | if (flags & CATV_OPT_e) |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 70 | bb_putchar('$'); |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 71 | } else if (flags & (c==9 ? CATV_OPT_t : CATV_OPT_v)) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 72 | printf("^%c", c+'@'); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 73 | continue; |
| 74 | } |
| 75 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 76 | bb_putchar(c); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 79 | if (ENABLE_FEATURE_CLEAN_UP && fd) |
| 80 | close(fd); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 81 | } while (*++argv); |
| 82 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 83 | fflush_stdout_and_exit(retval); |
Rob Landley | 8abbee4 | 2006-05-31 19:36:04 +0000 | [diff] [blame] | 84 | } |