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 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 13 | #define WANT_HEX_ESCAPES 1 |
| 14 | |
| 15 | /* Usual "this only works for ascii compatible encodings" disclaimer. */ |
| 16 | #undef _tolower |
| 17 | #define _tolower(X) ((X)|((char) 0x20)) |
| 18 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 19 | char FAST_FUNC bb_process_escape_sequence(const char **ptr) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 20 | { |
Denis Vlasenko | 248b4a7 | 2008-09-19 23:43:59 +0000 | [diff] [blame] | 21 | /* bash builtin "echo -e '\ec'" interprets \e as ESC, |
| 22 | * but coreutils "/bin/echo -e '\ec'" does not. |
Denys Vlasenko | 00f0ef4 | 2009-10-29 03:39:55 +0100 | [diff] [blame] | 23 | * manpages tend to support coreutils way. |
| 24 | * Update: coreutils added support for \e on 28 Oct 2009. */ |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 25 | static const char charmap[] ALIGN1 = { |
Denys Vlasenko | 00f0ef4 | 2009-10-29 03:39:55 +0100 | [diff] [blame] | 26 | 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\', 0, |
| 27 | '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\' }; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 28 | |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 29 | const char *p; |
| 30 | const char *q; |
Denis Vlasenko | 248b4a7 | 2008-09-19 23:43:59 +0000 | [diff] [blame] | 31 | unsigned num_digits; |
| 32 | unsigned r; |
| 33 | unsigned n; |
| 34 | unsigned d; |
| 35 | unsigned base; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 36 | |
| 37 | num_digits = n = 0; |
| 38 | base = 8; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 39 | q = *ptr; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 40 | |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 41 | #ifdef WANT_HEX_ESCAPES |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 42 | if (*q == 'x') { |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 43 | ++q; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 44 | base = 16; |
| 45 | ++num_digits; |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 46 | } |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 47 | #endif |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 48 | |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 49 | /* bash requires leading 0 in octal escapes: |
| 50 | * \02 works, \2 does not (prints \ and 2). |
| 51 | * We treat \2 as a valid octal escape sequence. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | do { |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 53 | d = (unsigned char)(*q) - '0'; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 54 | #ifdef WANT_HEX_ESCAPES |
| 55 | if (d >= 10) { |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 56 | d = (unsigned char)(_tolower(*q)) - 'a' + 10; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 57 | } |
| 58 | #endif |
| 59 | |
| 60 | if (d >= base) { |
| 61 | #ifdef WANT_HEX_ESCAPES |
| 62 | if ((base == 16) && (!--num_digits)) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | /* return '\\'; */ |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 64 | --q; |
| 65 | } |
| 66 | #endif |
Eric Andersen | ccfc448 | 2004-07-27 16:45:46 +0000 | [diff] [blame] | 67 | break; |
Eric Andersen | b2a3005 | 2004-07-26 12:11:32 +0000 | [diff] [blame] | 68 | } |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 69 | |
| 70 | r = n * base + d; |
| 71 | if (r > UCHAR_MAX) { |
| 72 | break; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 73 | } |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 74 | |
| 75 | n = r; |
| 76 | ++q; |
| 77 | } while (++num_digits < 3); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 78 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 79 | if (num_digits == 0) { /* mnemonic escape sequence? */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | p = charmap; |
| 81 | do { |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 82 | if (*p == *q) { |
| 83 | q++; |
| 84 | break; |
| 85 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 86 | } while (*++p); |
Denys Vlasenko | ecc2a2e | 2009-08-29 22:53:41 +0200 | [diff] [blame] | 87 | /* p points to found escape char or NUL, |
| 88 | * advance it and find what it translates to */ |
| 89 | p += sizeof(charmap) / 2; |
| 90 | n = *p; |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 91 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 92 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 93 | *ptr = q; |
Manuel Novoa III | 413db4d | 2004-07-29 23:15:16 +0000 | [diff] [blame] | 94 | |
Eric Andersen | e5dfced | 2001-04-09 22:48:12 +0000 | [diff] [blame] | 95 | return (char) n; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 96 | } |