* Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
* and Vladimir Oleynik <dzo@simtreas.ru>
*
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#include "libbb.h"
#undef _tolower
#define _tolower(X) ((X)|((char) 0x20))
-char bb_process_escape_sequence(const char **ptr)
+char FAST_FUNC bb_process_escape_sequence(const char **ptr)
{
- static const char charmap[] = {
- 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
- '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
+ /* bash builtin "echo -e '\ec'" interprets \e as ESC,
+ * but coreutils "/bin/echo -e '\ec'" does not.
+ * 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', '\\', '\\' };
const char *p;
const char *q;
- unsigned int num_digits;
- unsigned int r;
- unsigned int n;
- unsigned int d;
- unsigned int base;
+ unsigned num_digits;
+ unsigned r;
+ unsigned n;
+ unsigned d;
+ unsigned base;
num_digits = n = 0;
base = 8;
}
#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
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;