aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2012-03-02 08:27:50 -0600
committerRob Landley <rob@landley.net>2012-03-02 08:27:50 -0600
commit7339560d3f8581e988cfb7095918bf47b6e8f067 (patch)
tree732fc680e0bebb532bc1defefabdd6e65e1525b7
parent829503ea7f76c0ab52ba7c683f93878f56cab6a1 (diff)
downloadtoybox-7339560d3f8581e988cfb7095918bf47b6e8f067.tar.gz
Cleanup pass, and support unrecognized slash chars ala "echo -e \p".
-rwxr-xr-xscripts/test/echo.test2
-rw-r--r--toys/echo.c74
2 files changed, 38 insertions, 38 deletions
diff --git a/scripts/test/echo.test b/scripts/test/echo.test
index e269bf20..3f562fd8 100755
--- a/scripts/test/echo.test
+++ b/scripts/test/echo.test
@@ -33,3 +33,5 @@ testing "echo -e octal values" \
testing "echo -e hexadecimal values" \
"$CMD -ne '\x534 \x30 \x58\x59\x5a \x3F\x0A'"\
"S4 0 XYZ ?\n" "" ""
+
+testing "echo -e \p" "$CMD -e '\\p'" "\\p\n" "" ""
diff --git a/toys/echo.c b/toys/echo.c
index 6430dcbf..de4be54b 100644
--- a/toys/echo.c
+++ b/toys/echo.c
@@ -36,56 +36,54 @@ config ECHO
void echo_main(void)
{
- int i = 0;
- char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v";
+ int i = 0, out;
+ char *arg, *from = "\\abfnrtv", *to = "\\\a\b\f\n\r\t\v", *c;
for (;;) {
arg = toys.optargs[i];
if (!arg) break;
if (i++) xputc(' ');
- // Handle -e
+ // Should we output arg verbatim?
+
+ if (!(toys.optflags&2)) {
+ xprintf("%s", arg);
+ continue;
+ }
- if (toys.optflags&2) {
- int c, j = 0;
- for (;;) {
- c = arg[j++];
- if (!c) break;
- if (c == '\\') {
- char *found;
- int d = arg[j++];
+ // Handle -e
- // handle \escapes
+ for (c=arg;;) {
+ if (!(out = *(c++))) break;
- if (d) {
- found = strchr(from, d);
- if (found) c = to[found-from];
- else if (d == 'c') goto done;
- else if (d == '0') {
- int n = 0;
- c = 0;
- while (arg[j]>='0' && arg[j]<='7' && n++<3)
- c = (c*8)+arg[j++]-'0';
- } else if (d == 'x') {
- int n = 0;
- c = 0;
- while (n++<2) {
- if (arg[j]>='0' && arg[j]<='9')
- c = (c*16)+arg[j++]-'0';
- else {
- int temp = tolower(arg[j]);
- if (temp>='a' && temp<='f') {
- c = (c*16)+temp-'a'+10;
- j++;
- } else 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') {
+ out = 0;
+ while (*c>='0' && *c<='7' && n++<3)
+ out = (out*8)+*(c++)-'0';
+ } else if (slash == 'x') {
+ out = 0;
+ while (n++<2) {
+ if (*c>='0' && *c<='9')
+ out = (out*16)+*(c++)-'0';
+ else {
+ int temp = tolower(*c);
+ if (temp>='a' && temp<='f') {
+ out = (out*16)+temp-'a'+10;
+ c++;
+ } else break;
}
}
- }
- xputc(c);
+ // Slash in front of unknown character, print literal.
+ } else c--;
}
- } else xprintf("%s", arg);
+ xputc(out);
+ }
}
// Output "\n" if no -n