Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/libbb/process_escape_sequence.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 1528 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

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 <stdio.h>
12 #include <limits.h>
13 #include <ctype.h>
14 #include "libbb.h"
15
16 #define WANT_HEX_ESCAPES 1
17
18 /* Usual "this only works for ascii compatible encodings" disclaimer. */
19 #undef _tolower
20 #define _tolower(X) ((X)|((char) 0x20))
21
22 char bb_process_escape_sequence(const char **ptr)
23 {
24 static const char charmap[] = {
25 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
26 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
27
28 const char *p;
29 const char *q;
30 unsigned int num_digits;
31 unsigned int r;
32 unsigned int n;
33 unsigned int d;
34 unsigned int 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 int)(*q - '0');
50 #ifdef WANT_HEX_ESCAPES
51 if (d >= 10) {
52 d = ((unsigned int)(_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 }