~ubuntu-branches/ubuntu/quantal/konsole/quantal-proposed

« back to all changes in this revision

Viewing changes to src/ShellCommand.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2012-06-11 23:34:57 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120611233457-qbazusvhzhfg16hu
Tags: 4:4.8.90-0ubuntu1
New upstream beta release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
using Konsole::ShellCommand;
27
27
 
28
 
// expands environment variables in 'text'
29
 
// function copied from kdelibs/kio/kio/kurlcompletion.cpp
30
 
static bool expandEnv(QString& text);
31
 
 
32
28
ShellCommand::ShellCommand(const QString& aCommand)
33
29
{
34
30
    _arguments = KShell::splitArgs(aCommand);
92
88
    return result;
93
89
}
94
90
 
 
91
bool ShellCommand::isValidEnvCharacter(const QChar& ch)
 
92
{
 
93
    const ushort code = ch.unicode();
 
94
    return isValidLeadingEnvCharacter(ch) || ('0' <= code && code <= '9');
 
95
 
 
96
}
 
97
 
 
98
bool ShellCommand::isValidLeadingEnvCharacter(const QChar& ch)
 
99
{
 
100
    const ushort code = ch.unicode();
 
101
    return (code == '_') || ('A' <= code && code <= 'Z');
 
102
}
 
103
 
95
104
/*
96
105
 * expandEnv
97
106
 *
98
107
 * Expand environment variables in text. Escaped '$' characters are ignored.
99
108
 * Return true if any variables were expanded
100
109
 */
101
 
static bool expandEnv(QString& text)
 
110
bool ShellCommand::expandEnv(QString& text)
102
111
{
103
 
    // Find all environment variables beginning with '$'
104
 
    //
105
 
    int pos = 0;
 
112
    const QLatin1Char dollarChar('$');
 
113
    const QLatin1Char backslashChar('\\');
106
114
 
 
115
    int dollarPos = 0;
107
116
    bool expanded = false;
108
117
 
109
 
    while ((pos = text.indexOf(QLatin1Char('$'), pos)) != -1) {
110
 
        // Skip escaped '$'
111
 
        //
112
 
        if (pos > 0 && text.at(pos - 1) == QLatin1Char('\\')) {
113
 
            pos++;
114
 
            // Variable found => expand
115
 
            //
 
118
    // find and expand all environment variables beginning with '$'
 
119
    while ((dollarPos = text.indexOf(dollarChar, dollarPos)) != -1) {
 
120
 
 
121
        // if '$' is the last character, there is no way of expanding
 
122
        if (dollarPos == text.length() -1 ) {
 
123
            break;
 
124
        }
 
125
 
 
126
        // skip escaped '$'
 
127
        if (dollarPos > 0 && text.at(dollarPos - 1) == backslashChar) {
 
128
            dollarPos++;
 
129
            continue;
 
130
        }
 
131
 
 
132
        // if '$' is followed by an invalid leading character, skip this '$'
 
133
        if (!isValidLeadingEnvCharacter(text.at(dollarPos + 1))) {
 
134
            dollarPos++;
 
135
            continue;
 
136
        }
 
137
 
 
138
        int endPos = dollarPos + 1;
 
139
        Q_ASSERT( endPos < text.length());
 
140
        while (endPos < text.length() && isValidEnvCharacter(text.at(endPos))) {
 
141
            endPos++;
 
142
        }
 
143
 
 
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()));
 
147
 
 
148
        if (!value.isEmpty()) {
 
149
            text.replace(dollarPos, len, value);
 
150
            expanded = true;
 
151
            dollarPos = dollarPos + value.length();
116
152
        } else {
117
 
            // Find the end of the variable = next '/' or ' '
118
 
            //
119
 
            int pos2 = text.indexOf(QLatin1Char(' '), pos + 1);
120
 
            const int pos_tmp = text.indexOf(QLatin1Char('/'), pos + 1);
121
 
 
122
 
            if (pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2))
123
 
                pos2 = pos_tmp;
124
 
 
125
 
            if (pos2 == -1)
126
 
                pos2 = text.length();
127
 
 
128
 
            // Replace if the variable is terminated by '/' or ' '
129
 
            // and defined
130
 
            //
131
 
            if (pos2 >= 0) {
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()));
136
 
 
137
 
                if (!value.isEmpty()) {
138
 
                    expanded = true;
139
 
                    text.replace(pos, len, value);
140
 
                    pos = pos + value.length();
141
 
                } else {
142
 
                    pos = pos2;
143
 
                }
144
 
            }
 
153
            dollarPos = endPos;
145
154
        }
146
155
    }
147
156