Allow hex 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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  *
23  */
24
25 #include <stdio.h>
26 #include <limits.h>
27 #include "libbb.h"
28
29 char bb_process_escape_sequence(const char **ptr)
30 {
31         static const char charmap[] = {
32                 'a',  'b',  'f',  'n',  'r',  't',  'v',  '\\', 0,
33                 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
34
35         const char *p;
36         const char *q;
37         unsigned int num_digits;
38         unsigned int r;
39         unsigned int n;
40
41         n = 0;
42         q = *ptr;
43
44         num_digits = 0;
45         do {
46                 if (((unsigned int)(*q - '0')) <= 7) {
47                         r = n * 8 + (*q - '0');
48                         if (r <= UCHAR_MAX) {
49                                 n = r;
50                                 ++q;
51                                 if (++num_digits < 3) {
52                                         continue;
53                                 }
54                         }
55                 }
56                 break;
57         } while (1);
58
59         if (num_digits == 0) {  /* mnemonic escape sequence? */
60                 p = charmap;
61                 do {
62                         if (*p == *q) {
63                                 q++;
64                                 break;
65                         }
66                 } while (*++p);
67                 n = *(p+(sizeof(charmap)/2));
68         }
69
70         *ptr = q;
71         return (char) n;
72 }
73
74 /* END CODE */
75 /*
76 Local Variables:
77 c-file-style: "linux"
78 c-basic-offset: 4
79 tab-width: 4
80 End:
81 */