91
bool ShellCommand::isValidEnvCharacter(const QChar& ch)
93
const ushort code = ch.unicode();
94
return isValidLeadingEnvCharacter(ch) || ('0' <= code && code <= '9');
98
bool ShellCommand::isValidLeadingEnvCharacter(const QChar& ch)
100
const ushort code = ch.unicode();
101
return (code == '_') || ('A' <= code && code <= 'Z');
98
107
* Expand environment variables in text. Escaped '$' characters are ignored.
99
108
* Return true if any variables were expanded
101
static bool expandEnv(QString& text)
110
bool ShellCommand::expandEnv(QString& text)
103
// Find all environment variables beginning with '$'
112
const QLatin1Char dollarChar('$');
113
const QLatin1Char backslashChar('\\');
107
116
bool expanded = false;
109
while ((pos = text.indexOf(QLatin1Char('$'), pos)) != -1) {
112
if (pos > 0 && text.at(pos - 1) == QLatin1Char('\\')) {
114
// Variable found => expand
118
// find and expand all environment variables beginning with '$'
119
while ((dollarPos = text.indexOf(dollarChar, dollarPos)) != -1) {
121
// if '$' is the last character, there is no way of expanding
122
if (dollarPos == text.length() -1 ) {
127
if (dollarPos > 0 && text.at(dollarPos - 1) == backslashChar) {
132
// if '$' is followed by an invalid leading character, skip this '$'
133
if (!isValidLeadingEnvCharacter(text.at(dollarPos + 1))) {
138
int endPos = dollarPos + 1;
139
Q_ASSERT( endPos < text.length());
140
while (endPos < text.length() && isValidEnvCharacter(text.at(endPos))) {
144
const int len = endPos - dollarPos;
145
const QString key = text.mid(dollarPos + 1, len - 1);
146
const QString value = QString::fromLocal8Bit(qgetenv(key.toLocal8Bit()));
148
if (!value.isEmpty()) {
149
text.replace(dollarPos, len, value);
151
dollarPos = dollarPos + value.length();
117
// Find the end of the variable = next '/' or ' '
119
int pos2 = text.indexOf(QLatin1Char(' '), pos + 1);
120
const int pos_tmp = text.indexOf(QLatin1Char('/'), pos + 1);
122
if (pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2))
126
pos2 = text.length();
128
// Replace if the variable is terminated by '/' or ' '
132
const int len = pos2 - pos;
133
const QString key = text.mid(pos + 1, len - 1);
134
const QString value =
135
QString::fromLocal8Bit(qgetenv(key.toLocal8Bit()));
137
if (!value.isEmpty()) {
139
text.replace(pos, len, value);
140
pos = pos + value.length();