Port the splitCommand method (#79)

This commit is contained in:
Kevin Yue 2021-08-19 19:10:05 +08:00 committed by GitHub
parent 53c8572cf6
commit 42cae3ff26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 1 deletions

View File

@ -39,6 +39,47 @@ QString GPService::findBinary()
return nullptr;
}
/* Port from https://github.com/qt/qtbase/blob/11d1dcc6e263c5059f34b44d531c9ccdf7c0b1d6/src/corelib/io/qprocess.cpp#L2115 */
QStringList GPService::splitCommand(QStringView command)
{
QStringList args;
QString tmp;
int quoteCount = 0;
bool inQuote = false;
// handle quoting. tokens can be surrounded by double quotes
// "hello world". three consecutive double quotes represent
// the quote character itself.
for (int i = 0; i < command.size(); ++i) {
if (command.at(i) == QLatin1Char('"')) {
++quoteCount;
if (quoteCount == 3) {
// third consecutive quote
quoteCount = 0;
tmp += command.at(i);
}
continue;
}
if (quoteCount) {
if (quoteCount == 1)
inQuote = !inQuote;
quoteCount = 0;
}
if (!inQuote && command.at(i).isSpace()) {
if (!tmp.isEmpty()) {
args += tmp;
tmp.clear();
}
} else {
tmp += command.at(i);
}
}
if (!tmp.isEmpty())
args += tmp;
return args;
}
void GPService::quit()
{
if (openconnect->state() == QProcess::NotRunning) {
@ -65,7 +106,7 @@ void GPService::connect(QString server, QString username, QString passwd, QStrin
QStringList args;
args << QCoreApplication::arguments().mid(1)
<< "--protocol=gp"
<< QProcess::splitCommand(extraArgs)
<< splitCommand(extraArgs)
<< "-u" << username
<< "-C" << passwd
<< server;

View File

@ -53,6 +53,7 @@ private:
void log(QString msg);
static QString findBinary();
static QStringList splitCommand(QStringView command);
};
#endif // GLOBALPROTECTSERVICE_H