fix
[midnight-commander.git] / slang / slmisc.c
blob64af84cd1107f66b7b124b4f1b2e95f6c5e1c0d1
1 /* Copyright (c) 1992, 1999, 2001, 2002 John E. Davis
2 * This file is part of the S-Lang library.
4 * Trimmed down for use in GNU Midnight Commander.
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Perl Artistic License.
8 */
9 #include "slinclud.h"
11 #include "slang.h"
12 #include "_slang.h"
14 /* p and ch may point to the same buffer */
15 char *_SLexpand_escaped_char(char *p, char *ch)
17 int i = 0;
18 int max = 0, num, base = 0;
19 char ch1;
21 ch1 = *p++;
23 switch (ch1)
25 default: num = ch1; break;
26 case 'n': num = '\n'; break;
27 case 't': num = '\t'; break;
28 case 'v': num = '\v'; break;
29 case 'b': num = '\b'; break;
30 case 'r': num = '\r'; break;
31 case 'f': num = '\f'; break;
32 case 'E': case 'e': num = 27; break;
33 case 'a': num = 7;
34 break;
36 /* octal */
37 case '0': case '1': case '2': case '3':
38 case '4': case '5': case '6': case '7':
39 max = '7';
40 base = 8; i = 2; num = ch1 - '0';
41 break;
43 case 'd': /* decimal -- S-Lang extension */
44 base = 10;
45 i = 3;
46 max = '9';
47 num = 0;
48 break;
50 case 'x': /* hex */
51 base = 16;
52 max = '9';
53 i = 2;
54 num = 0;
55 break;
58 while (i--)
60 ch1 = *p;
62 if ((ch1 <= max) && (ch1 >= '0'))
64 num = base * num + (ch1 - '0');
66 else if (base == 16)
68 ch1 |= 0x20;
69 if ((ch1 < 'a') || ((ch1 > 'f'))) break;
70 num = base * num + 10 + (ch1 - 'a');
72 else break;
73 p++;
76 *ch = (char) num;
77 return p;