libbb: introduce and use strcpy_and_process_escape_sequences
[platform/upstream/busybox.git] / libbb / process_escape_sequence.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
6  * and Vladimir Oleynik <dzo@simtreas.ru>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10
11 #include "libbb.h"
12
13 #define WANT_HEX_ESCAPES 1
14
15 /* Usual "this only works for ascii compatible encodings" disclaimer. */
16 #undef _tolower
17 #define _tolower(X) ((X)|((char) 0x20))
18
19 char FAST_FUNC bb_process_escape_sequence(const char **ptr)
20 {
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', '\\', '\\' };
28
29         const char *p;
30         const char *q;
31         unsigned num_digits;
32         unsigned r;
33         unsigned n;
34         unsigned base;
35
36         num_digits = n = 0;
37         base = 8;
38         q = *ptr;
39
40 #if WANT_HEX_ESCAPES
41         if (*q == 'x') {
42                 ++q;
43                 base = 16;
44                 ++num_digits;
45         }
46 #endif
47
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. */
51         do {
52 #if !WANT_HEX_ESCAPES
53                 unsigned d = (unsigned char)(*q) - '0';
54 #else
55                 unsigned d = (unsigned char)_tolower(*q) - '0';
56                 if (d >= 10)
57                         d += ('0' - 'a' + 10);
58 #endif
59                 if (d >= base) {
60                         if (WANT_HEX_ESCAPES && base == 16) {
61                                 --num_digits;
62                                 if (num_digits == 0) {
63                                         /* \x<bad_char> */
64                                         --q; /* go back to x */
65                                 }
66                         }
67                         break;
68                 }
69
70                 r = n * base + d;
71                 if (r > UCHAR_MAX) {
72                         break;
73                 }
74
75                 n = r;
76                 ++q;
77         } while (++num_digits < 3);
78
79         if (num_digits == 0) {  /* mnemonic escape sequence? */
80                 p = charmap;
81                 do {
82                         if (*p == *q) {
83                                 q++;
84                                 break;
85                         }
86                 } while (*++p);
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;
92                 n = *p;
93         }
94
95         *ptr = q;
96
97         return (char) n;
98 }
99
100 char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src)
101 {
102         while (1) {
103                 char c, c1;
104                 c = c1 = *src++;
105                 if (c1 == '\\')
106                         c1 = bb_process_escape_sequence(&src);
107                 *dst = c1;
108                 if (c == '\0')
109                         return dst;
110                 dst++;
111         }
112 }