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 | * |
Bernhard Reutner-Fischer | b1629b1 | 2006-05-19 19:29:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <limits.h> |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 13 | #include <ctype.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
| 15 | |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 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 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 22 | char bb_process_escape_sequence(const char **ptr) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 23 | { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 24 | static const char charmap[] = { |
| 25 | 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, |
| 26 | '\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 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; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 38 | q = *ptr; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 40 | #ifdef WANT_HEX_ESCAPES |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 41 | if (*q == 'x') { |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 42 | ++q; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 43 | base = 16; |
| 44 | ++num_digits; |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 45 | } |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 46 | #endif |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 47 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 48 | do { |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 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)) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 59 | /* return '\\'; */ |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 60 | --q; |
| 61 | } |
| 62 | #endif |
Eric Andersen | ccfc448 | 2004-07-27 16:45:46 +0000 | [diff] [blame] | 63 | break; |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 64 | } |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 65 | |
| 66 | r = n * base + d; |
| 67 | if (r > UCHAR_MAX) { |
| 68 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 69 | } |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 70 | |
| 71 | n = r; |
| 72 | ++q; |
| 73 | } while (++num_digits < 3); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 74 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 75 | if (num_digits == 0) { /* mnemonic escape sequence? */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | p = charmap; |
| 77 | do { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 78 | if (*p == *q) { |
| 79 | q++; |
| 80 | break; |
| 81 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | } while (*++p); |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 83 | n = *(p+(sizeof(charmap)/2)); |
| 84 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 85 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 86 | *ptr = q; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 87 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 88 | return (char) n; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | } |