comparison toys/echo.c @ 71:40103a3ddcb0

Helps to "hg add" echo.c. Also, implement \0123 escapes for -e.
author Rob Landley <rob@landley.net>
date Sat, 20 Jan 2007 21:32:47 -0500
parents
children 116405f248cb
comparison
equal deleted inserted replaced
70:a1b464bbef08 71:40103a3ddcb0
1 /* vi: set sw=4 ts=4: */
2 /*
3 * echo.c - echo supporting -n and -e.
4 */
5
6 #include "toys.h"
7
8 int echo_main(void)
9 {
10 int i = 0;
11 char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v";
12
13 for (;;) {
14 arg = toys.optargs[i];
15 if (!arg) break;
16 if (i++) xputc(' ');
17
18 if (toys.optflags&2) {
19 int c, j = 0;
20 for (;;) {
21 c = arg[j++];
22 if (!c) break;
23 if (c == '\\') {
24 char *found;
25 int d = arg[j++];
26
27
28 if (d) {
29 found = strchr(from, d);
30 if (found) c = to[found-from];
31 else if (d == 'c') goto done;
32 else if (d == '0') {
33 c = 0;
34 while (arg[j]>='0' && arg[j]<='7')
35 c = (c*8)+arg[j++]-'0';
36 }
37 // \0123
38 }
39 }
40 xputc(c);
41 }
42 // \\ thingy
43 } else xprintf("%s", arg);
44 }
45 // Output "\n" if no -n
46 if (!(toys.optflags&1)) xputc('\n');
47 done:
48 xflush();
49 return 0;
50 }