blob: ef2717bdd62fde1fa8a93dbb04f5efd4440c551d [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Manuel Novoa III cad53642003-03-19 09:13:01 +00005 * Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
Glenn L McGrathb4a1baa2003-01-13 22:09:50 +00006 * and Vladimir Oleynik <dzo@simtreas.ru>
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
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 *
Eric Andersene5dfced2001-04-09 22:48:12 +000022 *
Eric Andersenaad1a882001-03-16 22:47:14 +000023 */
24
25#include <stdio.h>
26#include <limits.h>
27#include "libbb.h"
28
Manuel Novoa III cad53642003-03-19 09:13:01 +000029char bb_process_escape_sequence(const char **ptr)
Eric Andersenaad1a882001-03-16 22:47:14 +000030{
Eric Andersene5dfced2001-04-09 22:48:12 +000031 static const char charmap[] = {
32 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
33 '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' };
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Eric Andersene5dfced2001-04-09 22:48:12 +000035 const char *p;
36 const char *q;
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 unsigned int num_digits;
38 unsigned int r;
Eric Andersene5dfced2001-04-09 22:48:12 +000039 unsigned int n;
40
41 n = 0;
42 q = *ptr;
Eric Andersenaad1a882001-03-16 22:47:14 +000043
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 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 }
Eric Andersene5dfced2001-04-09 22:48:12 +000055 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 break;
57 } while (1);
Eric Andersenaad1a882001-03-16 22:47:14 +000058
Eric Andersene5dfced2001-04-09 22:48:12 +000059 if (num_digits == 0) { /* mnemonic escape sequence? */
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 p = charmap;
61 do {
Eric Andersene5dfced2001-04-09 22:48:12 +000062 if (*p == *q) {
63 q++;
64 break;
65 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 } while (*++p);
Eric Andersene5dfced2001-04-09 22:48:12 +000067 n = *(p+(sizeof(charmap)/2));
68 }
Eric Andersenaad1a882001-03-16 22:47:14 +000069
Eric Andersene5dfced2001-04-09 22:48:12 +000070 *ptr = q;
71 return (char) n;
Eric Andersenaad1a882001-03-16 22:47:14 +000072}
73
Eric Andersenaad1a882001-03-16 22:47:14 +000074/* END CODE */
75/*
76Local Variables:
77c-file-style: "linux"
78c-basic-offset: 4
79tab-width: 4
80End:
81*/