Skip to content

Commit

Permalink
[chip-tool] Support double quotes for arguments with spaces in intera…
Browse files Browse the repository at this point in the history
…ctive mode (project-chip#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]
  • Loading branch information
agners authored Jun 6, 2022
1 parent ae6e092 commit 588c6fa
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions examples/chip-tool/commands/interactive/InteractiveCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ">>> ";
Expand Down Expand Up @@ -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)
{
Expand All @@ -114,14 +117,19 @@ 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();
gIsCommandRunning = true;
mHandler->RunInteractive(argsCount, args);
gIsCommandRunning = false;

// Do not delete arg[0]
while (--argsCount)
delete[] args[argsCount];

return true;
}

0 comments on commit 588c6fa

Please sign in to comment.