OSDN Git Service

Simplify/unify listfiles recursion: populate directory node (and detect top of tree...
[android-x86/external-toybox.git] / toys / catv.c
1 /* vi: set sw=4 ts=4:
2  *
3  * cat -v implementation for toybox
4  *
5  * Copyright (C) 2006, 2007 Rob Landley <rob@landley.net>
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
9
10 USE_CATV(NEWTOY(catv, "vte", TOYFLAG_USR|TOYFLAG_BIN))
11
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 */
25
26 #include "toys.h"
27
28 // Callback function for loopfiles()
29
30 static void do_catv(int fd, char *name)
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) {
52                                         if (toys.optflags & 1) xputc('$');
53                                 } else if (toys.optflags & (c==9 ? 2 : 4)) {
54                                         printf("^%c", c+'@');
55                                         continue;
56                                 }
57                         }
58                         xputc(c);
59                 }
60         }
61 }
62
63 void catv_main(void)
64 {
65         toys.optflags^=4;
66         loopfiles(toys.optargs, do_catv);
67 }