Skip to content

Commit

Permalink
Correctly pass arguments to the shell
Browse files Browse the repository at this point in the history
The following command works; dbus stuffs and maybe more needs testing

./build/qterminal -e nvim -p "some file with spaces"\ and\ unquoted\
spaces second\ file

Fixes #335
  • Loading branch information
Chih-Hsuan Yen committed Jan 2, 2021
1 parent ccc8458 commit 3bc755f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ QTerminalApp * QTerminalApp::m_instance = nullptr;
exit(code);
}

void parse_args(int argc, char* argv[], QString& workdir, QString & shell_command, out bool& dropMode)
void parse_args(int argc, char* argv[], QString& workdir, QStringList & shell_command, out bool& dropMode)
{
int next_option;
dropMode = false;
Expand All @@ -87,13 +87,13 @@ void parse_args(int argc, char* argv[], QString& workdir, QString & shell_comman
workdir = QString::fromLocal8Bit(optarg);
break;
case 'e':
shell_command = QString::fromLocal8Bit(optarg);
shell_command << QString::fromLocal8Bit(optarg);
// #15 "Raw" -e params
// Passing "raw" params (like konsole -e mcedit /tmp/tmp.txt") is more preferable - then I can call QString("qterminal -e ") + cmd_line in other programs
while (optind < argc)
{
//printf("arg: %d - %s\n", optind, argv[optind]);
shell_command += QLatin1Char(' ') + QString::fromLocal8Bit(argv[optind++]);
shell_command << QString::fromLocal8Bit(argv[optind++]);
}
break;
case 'd':
Expand Down Expand Up @@ -143,7 +143,8 @@ int main(int argc, char *argv[])
app->registerOnDbus();
#endif

QString workdir, shell_command;
QString workdir;
QStringList shell_command;
bool dropMode;
parse_args(argc, argv, workdir, shell_command, dropMode);

Expand Down
30 changes: 19 additions & 11 deletions src/terminalconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "properties.h"
#include "termwidget.h"

TerminalConfig::TerminalConfig(const QString & wdir, const QString & shell)
TerminalConfig::TerminalConfig(const QString & wdir, const QStringList & shell)
{
m_workingDirectory = wdir;
m_shell = shell;
Expand All @@ -32,28 +32,36 @@ QString TerminalConfig::getWorkingDirectory()
return QTerminalApp::Instance()->getWorkingDirectory();
}

QString TerminalConfig::getShell()
QStringList TerminalConfig::getShell()
{
if (!m_shell.isEmpty())
return m_shell;

QString shellString;
if (!Properties::Instance()->shell.isEmpty())
return Properties::Instance()->shell;
QByteArray envShell = qgetenv("SHELL");
if (envShell.constData() != nullptr)
{
QString shellString = QString::fromLocal8Bit(envShell);
if (!shellString.isEmpty())
return shellString;
shellString = Properties::Instance()->shell;
}
else
{
QByteArray envShell = qgetenv("SHELL");
if (envShell.constData() != NULL)
{
shellString = QString::fromLocal8Bit(envShell);
}
}
return QString();
QStringList ret;
if (!shellString.isEmpty())
ret << shellString;
return ret;
}

void TerminalConfig::setWorkingDirectory(const QString &val)
{
m_workingDirectory = val;
}

void TerminalConfig::setShell(const QString &val)
void TerminalConfig::setShell(const QStringList &val)
{
m_shell = val;
}
Expand Down Expand Up @@ -98,7 +106,7 @@ TerminalConfig TerminalConfig::fromDbus(const QHash<QString,QVariant> &termArgs)
if (termArgs.contains(QLatin1String(DBUS_ARG_SHELL))) {
shell = variantToString(termArgs[QLatin1String(DBUS_ARG_SHELL)], shell);
}
return TerminalConfig(wdir, shell);
return TerminalConfig(wdir, QStringList() << shell);
}

#endif
Expand Down
10 changes: 5 additions & 5 deletions src/terminalconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ class TermWidget;
class TerminalConfig
{
public:
TerminalConfig(const QString & wdir, const QString & shell);
TerminalConfig(const QString & wdir, const QStringList & shell);
TerminalConfig(const TerminalConfig &cfg);
TerminalConfig();

QString getWorkingDirectory();
QString getShell();
QStringList getShell();

void setWorkingDirectory(const QString &val);
void setShell(const QString &val);
void setShell(const QStringList &val);
void provideCurrentDirectory(const QString &val);

#ifdef HAVE_QDBUS
Expand All @@ -32,7 +32,7 @@ class TerminalConfig
// True when
QString m_currentDirectory;
QString m_workingDirectory;
QString m_shell;
QStringList m_shell;
};

#endif
#endif
9 changes: 2 additions & 7 deletions src/termwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ TermWidgetImpl::TermWidgetImpl(TerminalConfig &cfg, QWidget * parent)

setWorkingDirectory(cfg.getWorkingDirectory());

QString shell = cfg.getShell();
QStringList shell = cfg.getShell();
if (!shell.isEmpty())
{
//qDebug() << "Shell program:" << shell;
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
QStringList parts = shell.split(QRegExp(QStringLiteral("\\s+")), Qt::SkipEmptyParts);
#else
QStringList parts = shell.split(QRegExp(QStringLiteral("\\s+")), QString::SkipEmptyParts);
#endif
//qDebug() << parts;
QStringList parts = shell;
setShellProgram(parts.at(0));
parts.removeAt(0);
if (parts.count())
Expand Down

0 comments on commit 3bc755f

Please sign in to comment.