Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) Manuel Novoa III <mjn3@codepoet.org> |
Glenn L McGrath | b4a1baa | 2003-01-13 22:09:50 +0000 | [diff] [blame] | 6 | * and Vladimir Oleynik <dzo@simtreas.ru> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 7 | * |
| 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 Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 22 | * |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <limits.h> |
| 27 | #include "libbb.h" |
| 28 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | char bb_process_escape_sequence(const char **ptr) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 30 | { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 31 | static const char charmap[] = { |
| 32 | 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, |
| 33 | '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 35 | const char *p; |
| 36 | const char *q; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | unsigned int num_digits; |
| 38 | unsigned int r; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 39 | unsigned int n; |
| 40 | |
| 41 | n = 0; |
| 42 | q = *ptr; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 43 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | 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 Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 55 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | break; |
| 57 | } while (1); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 58 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 59 | if (num_digits == 0) { /* mnemonic escape sequence? */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | p = charmap; |
| 61 | do { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 62 | if (*p == *q) { |
| 63 | q++; |
| 64 | break; |
| 65 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 66 | } while (*++p); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 67 | n = *(p+(sizeof(charmap)/2)); |
| 68 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 69 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 70 | *ptr = q; |
| 71 | return (char) n; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 74 | /* END CODE */ |
| 75 | /* |
| 76 | Local Variables: |
| 77 | c-file-style: "linux" |
| 78 | c-basic-offset: 4 |
| 79 | tab-width: 4 |
| 80 | End: |
| 81 | */ |