annotate toys/echo.c @ 233:d4176f3f3835

Zap toys/Config.in and instead create generated/Config.in from contents of toys/*.c. Move relevant info into comment at the top of each toys/*.c. Also convert more of Makefile into a thin wrapper around shell scripts that actually do the work. (Makefile is only still there for the user interface.)
author Rob Landley <rob@landley.net>
date Sat, 19 Jan 2008 17:08:39 -0600
parents 3227c5316260
children 163498bf547b
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
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
9 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
10 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
11 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
12 help
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
13 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
14
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
15 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
16 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
17
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
18 -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
19 -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
20 \\ backslash
d4176f3f3835 Zap toys/Config.in and instead create generated/Config.in from contents of
Rob Landley <rob@landley.net>
parents: 198
diff changeset
21 \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
22 \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
23 \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
24 \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
25 \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
26 \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
27 \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
28 \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
29 */
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 #include "toys.h"
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
32
186
25447caf1b4b Change command main() functions to return void, and exit(toys.exitval) from
Rob Landley <rob@landley.net>
parents: 156
diff changeset
33 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
34 {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 int i = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 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
37
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 for (;;) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 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
40 if (!arg) break;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 if (i++) xputc(' ');
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
42
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
43 // Handle -e
156
1e8f4b05cb65 Remove trailing whitespace (thanks to Charlie Shepherd), and a couple comment
Rob Landley <rob@landley.net>
parents: 72
diff changeset
44
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 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
46 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
47 for (;;) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 c = arg[j++];
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 if (!c) break;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 if (c == '\\') {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 char *found;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 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
53
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
54 // handle \escapes
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
55
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 if (d) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 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
58 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
59 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
60 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
61 c = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 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
63 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
64 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 }
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 xputc(c);
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 } 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
70 }
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
71
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 // 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
73 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
74 done:
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 xflush();
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 }