]> mj.ucw.cz Git - libucw.git/blob - lib/string.c
shell/config: check the default value from the command-line
[libucw.git] / lib / string.c
1 /*
2  *      UCW Library -- String Routines
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #undef LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "lib/chartype.h"
14 #include <stdlib.h>
15
16 /* Expands C99-like escape sequences.
17  * It is safe to use the same buffer for both input and output. */
18 byte *
19 str_unesc(byte *d, byte *s)
20 {
21   while (*s)
22     {
23       if (*s == '\\')
24         switch (s[1])
25           {
26             case 'a': *d++ = '\a'; s += 2; break;
27             case 'b': *d++ = '\b'; s += 2; break;
28             case 'f': *d++ = '\f'; s += 2; break;
29             case 'n': *d++ = '\n'; s += 2; break;
30             case 'r': *d++ = '\r'; s += 2; break;
31             case 't': *d++ = '\t'; s += 2; break;
32             case 'v': *d++ = '\v'; s += 2; break;
33             case '\?': *d++ = '\?'; s += 2; break;
34             case '\'': *d++ = '\''; s += 2; break;
35             case '\"': *d++ = '\"'; s += 2; break;
36             case '\\': *d++ = '\\'; s += 2; break;
37             case 'x':
38               if (!Cxdigit(s[2]))
39                 {
40                   s++;
41                   DBG("\\x used with no following hex digits");
42                 }
43               else
44                 {
45                   char *p;
46                   uns v = strtoul(s + 2, &p, 16);
47                   if (v <= 255)
48                     *d++ = v;
49                   else
50                     DBG("hex escape sequence out of range");
51                   s = (byte *)p;
52                 }
53               break;
54             default:
55               if (s[1] >= '0' && s[1] <= '7')
56                 {
57                   uns v = s[1] - '0';
58                   s += 2;
59                   for (uns i = 0; i < 2 && *s >= '0' && *s <= '7'; s++, i++)
60                     v = (v << 3) + *s - '0';
61                   if (v <= 255)
62                     *d++ = v;
63                   else
64                     DBG("octal escape sequence out of range");
65                 }
66               *d++ = *s++;
67               break;
68           }
69       else
70         *d++ = *s++;
71     }
72   *d = 0;
73   return d;
74 }
75