aboutsummaryrefslogtreecommitdiff
path: root/toys
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-10-18 17:14:12 -0500
committerRob Landley <rob@landley.net>2014-10-18 17:14:12 -0500
commit5fcc71581abdf8839d10786d7ac437cc5b0bf4c5 (patch)
tree4143360f7699d4b2b6f3daecf7ae36a2c625a913 /toys
parent01138b94e6ac9ff2d1258917f96c0c5c8c124021 (diff)
downloadtoybox-5fcc71581abdf8839d10786d7ac437cc5b0bf4c5.tar.gz
Factor out printf-style escape parsing logic from echo.c.
Diffstat (limited to 'toys')
-rw-r--r--toys/posix/echo.c22
1 files changed, 11 insertions, 11 deletions
diff --git a/toys/posix/echo.c b/toys/posix/echo.c
index 28284bfb..45890dc4 100644
--- a/toys/posix/echo.c
+++ b/toys/posix/echo.c
@@ -36,12 +36,12 @@ config ECHO
void echo_main(void)
{
int i = 0, out;
- char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v", *c;
+ char *arg, *c;
for (;;) {
arg = toys.optargs[i];
if (!arg) break;
- if (i++) xputc(' ');
+ if (i++) putchar(' ');
// Should we output arg verbatim?
@@ -52,19 +52,19 @@ void echo_main(void)
// Handle -e
- for (c=arg;;) {
+ for (c = arg;;) {
if (!(out = *(c++))) break;
// handle \escapes
if (out == '\\' && *c) {
- int n = 0, slash = *(c++);
- char *found = strchr(from, slash);
- if (found) out = to[found-from];
- else if (slash == 'c') goto done;
- else if (slash == '0') {
+ int slash = *(c++), n = unescape(slash);
+
+ if (n) out = n;
+ else if (slash=='c') goto done;
+ else if (slash=='0') {
out = 0;
while (*c>='0' && *c<='7' && n++<3) out = (out*8)+*(c++)-'0';
- } else if (slash == 'x') {
+ } else if (slash=='x') {
out = 0;
while (n++<2) {
if (*c>='0' && *c<='9') out = (out*16)+*(c++)-'0';
@@ -79,12 +79,12 @@ void echo_main(void)
// Slash in front of unknown character, print literal.
} else c--;
}
- xputc(out);
+ putchar(out);
}
}
// Output "\n" if no -n
- if (!(toys.optflags&FLAG_n)) xputc('\n');
+ if (!(toys.optflags&FLAG_n)) putchar('\n');
done:
xflush();
}