From 588c6fab673079cc1ad235b653f4942e0576c90b Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 6 Jun 2022 22:17:34 +0200 Subject: [PATCH] [chip-tool] Support double quotes for arguments with spaces in interactive mode (#19238) * [chip-tool] Support double quotes for arguments with spaces This is especially useful to pair to a SSID with spaces. * Restyled * Use correct allocation size * Add comment about arg[0] --- .../commands/interactive/InteractiveCommands.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 10459305562b48..dbb3a6fbc1c5f0 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -18,8 +18,10 @@ #include "InteractiveCommands.h" +#include #include #include +#include char kInteractiveModeName[] = ""; constexpr const char * kInteractiveModePrompt = ">>> "; @@ -102,9 +104,10 @@ bool InteractiveStartCommand::ParseCommand(char * command) char * args[kInteractiveModeArgumentsMaxLength]; args[0] = kInteractiveModeName; int argsCount = 1; + std::string arg; - char * token = strtok(command, " "); - while (token != nullptr) + std::stringstream ss(command); + while (ss >> std::quoted(arg)) { if (argsCount == kInteractiveModeArgumentsMaxLength) { @@ -114,8 +117,9 @@ bool InteractiveStartCommand::ParseCommand(char * command) return true; } - args[argsCount++] = token; - token = strtok(nullptr, " "); + char * carg = new char[arg.size() + 1]; + strcpy(carg, arg.c_str()); + args[argsCount++] = carg; } ClearLine(); @@ -123,5 +127,9 @@ bool InteractiveStartCommand::ParseCommand(char * command) mHandler->RunInteractive(argsCount, args); gIsCommandRunning = false; + // Do not delete arg[0] + while (--argsCount) + delete[] args[argsCount]; + return true; }