blob: c4479d9cf023e650f459990a1ef7c25b410fe153 [file] [log] [blame]
Rob Landley28964802008-01-19 17:08:39 -06001/* vi: set sw=4 ts=4:
2 *
Rob Landley1521a9e2006-11-25 16:06:55 -05003 * cat -v implementation for toybox
4 *
Rob Landleyfece5cb2007-12-03 20:05:57 -06005 * Copyright (C) 2006, 2007 Rob Landley <rob@landley.net>
Rob Landley28964802008-01-19 17:08:39 -06006 *
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 Landley1521a9e2006-11-25 16:06:55 -05009
Rob Landley55928b12008-01-19 17:43:27 -060010USE_CATV(NEWTOY(catv, "vte", TOYFLAG_USR|TOYFLAG_BIN))
11
Rob Landley28964802008-01-19 17:08:39 -060012config 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 Landley1521a9e2006-11-25 16:06:55 -050025
26#include "toys.h"
27
Rob Landley7634b552007-11-29 17:49:50 -060028// Callback function for loopfiles()
29
Rob Landleyce6750a2007-11-29 18:32:20 -060030static void do_catv(int fd, char *name)
Rob Landley7634b552007-11-29 17:49:50 -060031{
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 Landleyefda21c2007-11-29 18:14:37 -060052 if (toys.optflags & 1) xputc('$');
Rob Landley7634b552007-11-29 17:49:50 -060053 } else if (toys.optflags & (c==9 ? 2 : 4)) {
54 printf("^%c", c+'@');
55 continue;
56 }
57 }
Rob Landleyefda21c2007-11-29 18:14:37 -060058 xputc(c);
Rob Landley7634b552007-11-29 17:49:50 -060059 }
60 }
61}
62
Rob Landleyefda21c2007-11-29 18:14:37 -060063void catv_main(void)
Rob Landley1521a9e2006-11-25 16:06:55 -050064{
Rob Landley1521a9e2006-11-25 16:06:55 -050065 toys.optflags^=4;
Rob Landley7634b552007-11-29 17:49:50 -060066 loopfiles(toys.optargs, do_catv);
Rob Landley1521a9e2006-11-25 16:06:55 -050067}