1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
6 * and Vladimir Oleynik <dzo@simtreas.ru>
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 #define WANT_HEX_ESCAPES 1
15 /* Usual "this only works for ascii compatible encodings" disclaimer. */
17 #define _tolower(X) ((X)|((char) 0x20))
19 char FAST_FUNC bb_process_escape_sequence(const char **ptr)
21 /* bash builtin "echo -e '\ec'" interprets \e as ESC,
22 * but coreutils "/bin/echo -e '\ec'" does not.
23 * manpages tend to support coreutils way.
24 * Update: coreutils added support for \e on 28 Oct 2009. */
25 static const char charmap[] ALIGN1 = {
26 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\', 0,
27 '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
48 /* bash requires leading 0 in octal escapes:
49 * \02 works, \2 does not (prints \ and 2).
50 * We treat \2 as a valid octal escape sequence. */
53 unsigned d = (unsigned char)(*q) - '0';
55 unsigned d = (unsigned char)_tolower(*q) - '0';
57 d += ('0' - 'a' + 10);
60 if (WANT_HEX_ESCAPES && base == 16) {
62 if (num_digits == 0) {
64 --q; /* go back to x */
77 } while (++num_digits < 3);
79 if (num_digits == 0) { /* mnemonic escape sequence? */
87 /* p points to found escape char or NUL,
88 * advance it and find what it translates to.
89 * Note that unrecognized sequence \z returns '\'
90 * and leaves ptr pointing to z. */
91 p += sizeof(charmap) / 2;
100 char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src)
106 c1 = bb_process_escape_sequence(&src);