From 71753f7d2f89132cae0676de329bc31741434049 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 6 Jun 2022 17:54:36 +0200 Subject: [PATCH 1/4] [chip-tool] Support double quotes for arguments with spaces This is especially useful to pair to a SSID with spaces. --- .../commands/interactive/InteractiveCommands.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 10459305562b48..df75b80c6ae701 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -18,6 +18,8 @@ #include "InteractiveCommands.h" +#include +#include #include #include @@ -102,10 +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) { gIsCommandRunning = true; @@ -114,8 +116,9 @@ bool InteractiveStartCommand::ParseCommand(char * command) return true; } - args[argsCount++] = token; - token = strtok(nullptr, " "); + char* carg = new char[ arg.size() ]; + strcpy(carg, arg.c_str() ); + args[argsCount++] = carg; } ClearLine(); @@ -123,5 +126,8 @@ bool InteractiveStartCommand::ParseCommand(char * command) mHandler->RunInteractive(argsCount, args); gIsCommandRunning = false; + while (--argsCount) + delete[] args[argsCount]; + return true; } From 024aeea856431657c195129cef515bbc13ab87b0 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 6 Jun 2022 18:20:37 +0200 Subject: [PATCH 2/4] Restyled --- .../commands/interactive/InteractiveCommands.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index df75b80c6ae701..6576afabff61ec 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -19,9 +19,9 @@ #include "InteractiveCommands.h" #include -#include #include #include +#include char kInteractiveModeName[] = ""; constexpr const char * kInteractiveModePrompt = ">>> "; @@ -107,7 +107,8 @@ bool InteractiveStartCommand::ParseCommand(char * command) std::string arg; std::stringstream ss(command); - while (ss >> std::quoted(arg)) { + while (ss >> std::quoted(arg)) + { if (argsCount == kInteractiveModeArgumentsMaxLength) { gIsCommandRunning = true; @@ -116,8 +117,8 @@ bool InteractiveStartCommand::ParseCommand(char * command) return true; } - char* carg = new char[ arg.size() ]; - strcpy(carg, arg.c_str() ); + char * carg = new char[arg.size()]; + strcpy(carg, arg.c_str()); args[argsCount++] = carg; } From 1bf32ed6119ea551fdcc8b0b44f13a9a8bde8a1f Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 6 Jun 2022 19:41:22 +0200 Subject: [PATCH 3/4] Use correct allocation size --- examples/chip-tool/commands/interactive/InteractiveCommands.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 6576afabff61ec..2ef246cbbda077 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -117,7 +117,7 @@ bool InteractiveStartCommand::ParseCommand(char * command) return true; } - char * carg = new char[arg.size()]; + char * carg = new char[arg.size() + 1]; strcpy(carg, arg.c_str()); args[argsCount++] = carg; } From dfd4e7f75aa17655fc06b0efc1f32ccad7863756 Mon Sep 17 00:00:00 2001 From: Stefan Agner Date: Mon, 6 Jun 2022 19:42:28 +0200 Subject: [PATCH 4/4] Add comment about arg[0] --- examples/chip-tool/commands/interactive/InteractiveCommands.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp index 2ef246cbbda077..dbb3a6fbc1c5f0 100644 --- a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp +++ b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp @@ -127,6 +127,7 @@ bool InteractiveStartCommand::ParseCommand(char * command) mHandler->RunInteractive(argsCount, args); gIsCommandRunning = false; + // Do not delete arg[0] while (--argsCount) delete[] args[argsCount];