| 1 | /* |
| 2 | Copyright (C) 2007 by Robert Knight <[email protected]> |
| 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"cxx . . $ `<file>/lib/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 expandEnvcxx . . $ expandEnv(60608c8c3b163578).(QStringcxx . . $ QString# & text); |
| 34 | |
| 35 | ShellCommandcxx . . $ Konsole/ShellCommand#::ShellCommandcxx . . $ Konsole/ShellCommand#ShellCommand(3520e46b33348bf6).(const QStringcxx . . $ QString# & fullCommand) |
| 36 | { |
| 37 | bool inQuotes = false; |
| 38 | |
| 39 | QStringcxx . . $ QString# buildercxx . . $ QString#QString(ced63f7c635d850d).; |
| 40 | |
| 41 | for ( int i = 0 ; i < fullCommand.countcxx . . $ QString#count(455f465bc33b4cdf).() ; i++ ) { |
| 42 | QCharcxx . . $ QChar# ch = fullCommand[cxx . . $ QString#`operator[]`(13af7ae5a875439e).i]; |
| 43 | |
| 44 | const bool isLastChar = ( i == fullCommand.countcxx . . $ QString#count(455f465bc33b4cdf).() - 1 ); |
| 45 | const bool isQuote = ( ch ==cxx . . $ `operator==`(a468b2f18a3afe8). QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).('\'') || ch ==cxx . . $ `operator==`(a468b2f18a3afe8). QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).('\"') ); |
| 46 | |
| 47 | if ( !isLastChar && isQuote ) { |
| 48 | inQuotes = !inQuotes; |
| 49 | } else { |
| 50 | if ( (!ch.isSpacecxx . . $ QChar#isSpace(3482b152b9333168).() || inQuotes) && !isQuote ) { |
| 51 | builder.appendcxx . . $ QString#append(3a2ffbdf8ecc25e2).(ch); |
| 52 | } |
| 53 | |
| 54 | if ( (ch.isSpacecxx . . $ QChar#isSpace(3482b152b9333168).() && !inQuotes) || ( i == fullCommand.countcxx . . $ QString#count(455f465bc33b4cdf).()-1 ) ) { |
| 55 | _argumentscxx . . $ Konsole/ShellCommand#_arguments. <<cxx . . $ QStringList#`operator<<`(85116071a693d053). builder; |
| 56 | builder.clearcxx . . $ QString#clear(49f6e7a06ebc5aa8).(); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | ShellCommandcxx . . $ Konsole/ShellCommand#::ShellCommandcxx . . $ Konsole/ShellCommand#ShellCommand(4a8467afe4f213e4).(const QStringcxx . . $ QString# & command , const QStringListcxx . . $ QStringList# & arguments) |
| 62 | : _argumentscxx . . $ Konsole/ShellCommand#_arguments.(arguments) |
| 63 | { |
| 64 | if ( !_argumentscxx . . $ Konsole/ShellCommand#_arguments..isEmptycxx . . $ QList#isEmpty(3482b152b9333168).() ) { |
| 65 | _argumentscxx . . $ Konsole/ShellCommand#_arguments.[cxx . . $ QList#`operator[]`(7a0ef1c7079e8533).0] =cxx . . $ QString#`operator=`(b79fde0b0ce9b948). command; |
| 66 | } |
| 67 | } |
| 68 | QStringcxx . . $ QString# ShellCommandcxx . . $ Konsole/ShellCommand#::fullCommandcxx . . $ Konsole/ShellCommand#fullCommand(6ef7b6c27b00b43d).() const |
| 69 | { |
| 70 | return _argumentscxx . . $ Konsole/ShellCommand#_arguments..joincxx . . $ QListSpecialMethods#join(227e33e1a3bf1f91).(QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).(' ')); |
| 71 | } |
| 72 | QStringcxx . . $ QString# ShellCommandcxx . . $ Konsole/ShellCommand#::commandcxx . . $ Konsole/ShellCommand#command(6ef7b6c27b00b43d).() const |
| 73 | { |
| 74 | if ( !_argumentscxx . . $ Konsole/ShellCommand#_arguments..isEmptycxx . . $ QList#isEmpty(3482b152b9333168).() ) { |
| 75 | return _argumentscxx . . $ QString#QString(132dd6869045752b).[cxx . . $ QList#`operator[]`(e2681f9bb6b2f357).0]; |
| 76 | } else { |
| 77 | return QStringcxx . . $ QString#QString(ced63f7c635d850d).(); |
| 78 | } |
| 79 | } |
| 80 | QStringListcxx . . $ QStringList# ShellCommandcxx . . $ Konsole/ShellCommand#::argumentscxx . . $ Konsole/ShellCommand#arguments(e1dbd5c4feadc8f5).() const |
| 81 | { |
| 82 | return _argumentscxx . . $ Konsole/ShellCommand#_arguments.; |
| 83 | } |
| 84 | bool ShellCommandcxx . . $ Konsole/ShellCommand#::isRootCommandcxx . . $ Konsole/ShellCommand#isRootCommand(50ce9a9e25b4a850).() const |
| 85 | { |
| 86 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(0); // not implemented yet |
| 87 | return false; |
| 88 | } |
| 89 | bool ShellCommandcxx . . $ Konsole/ShellCommand#::isAvailablecxx . . $ Konsole/ShellCommand#isAvailable(50ce9a9e25b4a850).() const |
| 90 | { |
| 91 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(0); // not implemented yet |
| 92 | return false; |
| 93 | } |
| 94 | QStringListcxx . . $ QStringList# ShellCommandcxx . . $ Konsole/ShellCommand#::expandcxx . . $ Konsole/ShellCommand#expand(5a14ab7320a56439).(const QStringListcxx . . $ QStringList# & items) |
| 95 | { |
| 96 | QStringListcxx . . $ QStringList# resultcxx . . $ QStringList#QStringList(ced63f7c635d850d).; |
| 97 | |
| 98 | for(const QStringcxx . . $ QString# &item : items) { |
| 99 | result <<cxx . . $ QStringList#`operator<<`(85116071a693d053). expandcxx . . $ Konsole/ShellCommand#expand(fd988a2ab94d906b).(item); |
| 100 | } |
| 101 | |
| 102 | return result; |
| 103 | } |
| 104 | QStringcxx . . $ QString# ShellCommandcxx . . $ Konsole/ShellCommand#::expandcxx . . $ Konsole/ShellCommand#expand(fd988a2ab94d906b).(const QStringcxx . . $ QString# & text) |
| 105 | { |
| 106 | QStringcxx . . $ QString# result = textcxx . . $ QString#QString(132dd6869045752b).; |
| 107 | expandEnvcxx . . $ expandEnv(60608c8c3b163578).(result); |
| 108 | return resultcxx . . $ QString#QString(28715ad661ad3cec).; |
| 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 expandEnvcxx . . $ expandEnv(60608c8c3b163578).( QStringcxx . . $ 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.indexOfcxx . . $ QString#indexOf(75b556ba78e4903c).(QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).('$'), pos)) != -1 ) { |
| 126 | |
| 127 | // Skip escaped '$' |
| 128 | // |
| 129 | if ( pos > 0 && text.atcxx . . $ QString#at(13af7ae5a875439e).(pos-1) ==cxx . . $ `operator==`(a468b2f18a3afe8). QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).('\\') ) { |
| 130 | pos++; |
| 131 | } |
| 132 | // Variable found => expand |
| 133 | // |
| 134 | else { |
| 135 | // Find the end of the variable = next '/' or ' ' |
| 136 | // |
| 137 | int pos2 = text.indexOfcxx . . $ QString#indexOf(75b556ba78e4903c).( QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).(' '), pos+1 ); |
| 138 | int pos_tmp = text.indexOfcxx . . $ QString#indexOf(75b556ba78e4903c).( QLatin1Charcxx . . $ QChar#QChar(a50889c09eec06d9).('/'), 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.lengthcxx . . $ QString#length(455f465bc33b4cdf).(); |
| 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 | QStringcxx . . $ QString# key = text.midcxx . . $ QString#mid(44b7563da18aea72).( pos+1, len-1); |
| 154 | QStringcxx . . $ QString# value = |
| 155 | QStringcxx . . $ QString#::fromLocal8Bitcxx . . $ QString#fromLocal8Bit(4e15edac36d215be).( qgetenvcxx . . $ qgetenv(531409eeffcde449).(key.toLocal8Bitcxx . . $ QString#toLocal8Bit(ef52b713090bc85a).().constDatacxx . . $ QByteArray#constData(a9f41ea0e82d88cf).()) ); |
| 156 | |
| 157 | if ( !value.isEmptycxx . . $ QString#isEmpty(50ce9a9e25b4a850).() ) { |
| 158 | expanded = true; |
| 159 | text.replacecxx . . $ QString#replace(2ea42a074cd0ed6b).( pos, len, value ); |
| 160 | pos = pos + value.lengthcxx . . $ QString#length(455f465bc33b4cdf).(); |
| 161 | } else { |
| 162 | pos = pos2; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return expanded; |
| 169 | } |