blob: 13022b83eea5428a8c999f549b08486e4bf5a144 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
Eric Andersenaad1a882001-03-16 22:47:14 +000010#include "libbb.h"
11
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000012#define WANT_HEX_ESCAPES 1
13
14/* Usual "this only works for ascii compatible encodings" disclaimer. */
15#undef _tolower
16#define _tolower(X) ((X)|((char) 0x20))
17
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000018char FAST_FUNC bb_process_escape_sequence(const char **ptr)
Eric Andersenaad1a882001-03-16 22:47:14 +000019{
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000020 const char *q;
Denis Vlasenko248b4a72008-09-19 23:43:59 +000021 unsigned num_digits;
Denis Vlasenko248b4a72008-09-19 23:43:59 +000022 unsigned n;
Denis Vlasenko248b4a72008-09-19 23:43:59 +000023 unsigned base;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000024
25 num_digits = n = 0;
26 base = 8;
Eric Andersene5dfced2001-04-09 22:48:12 +000027 q = *ptr;
Eric Andersenaad1a882001-03-16 22:47:14 +000028
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020029 if (WANT_HEX_ESCAPES && *q == 'x') {
Eric Andersenb2a30052004-07-26 12:11:32 +000030 ++q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000031 base = 16;
32 ++num_digits;
Eric Andersenb2a30052004-07-26 12:11:32 +000033 }
34
Denys Vlasenkoecc2a2e2009-08-29 22:53:41 +020035 /* bash requires leading 0 in octal escapes:
36 * \02 works, \2 does not (prints \ and 2).
37 * We treat \2 as a valid octal escape sequence. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 do {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020039 unsigned r;
Denys Vlasenko53600592010-10-23 21:06:06 +020040 unsigned d = (unsigned char)(*q) - '0';
Denys Vlasenko9a2b6dc2018-11-29 13:15:57 +010041#if WANT_HEX_ESCAPES
Denys Vlasenko480c7e52018-11-29 12:34:50 +010042 if (d >= 10) {
Denys Vlasenko9a2b6dc2018-11-29 13:15:57 +010043 d = (unsigned char)_tolower(*q) - 'a';
44 //d += 10;
45 /* The above would map 'A'-'F' and 'a'-'f' to 10-15,
Denys Vlasenko480c7e52018-11-29 12:34:50 +010046 * however, some chars like '@' would map to 9 < base.
47 * Do not allow that, map invalid chars to N > base:
48 */
Denys Vlasenko480c7e52018-11-29 12:34:50 +010049 if ((int)d >= 0)
50 d += 10;
51 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000052#endif
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000053 if (d >= base) {
Denys Vlasenko53600592010-10-23 21:06:06 +020054 if (WANT_HEX_ESCAPES && base == 16) {
55 --num_digits;
56 if (num_digits == 0) {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020057 /* \x<bad_char>: return '\',
58 * leave ptr pointing to x */
59 return '\\';
Denys Vlasenko53600592010-10-23 21:06:06 +020060 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000061 }
Eric Andersenccfc4482004-07-27 16:45:46 +000062 break;
Eric Andersenb2a30052004-07-26 12:11:32 +000063 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000064
65 r = n * base + d;
66 if (r > UCHAR_MAX) {
67 break;
Eric Andersene5dfced2001-04-09 22:48:12 +000068 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000069
70 n = r;
71 ++q;
72 } while (++num_digits < 3);
Eric Andersenaad1a882001-03-16 22:47:14 +000073
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020074 if (num_digits == 0) {
75 /* Not octal or hex escape sequence.
76 * Is it one-letter one? */
77
78 /* bash builtin "echo -e '\ec'" interprets \e as ESC,
79 * but coreutils "/bin/echo -e '\ec'" does not.
80 * Manpages tend to support coreutils way.
81 * Update: coreutils added support for \e on 28 Oct 2009. */
82 static const char charmap[] ALIGN1 = {
Denys Vlasenkoa2d27a12010-10-25 12:14:21 +020083 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\', '\0',
84 '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\',
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020085 };
86 const char *p = charmap;
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 do {
Eric Andersene5dfced2001-04-09 22:48:12 +000088 if (*p == *q) {
89 q++;
90 break;
91 }
Denys Vlasenkoa2d27a12010-10-25 12:14:21 +020092 } while (*++p != '\0');
93 /* p points to found escape char or NUL,
Denys Vlasenko53600592010-10-23 21:06:06 +020094 * advance it and find what it translates to.
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020095 * Note that \NUL and unrecognized sequence \z return '\'
96 * and leave ptr pointing to NUL or z. */
97 n = p[sizeof(charmap) / 2];
Eric Andersene5dfced2001-04-09 22:48:12 +000098 }
Eric Andersenaad1a882001-03-16 22:47:14 +000099
Eric Andersene5dfced2001-04-09 22:48:12 +0000100 *ptr = q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +0000101
Eric Andersene5dfced2001-04-09 22:48:12 +0000102 return (char) n;
Eric Andersenaad1a882001-03-16 22:47:14 +0000103}
Denys Vlasenko53600592010-10-23 21:06:06 +0200104
105char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src)
106{
107 while (1) {
108 char c, c1;
109 c = c1 = *src++;
110 if (c1 == '\\')
111 c1 = bb_process_escape_sequence(&src);
112 *dst = c1;
113 if (c == '\0')
114 return dst;
115 dst++;
116 }
117}