OSDN Git Service

Update links and add some more spec comments.
[android-x86/external-toybox.git] / toys / echo.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * echo.c - echo supporting -n and -e.
4  *
5  * See http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html
6  */
7
8 #include "toys.h"
9
10 void echo_main(void)
11 {
12         int i = 0;
13         char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v";
14
15         for (;;) {
16                 arg = toys.optargs[i];
17                 if (!arg) break;
18                 if (i++) xputc(' ');
19
20                 // Handle -e
21
22                 if (toys.optflags&2) {
23                         int c, j = 0;
24                         for (;;) {
25                                 c = arg[j++];
26                                 if (!c) break;
27                                 if (c == '\\') {
28                                         char *found;
29                                         int d = arg[j++];
30
31                                         // handle \escapes
32
33                                         if (d) {
34                                                 found = strchr(from, d);
35                                                 if (found) c = to[found-from];
36                                                 else if (d == 'c') goto done;
37                                                 else if (d == '0') {
38                                                         c = 0;
39                                                         while (arg[j]>='0' && arg[j]<='7')
40                                                                 c = (c*8)+arg[j++]-'0';
41                                                 }
42                                         }
43                                 }
44                                 xputc(c);
45                         }
46                 } else xprintf("%s", arg);
47         }
48
49         // Output "\n" if no -n
50         if (!(toys.optflags&1)) xputc('\n');
51 done:
52         xflush();
53 }