1 /* echo.c - echo supporting -n and -e.
3 * Copyright 2007 Rob Landley <rob@landley.net>
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/echo.html
7 * Deviations from posix: we parse command line options, as Linux has
8 * consistently done since 1992. Posix defaults -e to on, we require -e.
9 * We also honor -- to _stop_ option parsing (bash doesn't, we go with
10 * consistency over compatibility here).
12 USE_ECHO(NEWTOY(echo, "^?en", TOYFLAG_BIN))
18 usage: echo [-ne] [args...]
20 Write each argument to stdout, with one space between each, followed
23 -n No trailing newline.
24 -e Process the following escape sequences:
26 \0NNN octal values (1 to 3 digits)
29 \c stop output here (avoids trailing newline)
35 \xHH hexadecimal values (1 to 2 digits)
47 arg = toys.optargs[i];
49 if (i++) putchar(' ');
51 // Should we output arg verbatim?
53 if (!(toys.optflags & FLAG_e)) {
61 if (!(out = *(c++))) break;
64 if (out == '\\' && *c) {
65 int slash = *(c++), n = unescape(slash);
68 else if (slash=='c') goto done;
69 else if (slash=='0') {
71 while (*c>='0' && *c<='7' && n++<3) out = (out*8)+*(c++)-'0';
72 } else if (slash=='x') {
75 if (*c>='0' && *c<='9') out = (out*16)+*(c++)-'0';
77 int temp = tolower(*c);
78 if (temp>='a' && temp<='f') {
79 out = (out*16)+temp-'a'+10;
84 // Slash in front of unknown character, print literal.
91 // Output "\n" if no -n
92 if (!(toys.optflags&FLAG_n)) putchar('\n');