annotate toys/echo.c @ 72:116405f248cb

Comment and whitespace cleanups
author Rob Landley <rob@landley.net>
date Sat, 20 Jan 2007 22:41:29 -0500
parents 40103a3ddcb0
children 1e8f4b05cb65
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4: */
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 /*
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.
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 */
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
5
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 #include "toys.h"
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
8 int echo_main(void)
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 int i = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v";
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
12
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 for (;;) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 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
15 if (!arg) break;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 if (i++) xputc(' ');
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
17
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
18 // Handle -e
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
19
71
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 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
21 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
22 for (;;) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 c = arg[j++];
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 if (!c) break;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 if (c == '\\') {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 char *found;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 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
28
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
diff changeset
29 // handle \escapes
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 if (d) {
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 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
33 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
34 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
35 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
36 c = 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 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
38 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
39 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 xputc(c);
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 }
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 } 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
45 }
72
116405f248cb Comment and whitespace cleanups
Rob Landley <rob@landley.net>
parents: 71
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 // 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
48 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
49 done:
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 xflush();
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 return 0;
40103a3ddcb0 Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 }