~verzegnassi-stefano/+junk/ubuntu-terminal-app-uitk13

« back to all changes in this revision

Viewing changes to src/plugin/qmltermwidget/qtermwidget/lib/ShellCommand.cpp

  • Committer: Filippo Scognamiglio
  • Date: 2014-10-25 04:42:31 UTC
  • Revision ID: flscogna@gmail.com-20141025044231-javjhusbqa171127
Initial reboot commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2007 by Robert Knight <robertknight@gmail.com>
 
3
 
 
4
    Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
19
    02110-1301  USA.
 
20
*/
 
21
 
 
22
// Own
 
23
#include "ShellCommand.h"
 
24
 
 
25
//some versions of gcc(4.3) require explicit include
 
26
#include <cstdlib>
 
27
 
 
28
 
 
29
using namespace Konsole;
 
30
 
 
31
// expands environment variables in 'text'
 
32
// function copied from kdelibs/kio/kio/kurlcompletion.cpp
 
33
static bool expandEnv(QString & text);
 
34
 
 
35
ShellCommand::ShellCommand(const QString & fullCommand)
 
36
{
 
37
    bool inQuotes = false;
 
38
 
 
39
    QString builder;
 
40
 
 
41
    for ( int i = 0 ; i < fullCommand.count() ; i++ ) {
 
42
        QChar ch = fullCommand[i];
 
43
 
 
44
        const bool isLastChar = ( i == fullCommand.count() - 1 );
 
45
        const bool isQuote = ( ch == '\'' || ch == '\"' );
 
46
 
 
47
        if ( !isLastChar && isQuote ) {
 
48
            inQuotes = !inQuotes;
 
49
        } else {
 
50
            if ( (!ch.isSpace() || inQuotes) && !isQuote ) {
 
51
                builder.append(ch);
 
52
            }
 
53
 
 
54
            if ( (ch.isSpace() && !inQuotes) || ( i == fullCommand.count()-1 ) ) {
 
55
                _arguments << builder;
 
56
                builder.clear();
 
57
            }
 
58
        }
 
59
    }
 
60
}
 
61
ShellCommand::ShellCommand(const QString & command , const QStringList & arguments)
 
62
{
 
63
    _arguments = arguments;
 
64
 
 
65
    if ( !_arguments.isEmpty() ) {
 
66
        _arguments[0] = command;
 
67
    }
 
68
}
 
69
QString ShellCommand::fullCommand() const
 
70
{
 
71
    return _arguments.join(QChar(' '));
 
72
}
 
73
QString ShellCommand::command() const
 
74
{
 
75
    if ( !_arguments.isEmpty() ) {
 
76
        return _arguments[0];
 
77
    } else {
 
78
        return QString();
 
79
    }
 
80
}
 
81
QStringList ShellCommand::arguments() const
 
82
{
 
83
    return _arguments;
 
84
}
 
85
bool ShellCommand::isRootCommand() const
 
86
{
 
87
    Q_ASSERT(0); // not implemented yet
 
88
    return false;
 
89
}
 
90
bool ShellCommand::isAvailable() const
 
91
{
 
92
    Q_ASSERT(0); // not implemented yet
 
93
    return false;
 
94
}
 
95
QStringList ShellCommand::expand(const QStringList & items)
 
96
{
 
97
    QStringList result;
 
98
 
 
99
    foreach( QString item , items )
 
100
    result << expand(item);
 
101
 
 
102
    return result;
 
103
}
 
104
QString ShellCommand::expand(const QString & text)
 
105
{
 
106
    QString result = text;
 
107
    expandEnv(result);
 
108
    return result;
 
109
}
 
110
 
 
111
/*
 
112
 * expandEnv
 
113
 *
 
114
 * Expand environment variables in text. Escaped '$' characters are ignored.
 
115
 * Return true if any variables were expanded
 
116
 */
 
117
static bool expandEnv( QString & text )
 
118
{
 
119
    // Find all environment variables beginning with '$'
 
120
    //
 
121
    int pos = 0;
 
122
 
 
123
    bool expanded = false;
 
124
 
 
125
    while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
 
126
 
 
127
        // Skip escaped '$'
 
128
        //
 
129
        if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
 
130
            pos++;
 
131
        }
 
132
        // Variable found => expand
 
133
        //
 
134
        else {
 
135
            // Find the end of the variable = next '/' or ' '
 
136
            //
 
137
            int pos2 = text.indexOf( QLatin1Char(' '), pos+1 );
 
138
            int pos_tmp = text.indexOf( QLatin1Char('/'), pos+1 );
 
139
 
 
140
            if ( pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2) ) {
 
141
                pos2 = pos_tmp;
 
142
            }
 
143
 
 
144
            if ( pos2 == -1 ) {
 
145
                pos2 = text.length();
 
146
            }
 
147
 
 
148
            // Replace if the variable is terminated by '/' or ' '
 
149
            // and defined
 
150
            //
 
151
            if ( pos2 >= 0 ) {
 
152
                int len = pos2 - pos;
 
153
                QString key = text.mid( pos+1, len-1);
 
154
                QString value =
 
155
                    QString::fromLocal8Bit( ::getenv(key.toLocal8Bit()) );
 
156
 
 
157
                if ( !value.isEmpty() ) {
 
158
                    expanded = true;
 
159
                    text.replace( pos, len, value );
 
160
                    pos = pos + value.length();
 
161
                } else {
 
162
                    pos = pos2;
 
163
                }
 
164
            }
 
165
        }
 
166
    }
 
167
 
 
168
    return expanded;
 
169
}