Skip to content

Commit

Permalink
Merge pull request cyberbotics#1713 from cyberbotics/fix-controller-args
Browse files Browse the repository at this point in the history
Fixed controller args with double quotes
  • Loading branch information
omichel authored Jun 2, 2020
2 parents b210aca + af6038e commit 3506ff5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/webots/control/WbController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ void WbController::start() {
if (mCommand.isEmpty()) // python has wrong version or Matlab 64 not available
return;

info(tr("Starting controller: %1").arg(mCommand + "!" + mArguments.join("+")));
info(tr("Starting controller: %1").arg(commandLine()));

#ifdef __linux__
if (!qgetenv("WEBOTS_FIREJAIL_CONTROLLERS").isEmpty() && mRobot->findField("controller")) {
Expand Down Expand Up @@ -645,7 +645,7 @@ void WbController::reportMissingCommand(const QString &command) {
}

void WbController::reportFailedStart() {
warn(tr("failed to start: %1").arg(mCommand + " " + mArguments.join(" ")));
warn(tr("failed to start: %1").arg(commandLine()));

switch (mType) {
case WbFileUtil::EXECUTABLE: {
Expand Down Expand Up @@ -729,8 +729,7 @@ void WbController::startVoidExecutable() {
copyBinaryAndDependencies(mCommand);

mCommand = QDir::toNativeSeparators(mCommand);
if (!args().isEmpty())
mArguments << args().split(" ");
mArguments << argsList();
}

void WbController::startExecutable() {
Expand All @@ -739,8 +738,7 @@ void WbController::startExecutable() {
copyBinaryAndDependencies(mCommand);

mCommand = QDir::toNativeSeparators(mCommand);
if (!args().isEmpty())
mArguments << args().split(" ");
mArguments << argsList();
}

void WbController::startJava(bool jar) {
Expand Down Expand Up @@ -774,8 +772,7 @@ void WbController::startJava(bool jar) {
if (!mJavaOptions.isEmpty())
mArguments << mJavaOptions.split(" ");
mArguments << name();
if (!args().isEmpty())
mArguments << args().split(" ");
mArguments << argsList();
}

void WbController::startPython() {
Expand All @@ -786,8 +783,7 @@ void WbController::startPython() {
if (!mPythonOptions.isEmpty())
mArguments << mPythonOptions.split(" ");
mArguments << name() + ".py";
if (!args().isEmpty())
mArguments << args().split(" ");
mArguments << argsList();
}

void WbController::startMatlab() {
Expand Down Expand Up @@ -887,6 +883,36 @@ const QString &WbController::args() const {
return mRobot->controllerArgs();
}

// Extract the argument list from the Robot.controllerArgs string
// Double quotes are removed from each argument string, as it should
QStringList WbController::argsList() const {
QStringList list;
const QString args = mRobot->controllerArgs().trimmed();
if (args.length() == 0)
return list;
bool quote = false;
int previous = 0;
for (int i = 0; i < args.length(); i++) {
if (args[i] == '"')
quote = !quote;
if (args[i] == ' ' && !quote) {
const QString argument = args.mid(previous, i - previous).replace("\"", "").trimmed();
if (!argument.isEmpty())
list << args.mid(previous, i - previous).replace("\"", "");
previous = i + 1;
}
}
list << args.mid(previous).replace("\"", "");
return list;
}

QString WbController::commandLine() const { // returns the command line with double quotes if needed
QString commandLine = mCommand.contains(" ") ? "\"" + mCommand + "\"" : mCommand;
foreach (const QString argument, mArguments)
commandLine += " " + (argument.contains(" ") ? "\"" + argument + "\"" : argument);
return commandLine;
}

void WbController::handleControllerExit() {
if (mRobot->controllerName() == "<extern>") {
processFinished(0, QProcess::NormalExit);
Expand Down
2 changes: 2 additions & 0 deletions src/webots/control/WbController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public slots:
void copyBinaryAndDependencies(const QString &filename);
void appendMessageToBuffer(const QString &message, QString *buffer);
void flushBuffer(QString *buffer);
QStringList argsList() const;
QString commandLine() const;

private slots:
void readStdout();
Expand Down

0 comments on commit 3506ff5

Please sign in to comment.