blob: 59d0d3ea8ba5572e59f685ac6527b76bb2609385 [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#if !WANT_HEX_ESCAPES
41 unsigned d = (unsigned char)(*q) - '0';
42#else
43 unsigned d = (unsigned char)_tolower(*q) - '0';
44 if (d >= 10)
45 d += ('0' - 'a' + 10);
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000046#endif
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000047 if (d >= base) {
Denys Vlasenko53600592010-10-23 21:06:06 +020048 if (WANT_HEX_ESCAPES && base == 16) {
49 --num_digits;
50 if (num_digits == 0) {
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020051 /* \x<bad_char>: return '\',
52 * leave ptr pointing to x */
53 return '\\';
Denys Vlasenko53600592010-10-23 21:06:06 +020054 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000055 }
Eric Andersenccfc4482004-07-27 16:45:46 +000056 break;
Eric Andersenb2a30052004-07-26 12:11:32 +000057 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000058
59 r = n * base + d;
60 if (r > UCHAR_MAX) {
61 break;
Eric Andersene5dfced2001-04-09 22:48:12 +000062 }
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000063
64 n = r;
65 ++q;
66 } while (++num_digits < 3);
Eric Andersenaad1a882001-03-16 22:47:14 +000067
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020068 if (num_digits == 0) {
69 /* Not octal or hex escape sequence.
70 * Is it one-letter one? */
71
72 /* bash builtin "echo -e '\ec'" interprets \e as ESC,
73 * but coreutils "/bin/echo -e '\ec'" does not.
74 * Manpages tend to support coreutils way.
75 * Update: coreutils added support for \e on 28 Oct 2009. */
76 static const char charmap[] ALIGN1 = {
Denys Vlasenkoa2d27a12010-10-25 12:14:21 +020077 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\', '\0',
78 '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\',
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020079 };
80 const char *p = charmap;
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 do {
Eric Andersene5dfced2001-04-09 22:48:12 +000082 if (*p == *q) {
83 q++;
84 break;
85 }
Denys Vlasenkoa2d27a12010-10-25 12:14:21 +020086 } while (*++p != '\0');
87 /* p points to found escape char or NUL,
Denys Vlasenko53600592010-10-23 21:06:06 +020088 * advance it and find what it translates to.
Denys Vlasenko2b299fe2010-10-24 01:58:04 +020089 * Note that \NUL and unrecognized sequence \z return '\'
90 * and leave ptr pointing to NUL or z. */
91 n = p[sizeof(charmap) / 2];
Eric Andersene5dfced2001-04-09 22:48:12 +000092 }
Eric Andersenaad1a882001-03-16 22:47:14 +000093
Eric Andersene5dfced2001-04-09 22:48:12 +000094 *ptr = q;
Manuel Novoa III 413db4d2004-07-29 23:15:16 +000095
Eric Andersene5dfced2001-04-09 22:48:12 +000096 return (char) n;
Eric Andersenaad1a882001-03-16 22:47:14 +000097}
Denys Vlasenko53600592010-10-23 21:06:06 +020098
99char* FAST_FUNC strcpy_and_process_escape_sequences(char *dst, const char *src)
100{
101 while (1) {
102 char c, c1;
103 c = c1 = *src++;
104 if (c1 == '\\')
105 c1 = bb_process_escape_sequence(&src);
106 *dst = c1;
107 if (c == '\0')
108 return dst;
109 dst++;
110 }
111}