MauiKit Terminal

ShellCommand.cpp
1/*
2 SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7// Own
8#include "ShellCommand.h"
9
10// KDE
11#include <KShell>
12
14
16 : _arguments(KShell::splitArgs(aCommand))
17{
18}
19
20ShellCommand::ShellCommand(const QString &aCommand, const QStringList &aArguments)
21 : _arguments(aArguments)
22{
23 if (!_arguments.isEmpty()) {
24 _arguments[0] = aCommand;
25 }
26}
27
29{
30 QStringList quotedArgs(_arguments);
31 for (int i = 0; i < quotedArgs.size(); i++) {
32 QString arg = quotedArgs.at(i);
33 bool hasSpace = false;
34 for (int j = 0; j < arg.size(); j++) {
35 if (arg[j].isSpace()) {
36 hasSpace = true;
37 }
38 }
39 if (hasSpace) {
40 quotedArgs[i] = QLatin1Char('\"') + arg + QLatin1Char('\"');
41 }
42 }
43 return quotedArgs.join(QLatin1Char(' '));
44}
45
47{
48 if (!_arguments.isEmpty()) {
49 return _arguments[0];
50 }
51 return QString();
52}
53
55{
56 return _arguments;
57}
58
60{
61 QStringList result;
62 result.reserve(items.size());
63
64 for (const QString &item : items) {
65 result << expand(item);
66 }
67
68 return result;
69}
70
72{
73 QString result = text;
74 expandEnv(result);
75 return result;
76}
77
78bool ShellCommand::isValidEnvCharacter(const QChar &ch)
79{
80 const ushort code = ch.unicode();
81 return isValidLeadingEnvCharacter(ch) || ('0' <= code && code <= '9');
82}
83
84bool ShellCommand::isValidLeadingEnvCharacter(const QChar &ch)
85{
86 const ushort code = ch.unicode();
87 return (code == '_') || ('A' <= code && code <= 'Z');
88}
89
90/*
91 * expandEnv
92 *
93 * Expand environment variables in text. Escaped '$' characters are ignored.
94 * Return true if any variables were expanded
95 */
96bool ShellCommand::expandEnv(QString &text)
97{
98 const QLatin1Char dollarChar('$');
99 const QLatin1Char backslashChar('\\');
100
101 int dollarPos = 0;
102 bool expanded = false;
103
104 // find and expand all environment variables beginning with '$'
105 while ((dollarPos = text.indexOf(dollarChar, dollarPos)) != -1) {
106 // if '$' is the last character, there is no way of expanding
107 if (dollarPos == text.length() - 1) {
108 break;
109 }
110
111 // skip escaped '$'
112 if (dollarPos > 0 && text.at(dollarPos - 1) == backslashChar) {
113 dollarPos++;
114 continue;
115 }
116
117 // if '$' is followed by an invalid leading character, skip this '$'
118 if (!isValidLeadingEnvCharacter(text.at(dollarPos + 1))) {
119 dollarPos++;
120 continue;
121 }
122
123 int endPos = dollarPos + 1;
124 Q_ASSERT(endPos < text.length());
125 while (endPos < text.length() && isValidEnvCharacter(text.at(endPos))) {
126 endPos++;
127 }
128
129 const int len = endPos - dollarPos;
130 const QString key = text.mid(dollarPos + 1, len - 1);
131 const QString value = QString::fromLocal8Bit(qgetenv(key.toLocal8Bit().constData()));
132
133 if (!value.isEmpty()) {
134 text.replace(dollarPos, len, value);
135 expanded = true;
136 dollarPos = dollarPos + value.length();
137 } else {
138 dollarPos = endPos;
139 }
140 }
141
142 return expanded;
143}
A class to parse and extract information about shell commands.
QStringList arguments() const
Returns the arguments.
QString fullCommand() const
Returns the full command line.
ShellCommand(const QString &aCommand)
Constructs a ShellCommand from a command line.
static QString expand(const QString &text)
Expands environment variables in text .
QString command() const
Returns the command.
const char * constData() const const
char16_t & unicode()
const_reference at(qsizetype i) const const
bool isEmpty() const const
void reserve(qsizetype size)
qsizetype size() const const
const QChar at(qsizetype position) const const
QString fromLocal8Bit(QByteArrayView str)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
qsizetype size() const const
QByteArray toLocal8Bit() const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.