From ed90d39cd782f2491754bfc67f05247f521e9e59 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 25 Jan 2020 12:16:45 +0100 Subject: [PATCH] Shell: Allow empty tokens if enclosed in single or double quotes Previously the shell parser would discard empty tokens. We now allow them when they are enclosed in quotes (either '' or "") This means that a command like _echo ""_ will actually pass an empty string to /bin/echo in argv[1] now. --- Shell/Parser.cpp | 8 ++++---- Shell/Parser.h | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Shell/Parser.cpp b/Shell/Parser.cpp index e08aa054f2..9fcc1a2683 100644 --- a/Shell/Parser.cpp +++ b/Shell/Parser.cpp @@ -29,9 +29,9 @@ #include #include -void Parser::commit_token() +void Parser::commit_token(AllowEmptyToken allow_empty) { - if (m_token.is_empty()) + if (allow_empty == AllowEmptyToken::No && m_token.is_empty()) return; if (m_state == InRedirectionPath) { m_redirections.last().path = String::copy(m_token); @@ -239,7 +239,7 @@ Vector Parser::parse() break; case State::InSingleQuotes: if (ch == '\'') { - commit_token(); + commit_token(AllowEmptyToken::Yes); m_state = State::Free; break; } @@ -247,7 +247,7 @@ Vector Parser::parse() break; case State::InDoubleQuotes: if (ch == '\"') { - commit_token(); + commit_token(AllowEmptyToken::Yes); m_state = State::Free; break; } diff --git a/Shell/Parser.h b/Shell/Parser.h index fb516d95b1..06950e02ff 100644 --- a/Shell/Parser.h +++ b/Shell/Parser.h @@ -67,7 +67,8 @@ public: Vector parse(); private: - void commit_token(); + enum class AllowEmptyToken { No, Yes }; + void commit_token(AllowEmptyToken = AllowEmptyToken::No); void commit_subcommand(); void commit_command(); void do_pipe();