Magellan Linux

Contents of /tags/mkinitrd-6_1_5/busybox/libbb/process_escape_sequence.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 899 - (show annotations) (download)
Wed Aug 5 17:52:52 2009 UTC (14 years, 10 months ago) by niro
File MIME type: text/plain
File size: 1636 byte(s)
tagged 'mkinitrd-6_1_5'
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 tarball for details.
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 static const char charmap[] ALIGN1 = {
25 'a', 'b', /*'e',*/ 'f', 'n', 'r', 't', 'v', '\\', 0,
26 '\a', '\b', /*27,*/ '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
27
28 const char *p;
29 const char *q;
30 unsigned num_digits;
31 unsigned r;
32 unsigned n;
33 unsigned d;
34 unsigned base;
35
36 num_digits = n = 0;
37 base = 8;
38 q = *ptr;
39
40 #ifdef WANT_HEX_ESCAPES
41 if (*q == 'x') {
42 ++q;
43 base = 16;
44 ++num_digits;
45 }
46 #endif
47
48 do {
49 d = (unsigned char)(*q) - '0';
50 #ifdef WANT_HEX_ESCAPES
51 if (d >= 10) {
52 d = (unsigned char)(_tolower(*q)) - 'a' + 10;
53 }
54 #endif
55
56 if (d >= base) {
57 #ifdef WANT_HEX_ESCAPES
58 if ((base == 16) && (!--num_digits)) {
59 /* return '\\'; */
60 --q;
61 }
62 #endif
63 break;
64 }
65
66 r = n * base + d;
67 if (r > UCHAR_MAX) {
68 break;
69 }
70
71 n = r;
72 ++q;
73 } while (++num_digits < 3);
74
75 if (num_digits == 0) { /* mnemonic escape sequence? */
76 p = charmap;
77 do {
78 if (*p == *q) {
79 q++;
80 break;
81 }
82 } while (*++p);
83 n = *(p + (sizeof(charmap)/2));
84 }
85
86 *ptr = q;
87
88 return (char) n;
89 }