~larryprice/acolyterm/release-0.1

« back to all changes in this revision

Viewing changes to src/plugin/konsole/ShellCommand.cpp

  • Committer: Larry Price
  • Date: 2016-06-15 14:47:59 UTC
  • Revision ID: larry.price@canonical.com-20160615144759-6wopn0gxwgta3x1n
Updating QMLTermWidget and removing unnecessary konsole codebase

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
 
// expands environment variables in 'text'
30
 
// function copied from kdelibs/kio/kio/kurlcompletion.cpp
31
 
static bool expandEnv(QString & text);
32
 
 
33
 
ShellCommand::ShellCommand(const QString & fullCommand)
34
 
{
35
 
    bool inQuotes = false;
36
 
 
37
 
    QString builder;
38
 
 
39
 
    for ( int i = 0 ; i < fullCommand.count() ; i++ ) {
40
 
        QChar ch = fullCommand[i];
41
 
 
42
 
        const bool isLastChar = ( i == fullCommand.count() - 1 );
43
 
        const bool isQuote = ( ch == '\'' || ch == '\"' );
44
 
 
45
 
        if ( !isLastChar && isQuote ) {
46
 
            inQuotes = !inQuotes;
47
 
        } else {
48
 
            if ( (!ch.isSpace() || inQuotes) && !isQuote ) {
49
 
                builder.append(ch);
50
 
            }
51
 
 
52
 
            if ( (ch.isSpace() && !inQuotes) || ( i == fullCommand.count()-1 ) ) {
53
 
                _arguments << builder;
54
 
                builder.clear();
55
 
            }
56
 
        }
57
 
    }
58
 
}
59
 
ShellCommand::ShellCommand(const QString & command , const QStringList & arguments)
60
 
{
61
 
    _arguments = arguments;
62
 
 
63
 
    if ( !_arguments.isEmpty() ) {
64
 
        _arguments[0] == command;
65
 
    }
66
 
}
67
 
QString ShellCommand::fullCommand() const
68
 
{
69
 
    return _arguments.join(QChar(' '));
70
 
}
71
 
QString ShellCommand::command() const
72
 
{
73
 
    if ( !_arguments.isEmpty() ) {
74
 
        return _arguments[0];
75
 
    } else {
76
 
        return QString();
77
 
    }
78
 
}
79
 
QStringList ShellCommand::arguments() const
80
 
{
81
 
    return _arguments;
82
 
}
83
 
bool ShellCommand::isRootCommand() const
84
 
{
85
 
    Q_ASSERT(0); // not implemented yet
86
 
    return false;
87
 
}
88
 
bool ShellCommand::isAvailable() const
89
 
{
90
 
    Q_ASSERT(0); // not implemented yet
91
 
    return false;
92
 
}
93
 
QStringList ShellCommand::expand(const QStringList & items)
94
 
{
95
 
    QStringList result;
96
 
 
97
 
    foreach( QString item , items )
98
 
    result << expand(item);
99
 
 
100
 
    return result;
101
 
}
102
 
QString ShellCommand::expand(const QString & text)
103
 
{
104
 
    QString result = text;
105
 
    expandEnv(result);
106
 
    return result;
107
 
}
108
 
 
109
 
/*
110
 
 * expandEnv
111
 
 *
112
 
 * Expand environment variables in text. Escaped '$' characters are ignored.
113
 
 * Return true if any variables were expanded
114
 
 */
115
 
static bool expandEnv( QString & text )
116
 
{
117
 
    // Find all environment variables beginning with '$'
118
 
    //
119
 
    int pos = 0;
120
 
 
121
 
    bool expanded = false;
122
 
 
123
 
    while ( (pos = text.indexOf(QLatin1Char('$'), pos)) != -1 ) {
124
 
 
125
 
        // Skip escaped '$'
126
 
        //
127
 
        if ( pos > 0 && text.at(pos-1) == QLatin1Char('\\') ) {
128
 
            pos++;
129
 
        }
130
 
        // Variable found => expand
131
 
        //
132
 
        else {
133
 
            // Find the end of the variable = next '/' or ' '
134
 
            //
135
 
            int pos2 = text.indexOf( QLatin1Char(' '), pos+1 );
136
 
            int pos_tmp = text.indexOf( QLatin1Char('/'), pos+1 );
137
 
 
138
 
            if ( pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2) ) {
139
 
                pos2 = pos_tmp;
140
 
            }
141
 
 
142
 
            if ( pos2 == -1 ) {
143
 
                pos2 = text.length();
144
 
            }
145
 
 
146
 
            // Replace if the variable is terminated by '/' or ' '
147
 
            // and defined
148
 
            //
149
 
            if ( pos2 >= 0 ) {
150
 
                int len = pos2 - pos;
151
 
                QString key = text.mid( pos+1, len-1);
152
 
                QString value =
153
 
                    QString::fromLocal8Bit( ::getenv(key.toLocal8Bit()) );
154
 
 
155
 
                if ( !value.isEmpty() ) {
156
 
                    expanded = true;
157
 
                    text.replace( pos, len, value );
158
 
                    pos = pos + value.length();
159
 
                } else {
160
 
                    pos = pos2;
161
 
                }
162
 
            }
163
 
        }
164
 
    }
165
 
 
166
 
    return expanded;
167
 
}