[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]
diff --git a/examples/chip-tool/commands/interactive/InteractiveCommands.cpp b/examples/chip-tool/commands/interactive/InteractiveCommands.cpp
index 1045930..dbb3a6f 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 <iomanip>
#include <readline/history.h>
#include <readline/readline.h>
+#include <sstream>
char kInteractiveModeName[] = "";
constexpr const char * kInteractiveModePrompt = ">>> ";
@@ -102,9 +104,10 @@
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 @@
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 @@
mHandler->RunInteractive(argsCount, args);
gIsCommandRunning = false;
+ // Do not delete arg[0]
+ while (--argsCount)
+ delete[] args[argsCount];
+
return true;
}