From 46a6daccfeef4bded671cdc387f4d0c263b1c384 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Mon, 19 Jun 2023 01:07:00 +0300 Subject: [PATCH] Userspace: Shell argument parsing now appriciates quotes --- userspace/Shell/main.cpp | 51 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/userspace/Shell/main.cpp b/userspace/Shell/main.cpp index b895a407..5987888c 100644 --- a/userspace/Shell/main.cpp +++ b/userspace/Shell/main.cpp @@ -12,10 +12,55 @@ struct termios old_termios, new_termios; BAN::Vector parse_command(BAN::StringView command) { - auto args = MUST(command.split(' ')); + enum class State + { + Normal, + SingleQuote, + DoubleQuote, + }; + BAN::Vector result; - for (auto arg : args) - MUST(result.push_back(arg)); + + State state = State::Normal; + BAN::String current; + for (char c : command) + { + switch (state) + { + case State::Normal: + if (c == '\'') + state = State::SingleQuote; + else if (c == '"') + state = State::DoubleQuote; + else if (!isspace(c)) + MUST(current.push_back(c)); + else + { + if (!current.empty()) + { + MUST(result.push_back(current)); + current.clear(); + } + } + break; + case State::SingleQuote: + if (c == '\'') + state = State::Normal; + else + MUST(current.push_back(c)); + break; + case State::DoubleQuote: + if (c == '"') + state = State::Normal; + else + MUST(current.push_back(c)); + break; + } + } + + // FIXME: handle state != State::Normal + MUST(result.push_back(BAN::move(current))); + return result; }