annotate toys/echo.c @ 234:163498bf547b

Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
author Rob Landley <rob@landley.net>
date Sat, 19 Jan 2008 17:43:27 -0600
parents d4176f3f3835
children 3de1ea4d6b56
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: 198
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: 198
diff changeset
2 *
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * echo.c - echo supporting -n and -e.
194
30a6db5a95c2 Add comments about SUSv3 specs (or lack thereof).
Rob Landley <rob@landley.net>
parents: 186
diff changeset
4 *
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
5 * Copyright 2007 Rob Landley <rob@landley.net>
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
6 *
198
3227c5316260 Update links and add some more spec comments.
Rob Landley <rob@landley.net>
parents: 194
diff changeset
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
8
234
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
9 USE_ECHO(NEWTOY(echo, "+en", TOYFLAG_BIN))
163498bf547b Move NEWTOY() list from end of toylist.h to generated/newtoys.h.
Rob Landley <rob@landley.net>
parents: 233
diff changeset
10
233
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
11 config ECHO
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
12 bool "echo"
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
13 default y
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
14 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
15 usage: echo [-ne] [args...]
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
16
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
17 Write each argument to stdout, with one space between each, followed
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
18 by a newline.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
19
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
20 -n No trailing newline.
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
21 -e Process the following escape sequences:
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
22 \\ backslash
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
23 \a alert (beep/flash)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
24 \b backspace
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
25 \c Stop output here (avoids trailing newline)
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
26 \f form feed
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
27 \n newline
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
28 \r carriage return
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
29 \t horizontal tab
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
30 \v vertical tab
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
31 */
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 #include "toys.h"
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
34
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 156
diff changeset
35 void echo_main(void)
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 int i = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v";
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 72
diff changeset
39
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 for (;;) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 arg = toys.optargs[i];
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 if (!arg) break;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 if (i++) xputc(' ');
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
44
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
45 // Handle -e
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 72
diff changeset
46
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 if (toys.optflags&2) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 int c, j = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 for (;;) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 c = arg[j++];
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 if (!c) break;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 if (c == '\\') {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 char *found;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 int d = arg[j++];
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
55
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
56 // handle \escapes
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 if (d) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 found = strchr(from, d);
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 if (found) c = to[found-from];
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 else if (d == 'c') goto done;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 else if (d == '0') {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 c = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 while (arg[j]>='0' && arg[j]<='7')
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 c = (c*8)+arg[j++]-'0';
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 xputc(c);
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
71 } else xprintf("%s", arg);
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 }
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
73
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 // Output "\n" if no -n
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 if (!(toys.optflags&1)) xputc('\n');
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 done:
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 xflush();
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
78 }