traceroute: fix help text to not show -6 when traceroute6 is off
[oweals/busybox.git] / libbb / process_escape_sequence.c
index 6de2cacddcf74a742957b19a7b0ec4294c04f323..3ad908b57e07b14eb878b672a2eb7c1d4c378bb1 100644 (file)
@@ -20,10 +20,11 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
 {
        /* bash builtin "echo -e '\ec'" interprets \e as ESC,
         * but coreutils "/bin/echo -e '\ec'" does not.
-        * manpages tend to support coreutils way. */
+        * manpages tend to support coreutils way.
+        * Update: coreutils added support for \e on 28 Oct 2009. */
        static const char charmap[] ALIGN1 = {
-               'a',  'b', /*'e',*/ 'f',  'n',  'r',  't',  'v',  '\\', 0,
-               '\a', '\b', /*27,*/ '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
+               'a',  'b', 'e', 'f',  'n',  'r',  't',  'v',  '\\', 0,
+               '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
 
        const char *p;
        const char *q;
@@ -45,6 +46,9 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
        }
 #endif
 
+       /* bash requires leading 0 in octal escapes:
+        * \02 works, \2 does not (prints \ and 2).
+        * We treat \2 as a valid octal escape sequence. */
        do {
                d = (unsigned char)(*q) - '0';
 #ifdef WANT_HEX_ESCAPES
@@ -80,7 +84,10 @@ char FAST_FUNC bb_process_escape_sequence(const char **ptr)
                                break;
                        }
                } while (*++p);
-               n = *(p + (sizeof(charmap)/2));
+               /* p points to found escape char or NUL,
+                * advance it and find what it translates to */
+               p += sizeof(charmap) / 2;
+               n = *p;
        }
 
        *ptr = q;