Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 3 | * cat -v implementation for toybox |
| 4 | * |
Rob Landley | fece5cb | 2007-12-03 20:05:57 -0600 | [diff] [blame] | 5 | * Copyright (C) 2006, 2007 Rob Landley <rob@landley.net> |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 6 | * |
| 7 | * Not in SUSv3, but see "Cat -v considered harmful" at |
| 8 | * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 9 | |
Rob Landley | 55928b1 | 2008-01-19 17:43:27 -0600 | [diff] [blame] | 10 | USE_CATV(NEWTOY(catv, "vte", TOYFLAG_USR|TOYFLAG_BIN)) |
| 11 | |
Rob Landley | 2896480 | 2008-01-19 17:08:39 -0600 | [diff] [blame] | 12 | config CATV |
| 13 | bool "catv" |
| 14 | default y |
| 15 | help |
| 16 | usage: catv [-evt] [filename...] |
| 17 | |
| 18 | Display nonprinting characters as escape sequences. Use M-x for |
| 19 | high ascii characters (>127), and ^x for other nonprinting chars. |
| 20 | |
| 21 | -e Mark each newline with $ |
| 22 | -t Show tabs as ^I |
| 23 | -v Don't use ^x or M-x escapes. |
| 24 | */ |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 25 | |
| 26 | #include "toys.h" |
| 27 | |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 28 | // Callback function for loopfiles() |
| 29 | |
Rob Landley | ce6750a | 2007-11-29 18:32:20 -0600 | [diff] [blame] | 30 | static void do_catv(int fd, char *name) |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 31 | { |
| 32 | for(;;) { |
| 33 | int i, len; |
| 34 | |
| 35 | len = read(fd, toybuf, sizeof(toybuf)); |
| 36 | if (len < 0) toys.exitval = EXIT_FAILURE; |
| 37 | if (len < 1) break; |
| 38 | for (i=0; i<len; i++) { |
| 39 | char c=toybuf[i]; |
| 40 | |
| 41 | if (c > 126 && (toys.optflags & 4)) { |
| 42 | if (c == 127) { |
| 43 | printf("^?"); |
| 44 | continue; |
| 45 | } else { |
| 46 | printf("M-"); |
| 47 | c -= 128; |
| 48 | } |
| 49 | } |
| 50 | if (c < 32) { |
| 51 | if (c == 10) { |
Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 52 | if (toys.optflags & 1) xputc('$'); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 53 | } else if (toys.optflags & (c==9 ? 2 : 4)) { |
| 54 | printf("^%c", c+'@'); |
| 55 | continue; |
| 56 | } |
| 57 | } |
Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 58 | xputc(c); |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
Rob Landley | efda21c | 2007-11-29 18:14:37 -0600 | [diff] [blame] | 63 | void catv_main(void) |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 64 | { |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 65 | toys.optflags^=4; |
Rob Landley | 7634b55 | 2007-11-29 17:49:50 -0600 | [diff] [blame] | 66 | loopfiles(toys.optargs, do_catv); |
Rob Landley | 1521a9e | 2006-11-25 16:06:55 -0500 | [diff] [blame] | 67 | } |