2 * UCW Library -- String Unescaping
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
5 * (c) 2007 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include "lib/string.h"
15 #include "lib/chartype.h"
18 /* Expands C99-like escape sequences.
19 * It is safe to use the same buffer for both input and output. */
21 str_unesc(char *d, const char *s)
28 case 'a': *d++ = '\a'; s += 2; break;
29 case 'b': *d++ = '\b'; s += 2; break;
30 case 'f': *d++ = '\f'; s += 2; break;
31 case 'n': *d++ = '\n'; s += 2; break;
32 case 'r': *d++ = '\r'; s += 2; break;
33 case 't': *d++ = '\t'; s += 2; break;
34 case 'v': *d++ = '\v'; s += 2; break;
35 case '\?': *d++ = '\?'; s += 2; break;
36 case '\'': *d++ = '\''; s += 2; break;
37 case '\"': *d++ = '\"'; s += 2; break;
38 case '\\': *d++ = '\\'; s += 2; break;
43 DBG("\\x used with no following hex digits");
48 uns v = strtoul(s + 2, &p, 16);
52 DBG("hex escape sequence out of range");
57 if (s[1] >= '0' && s[1] <= '7')
61 for (uns i = 0; i < 2 && *s >= '0' && *s <= '7'; s++, i++)
62 v = (v << 3) + *s - '0';
66 DBG("octal escape sequence out of range");
84 int main(int argc, char **argv)
89 char tmp[strlen(argv[1]) + 1];
90 int len = str_unesc(tmp, argv[1]) - tmp;
93 mem_to_hex(hex, tmp, len, ' ');