blob: 7b1d97f9c29f80f44fa1673896e2eade6da31d79 [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 */
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011#include "libbb.h"
12
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000013#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 Vlasenkodefc1ea2008-06-27 02:52:20 +000019char FAST_FUNC bb_process_escape_sequence(const char **ptr)
Eric Andersenaad1a882001-03-16 22:47:14 +000020{
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000021 const char *q;
Denis Vlasenko248b4a72008-09-19 23:43:59 +000022 unsigned num_digits;
Denis Vlasenko248b4a72008-09-19 23:43:59 +000023 unsigned n;
Denis Vlasenko248b4a72008-09-19 23:43:59 +000024 unsigned base;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000025
26 num_digits = n = 0;
27 base = 8;
Eric Andersene5dfced2001-04-09 22:48:12 +000028 q = *ptr;
Eric Andersenaad1a882001-03-16 22:47:14 +000029
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020030 if (WANT_HEX_ESCAPES && *q == 'x') {
Eric Andersenb2a30052004-07-26 12:11:32 +000031 ++q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000032 base = 16;
33 ++num_digits;
Eric Andersenb2a30052004-07-26 12:11:32 +000034 }
35
Denys Vlasenkoecc2a2e2009-08-29 22:53:41 +020036 /* bash requires leading 0 in octal escapes:
37 * \02 works, \2 does not (prints \ and 2).
38 * We treat \2 as a valid octal escape sequence. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 do {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020040 unsigned r;
Denys Vlasenko53600592010-10-23 21:06:06 +020041#if !WANT_HEX_ESCAPES
42 unsigned d = (unsigned char)(*q) - '0';
43#else
44 unsigned d = (unsigned char)_tolower(*q) - '0';
45 if (d >= 10)
46 d += ('0' - 'a' + 10);
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000047#endif
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000048 if (d >= base) {
Denys Vlasenko53600592010-10-23 21:06:06 +020049 if (WANT_HEX_ESCAPES && base == 16) {
50 --num_digits;
51 if (num_digits == 0) {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020052 /* \x<bad_char>: return '\',
53 * leave ptr pointing to x */
54 return '\\';
Denys Vlasenko53600592010-10-23 21:06:06 +020055 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000056 }
Eric Andersenccfc4482004-07-27 16:45:46 +000057 break;
Eric Andersenb2a30052004-07-26 12:11:32 +000058 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000059
60 r = n * base + d;
61 if (r > UCHAR_MAX) {
62 break;
Eric Andersene5dfced2001-04-09 22:48:12 +000063 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000064
65 n = r;
66 ++q;
67 } while (++num_digits < 3);
Eric Andersenaad1a882001-03-16 22:47:14 +000068
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020069 if (num_digits == 0) {
70 /* Not octal or hex escape sequence.
71 * Is it one-letter one? */
72
73 /* bash builtin "echo -e '\ec'" interprets \e as ESC,
74 * but coreutils "/bin/echo -e '\ec'" does not.
75 * Manpages tend to support coreutils way.
76 * Update: coreutils added support for \e on 28 Oct 2009. */
77 static const char charmap[] ALIGN1 = {
78 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\',
79 '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\',
80 };
81 const char *p = charmap;
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 do {
Eric Andersene5dfced2001-04-09 22:48:12 +000083 if (*p == *q) {
84 q++;
85 break;
86 }
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020087 } while (*++p != '\\');
88 /* p points to found escape char or '\',
Denys Vlasenko53600592010-10-23 21:06:06 +020089 * advance it and find what it translates to.
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020090 * Note that \NUL and unrecognized sequence \z return '\'
91 * and leave ptr pointing to NUL or z. */
92 n = p[sizeof(charmap) / 2];
Eric Andersene5dfced2001-04-09 22:48:12 +000093 }
Eric Andersenaad1a882001-03-16 22:47:14 +000094
Eric Andersene5dfced2001-04-09 22:48:12 +000095 *ptr = q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000096
Eric Andersene5dfced2001-04-09 22:48:12 +000097 return (char) n;
Eric Andersenaad1a882001-03-16 22:47:14 +000098}
Denys Vlasenko53600592010-10-23 21:06:06 +020099
100char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src)
101{
102 while (1) {
103 char c, c1;
104 c = c1 = *src++;
105 if (c1 == '\\')
106 c1 = bb_process_escape_sequence(&src);
107 *dst = c1;
108 if (c == '\0')
109 return dst;
110 dst++;
111 }
112}