Mercurial > hg > octave-lyh
annotate gui/src/terminal/ShellCommand.cpp @ 13622:e744593197ef
added scrollToBottomRequest signal for terminal and flipped around progress bar an dstatus bar in browser widget, so the handle is on the right corner.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Thu, 18 Aug 2011 13:36:20 +0200 |
parents | c70511cf64ee |
children |
rev | line source |
---|---|
13501 | 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 // expands environment variables in 'text' | |
29 // function copied from kdelibs/kio/kio/kurlcompletion.cpp | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
30 static bool expandEnv (QString & text); |
13501 | 31 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
32 ShellCommand::ShellCommand (const QString & fullCommand) |
13501 | 33 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
34 bool inQuotes = false; |
13501 | 35 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
36 QString builder; |
13501 | 37 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
38 for (int i = 0; i < fullCommand.count (); i++) |
13501 | 39 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
40 QChar ch = fullCommand[i]; |
13501 | 41 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
42 const bool isLastChar = (i == fullCommand.count () - 1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
43 const bool isQuote = (ch == '\'' || ch == '\"'); |
13501 | 44 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
45 if (!isLastChar && isQuote) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
46 inQuotes = !inQuotes; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
47 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
48 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
49 if ((!ch.isSpace () || inQuotes) && !isQuote) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
50 builder.append (ch); |
13501 | 51 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
52 if ((ch.isSpace () && !inQuotes) || (i == fullCommand.count () - 1)) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
53 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
54 _arguments << builder; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
55 builder.clear (); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
56 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
57 } |
13501 | 58 } |
59 } | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
60 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
61 ShellCommand::ShellCommand (const QString & command, |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
62 const QStringList & arguments) |
13501 | 63 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
64 _arguments = arguments; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
65 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
66 if (!_arguments.isEmpty ()) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
67 _arguments[0] == command; |
13501 | 68 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
69 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
70 QString |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
71 ShellCommand::fullCommand () const |
13501 | 72 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
73 return _arguments.join (QChar (' ')); |
13501 | 74 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
75 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
76 QString |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
77 ShellCommand::command () const |
13501 | 78 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
79 if (!_arguments.isEmpty ()) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
80 return _arguments[0]; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
81 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
82 return QString (); |
13501 | 83 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
84 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
85 QStringList |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
86 ShellCommand::arguments () const |
13501 | 87 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
88 return _arguments; |
13501 | 89 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
90 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
91 bool |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
92 ShellCommand::isRootCommand () const |
13501 | 93 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
94 Q_ASSERT (0); // not implemented yet |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
95 return false; |
13501 | 96 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
97 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
98 bool |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
99 ShellCommand::isAvailable () const |
13501 | 100 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
101 Q_ASSERT (0); // not implemented yet |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
102 return false; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
103 } |
13501 | 104 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
105 QStringList |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
106 ShellCommand::expand (const QStringList & items) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
107 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
108 QStringList result; |
13501 | 109 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
110 foreach (QString item, items) result << expand (item); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
111 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
112 return result; |
13501 | 113 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
114 |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
115 QString |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
116 ShellCommand::expand (const QString & text) |
13501 | 117 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
118 QString result = text; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
119 expandEnv (result); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
120 return result; |
13501 | 121 } |
122 | |
123 /* | |
124 * expandEnv | |
125 * | |
126 * Expand environment variables in text. Escaped '$' characters are ignored. | |
127 * Return true if any variables were expanded | |
128 */ | |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
129 static bool |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
130 expandEnv (QString & text) |
13501 | 131 { |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
132 // Find all environment variables beginning with '$' |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
133 // |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
134 int pos = 0; |
13501 | 135 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
136 bool expanded = false; |
13501 | 137 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
138 while ((pos = text.indexOf (QLatin1Char ('$'), pos)) != -1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
139 { |
13501 | 140 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
141 // Skip escaped '$' |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
142 // |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
143 if (pos > 0 && text.at (pos - 1) == QLatin1Char ('\\')) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
144 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
145 pos++; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
146 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
147 // Variable found => expand |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
148 // |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
149 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
150 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
151 // Find the end of the variable = next '/' or ' ' |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
152 // |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
153 int pos2 = text.indexOf (QLatin1Char (' '), pos + 1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
154 int pos_tmp = text.indexOf (QLatin1Char ('/'), pos + 1); |
13501 | 155 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
156 if (pos2 == -1 || (pos_tmp != -1 && pos_tmp < pos2)) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
157 pos2 = pos_tmp; |
13501 | 158 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
159 if (pos2 == -1) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
160 pos2 = text.length (); |
13501 | 161 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
162 // Replace if the variable is terminated by '/' or ' ' |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
163 // and defined |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
164 // |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
165 if (pos2 >= 0) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
166 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
167 int len = pos2 - pos; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
168 QString key = text.mid (pos + 1, len - 1); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
169 QString value = |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
170 QString::fromLocal8Bit (::getenv (key.toLocal8Bit ())); |
13501 | 171 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
172 if (!value.isEmpty ()) |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
173 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
174 expanded = true; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
175 text.replace (pos, len, value); |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
176 pos = pos + value.length (); |
13501 | 177 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
178 else |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
179 { |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
180 pos = pos2; |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
181 } |
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
182 } |
13501 | 183 } |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
184 } |
13501 | 185 |
13506
c70511cf64ee
Reformatted to GNU Style.
Jacob Dawid <jacob.dawid@googlemail.com>
parents:
13501
diff
changeset
|
186 return expanded; |
13501 | 187 } |