annotate toys/other/catv.c @ 653:2986aa63a021

Move commands into "posix", "lsb", and "other" menus/directories.
author Rob Landley <rob@landley.net>
date Sat, 25 Aug 2012 14:25:22 -0500
parents toys/catv.c@163498bf547b
children 6df4ccc0acbe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
1 /* vi: set sw=4 ts=4:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
2 *
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * cat -v implementation for toybox
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
194
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 187
diff changeset
5 * Copyright (C) 2006, 2007 Rob Landley <rob@landley.net>
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
6 *
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
7 * Not in SUSv3, but see "Cat -v considered harmful" at
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
8 * http://cm.bell-labs.com/cm/cs/doc/84/kp.ps.gz
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
9
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
10 USE_CATV(NEWTOY(catv, "vte", TOYFLAG_USR|TOYFLAG_BIN))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
11
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
12 config CATV
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
13 bool "catv"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
14 default y
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
15 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
16 usage: catv [-evt] [filename...]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
17
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
18 Display nonprinting characters as escape sequences. Use M-x for
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
19 high ascii characters (>127), and ^x for other nonprinting chars.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
20
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
21 -e Mark each newline with $
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
22 -t Show tabs as ^I
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
23 -v Don't use ^x or M-x escapes.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 194
diff changeset
24 */
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
25
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
28 // Callback function for loopfiles()
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
29
187
c983a0af6d4e Add sha1sum. (No -c mode yet.) Slight tweaks to loopfiles() too.
Rob Landley <rob@landley.net>
parents: 186
diff changeset
30 static void do_catv(int fd, char *name)
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
31 {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
32 for(;;) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
33 int i, len;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
34
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
35 len = read(fd, toybuf, sizeof(toybuf));
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
36 if (len < 0) toys.exitval = EXIT_FAILURE;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
37 if (len < 1) break;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
38 for (i=0; i<len; i++) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
39 char c=toybuf[i];
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
40
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
41 if (c > 126 && (toys.optflags & 4)) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
42 if (c == 127) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
43 printf("^?");
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
44 continue;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
45 } else {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
46 printf("M-");
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
47 c -= 128;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
48 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
49 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
50 if (c < 32) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
51 if (c == 10) {
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 185
diff changeset
52 if (toys.optflags & 1) xputc('$');
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
53 } else if (toys.optflags & (c==9 ? 2 : 4)) {
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
54 printf("^%c", c+'@');
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
55 continue;
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
56 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
57 }
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 185
diff changeset
58 xputc(c);
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
59 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
60 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
61 }
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
62
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 185
diff changeset
63 void catv_main(void)
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 {
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 toys.optflags^=4;
185
29e2051296fd Add loopfiles() function, make catv use it.
Rob Landley <rob@landley.net>
parents: 155
diff changeset
66 loopfiles(toys.optargs, do_catv);
35
f2c7f0799ebe Add cat -v.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 }