Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/io/qprocess.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Copyright (C) 2015 Intel Corporation | - | ||||||||||||||||||
5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
8 | ** | - | ||||||||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
15 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
16 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
17 | ** | - | ||||||||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
20 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
26 | ** | - | ||||||||||||||||||
27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
30 | ** | - | ||||||||||||||||||
31 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
32 | ** | - | ||||||||||||||||||
33 | ****************************************************************************/ | - | ||||||||||||||||||
34 | - | |||||||||||||||||||
35 | //#define QPROCESS_DEBUG | - | ||||||||||||||||||
36 | - | |||||||||||||||||||
37 | #include <qdebug.h> | - | ||||||||||||||||||
38 | #include <qdir.h> | - | ||||||||||||||||||
39 | #if defined(Q_OS_WIN) | - | ||||||||||||||||||
40 | #include <qtimer.h> | - | ||||||||||||||||||
41 | #endif | - | ||||||||||||||||||
42 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
43 | #include <qstring.h> | - | ||||||||||||||||||
44 | #include <ctype.h> | - | ||||||||||||||||||
45 | #if !defined(Q_OS_WINCE) | - | ||||||||||||||||||
46 | #include <errno.h> | - | ||||||||||||||||||
47 | #endif | - | ||||||||||||||||||
48 | - | |||||||||||||||||||
49 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
50 | /* | - | ||||||||||||||||||
51 | Returns a human readable representation of the first \a len | - | ||||||||||||||||||
52 | characters in \a data. | - | ||||||||||||||||||
53 | */ | - | ||||||||||||||||||
54 | static QByteArray qt_prettyDebug(const char *data, int len, int maxSize) | - | ||||||||||||||||||
55 | { | - | ||||||||||||||||||
56 | if (!data) return "(null)"; | - | ||||||||||||||||||
57 | QByteArray out; | - | ||||||||||||||||||
58 | for (int i = 0; i < len && i < maxSize; ++i) { | - | ||||||||||||||||||
59 | char c = data[i]; | - | ||||||||||||||||||
60 | if (isprint(c)) { | - | ||||||||||||||||||
61 | out += c; | - | ||||||||||||||||||
62 | } else switch (c) { | - | ||||||||||||||||||
63 | case '\n': out += "\\n"; break; | - | ||||||||||||||||||
64 | case '\r': out += "\\r"; break; | - | ||||||||||||||||||
65 | case '\t': out += "\\t"; break; | - | ||||||||||||||||||
66 | default: | - | ||||||||||||||||||
67 | char buf[5]; | - | ||||||||||||||||||
68 | qsnprintf(buf, sizeof(buf), "\\%3o", c); | - | ||||||||||||||||||
69 | buf[4] = '\0'; | - | ||||||||||||||||||
70 | out += QByteArray(buf); | - | ||||||||||||||||||
71 | } | - | ||||||||||||||||||
72 | } | - | ||||||||||||||||||
73 | - | |||||||||||||||||||
74 | if (len < maxSize) | - | ||||||||||||||||||
75 | out += "..."; | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | return out; | - | ||||||||||||||||||
78 | } | - | ||||||||||||||||||
79 | - | |||||||||||||||||||
80 | QT_END_NAMESPACE | - | ||||||||||||||||||
81 | - | |||||||||||||||||||
82 | #endif | - | ||||||||||||||||||
83 | - | |||||||||||||||||||
84 | #include "qprocess.h" | - | ||||||||||||||||||
85 | #include "qprocess_p.h" | - | ||||||||||||||||||
86 | - | |||||||||||||||||||
87 | #include <qbytearray.h> | - | ||||||||||||||||||
88 | #include <qelapsedtimer.h> | - | ||||||||||||||||||
89 | #include <qcoreapplication.h> | - | ||||||||||||||||||
90 | #include <qsocketnotifier.h> | - | ||||||||||||||||||
91 | #include <qtimer.h> | - | ||||||||||||||||||
92 | - | |||||||||||||||||||
93 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
94 | #include <qwineventnotifier.h> | - | ||||||||||||||||||
95 | #else | - | ||||||||||||||||||
96 | #include <private/qcore_unix_p.h> | - | ||||||||||||||||||
97 | #endif | - | ||||||||||||||||||
98 | - | |||||||||||||||||||
99 | #ifndef QT_NO_PROCESS | - | ||||||||||||||||||
100 | - | |||||||||||||||||||
101 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
102 | - | |||||||||||||||||||
103 | /*! | - | ||||||||||||||||||
104 | \since 5.6 | - | ||||||||||||||||||
105 | - | |||||||||||||||||||
106 | \macro QT_NO_PROCESS_COMBINED_ARGUMENT_START | - | ||||||||||||||||||
107 | \relates QProcess | - | ||||||||||||||||||
108 | - | |||||||||||||||||||
109 | Disables the | - | ||||||||||||||||||
110 | \l {QProcess::start(const QString &, OpenMode)}{QProcess::start()} | - | ||||||||||||||||||
111 | overload taking a single string. | - | ||||||||||||||||||
112 | In most cases where it is used, the user intends for the first argument | - | ||||||||||||||||||
113 | to be treated atomically as per the other overload. | - | ||||||||||||||||||
114 | - | |||||||||||||||||||
115 | \sa QProcess::start(const QString &command, OpenMode mode) | - | ||||||||||||||||||
116 | */ | - | ||||||||||||||||||
117 | - | |||||||||||||||||||
118 | /*! | - | ||||||||||||||||||
119 | \class QProcessEnvironment | - | ||||||||||||||||||
120 | \inmodule QtCore | - | ||||||||||||||||||
121 | - | |||||||||||||||||||
122 | \brief The QProcessEnvironment class holds the environment variables that | - | ||||||||||||||||||
123 | can be passed to a program. | - | ||||||||||||||||||
124 | - | |||||||||||||||||||
125 | \ingroup io | - | ||||||||||||||||||
126 | \ingroup misc | - | ||||||||||||||||||
127 | \ingroup shared | - | ||||||||||||||||||
128 | \reentrant | - | ||||||||||||||||||
129 | \since 4.6 | - | ||||||||||||||||||
130 | - | |||||||||||||||||||
131 | A process's environment is composed of a set of key=value pairs known as | - | ||||||||||||||||||
132 | environment variables. The QProcessEnvironment class wraps that concept | - | ||||||||||||||||||
133 | and allows easy manipulation of those variables. It's meant to be used | - | ||||||||||||||||||
134 | along with QProcess, to set the environment for child processes. It | - | ||||||||||||||||||
135 | cannot be used to change the current process's environment. | - | ||||||||||||||||||
136 | - | |||||||||||||||||||
137 | The environment of the calling process can be obtained using | - | ||||||||||||||||||
138 | QProcessEnvironment::systemEnvironment(). | - | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | On Unix systems, the variable names are case-sensitive. Note that the | - | ||||||||||||||||||
141 | Unix environment allows both variable names and contents to contain arbitrary | - | ||||||||||||||||||
142 | binary data (except for the NUL character). QProcessEnvironment will preserve | - | ||||||||||||||||||
143 | such variables, but does not support manipulating variables whose names or | - | ||||||||||||||||||
144 | values cannot be encoded by the current locale settings (see | - | ||||||||||||||||||
145 | QTextCodec::codecForLocale). | - | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | On Windows, the variable names are case-insensitive, but case-preserving. | - | ||||||||||||||||||
148 | QProcessEnvironment behaves accordingly. | - | ||||||||||||||||||
149 | - | |||||||||||||||||||
150 | On Windows CE, the concept of environment does not exist. This class will | - | ||||||||||||||||||
151 | keep the values set for compatibility with other platforms, but the values | - | ||||||||||||||||||
152 | set will have no effect on the processes being created. | - | ||||||||||||||||||
153 | - | |||||||||||||||||||
154 | \sa QProcess, QProcess::systemEnvironment(), QProcess::setProcessEnvironment() | - | ||||||||||||||||||
155 | */ | - | ||||||||||||||||||
156 | - | |||||||||||||||||||
157 | QStringList QProcessEnvironmentPrivate::toList() const | - | ||||||||||||||||||
158 | { | - | ||||||||||||||||||
159 | QStringList result; | - | ||||||||||||||||||
160 | result.reserve(hash.size()); | - | ||||||||||||||||||
161 | for (Hash::const_iterator it = hash.cbegin(), end = hash.cend(); it != end; ++it)
| 1954-13934 | ||||||||||||||||||
162 | result << nameToString(it.key()) + QLatin1Char('=') + valueToString(it.value()); executed 13934 times by 2 tests: result << nameToString(it.key()) + QLatin1Char('=') + valueToString(it.value()); Executed by:
| 13934 | ||||||||||||||||||
163 | return result; executed 1954 times by 2 tests: return result; Executed by:
| 1954 | ||||||||||||||||||
164 | } | - | ||||||||||||||||||
165 | - | |||||||||||||||||||
166 | QProcessEnvironment QProcessEnvironmentPrivate::fromList(const QStringList &list) | - | ||||||||||||||||||
167 | { | - | ||||||||||||||||||
168 | QProcessEnvironment env; | - | ||||||||||||||||||
169 | QStringList::ConstIterator it = list.constBegin(), | - | ||||||||||||||||||
170 | end = list.constEnd(); | - | ||||||||||||||||||
171 | for ( ; it != end; ++it) {
| 137-4176 | ||||||||||||||||||
172 | int pos = it->indexOf(QLatin1Char('='), 1); | - | ||||||||||||||||||
173 | if (pos < 1)
| 0-4176 | ||||||||||||||||||
174 | continue; never executed: continue; | 0 | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | QString value = it->mid(pos + 1); | - | ||||||||||||||||||
177 | QString name = *it; | - | ||||||||||||||||||
178 | name.truncate(pos); | - | ||||||||||||||||||
179 | env.insert(name, value); | - | ||||||||||||||||||
180 | } executed 4176 times by 4 tests: end of block Executed by:
| 4176 | ||||||||||||||||||
181 | return env; executed 137 times by 4 tests: return env; Executed by:
| 137 | ||||||||||||||||||
182 | } | - | ||||||||||||||||||
183 | - | |||||||||||||||||||
184 | QStringList QProcessEnvironmentPrivate::keys() const | - | ||||||||||||||||||
185 | { | - | ||||||||||||||||||
186 | QStringList result; | - | ||||||||||||||||||
187 | result.reserve(hash.size()); | - | ||||||||||||||||||
188 | Hash::ConstIterator it = hash.constBegin(), | - | ||||||||||||||||||
189 | end = hash.constEnd(); | - | ||||||||||||||||||
190 | for ( ; it != end; ++it)
| 5-41 | ||||||||||||||||||
191 | result << nameToString(it.key()); executed 41 times by 2 tests: result << nameToString(it.key()); Executed by:
| 41 | ||||||||||||||||||
192 | return result; executed 5 times by 2 tests: return result; Executed by:
| 5 | ||||||||||||||||||
193 | } | - | ||||||||||||||||||
194 | - | |||||||||||||||||||
195 | void QProcessEnvironmentPrivate::insert(const QProcessEnvironmentPrivate &other) | - | ||||||||||||||||||
196 | { | - | ||||||||||||||||||
197 | Hash::ConstIterator it = other.hash.constBegin(), | - | ||||||||||||||||||
198 | end = other.hash.constEnd(); | - | ||||||||||||||||||
199 | for ( ; it != end; ++it)
| 2-6 | ||||||||||||||||||
200 | hash.insert(it.key(), it.value()); executed 6 times by 1 test: hash.insert(it.key(), it.value()); Executed by:
| 6 | ||||||||||||||||||
201 | - | |||||||||||||||||||
202 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
203 | QHash<QString, Key>::ConstIterator nit = other.nameMap.constBegin(), | - | ||||||||||||||||||
204 | nend = other.nameMap.constEnd(); | - | ||||||||||||||||||
205 | for ( ; nit != nend; ++nit)
| 2-6 | ||||||||||||||||||
206 | nameMap.insert(nit.key(), nit.value()); executed 6 times by 1 test: nameMap.insert(nit.key(), nit.value()); Executed by:
| 6 | ||||||||||||||||||
207 | #endif | - | ||||||||||||||||||
208 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
209 | - | |||||||||||||||||||
210 | /*! | - | ||||||||||||||||||
211 | Creates a new QProcessEnvironment object. This constructor creates an | - | ||||||||||||||||||
212 | empty environment. If set on a QProcess, this will cause the current | - | ||||||||||||||||||
213 | environment variables to be removed. | - | ||||||||||||||||||
214 | */ | - | ||||||||||||||||||
215 | QProcessEnvironment::QProcessEnvironment() | - | ||||||||||||||||||
216 | : d(0) | - | ||||||||||||||||||
217 | { | - | ||||||||||||||||||
218 | } executed 2237 times by 35 tests: end of block Executed by:
| 2237 | ||||||||||||||||||
219 | - | |||||||||||||||||||
220 | /*! | - | ||||||||||||||||||
221 | Frees the resources associated with this QProcessEnvironment object. | - | ||||||||||||||||||
222 | */ | - | ||||||||||||||||||
223 | QProcessEnvironment::~QProcessEnvironment() | - | ||||||||||||||||||
224 | { | - | ||||||||||||||||||
225 | } | - | ||||||||||||||||||
226 | - | |||||||||||||||||||
227 | /*! | - | ||||||||||||||||||
228 | Creates a QProcessEnvironment object that is a copy of \a other. | - | ||||||||||||||||||
229 | */ | - | ||||||||||||||||||
230 | QProcessEnvironment::QProcessEnvironment(const QProcessEnvironment &other) | - | ||||||||||||||||||
231 | : d(other.d) | - | ||||||||||||||||||
232 | { | - | ||||||||||||||||||
233 | } executed 673 times by 1 test: end of block Executed by:
| 673 | ||||||||||||||||||
234 | - | |||||||||||||||||||
235 | /*! | - | ||||||||||||||||||
236 | Copies the contents of the \a other QProcessEnvironment object into this | - | ||||||||||||||||||
237 | one. | - | ||||||||||||||||||
238 | */ | - | ||||||||||||||||||
239 | QProcessEnvironment &QProcessEnvironment::operator=(const QProcessEnvironment &other) | - | ||||||||||||||||||
240 | { | - | ||||||||||||||||||
241 | d = other.d; | - | ||||||||||||||||||
242 | return *this; executed 1165 times by 6 tests: return *this; Executed by:
| 1165 | ||||||||||||||||||
243 | } | - | ||||||||||||||||||
244 | - | |||||||||||||||||||
245 | /*! | - | ||||||||||||||||||
246 | \fn void QProcessEnvironment::swap(QProcessEnvironment &other) | - | ||||||||||||||||||
247 | \since 5.0 | - | ||||||||||||||||||
248 | - | |||||||||||||||||||
249 | Swaps this process environment instance with \a other. This | - | ||||||||||||||||||
250 | function is very fast and never fails. | - | ||||||||||||||||||
251 | */ | - | ||||||||||||||||||
252 | - | |||||||||||||||||||
253 | /*! | - | ||||||||||||||||||
254 | \fn bool QProcessEnvironment::operator !=(const QProcessEnvironment &other) const | - | ||||||||||||||||||
255 | - | |||||||||||||||||||
256 | Returns \c true if this and the \a other QProcessEnvironment objects are different. | - | ||||||||||||||||||
257 | - | |||||||||||||||||||
258 | \sa operator==() | - | ||||||||||||||||||
259 | */ | - | ||||||||||||||||||
260 | - | |||||||||||||||||||
261 | /*! | - | ||||||||||||||||||
262 | Returns \c true if this and the \a other QProcessEnvironment objects are equal. | - | ||||||||||||||||||
263 | - | |||||||||||||||||||
264 | Two QProcessEnvironment objects are considered equal if they have the same | - | ||||||||||||||||||
265 | set of key=value pairs. The comparison of keys is done case-sensitive on | - | ||||||||||||||||||
266 | platforms where the environment is case-sensitive. | - | ||||||||||||||||||
267 | - | |||||||||||||||||||
268 | \sa operator!=(), contains() | - | ||||||||||||||||||
269 | */ | - | ||||||||||||||||||
270 | bool QProcessEnvironment::operator==(const QProcessEnvironment &other) const | - | ||||||||||||||||||
271 | { | - | ||||||||||||||||||
272 | if (d == other.d)
| 3-6 | ||||||||||||||||||
273 | return true; executed 3 times by 1 test: return true; Executed by:
| 3 | ||||||||||||||||||
274 | if (d) {
| 0-6 | ||||||||||||||||||
275 | if (other.d) {
| 1-5 | ||||||||||||||||||
276 | QProcessEnvironmentPrivate::OrderedMutexLocker locker(d, other.d); | - | ||||||||||||||||||
277 | return d->hash == other.d->hash; executed 5 times by 1 test: return d->hash == other.d->hash; Executed by:
| 5 | ||||||||||||||||||
278 | } else { | - | ||||||||||||||||||
279 | return isEmpty(); executed 1 time by 1 test: return isEmpty(); Executed by:
| 1 | ||||||||||||||||||
280 | } | - | ||||||||||||||||||
281 | } else { | - | ||||||||||||||||||
282 | return other.isEmpty(); never executed: return other.isEmpty(); | 0 | ||||||||||||||||||
283 | } | - | ||||||||||||||||||
284 | } | - | ||||||||||||||||||
285 | - | |||||||||||||||||||
286 | /*! | - | ||||||||||||||||||
287 | Returns \c true if this QProcessEnvironment object is empty: that is | - | ||||||||||||||||||
288 | there are no key=value pairs set. | - | ||||||||||||||||||
289 | - | |||||||||||||||||||
290 | \sa clear(), systemEnvironment(), insert() | - | ||||||||||||||||||
291 | */ | - | ||||||||||||||||||
292 | bool QProcessEnvironment::isEmpty() const | - | ||||||||||||||||||
293 | { | - | ||||||||||||||||||
294 | // Needs no locking, as no hash nodes are accessed | - | ||||||||||||||||||
295 | return d ? d->hash.isEmpty() : true; executed 684 times by 4 tests: return d ? d->hash.isEmpty() : true; Executed by:
| 3-684 | ||||||||||||||||||
296 | } | - | ||||||||||||||||||
297 | - | |||||||||||||||||||
298 | /*! | - | ||||||||||||||||||
299 | Removes all key=value pairs from this QProcessEnvironment object, making | - | ||||||||||||||||||
300 | it empty. | - | ||||||||||||||||||
301 | - | |||||||||||||||||||
302 | \sa isEmpty(), systemEnvironment() | - | ||||||||||||||||||
303 | */ | - | ||||||||||||||||||
304 | void QProcessEnvironment::clear() | - | ||||||||||||||||||
305 | { | - | ||||||||||||||||||
306 | if (d)
| 0-7 | ||||||||||||||||||
307 | d->hash.clear(); executed 7 times by 1 test: d->hash.clear(); Executed by:
| 7 | ||||||||||||||||||
308 | // Unix: Don't clear d->nameMap, as the environment is likely to be | - | ||||||||||||||||||
309 | // re-populated with the same keys again. | - | ||||||||||||||||||
310 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||||||||
311 | - | |||||||||||||||||||
312 | /*! | - | ||||||||||||||||||
313 | Returns \c true if the environment variable of name \a name is found in | - | ||||||||||||||||||
314 | this QProcessEnvironment object. | - | ||||||||||||||||||
315 | - | |||||||||||||||||||
316 | - | |||||||||||||||||||
317 | \sa insert(), value() | - | ||||||||||||||||||
318 | */ | - | ||||||||||||||||||
319 | bool QProcessEnvironment::contains(const QString &name) const | - | ||||||||||||||||||
320 | { | - | ||||||||||||||||||
321 | if (!d)
| 0-14 | ||||||||||||||||||
322 | return false; never executed: return false; | 0 | ||||||||||||||||||
323 | QProcessEnvironmentPrivate::MutexLocker locker(d); | - | ||||||||||||||||||
324 | return d->hash.contains(d->prepareName(name)); executed 14 times by 2 tests: return d->hash.contains(d->prepareName(name)); Executed by:
| 14 | ||||||||||||||||||
325 | } | - | ||||||||||||||||||
326 | - | |||||||||||||||||||
327 | /*! | - | ||||||||||||||||||
328 | Inserts the environment variable of name \a name and contents \a value | - | ||||||||||||||||||
329 | into this QProcessEnvironment object. If that variable already existed, | - | ||||||||||||||||||
330 | it is replaced by the new value. | - | ||||||||||||||||||
331 | - | |||||||||||||||||||
332 | On most systems, inserting a variable with no contents will have the | - | ||||||||||||||||||
333 | same effect for applications as if the variable had not been set at all. | - | ||||||||||||||||||
334 | However, to guarantee that there are no incompatibilities, to remove a | - | ||||||||||||||||||
335 | variable, please use the remove() function. | - | ||||||||||||||||||
336 | - | |||||||||||||||||||
337 | \sa contains(), remove(), value() | - | ||||||||||||||||||
338 | */ | - | ||||||||||||||||||
339 | void QProcessEnvironment::insert(const QString &name, const QString &value) | - | ||||||||||||||||||
340 | { | - | ||||||||||||||||||
341 | // our re-impl of detach() detaches from null | - | ||||||||||||||||||
342 | d.detach(); // detach before prepareName() | - | ||||||||||||||||||
343 | d->hash.insert(d->prepareName(name), d->prepareValue(value)); | - | ||||||||||||||||||
344 | } executed 4353 times by 7 tests: end of block Executed by:
| 4353 | ||||||||||||||||||
345 | - | |||||||||||||||||||
346 | /*! | - | ||||||||||||||||||
347 | Removes the environment variable identified by \a name from this | - | ||||||||||||||||||
348 | QProcessEnvironment object. If that variable did not exist before, | - | ||||||||||||||||||
349 | nothing happens. | - | ||||||||||||||||||
350 | - | |||||||||||||||||||
351 | - | |||||||||||||||||||
352 | \sa contains(), insert(), value() | - | ||||||||||||||||||
353 | */ | - | ||||||||||||||||||
354 | void QProcessEnvironment::remove(const QString &name) | - | ||||||||||||||||||
355 | { | - | ||||||||||||||||||
356 | if (d) {
| 0-3 | ||||||||||||||||||
357 | d.detach(); // detach before prepareName() | - | ||||||||||||||||||
358 | d->hash.remove(d->prepareName(name)); | - | ||||||||||||||||||
359 | } executed 3 times by 2 tests: end of block Executed by:
| 3 | ||||||||||||||||||
360 | } executed 3 times by 2 tests: end of block Executed by:
| 3 | ||||||||||||||||||
361 | - | |||||||||||||||||||
362 | /*! | - | ||||||||||||||||||
363 | Searches this QProcessEnvironment object for a variable identified by | - | ||||||||||||||||||
364 | \a name and returns its value. If the variable is not found in this object, | - | ||||||||||||||||||
365 | then \a defaultValue is returned instead. | - | ||||||||||||||||||
366 | - | |||||||||||||||||||
367 | \sa contains(), insert(), remove() | - | ||||||||||||||||||
368 | */ | - | ||||||||||||||||||
369 | QString QProcessEnvironment::value(const QString &name, const QString &defaultValue) const | - | ||||||||||||||||||
370 | { | - | ||||||||||||||||||
371 | if (!d)
| 0-38 | ||||||||||||||||||
372 | return defaultValue; never executed: return defaultValue; | 0 | ||||||||||||||||||
373 | - | |||||||||||||||||||
374 | QProcessEnvironmentPrivate::MutexLocker locker(d); | - | ||||||||||||||||||
375 | QProcessEnvironmentPrivate::Hash::ConstIterator it = d->hash.constFind(d->prepareName(name)); | - | ||||||||||||||||||
376 | if (it == d->hash.constEnd())
| 10-28 | ||||||||||||||||||
377 | return defaultValue; executed 10 times by 2 tests: return defaultValue; Executed by:
| 10 | ||||||||||||||||||
378 | - | |||||||||||||||||||
379 | return d->valueToString(it.value()); executed 28 times by 3 tests: return d->valueToString(it.value()); Executed by:
| 28 | ||||||||||||||||||
380 | } | - | ||||||||||||||||||
381 | - | |||||||||||||||||||
382 | /*! | - | ||||||||||||||||||
383 | Converts this QProcessEnvironment object into a list of strings, one for | - | ||||||||||||||||||
384 | each environment variable that is set. The environment variable's name | - | ||||||||||||||||||
385 | and its value are separated by an equal character ('='). | - | ||||||||||||||||||
386 | - | |||||||||||||||||||
387 | The QStringList contents returned by this function are suitable for | - | ||||||||||||||||||
388 | presentation. | - | ||||||||||||||||||
389 | Use with the QProcess::setEnvironment function is not recommended due to | - | ||||||||||||||||||
390 | potential encoding problems under Unix, and worse performance. | - | ||||||||||||||||||
391 | - | |||||||||||||||||||
392 | \sa systemEnvironment(), QProcess::systemEnvironment(), | - | ||||||||||||||||||
393 | QProcess::setProcessEnvironment() | - | ||||||||||||||||||
394 | */ | - | ||||||||||||||||||
395 | QStringList QProcessEnvironment::toStringList() const | - | ||||||||||||||||||
396 | { | - | ||||||||||||||||||
397 | if (!d)
| 2-1954 | ||||||||||||||||||
398 | return QStringList(); executed 2 times by 2 tests: return QStringList(); Executed by:
| 2 | ||||||||||||||||||
399 | QProcessEnvironmentPrivate::MutexLocker locker(d); | - | ||||||||||||||||||
400 | return d->toList(); executed 1954 times by 2 tests: return d->toList(); Executed by:
| 1954 | ||||||||||||||||||
401 | } | - | ||||||||||||||||||
402 | - | |||||||||||||||||||
403 | /*! | - | ||||||||||||||||||
404 | \since 4.8 | - | ||||||||||||||||||
405 | - | |||||||||||||||||||
406 | Returns a list containing all the variable names in this QProcessEnvironment | - | ||||||||||||||||||
407 | object. | - | ||||||||||||||||||
408 | */ | - | ||||||||||||||||||
409 | QStringList QProcessEnvironment::keys() const | - | ||||||||||||||||||
410 | { | - | ||||||||||||||||||
411 | if (!d)
| 1-5 | ||||||||||||||||||
412 | return QStringList(); executed 1 time by 1 test: return QStringList(); Executed by:
| 1 | ||||||||||||||||||
413 | QProcessEnvironmentPrivate::MutexLocker locker(d); | - | ||||||||||||||||||
414 | return d->keys(); executed 5 times by 2 tests: return d->keys(); Executed by:
| 5 | ||||||||||||||||||
415 | } | - | ||||||||||||||||||
416 | - | |||||||||||||||||||
417 | /*! | - | ||||||||||||||||||
418 | \overload | - | ||||||||||||||||||
419 | \since 4.8 | - | ||||||||||||||||||
420 | - | |||||||||||||||||||
421 | Inserts the contents of \a e in this QProcessEnvironment object. Variables in | - | ||||||||||||||||||
422 | this object that also exist in \a e will be overwritten. | - | ||||||||||||||||||
423 | */ | - | ||||||||||||||||||
424 | void QProcessEnvironment::insert(const QProcessEnvironment &e) | - | ||||||||||||||||||
425 | { | - | ||||||||||||||||||
426 | if (!e.d)
| 0-2 | ||||||||||||||||||
427 | return; never executed: return; | 0 | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | // our re-impl of detach() detaches from null | - | ||||||||||||||||||
430 | QProcessEnvironmentPrivate::MutexLocker locker(e.d); | - | ||||||||||||||||||
431 | d->insert(*e.d); | - | ||||||||||||||||||
432 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
433 | - | |||||||||||||||||||
434 | void QProcessPrivate::Channel::clear() | - | ||||||||||||||||||
435 | { | - | ||||||||||||||||||
436 | switch (type) { | - | ||||||||||||||||||
437 | case PipeSource: executed 3 times by 1 test: case PipeSource: Executed by:
| 3 | ||||||||||||||||||
438 | Q_ASSERT(process); | - | ||||||||||||||||||
439 | process->stdinChannel.type = Normal; | - | ||||||||||||||||||
440 | process->stdinChannel.process = 0; | - | ||||||||||||||||||
441 | break; executed 3 times by 1 test: break; Executed by:
| 3 | ||||||||||||||||||
442 | case PipeSink: never executed: case PipeSink: | 0 | ||||||||||||||||||
443 | Q_ASSERT(process); | - | ||||||||||||||||||
444 | process->stdoutChannel.type = Normal; | - | ||||||||||||||||||
445 | process->stdoutChannel.process = 0; | - | ||||||||||||||||||
446 | break; never executed: break; | 0 | ||||||||||||||||||
447 | } | - | ||||||||||||||||||
448 | - | |||||||||||||||||||
449 | type = Normal; | - | ||||||||||||||||||
450 | file.clear(); | - | ||||||||||||||||||
451 | process = 0; | - | ||||||||||||||||||
452 | } executed 21 times by 1 test: end of block Executed by:
| 21 | ||||||||||||||||||
453 | - | |||||||||||||||||||
454 | /*! \fn bool QProcessPrivate::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid) | - | ||||||||||||||||||
455 | - | |||||||||||||||||||
456 | \internal | - | ||||||||||||||||||
457 | */ | - | ||||||||||||||||||
458 | - | |||||||||||||||||||
459 | /*! | - | ||||||||||||||||||
460 | \class QProcess | - | ||||||||||||||||||
461 | \inmodule QtCore | - | ||||||||||||||||||
462 | - | |||||||||||||||||||
463 | \brief The QProcess class is used to start external programs and | - | ||||||||||||||||||
464 | to communicate with them. | - | ||||||||||||||||||
465 | - | |||||||||||||||||||
466 | \ingroup io | - | ||||||||||||||||||
467 | - | |||||||||||||||||||
468 | \reentrant | - | ||||||||||||||||||
469 | - | |||||||||||||||||||
470 | \section1 Running a Process | - | ||||||||||||||||||
471 | - | |||||||||||||||||||
472 | To start a process, pass the name and command line arguments of | - | ||||||||||||||||||
473 | the program you want to run as arguments to start(). Arguments | - | ||||||||||||||||||
474 | are supplied as individual strings in a QStringList. | - | ||||||||||||||||||
475 | - | |||||||||||||||||||
476 | Alternatively, you can set the program to run with setProgram() | - | ||||||||||||||||||
477 | and setArguments(), and then call start() or open(). | - | ||||||||||||||||||
478 | - | |||||||||||||||||||
479 | For example, the following code snippet runs the analog clock | - | ||||||||||||||||||
480 | example in the Fusion style on X11 platforms by passing strings | - | ||||||||||||||||||
481 | containing "-style" and "fusion" as two items in the list of | - | ||||||||||||||||||
482 | arguments: | - | ||||||||||||||||||
483 | - | |||||||||||||||||||
484 | \snippet qprocess/qprocess-simpleexecution.cpp 0 | - | ||||||||||||||||||
485 | \dots | - | ||||||||||||||||||
486 | \snippet qprocess/qprocess-simpleexecution.cpp 1 | - | ||||||||||||||||||
487 | \snippet qprocess/qprocess-simpleexecution.cpp 2 | - | ||||||||||||||||||
488 | - | |||||||||||||||||||
489 | QProcess then enters the \l Starting state, and when the program | - | ||||||||||||||||||
490 | has started, QProcess enters the \l Running state and emits | - | ||||||||||||||||||
491 | started(). | - | ||||||||||||||||||
492 | - | |||||||||||||||||||
493 | QProcess allows you to treat a process as a sequential I/O | - | ||||||||||||||||||
494 | device. You can write to and read from the process just as you | - | ||||||||||||||||||
495 | would access a network connection using QTcpSocket. You can then | - | ||||||||||||||||||
496 | write to the process's standard input by calling write(), and | - | ||||||||||||||||||
497 | read the standard output by calling read(), readLine(), and | - | ||||||||||||||||||
498 | getChar(). Because it inherits QIODevice, QProcess can also be | - | ||||||||||||||||||
499 | used as an input source for QXmlReader, or for generating data to | - | ||||||||||||||||||
500 | be uploaded using QNetworkAccessManager. | - | ||||||||||||||||||
501 | - | |||||||||||||||||||
502 | \note On Windows CE, reading and writing to a process | - | ||||||||||||||||||
503 | is not supported. | - | ||||||||||||||||||
504 | - | |||||||||||||||||||
505 | When the process exits, QProcess reenters the \l NotRunning state | - | ||||||||||||||||||
506 | (the initial state), and emits finished(). | - | ||||||||||||||||||
507 | - | |||||||||||||||||||
508 | The finished() signal provides the exit code and exit status of | - | ||||||||||||||||||
509 | the process as arguments, and you can also call exitCode() to | - | ||||||||||||||||||
510 | obtain the exit code of the last process that finished, and | - | ||||||||||||||||||
511 | exitStatus() to obtain its exit status. If an error occurs at | - | ||||||||||||||||||
512 | any point in time, QProcess will emit the errorOccurred() signal. | - | ||||||||||||||||||
513 | You can also call error() to find the type of error that occurred | - | ||||||||||||||||||
514 | last, and state() to find the current process state. | - | ||||||||||||||||||
515 | - | |||||||||||||||||||
516 | \section1 Communicating via Channels | - | ||||||||||||||||||
517 | - | |||||||||||||||||||
518 | Processes have two predefined output channels: The standard | - | ||||||||||||||||||
519 | output channel (\c stdout) supplies regular console output, and | - | ||||||||||||||||||
520 | the standard error channel (\c stderr) usually supplies the | - | ||||||||||||||||||
521 | errors that are printed by the process. These channels represent | - | ||||||||||||||||||
522 | two separate streams of data. You can toggle between them by | - | ||||||||||||||||||
523 | calling setReadChannel(). QProcess emits readyRead() when data is | - | ||||||||||||||||||
524 | available on the current read channel. It also emits | - | ||||||||||||||||||
525 | readyReadStandardOutput() when new standard output data is | - | ||||||||||||||||||
526 | available, and when new standard error data is available, | - | ||||||||||||||||||
527 | readyReadStandardError() is emitted. Instead of calling read(), | - | ||||||||||||||||||
528 | readLine(), or getChar(), you can explicitly read all data from | - | ||||||||||||||||||
529 | either of the two channels by calling readAllStandardOutput() or | - | ||||||||||||||||||
530 | readAllStandardError(). | - | ||||||||||||||||||
531 | - | |||||||||||||||||||
532 | The terminology for the channels can be misleading. Be aware that | - | ||||||||||||||||||
533 | the process's output channels correspond to QProcess's | - | ||||||||||||||||||
534 | \e read channels, whereas the process's input channels correspond | - | ||||||||||||||||||
535 | to QProcess's \e write channels. This is because what we read | - | ||||||||||||||||||
536 | using QProcess is the process's output, and what we write becomes | - | ||||||||||||||||||
537 | the process's input. | - | ||||||||||||||||||
538 | - | |||||||||||||||||||
539 | QProcess can merge the two output channels, so that standard | - | ||||||||||||||||||
540 | output and standard error data from the running process both use | - | ||||||||||||||||||
541 | the standard output channel. Call setProcessChannelMode() with | - | ||||||||||||||||||
542 | MergedChannels before starting the process to activate | - | ||||||||||||||||||
543 | this feature. You also have the option of forwarding the output of | - | ||||||||||||||||||
544 | the running process to the calling, main process, by passing | - | ||||||||||||||||||
545 | ForwardedChannels as the argument. It is also possible to forward | - | ||||||||||||||||||
546 | only one of the output channels - typically one would use | - | ||||||||||||||||||
547 | ForwardedErrorChannel, but ForwardedOutputChannel also exists. | - | ||||||||||||||||||
548 | Note that using channel forwarding is typically a bad idea in GUI | - | ||||||||||||||||||
549 | applications - you should present errors graphically instead. | - | ||||||||||||||||||
550 | - | |||||||||||||||||||
551 | Certain processes need special environment settings in order to | - | ||||||||||||||||||
552 | operate. You can set environment variables for your process by | - | ||||||||||||||||||
553 | calling setProcessEnvironment(). To set a working directory, call | - | ||||||||||||||||||
554 | setWorkingDirectory(). By default, processes are run in the | - | ||||||||||||||||||
555 | current working directory of the calling process. | - | ||||||||||||||||||
556 | - | |||||||||||||||||||
557 | The positioning and the screen Z-order of windows belonging to | - | ||||||||||||||||||
558 | GUI applications started with QProcess are controlled by | - | ||||||||||||||||||
559 | the underlying windowing system. For Qt 5 applications, the | - | ||||||||||||||||||
560 | positioning can be specified using the \c{-qwindowgeometry} | - | ||||||||||||||||||
561 | command line option; X11 applications generally accept a | - | ||||||||||||||||||
562 | \c{-geometry} command line option. | - | ||||||||||||||||||
563 | - | |||||||||||||||||||
564 | \note On QNX, setting the working directory may cause all | - | ||||||||||||||||||
565 | application threads, with the exception of the QProcess caller | - | ||||||||||||||||||
566 | thread, to temporarily freeze during the spawning process, | - | ||||||||||||||||||
567 | owing to a limitation in the operating system. | - | ||||||||||||||||||
568 | - | |||||||||||||||||||
569 | \section1 Synchronous Process API | - | ||||||||||||||||||
570 | - | |||||||||||||||||||
571 | QProcess provides a set of functions which allow it to be used | - | ||||||||||||||||||
572 | without an event loop, by suspending the calling thread until | - | ||||||||||||||||||
573 | certain signals are emitted: | - | ||||||||||||||||||
574 | - | |||||||||||||||||||
575 | \list | - | ||||||||||||||||||
576 | \li waitForStarted() blocks until the process has started. | - | ||||||||||||||||||
577 | - | |||||||||||||||||||
578 | \li waitForReadyRead() blocks until new data is | - | ||||||||||||||||||
579 | available for reading on the current read channel. | - | ||||||||||||||||||
580 | - | |||||||||||||||||||
581 | \li waitForBytesWritten() blocks until one payload of | - | ||||||||||||||||||
582 | data has been written to the process. | - | ||||||||||||||||||
583 | - | |||||||||||||||||||
584 | \li waitForFinished() blocks until the process has finished. | - | ||||||||||||||||||
585 | \endlist | - | ||||||||||||||||||
586 | - | |||||||||||||||||||
587 | Calling these functions from the main thread (the thread that | - | ||||||||||||||||||
588 | calls QApplication::exec()) may cause your user interface to | - | ||||||||||||||||||
589 | freeze. | - | ||||||||||||||||||
590 | - | |||||||||||||||||||
591 | The following example runs \c gzip to compress the string "Qt | - | ||||||||||||||||||
592 | rocks!", without an event loop: | - | ||||||||||||||||||
593 | - | |||||||||||||||||||
594 | \snippet process/process.cpp 0 | - | ||||||||||||||||||
595 | - | |||||||||||||||||||
596 | \section1 Notes for Windows Users | - | ||||||||||||||||||
597 | - | |||||||||||||||||||
598 | Some Windows commands (for example, \c dir) are not provided by | - | ||||||||||||||||||
599 | separate applications, but by the command interpreter itself. | - | ||||||||||||||||||
600 | If you attempt to use QProcess to execute these commands directly, | - | ||||||||||||||||||
601 | it won't work. One possible solution is to execute the command | - | ||||||||||||||||||
602 | interpreter itself (\c{cmd.exe} on some Windows systems), and ask | - | ||||||||||||||||||
603 | the interpreter to execute the desired command. | - | ||||||||||||||||||
604 | - | |||||||||||||||||||
605 | \sa QBuffer, QFile, QTcpSocket | - | ||||||||||||||||||
606 | */ | - | ||||||||||||||||||
607 | - | |||||||||||||||||||
608 | /*! | - | ||||||||||||||||||
609 | \enum QProcess::ProcessChannel | - | ||||||||||||||||||
610 | - | |||||||||||||||||||
611 | This enum describes the process channels used by the running process. | - | ||||||||||||||||||
612 | Pass one of these values to setReadChannel() to set the | - | ||||||||||||||||||
613 | current read channel of QProcess. | - | ||||||||||||||||||
614 | - | |||||||||||||||||||
615 | \value StandardOutput The standard output (stdout) of the running | - | ||||||||||||||||||
616 | process. | - | ||||||||||||||||||
617 | - | |||||||||||||||||||
618 | \value StandardError The standard error (stderr) of the running | - | ||||||||||||||||||
619 | process. | - | ||||||||||||||||||
620 | - | |||||||||||||||||||
621 | \sa setReadChannel() | - | ||||||||||||||||||
622 | */ | - | ||||||||||||||||||
623 | - | |||||||||||||||||||
624 | /*! | - | ||||||||||||||||||
625 | \enum QProcess::ProcessChannelMode | - | ||||||||||||||||||
626 | - | |||||||||||||||||||
627 | This enum describes the process output channel modes of QProcess. | - | ||||||||||||||||||
628 | Pass one of these values to setProcessChannelMode() to set the | - | ||||||||||||||||||
629 | current read channel mode. | - | ||||||||||||||||||
630 | - | |||||||||||||||||||
631 | \value SeparateChannels QProcess manages the output of the | - | ||||||||||||||||||
632 | running process, keeping standard output and standard error data | - | ||||||||||||||||||
633 | in separate internal buffers. You can select the QProcess's | - | ||||||||||||||||||
634 | current read channel by calling setReadChannel(). This is the | - | ||||||||||||||||||
635 | default channel mode of QProcess. | - | ||||||||||||||||||
636 | - | |||||||||||||||||||
637 | \value MergedChannels QProcess merges the output of the running | - | ||||||||||||||||||
638 | process into the standard output channel (\c stdout). The | - | ||||||||||||||||||
639 | standard error channel (\c stderr) will not receive any data. The | - | ||||||||||||||||||
640 | standard output and standard error data of the running process | - | ||||||||||||||||||
641 | are interleaved. | - | ||||||||||||||||||
642 | - | |||||||||||||||||||
643 | \value ForwardedChannels QProcess forwards the output of the | - | ||||||||||||||||||
644 | running process onto the main process. Anything the child process | - | ||||||||||||||||||
645 | writes to its standard output and standard error will be written | - | ||||||||||||||||||
646 | to the standard output and standard error of the main process. | - | ||||||||||||||||||
647 | - | |||||||||||||||||||
648 | \value ForwardedErrorChannel QProcess manages the standard output | - | ||||||||||||||||||
649 | of the running process, but forwards its standard error onto the | - | ||||||||||||||||||
650 | main process. This reflects the typical use of command line tools | - | ||||||||||||||||||
651 | as filters, where the standard output is redirected to another | - | ||||||||||||||||||
652 | process or a file, while standard error is printed to the console | - | ||||||||||||||||||
653 | for diagnostic purposes. | - | ||||||||||||||||||
654 | (This value was introduced in Qt 5.2.) | - | ||||||||||||||||||
655 | - | |||||||||||||||||||
656 | \value ForwardedOutputChannel Complementary to ForwardedErrorChannel. | - | ||||||||||||||||||
657 | (This value was introduced in Qt 5.2.) | - | ||||||||||||||||||
658 | - | |||||||||||||||||||
659 | \note Windows intentionally suppresses output from GUI-only | - | ||||||||||||||||||
660 | applications to inherited consoles. | - | ||||||||||||||||||
661 | This does \e not apply to output redirected to files or pipes. | - | ||||||||||||||||||
662 | To forward the output of GUI-only applications on the console | - | ||||||||||||||||||
663 | nonetheless, you must use SeparateChannels and do the forwarding | - | ||||||||||||||||||
664 | yourself by reading the output and writing it to the appropriate | - | ||||||||||||||||||
665 | output channels. | - | ||||||||||||||||||
666 | - | |||||||||||||||||||
667 | \sa setProcessChannelMode() | - | ||||||||||||||||||
668 | */ | - | ||||||||||||||||||
669 | - | |||||||||||||||||||
670 | /*! | - | ||||||||||||||||||
671 | \enum QProcess::InputChannelMode | - | ||||||||||||||||||
672 | \since 5.2 | - | ||||||||||||||||||
673 | - | |||||||||||||||||||
674 | This enum describes the process input channel modes of QProcess. | - | ||||||||||||||||||
675 | Pass one of these values to setInputChannelMode() to set the | - | ||||||||||||||||||
676 | current write channel mode. | - | ||||||||||||||||||
677 | - | |||||||||||||||||||
678 | \value ManagedInputChannel QProcess manages the input of the running | - | ||||||||||||||||||
679 | process. This is the default input channel mode of QProcess. | - | ||||||||||||||||||
680 | - | |||||||||||||||||||
681 | \value ForwardedInputChannel QProcess forwards the input of the main | - | ||||||||||||||||||
682 | process onto the running process. The child process reads its standard | - | ||||||||||||||||||
683 | input from the same source as the main process. | - | ||||||||||||||||||
684 | Note that the main process must not try to read its standard input | - | ||||||||||||||||||
685 | while the child process is running. | - | ||||||||||||||||||
686 | - | |||||||||||||||||||
687 | \sa setInputChannelMode() | - | ||||||||||||||||||
688 | */ | - | ||||||||||||||||||
689 | - | |||||||||||||||||||
690 | /*! | - | ||||||||||||||||||
691 | \enum QProcess::ProcessError | - | ||||||||||||||||||
692 | - | |||||||||||||||||||
693 | This enum describes the different types of errors that are | - | ||||||||||||||||||
694 | reported by QProcess. | - | ||||||||||||||||||
695 | - | |||||||||||||||||||
696 | \value FailedToStart The process failed to start. Either the | - | ||||||||||||||||||
697 | invoked program is missing, or you may have insufficient | - | ||||||||||||||||||
698 | permissions to invoke the program. | - | ||||||||||||||||||
699 | - | |||||||||||||||||||
700 | \value Crashed The process crashed some time after starting | - | ||||||||||||||||||
701 | successfully. | - | ||||||||||||||||||
702 | - | |||||||||||||||||||
703 | \value Timedout The last waitFor...() function timed out. The | - | ||||||||||||||||||
704 | state of QProcess is unchanged, and you can try calling | - | ||||||||||||||||||
705 | waitFor...() again. | - | ||||||||||||||||||
706 | - | |||||||||||||||||||
707 | \value WriteError An error occurred when attempting to write to the | - | ||||||||||||||||||
708 | process. For example, the process may not be running, or it may | - | ||||||||||||||||||
709 | have closed its input channel. | - | ||||||||||||||||||
710 | - | |||||||||||||||||||
711 | \value ReadError An error occurred when attempting to read from | - | ||||||||||||||||||
712 | the process. For example, the process may not be running. | - | ||||||||||||||||||
713 | - | |||||||||||||||||||
714 | \value UnknownError An unknown error occurred. This is the default | - | ||||||||||||||||||
715 | return value of error(). | - | ||||||||||||||||||
716 | - | |||||||||||||||||||
717 | \sa error() | - | ||||||||||||||||||
718 | */ | - | ||||||||||||||||||
719 | - | |||||||||||||||||||
720 | /*! | - | ||||||||||||||||||
721 | \enum QProcess::ProcessState | - | ||||||||||||||||||
722 | - | |||||||||||||||||||
723 | This enum describes the different states of QProcess. | - | ||||||||||||||||||
724 | - | |||||||||||||||||||
725 | \value NotRunning The process is not running. | - | ||||||||||||||||||
726 | - | |||||||||||||||||||
727 | \value Starting The process is starting, but the program has not | - | ||||||||||||||||||
728 | yet been invoked. | - | ||||||||||||||||||
729 | - | |||||||||||||||||||
730 | \value Running The process is running and is ready for reading and | - | ||||||||||||||||||
731 | writing. | - | ||||||||||||||||||
732 | - | |||||||||||||||||||
733 | \sa state() | - | ||||||||||||||||||
734 | */ | - | ||||||||||||||||||
735 | - | |||||||||||||||||||
736 | /*! | - | ||||||||||||||||||
737 | \enum QProcess::ExitStatus | - | ||||||||||||||||||
738 | - | |||||||||||||||||||
739 | This enum describes the different exit statuses of QProcess. | - | ||||||||||||||||||
740 | - | |||||||||||||||||||
741 | \value NormalExit The process exited normally. | - | ||||||||||||||||||
742 | - | |||||||||||||||||||
743 | \value CrashExit The process crashed. | - | ||||||||||||||||||
744 | - | |||||||||||||||||||
745 | \sa exitStatus() | - | ||||||||||||||||||
746 | */ | - | ||||||||||||||||||
747 | - | |||||||||||||||||||
748 | /*! | - | ||||||||||||||||||
749 | \fn void QProcess::error(QProcess::ProcessError error) | - | ||||||||||||||||||
750 | \obsolete | - | ||||||||||||||||||
751 | - | |||||||||||||||||||
752 | Use errorOccurred() instead. | - | ||||||||||||||||||
753 | */ | - | ||||||||||||||||||
754 | - | |||||||||||||||||||
755 | /*! | - | ||||||||||||||||||
756 | \fn void QProcess::errorOccurred(QProcess::ProcessError error) | - | ||||||||||||||||||
757 | \since 5.6 | - | ||||||||||||||||||
758 | - | |||||||||||||||||||
759 | This signal is emitted when an error occurs with the process. The | - | ||||||||||||||||||
760 | specified \a error describes the type of error that occurred. | - | ||||||||||||||||||
761 | */ | - | ||||||||||||||||||
762 | - | |||||||||||||||||||
763 | /*! | - | ||||||||||||||||||
764 | \fn void QProcess::started() | - | ||||||||||||||||||
765 | - | |||||||||||||||||||
766 | This signal is emitted by QProcess when the process has started, | - | ||||||||||||||||||
767 | and state() returns \l Running. | - | ||||||||||||||||||
768 | */ | - | ||||||||||||||||||
769 | - | |||||||||||||||||||
770 | /*! | - | ||||||||||||||||||
771 | \fn void QProcess::stateChanged(QProcess::ProcessState newState) | - | ||||||||||||||||||
772 | - | |||||||||||||||||||
773 | This signal is emitted whenever the state of QProcess changes. The | - | ||||||||||||||||||
774 | \a newState argument is the state QProcess changed to. | - | ||||||||||||||||||
775 | */ | - | ||||||||||||||||||
776 | - | |||||||||||||||||||
777 | /*! | - | ||||||||||||||||||
778 | \fn void QProcess::finished(int exitCode) | - | ||||||||||||||||||
779 | \obsolete | - | ||||||||||||||||||
780 | \overload | - | ||||||||||||||||||
781 | - | |||||||||||||||||||
782 | Use finished(int exitCode, QProcess::ExitStatus status) instead. | - | ||||||||||||||||||
783 | */ | - | ||||||||||||||||||
784 | - | |||||||||||||||||||
785 | /*! | - | ||||||||||||||||||
786 | \fn void QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus) | - | ||||||||||||||||||
787 | - | |||||||||||||||||||
788 | This signal is emitted when the process finishes. \a exitCode is the exit | - | ||||||||||||||||||
789 | code of the process (only valid for normal exits), and \a exitStatus is | - | ||||||||||||||||||
790 | the exit status. | - | ||||||||||||||||||
791 | After the process has finished, the buffers in QProcess are still intact. | - | ||||||||||||||||||
792 | You can still read any data that the process may have written before it | - | ||||||||||||||||||
793 | finished. | - | ||||||||||||||||||
794 | - | |||||||||||||||||||
795 | \sa exitStatus() | - | ||||||||||||||||||
796 | */ | - | ||||||||||||||||||
797 | - | |||||||||||||||||||
798 | /*! | - | ||||||||||||||||||
799 | \fn void QProcess::readyReadStandardOutput() | - | ||||||||||||||||||
800 | - | |||||||||||||||||||
801 | This signal is emitted when the process has made new data | - | ||||||||||||||||||
802 | available through its standard output channel (\c stdout). It is | - | ||||||||||||||||||
803 | emitted regardless of the current \l{readChannel()}{read channel}. | - | ||||||||||||||||||
804 | - | |||||||||||||||||||
805 | \sa readAllStandardOutput(), readChannel() | - | ||||||||||||||||||
806 | */ | - | ||||||||||||||||||
807 | - | |||||||||||||||||||
808 | /*! | - | ||||||||||||||||||
809 | \fn void QProcess::readyReadStandardError() | - | ||||||||||||||||||
810 | - | |||||||||||||||||||
811 | This signal is emitted when the process has made new data | - | ||||||||||||||||||
812 | available through its standard error channel (\c stderr). It is | - | ||||||||||||||||||
813 | emitted regardless of the current \l{readChannel()}{read | - | ||||||||||||||||||
814 | channel}. | - | ||||||||||||||||||
815 | - | |||||||||||||||||||
816 | \sa readAllStandardError(), readChannel() | - | ||||||||||||||||||
817 | */ | - | ||||||||||||||||||
818 | - | |||||||||||||||||||
819 | /*! | - | ||||||||||||||||||
820 | \internal | - | ||||||||||||||||||
821 | */ | - | ||||||||||||||||||
822 | QProcessPrivate::QProcessPrivate() | - | ||||||||||||||||||
823 | { | - | ||||||||||||||||||
824 | processChannel = QProcess::StandardOutput; | - | ||||||||||||||||||
825 | processChannelMode = QProcess::SeparateChannels; | - | ||||||||||||||||||
826 | inputChannelMode = QProcess::ManagedInputChannel; | - | ||||||||||||||||||
827 | processError = QProcess::UnknownError; | - | ||||||||||||||||||
828 | processState = QProcess::NotRunning; | - | ||||||||||||||||||
829 | pid = 0; | - | ||||||||||||||||||
830 | sequenceNumber = 0; | - | ||||||||||||||||||
831 | exitCode = 0; | - | ||||||||||||||||||
832 | exitStatus = QProcess::NormalExit; | - | ||||||||||||||||||
833 | startupSocketNotifier = 0; | - | ||||||||||||||||||
834 | deathNotifier = 0; | - | ||||||||||||||||||
835 | childStartedPipe[0] = INVALID_Q_PIPE; | - | ||||||||||||||||||
836 | childStartedPipe[1] = INVALID_Q_PIPE; | - | ||||||||||||||||||
837 | forkfd = -1; | - | ||||||||||||||||||
838 | exitCode = 0; | - | ||||||||||||||||||
839 | crashed = false; | - | ||||||||||||||||||
840 | dying = false; | - | ||||||||||||||||||
841 | emittedReadyRead = false; | - | ||||||||||||||||||
842 | emittedBytesWritten = false; | - | ||||||||||||||||||
843 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
844 | stdinWriteTrigger = 0; | - | ||||||||||||||||||
845 | processFinishedNotifier = 0; | - | ||||||||||||||||||
846 | #endif // Q_OS_WIN | - | ||||||||||||||||||
847 | } executed 1730 times by 34 tests: end of block Executed by:
| 1730 | ||||||||||||||||||
848 | - | |||||||||||||||||||
849 | /*! | - | ||||||||||||||||||
850 | \internal | - | ||||||||||||||||||
851 | */ | - | ||||||||||||||||||
852 | QProcessPrivate::~QProcessPrivate() | - | ||||||||||||||||||
853 | { | - | ||||||||||||||||||
854 | if (stdinChannel.process)
| 3-1731 | ||||||||||||||||||
855 | stdinChannel.process->stdoutChannel.clear(); executed 3 times by 1 test: stdinChannel.process->stdoutChannel.clear(); Executed by:
| 3 | ||||||||||||||||||
856 | if (stdoutChannel.process)
| 0-1734 | ||||||||||||||||||
857 | stdoutChannel.process->stdinChannel.clear(); never executed: stdoutChannel.process->stdinChannel.clear(); | 0 | ||||||||||||||||||
858 | } executed 1734 times by 39 tests: end of block Executed by:
| 1734 | ||||||||||||||||||
859 | - | |||||||||||||||||||
860 | /*! | - | ||||||||||||||||||
861 | \internal | - | ||||||||||||||||||
862 | */ | - | ||||||||||||||||||
863 | void QProcessPrivate::cleanup() | - | ||||||||||||||||||
864 | { | - | ||||||||||||||||||
865 | q_func()->setProcessState(QProcess::NotRunning); | - | ||||||||||||||||||
866 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
867 | if (pid) { | - | ||||||||||||||||||
868 | CloseHandle(pid->hThread); | - | ||||||||||||||||||
869 | CloseHandle(pid->hProcess); | - | ||||||||||||||||||
870 | delete pid; | - | ||||||||||||||||||
871 | pid = 0; | - | ||||||||||||||||||
872 | } | - | ||||||||||||||||||
873 | if (stdinWriteTrigger) { | - | ||||||||||||||||||
874 | delete stdinWriteTrigger; | - | ||||||||||||||||||
875 | stdinWriteTrigger = 0; | - | ||||||||||||||||||
876 | } | - | ||||||||||||||||||
877 | if (processFinishedNotifier) { | - | ||||||||||||||||||
878 | delete processFinishedNotifier; | - | ||||||||||||||||||
879 | processFinishedNotifier = 0; | - | ||||||||||||||||||
880 | } | - | ||||||||||||||||||
881 | - | |||||||||||||||||||
882 | #endif | - | ||||||||||||||||||
883 | pid = 0; | - | ||||||||||||||||||
884 | sequenceNumber = 0; | - | ||||||||||||||||||
885 | dying = false; | - | ||||||||||||||||||
886 | - | |||||||||||||||||||
887 | if (stdoutChannel.notifier) {
| 1826-2382 | ||||||||||||||||||
888 | delete stdoutChannel.notifier; | - | ||||||||||||||||||
889 | stdoutChannel.notifier = 0; | - | ||||||||||||||||||
890 | } executed 2382 times by 36 tests: end of block Executed by:
| 2382 | ||||||||||||||||||
891 | if (stderrChannel.notifier) {
| 1835-2373 | ||||||||||||||||||
892 | delete stderrChannel.notifier; | - | ||||||||||||||||||
893 | stderrChannel.notifier = 0; | - | ||||||||||||||||||
894 | } executed 2373 times by 35 tests: end of block Executed by:
| 2373 | ||||||||||||||||||
895 | if (stdinChannel.notifier) {
| 2020-2188 | ||||||||||||||||||
896 | delete stdinChannel.notifier; | - | ||||||||||||||||||
897 | stdinChannel.notifier = 0; | - | ||||||||||||||||||
898 | } executed 2188 times by 33 tests: end of block Executed by:
| 2188 | ||||||||||||||||||
899 | if (startupSocketNotifier) {
| 0-4208 | ||||||||||||||||||
900 | delete startupSocketNotifier; | - | ||||||||||||||||||
901 | startupSocketNotifier = 0; | - | ||||||||||||||||||
902 | } never executed: end of block | 0 | ||||||||||||||||||
903 | if (deathNotifier) {
| 0-4208 | ||||||||||||||||||
904 | delete deathNotifier; | - | ||||||||||||||||||
905 | deathNotifier = 0; | - | ||||||||||||||||||
906 | } never executed: end of block | 0 | ||||||||||||||||||
907 | closeChannel(&stdoutChannel); | - | ||||||||||||||||||
908 | closeChannel(&stderrChannel); | - | ||||||||||||||||||
909 | closeChannel(&stdinChannel); | - | ||||||||||||||||||
910 | destroyPipe(childStartedPipe); | - | ||||||||||||||||||
911 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
912 | if (forkfd != -1)
| 0-4208 | ||||||||||||||||||
913 | qt_safe_close(forkfd); never executed: qt_safe_close(forkfd); | 0 | ||||||||||||||||||
914 | forkfd = -1; | - | ||||||||||||||||||
915 | #endif | - | ||||||||||||||||||
916 | } executed 4208 times by 43 tests: end of block Executed by:
| 4208 | ||||||||||||||||||
917 | - | |||||||||||||||||||
918 | /*! | - | ||||||||||||||||||
919 | \internal | - | ||||||||||||||||||
920 | */ | - | ||||||||||||||||||
921 | void QProcessPrivate::setError(QProcess::ProcessError error, const QString &description) | - | ||||||||||||||||||
922 | { | - | ||||||||||||||||||
923 | processError = error; | - | ||||||||||||||||||
924 | if (description.isEmpty()) {
| 104-710 | ||||||||||||||||||
925 | switch (error) { | - | ||||||||||||||||||
926 | case QProcess::FailedToStart: never executed: case QProcess::FailedToStart: | 0 | ||||||||||||||||||
927 | errorString = QProcess::tr("Process failed to start"); | - | ||||||||||||||||||
928 | break; never executed: break; | 0 | ||||||||||||||||||
929 | case QProcess::Crashed: executed 90 times by 5 tests: case QProcess::Crashed: Executed by:
| 90 | ||||||||||||||||||
930 | errorString = QProcess::tr("Process crashed"); | - | ||||||||||||||||||
931 | break; executed 90 times by 5 tests: break; Executed by:
| 90 | ||||||||||||||||||
932 | case QProcess::Timedout: executed 9 times by 3 tests: case QProcess::Timedout: Executed by:
| 9 | ||||||||||||||||||
933 | errorString = QProcess::tr("Process operation timed out"); | - | ||||||||||||||||||
934 | break; executed 9 times by 3 tests: break; Executed by:
| 9 | ||||||||||||||||||
935 | case QProcess::ReadError: never executed: case QProcess::ReadError: | 0 | ||||||||||||||||||
936 | errorString = QProcess::tr("Error reading from process"); | - | ||||||||||||||||||
937 | break; never executed: break; | 0 | ||||||||||||||||||
938 | case QProcess::WriteError: executed 5 times by 1 test: case QProcess::WriteError: Executed by:
| 5 | ||||||||||||||||||
939 | errorString = QProcess::tr("Error writing to process"); | - | ||||||||||||||||||
940 | break; executed 5 times by 1 test: break; Executed by:
| 5 | ||||||||||||||||||
941 | case QProcess::UnknownError: never executed: case QProcess::UnknownError: | 0 | ||||||||||||||||||
942 | errorString.clear(); | - | ||||||||||||||||||
943 | break; never executed: break; | 0 | ||||||||||||||||||
944 | } | - | ||||||||||||||||||
945 | } else { executed 104 times by 6 tests: end of block Executed by:
| 104 | ||||||||||||||||||
946 | errorString = description; | - | ||||||||||||||||||
947 | } executed 710 times by 11 tests: end of block Executed by:
| 710 | ||||||||||||||||||
948 | } | - | ||||||||||||||||||
949 | - | |||||||||||||||||||
950 | /*! | - | ||||||||||||||||||
951 | \internal | - | ||||||||||||||||||
952 | */ | - | ||||||||||||||||||
953 | void QProcessPrivate::setErrorAndEmit(QProcess::ProcessError error, const QString &description) | - | ||||||||||||||||||
954 | { | - | ||||||||||||||||||
955 | Q_Q(QProcess); | - | ||||||||||||||||||
956 | Q_ASSERT(error != QProcess::UnknownError); | - | ||||||||||||||||||
957 | setError(error, description); | - | ||||||||||||||||||
958 | emit q->errorOccurred(processError); | - | ||||||||||||||||||
959 | emit q->error(processError); | - | ||||||||||||||||||
960 | } executed 805 times by 15 tests: end of block Executed by:
| 805 | ||||||||||||||||||
961 | - | |||||||||||||||||||
962 | /*! | - | ||||||||||||||||||
963 | \internal | - | ||||||||||||||||||
964 | Returns true if we emitted readyRead(). | - | ||||||||||||||||||
965 | */ | - | ||||||||||||||||||
966 | bool QProcessPrivate::tryReadFromChannel(Channel *channel) | - | ||||||||||||||||||
967 | { | - | ||||||||||||||||||
968 | Q_Q(QProcess); | - | ||||||||||||||||||
969 | if (channel->pipe[0] == INVALID_Q_PIPE)
| 3534-38181 | ||||||||||||||||||
970 | return false; executed 3534 times by 29 tests: return false; Executed by:
| 3534 | ||||||||||||||||||
971 | - | |||||||||||||||||||
972 | qint64 available = bytesAvailableInChannel(channel); | - | ||||||||||||||||||
973 | if (available == 0)
| 3509-34672 | ||||||||||||||||||
974 | available = 1; // always try to read at least one byte executed 3509 times by 29 tests: available = 1; Executed by:
| 3509 | ||||||||||||||||||
975 | - | |||||||||||||||||||
976 | char *ptr = channel->buffer.reserve(available); | - | ||||||||||||||||||
977 | qint64 readBytes = readFromChannel(channel, ptr, available); | - | ||||||||||||||||||
978 | if (readBytes <= 0)
| 3509-34672 | ||||||||||||||||||
979 | channel->buffer.chop(available); executed 3509 times by 29 tests: channel->buffer.chop(available); Executed by:
| 3509 | ||||||||||||||||||
980 | if (readBytes == -2) {
| 2-38179 | ||||||||||||||||||
981 | // EWOULDBLOCK | - | ||||||||||||||||||
982 | return false; executed 2 times by 1 test: return false; Executed by:
| 2 | ||||||||||||||||||
983 | } | - | ||||||||||||||||||
984 | if (readBytes == -1) {
| 0-38179 | ||||||||||||||||||
985 | setErrorAndEmit(QProcess::ReadError); | - | ||||||||||||||||||
986 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
987 | qDebug("QProcessPrivate::tryReadFromChannel(%d), failed to read from the process", channel - &stdinChannel); | - | ||||||||||||||||||
988 | #endif | - | ||||||||||||||||||
989 | return false; never executed: return false; | 0 | ||||||||||||||||||
990 | } | - | ||||||||||||||||||
991 | if (readBytes == 0) {
| 3507-34672 | ||||||||||||||||||
992 | // EOF | - | ||||||||||||||||||
993 | if (channel->notifier)
| 162-3345 | ||||||||||||||||||
994 | channel->notifier->setEnabled(false); executed 3345 times by 27 tests: channel->notifier->setEnabled(false); Executed by:
| 3345 | ||||||||||||||||||
995 | closeChannel(channel); | - | ||||||||||||||||||
996 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
997 | qDebug("QProcessPrivate::tryReadFromChannel(%d), 0 bytes available", channel - &stdinChannel); | - | ||||||||||||||||||
998 | #endif | - | ||||||||||||||||||
999 | return false; executed 3507 times by 29 tests: return false; Executed by:
| 3507 | ||||||||||||||||||
1000 | } | - | ||||||||||||||||||
1001 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1002 | qDebug("QProcessPrivate::tryReadFromChannel(%d), read %d bytes from the process' output", channel - &stdinChannel | - | ||||||||||||||||||
1003 | int(readBytes)); | - | ||||||||||||||||||
1004 | #endif | - | ||||||||||||||||||
1005 | - | |||||||||||||||||||
1006 | if (channel->closed) {
| 40-34632 | ||||||||||||||||||
1007 | channel->buffer.chop(readBytes); | - | ||||||||||||||||||
1008 | return false; executed 40 times by 1 test: return false; Executed by:
| 40 | ||||||||||||||||||
1009 | } | - | ||||||||||||||||||
1010 | - | |||||||||||||||||||
1011 | channel->buffer.chop(available - readBytes); | - | ||||||||||||||||||
1012 | - | |||||||||||||||||||
1013 | bool didRead = false; | - | ||||||||||||||||||
1014 | bool isStdout = channel == &stdoutChannel; | - | ||||||||||||||||||
1015 | if (readBytes == 0) {
| 0-34632 | ||||||||||||||||||
1016 | if (channel->notifier)
| 0 | ||||||||||||||||||
1017 | channel->notifier->setEnabled(false); never executed: channel->notifier->setEnabled(false); | 0 | ||||||||||||||||||
1018 | } else if ((processChannel == QProcess::StandardOutput) == isStdout) { never executed: end of block
| 0-33489 | ||||||||||||||||||
1019 | didRead = true; | - | ||||||||||||||||||
1020 | if (!emittedReadyRead) {
| 1-33488 | ||||||||||||||||||
1021 | emittedReadyRead = true; | - | ||||||||||||||||||
1022 | emit q->readyRead(); | - | ||||||||||||||||||
1023 | emittedReadyRead = false; | - | ||||||||||||||||||
1024 | } executed 33488 times by 22 tests: end of block Executed by:
| 33488 | ||||||||||||||||||
1025 | } executed 33489 times by 22 tests: end of block Executed by:
| 33489 | ||||||||||||||||||
1026 | if (isStdout)
| 768-33864 | ||||||||||||||||||
1027 | emit q->readyReadStandardOutput(QProcess::QPrivateSignal()); executed 33864 times by 22 tests: q->readyReadStandardOutput(QProcess::QPrivateSignal()); Executed by:
| 33864 | ||||||||||||||||||
1028 | else | - | ||||||||||||||||||
1029 | emit q->readyReadStandardError(QProcess::QPrivateSignal()); executed 768 times by 10 tests: q->readyReadStandardError(QProcess::QPrivateSignal()); Executed by:
| 768 | ||||||||||||||||||
1030 | return didRead; executed 34632 times by 25 tests: return didRead; Executed by:
| 34632 | ||||||||||||||||||
1031 | } | - | ||||||||||||||||||
1032 | - | |||||||||||||||||||
1033 | /*! | - | ||||||||||||||||||
1034 | \internal | - | ||||||||||||||||||
1035 | */ | - | ||||||||||||||||||
1036 | bool QProcessPrivate::_q_canReadStandardOutput() | - | ||||||||||||||||||
1037 | { | - | ||||||||||||||||||
1038 | return tryReadFromChannel(&stdoutChannel); executed 37412 times by 29 tests: return tryReadFromChannel(&stdoutChannel); Executed by:
| 37412 | ||||||||||||||||||
1039 | } | - | ||||||||||||||||||
1040 | - | |||||||||||||||||||
1041 | /*! | - | ||||||||||||||||||
1042 | \internal | - | ||||||||||||||||||
1043 | */ | - | ||||||||||||||||||
1044 | bool QProcessPrivate::_q_canReadStandardError() | - | ||||||||||||||||||
1045 | { | - | ||||||||||||||||||
1046 | return tryReadFromChannel(&stderrChannel); executed 4303 times by 29 tests: return tryReadFromChannel(&stderrChannel); Executed by:
| 4303 | ||||||||||||||||||
1047 | } | - | ||||||||||||||||||
1048 | - | |||||||||||||||||||
1049 | /*! | - | ||||||||||||||||||
1050 | \internal | - | ||||||||||||||||||
1051 | */ | - | ||||||||||||||||||
1052 | bool QProcessPrivate::_q_canWrite() | - | ||||||||||||||||||
1053 | { | - | ||||||||||||||||||
1054 | if (stdinChannel.notifier)
| 0-629 | ||||||||||||||||||
1055 | stdinChannel.notifier->setEnabled(false); executed 629 times by 6 tests: stdinChannel.notifier->setEnabled(false); Executed by:
| 629 | ||||||||||||||||||
1056 | - | |||||||||||||||||||
1057 | if (stdinChannel.buffer.isEmpty()) {
| 0-629 | ||||||||||||||||||
1058 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1059 | qDebug("QProcessPrivate::canWrite(), not writing anything (empty write buffer)."); | - | ||||||||||||||||||
1060 | #endif | - | ||||||||||||||||||
1061 | return false; never executed: return false; | 0 | ||||||||||||||||||
1062 | } | - | ||||||||||||||||||
1063 | - | |||||||||||||||||||
1064 | const bool writeSucceeded = writeToStdin(); | - | ||||||||||||||||||
1065 | - | |||||||||||||||||||
1066 | if (stdinChannel.notifier && !stdinChannel.buffer.isEmpty())
| 0-629 | ||||||||||||||||||
1067 | stdinChannel.notifier->setEnabled(true); executed 146 times by 2 tests: stdinChannel.notifier->setEnabled(true); Executed by:
| 146 | ||||||||||||||||||
1068 | if (stdinChannel.buffer.isEmpty() && stdinChannel.closed)
| 29-483 | ||||||||||||||||||
1069 | closeWriteChannel(); executed 29 times by 3 tests: closeWriteChannel(); Executed by:
| 29 | ||||||||||||||||||
1070 | return writeSucceeded; executed 629 times by 6 tests: return writeSucceeded; Executed by:
| 629 | ||||||||||||||||||
1071 | } | - | ||||||||||||||||||
1072 | - | |||||||||||||||||||
1073 | /*! | - | ||||||||||||||||||
1074 | \internal | - | ||||||||||||||||||
1075 | */ | - | ||||||||||||||||||
1076 | bool QProcessPrivate::_q_processDied() | - | ||||||||||||||||||
1077 | { | - | ||||||||||||||||||
1078 | Q_Q(QProcess); | - | ||||||||||||||||||
1079 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1080 | qDebug("QProcessPrivate::_q_processDied()"); | - | ||||||||||||||||||
1081 | #endif | - | ||||||||||||||||||
1082 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
1083 | if (!waitForDeadChild())
| 0-1770 | ||||||||||||||||||
1084 | return false; never executed: return false; | 0 | ||||||||||||||||||
1085 | #endif | - | ||||||||||||||||||
1086 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1087 | if (processFinishedNotifier) | - | ||||||||||||||||||
1088 | processFinishedNotifier->setEnabled(false); | - | ||||||||||||||||||
1089 | drainOutputPipes(); | - | ||||||||||||||||||
1090 | #endif | - | ||||||||||||||||||
1091 | - | |||||||||||||||||||
1092 | // the process may have died before it got a chance to report that it was | - | ||||||||||||||||||
1093 | // either running or stopped, so we will call _q_startupNotification() and | - | ||||||||||||||||||
1094 | // give it a chance to emit started() or errorOccurred(FailedToStart). | - | ||||||||||||||||||
1095 | if (processState == QProcess::Starting) {
| 22-1748 | ||||||||||||||||||
1096 | if (!_q_startupNotification())
| 0-22 | ||||||||||||||||||
1097 | return true; never executed: return true; | 0 | ||||||||||||||||||
1098 | } executed 22 times by 1 test: end of block Executed by:
| 22 | ||||||||||||||||||
1099 | - | |||||||||||||||||||
1100 | if (dying) {
| 0-1770 | ||||||||||||||||||
1101 | // at this point we know the process is dead. prevent | - | ||||||||||||||||||
1102 | // reentering this slot recursively by calling waitForFinished() | - | ||||||||||||||||||
1103 | // or opening a dialog inside slots connected to the readyRead | - | ||||||||||||||||||
1104 | // signals emitted below. | - | ||||||||||||||||||
1105 | return true; never executed: return true; | 0 | ||||||||||||||||||
1106 | } | - | ||||||||||||||||||
1107 | dying = true; | - | ||||||||||||||||||
1108 | - | |||||||||||||||||||
1109 | // in case there is data in the pipe line and this slot by chance | - | ||||||||||||||||||
1110 | // got called before the read notifications, call these two slots | - | ||||||||||||||||||
1111 | // so the data is made available before the process dies. | - | ||||||||||||||||||
1112 | _q_canReadStandardOutput(); | - | ||||||||||||||||||
1113 | _q_canReadStandardError(); | - | ||||||||||||||||||
1114 | - | |||||||||||||||||||
1115 | findExitCode(); | - | ||||||||||||||||||
1116 | - | |||||||||||||||||||
1117 | if (crashed) {
| 90-1680 | ||||||||||||||||||
1118 | exitStatus = QProcess::CrashExit; | - | ||||||||||||||||||
1119 | setErrorAndEmit(QProcess::Crashed); | - | ||||||||||||||||||
1120 | } executed 90 times by 5 tests: end of block Executed by:
| 90 | ||||||||||||||||||
1121 | - | |||||||||||||||||||
1122 | bool wasRunning = (processState == QProcess::Running); | - | ||||||||||||||||||
1123 | - | |||||||||||||||||||
1124 | cleanup(); | - | ||||||||||||||||||
1125 | - | |||||||||||||||||||
1126 | if (wasRunning) {
| 1-1769 | ||||||||||||||||||
1127 | // we received EOF now: | - | ||||||||||||||||||
1128 | emit q->readChannelFinished(); | - | ||||||||||||||||||
1129 | // in the future: | - | ||||||||||||||||||
1130 | //emit q->standardOutputClosed(); | - | ||||||||||||||||||
1131 | //emit q->standardErrorClosed(); | - | ||||||||||||||||||
1132 | - | |||||||||||||||||||
1133 | emit q->finished(exitCode); | - | ||||||||||||||||||
1134 | emit q->finished(exitCode, exitStatus); | - | ||||||||||||||||||
1135 | } executed 1769 times by 29 tests: end of block Executed by:
| 1769 | ||||||||||||||||||
1136 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1137 | qDebug("QProcessPrivate::_q_processDied() process is dead"); | - | ||||||||||||||||||
1138 | #endif | - | ||||||||||||||||||
1139 | return true; executed 1770 times by 29 tests: return true; Executed by:
| 1770 | ||||||||||||||||||
1140 | } | - | ||||||||||||||||||
1141 | - | |||||||||||||||||||
1142 | /*! | - | ||||||||||||||||||
1143 | \internal | - | ||||||||||||||||||
1144 | */ | - | ||||||||||||||||||
1145 | bool QProcessPrivate::_q_startupNotification() | - | ||||||||||||||||||
1146 | { | - | ||||||||||||||||||
1147 | Q_Q(QProcess); | - | ||||||||||||||||||
1148 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1149 | qDebug("QProcessPrivate::startupNotification()"); | - | ||||||||||||||||||
1150 | #endif | - | ||||||||||||||||||
1151 | - | |||||||||||||||||||
1152 | if (startupSocketNotifier)
| 81-2392 | ||||||||||||||||||
1153 | startupSocketNotifier->setEnabled(false); executed 2392 times by 36 tests: startupSocketNotifier->setEnabled(false); Executed by:
| 2392 | ||||||||||||||||||
1154 | QString errorMessage; | - | ||||||||||||||||||
1155 | if (processStarted(&errorMessage)) {
| 704-1769 | ||||||||||||||||||
1156 | q->setProcessState(QProcess::Running); | - | ||||||||||||||||||
1157 | emit q->started(QProcess::QPrivateSignal()); | - | ||||||||||||||||||
1158 | return true; executed 1769 times by 29 tests: return true; Executed by:
| 1769 | ||||||||||||||||||
1159 | } | - | ||||||||||||||||||
1160 | - | |||||||||||||||||||
1161 | q->setProcessState(QProcess::NotRunning); | - | ||||||||||||||||||
1162 | setErrorAndEmit(QProcess::FailedToStart, errorMessage); | - | ||||||||||||||||||
1163 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
1164 | // make sure the process manager removes this entry | - | ||||||||||||||||||
1165 | waitForDeadChild(); | - | ||||||||||||||||||
1166 | findExitCode(); | - | ||||||||||||||||||
1167 | #endif | - | ||||||||||||||||||
1168 | cleanup(); | - | ||||||||||||||||||
1169 | return false; executed 704 times by 11 tests: return false; Executed by:
| 704 | ||||||||||||||||||
1170 | } | - | ||||||||||||||||||
1171 | - | |||||||||||||||||||
1172 | /*! | - | ||||||||||||||||||
1173 | \internal | - | ||||||||||||||||||
1174 | */ | - | ||||||||||||||||||
1175 | void QProcessPrivate::closeWriteChannel() | - | ||||||||||||||||||
1176 | { | - | ||||||||||||||||||
1177 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1178 | qDebug("QProcessPrivate::closeWriteChannel()"); | - | ||||||||||||||||||
1179 | #endif | - | ||||||||||||||||||
1180 | if (stdinChannel.notifier) {
| 0-199 | ||||||||||||||||||
1181 | delete stdinChannel.notifier; | - | ||||||||||||||||||
1182 | stdinChannel.notifier = 0; | - | ||||||||||||||||||
1183 | } executed 199 times by 6 tests: end of block Executed by:
| 199 | ||||||||||||||||||
1184 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1185 | // ### Find a better fix, feeding the process little by little | - | ||||||||||||||||||
1186 | // instead. | - | ||||||||||||||||||
1187 | flushPipeWriter(); | - | ||||||||||||||||||
1188 | #endif | - | ||||||||||||||||||
1189 | closeChannel(&stdinChannel); | - | ||||||||||||||||||
1190 | } executed 199 times by 6 tests: end of block Executed by:
| 199 | ||||||||||||||||||
1191 | - | |||||||||||||||||||
1192 | /*! | - | ||||||||||||||||||
1193 | Constructs a QProcess object with the given \a parent. | - | ||||||||||||||||||
1194 | */ | - | ||||||||||||||||||
1195 | QProcess::QProcess(QObject *parent) | - | ||||||||||||||||||
1196 | : QIODevice(*new QProcessPrivate, parent) | - | ||||||||||||||||||
1197 | { | - | ||||||||||||||||||
1198 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1199 | qDebug("QProcess::QProcess(%p)", parent); | - | ||||||||||||||||||
1200 | #endif | - | ||||||||||||||||||
1201 | } executed 1730 times by 34 tests: end of block Executed by:
| 1730 | ||||||||||||||||||
1202 | - | |||||||||||||||||||
1203 | /*! | - | ||||||||||||||||||
1204 | Destructs the QProcess object, i.e., killing the process. | - | ||||||||||||||||||
1205 | - | |||||||||||||||||||
1206 | Note that this function will not return until the process is | - | ||||||||||||||||||
1207 | terminated. | - | ||||||||||||||||||
1208 | */ | - | ||||||||||||||||||
1209 | QProcess::~QProcess() | - | ||||||||||||||||||
1210 | { | - | ||||||||||||||||||
1211 | Q_D(QProcess); | - | ||||||||||||||||||
1212 | if (d->processState != NotRunning) {
| 1-1733 | ||||||||||||||||||
1213 | qWarning().nospace() | - | ||||||||||||||||||
1214 | << "QProcess: Destroyed while process (" << QDir::toNativeSeparators(program()) << ") is still running."; | - | ||||||||||||||||||
1215 | kill(); | - | ||||||||||||||||||
1216 | waitForFinished(); | - | ||||||||||||||||||
1217 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1218 | #ifdef Q_OS_UNIX | - | ||||||||||||||||||
1219 | // make sure the process manager removes this entry | - | ||||||||||||||||||
1220 | d->findExitCode(); | - | ||||||||||||||||||
1221 | #endif | - | ||||||||||||||||||
1222 | d->cleanup(); | - | ||||||||||||||||||
1223 | } executed 1734 times by 39 tests: end of block Executed by:
| 1734 | ||||||||||||||||||
1224 | - | |||||||||||||||||||
1225 | /*! | - | ||||||||||||||||||
1226 | \obsolete | - | ||||||||||||||||||
1227 | Returns the read channel mode of the QProcess. This function is | - | ||||||||||||||||||
1228 | equivalent to processChannelMode() | - | ||||||||||||||||||
1229 | - | |||||||||||||||||||
1230 | \sa processChannelMode() | - | ||||||||||||||||||
1231 | */ | - | ||||||||||||||||||
1232 | QProcess::ProcessChannelMode QProcess::readChannelMode() const | - | ||||||||||||||||||
1233 | { | - | ||||||||||||||||||
1234 | return processChannelMode(); executed 4 times by 1 test: return processChannelMode(); Executed by:
| 4 | ||||||||||||||||||
1235 | } | - | ||||||||||||||||||
1236 | - | |||||||||||||||||||
1237 | /*! | - | ||||||||||||||||||
1238 | \obsolete | - | ||||||||||||||||||
1239 | - | |||||||||||||||||||
1240 | Use setProcessChannelMode(\a mode) instead. | - | ||||||||||||||||||
1241 | - | |||||||||||||||||||
1242 | \sa setProcessChannelMode() | - | ||||||||||||||||||
1243 | */ | - | ||||||||||||||||||
1244 | void QProcess::setReadChannelMode(ProcessChannelMode mode) | - | ||||||||||||||||||
1245 | { | - | ||||||||||||||||||
1246 | setProcessChannelMode(mode); | - | ||||||||||||||||||
1247 | } executed 24 times by 4 tests: end of block Executed by:
| 24 | ||||||||||||||||||
1248 | - | |||||||||||||||||||
1249 | /*! | - | ||||||||||||||||||
1250 | \since 4.2 | - | ||||||||||||||||||
1251 | - | |||||||||||||||||||
1252 | Returns the channel mode of the QProcess standard output and | - | ||||||||||||||||||
1253 | standard error channels. | - | ||||||||||||||||||
1254 | - | |||||||||||||||||||
1255 | \sa setProcessChannelMode(), ProcessChannelMode, setReadChannel() | - | ||||||||||||||||||
1256 | */ | - | ||||||||||||||||||
1257 | QProcess::ProcessChannelMode QProcess::processChannelMode() const | - | ||||||||||||||||||
1258 | { | - | ||||||||||||||||||
1259 | Q_D(const QProcess); | - | ||||||||||||||||||
1260 | return d->processChannelMode; executed 9 times by 2 tests: return d->processChannelMode; Executed by:
| 9 | ||||||||||||||||||
1261 | } | - | ||||||||||||||||||
1262 | - | |||||||||||||||||||
1263 | /*! | - | ||||||||||||||||||
1264 | \since 4.2 | - | ||||||||||||||||||
1265 | - | |||||||||||||||||||
1266 | Sets the channel mode of the QProcess standard output and standard | - | ||||||||||||||||||
1267 | error channels to the \a mode specified. | - | ||||||||||||||||||
1268 | This mode will be used the next time start() is called. For example: | - | ||||||||||||||||||
1269 | - | |||||||||||||||||||
1270 | \snippet code/src_corelib_io_qprocess.cpp 0 | - | ||||||||||||||||||
1271 | - | |||||||||||||||||||
1272 | \sa processChannelMode(), ProcessChannelMode, setReadChannel() | - | ||||||||||||||||||
1273 | */ | - | ||||||||||||||||||
1274 | void QProcess::setProcessChannelMode(ProcessChannelMode mode) | - | ||||||||||||||||||
1275 | { | - | ||||||||||||||||||
1276 | Q_D(QProcess); | - | ||||||||||||||||||
1277 | d->processChannelMode = mode; | - | ||||||||||||||||||
1278 | } executed 170 times by 14 tests: end of block Executed by:
| 170 | ||||||||||||||||||
1279 | - | |||||||||||||||||||
1280 | /*! | - | ||||||||||||||||||
1281 | \since 5.2 | - | ||||||||||||||||||
1282 | - | |||||||||||||||||||
1283 | Returns the channel mode of the QProcess standard input channel. | - | ||||||||||||||||||
1284 | - | |||||||||||||||||||
1285 | \sa setInputChannelMode(), InputChannelMode | - | ||||||||||||||||||
1286 | */ | - | ||||||||||||||||||
1287 | QProcess::InputChannelMode QProcess::inputChannelMode() const | - | ||||||||||||||||||
1288 | { | - | ||||||||||||||||||
1289 | Q_D(const QProcess); | - | ||||||||||||||||||
1290 | return d->inputChannelMode; executed 5 times by 1 test: return d->inputChannelMode; Executed by:
| 5 | ||||||||||||||||||
1291 | } | - | ||||||||||||||||||
1292 | - | |||||||||||||||||||
1293 | /*! | - | ||||||||||||||||||
1294 | \since 5.2 | - | ||||||||||||||||||
1295 | - | |||||||||||||||||||
1296 | Sets the channel mode of the QProcess standard input | - | ||||||||||||||||||
1297 | channel to the \a mode specified. | - | ||||||||||||||||||
1298 | This mode will be used the next time start() is called. | - | ||||||||||||||||||
1299 | - | |||||||||||||||||||
1300 | \sa inputChannelMode(), InputChannelMode | - | ||||||||||||||||||
1301 | */ | - | ||||||||||||||||||
1302 | void QProcess::setInputChannelMode(InputChannelMode mode) | - | ||||||||||||||||||
1303 | { | - | ||||||||||||||||||
1304 | Q_D(QProcess); | - | ||||||||||||||||||
1305 | d->inputChannelMode = mode; | - | ||||||||||||||||||
1306 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||
1307 | - | |||||||||||||||||||
1308 | /*! | - | ||||||||||||||||||
1309 | Returns the current read channel of the QProcess. | - | ||||||||||||||||||
1310 | - | |||||||||||||||||||
1311 | \sa setReadChannel() | - | ||||||||||||||||||
1312 | */ | - | ||||||||||||||||||
1313 | QProcess::ProcessChannel QProcess::readChannel() const | - | ||||||||||||||||||
1314 | { | - | ||||||||||||||||||
1315 | Q_D(const QProcess); | - | ||||||||||||||||||
1316 | return d->processChannel; executed 2245 times by 16 tests: return d->processChannel; Executed by:
| 2245 | ||||||||||||||||||
1317 | } | - | ||||||||||||||||||
1318 | - | |||||||||||||||||||
1319 | /*! | - | ||||||||||||||||||
1320 | Sets the current read channel of the QProcess to the given \a | - | ||||||||||||||||||
1321 | channel. The current input channel is used by the functions | - | ||||||||||||||||||
1322 | read(), readAll(), readLine(), and getChar(). It also determines | - | ||||||||||||||||||
1323 | which channel triggers QProcess to emit readyRead(). | - | ||||||||||||||||||
1324 | - | |||||||||||||||||||
1325 | \sa readChannel() | - | ||||||||||||||||||
1326 | */ | - | ||||||||||||||||||
1327 | void QProcess::setReadChannel(ProcessChannel channel) | - | ||||||||||||||||||
1328 | { | - | ||||||||||||||||||
1329 | Q_D(QProcess); | - | ||||||||||||||||||
1330 | if (d->processChannel != channel) {
| 1313-3302 | ||||||||||||||||||
1331 | QByteArray buf = d->buffer.readAll(); | - | ||||||||||||||||||
1332 | if (d->processChannel == QProcess::StandardOutput) {
| 1609-1693 | ||||||||||||||||||
1333 | for (int i = buf.size() - 1; i >= 0; --i)
| 7-1693 | ||||||||||||||||||
1334 | d->stdoutChannel.buffer.ungetChar(buf.at(i)); executed 7 times by 1 test: d->stdoutChannel.buffer.ungetChar(buf.at(i)); Executed by:
| 7 | ||||||||||||||||||
1335 | } else { executed 1693 times by 15 tests: end of block Executed by:
| 1693 | ||||||||||||||||||
1336 | for (int i = buf.size() - 1; i >= 0; --i)
| 7-1609 | ||||||||||||||||||
1337 | d->stderrChannel.buffer.ungetChar(buf.at(i)); executed 7 times by 1 test: d->stderrChannel.buffer.ungetChar(buf.at(i)); Executed by:
| 7 | ||||||||||||||||||
1338 | } executed 1609 times by 12 tests: end of block Executed by:
| 1609 | ||||||||||||||||||
1339 | } | - | ||||||||||||||||||
1340 | d->processChannel = channel; | - | ||||||||||||||||||
1341 | } executed 4615 times by 18 tests: end of block Executed by:
| 4615 | ||||||||||||||||||
1342 | - | |||||||||||||||||||
1343 | /*! | - | ||||||||||||||||||
1344 | Closes the read channel \a channel. After calling this function, | - | ||||||||||||||||||
1345 | QProcess will no longer receive data on the channel. Any data that | - | ||||||||||||||||||
1346 | has already been received is still available for reading. | - | ||||||||||||||||||
1347 | - | |||||||||||||||||||
1348 | Call this function to save memory, if you are not interested in | - | ||||||||||||||||||
1349 | the output of the process. | - | ||||||||||||||||||
1350 | - | |||||||||||||||||||
1351 | \sa closeWriteChannel(), setReadChannel() | - | ||||||||||||||||||
1352 | */ | - | ||||||||||||||||||
1353 | void QProcess::closeReadChannel(ProcessChannel channel) | - | ||||||||||||||||||
1354 | { | - | ||||||||||||||||||
1355 | Q_D(QProcess); | - | ||||||||||||||||||
1356 | - | |||||||||||||||||||
1357 | if (channel == StandardOutput)
| 6 | ||||||||||||||||||
1358 | d->stdoutChannel.closed = true; executed 6 times by 1 test: d->stdoutChannel.closed = true; Executed by:
| 6 | ||||||||||||||||||
1359 | else | - | ||||||||||||||||||
1360 | d->stderrChannel.closed = true; executed 6 times by 1 test: d->stderrChannel.closed = true; Executed by:
| 6 | ||||||||||||||||||
1361 | } | - | ||||||||||||||||||
1362 | - | |||||||||||||||||||
1363 | /*! | - | ||||||||||||||||||
1364 | Schedules the write channel of QProcess to be closed. The channel | - | ||||||||||||||||||
1365 | will close once all data has been written to the process. After | - | ||||||||||||||||||
1366 | calling this function, any attempts to write to the process will | - | ||||||||||||||||||
1367 | fail. | - | ||||||||||||||||||
1368 | - | |||||||||||||||||||
1369 | Closing the write channel is necessary for programs that read | - | ||||||||||||||||||
1370 | input data until the channel has been closed. For example, the | - | ||||||||||||||||||
1371 | program "more" is used to display text data in a console on both | - | ||||||||||||||||||
1372 | Unix and Windows. But it will not display the text data until | - | ||||||||||||||||||
1373 | QProcess's write channel has been closed. Example: | - | ||||||||||||||||||
1374 | - | |||||||||||||||||||
1375 | \snippet code/src_corelib_io_qprocess.cpp 1 | - | ||||||||||||||||||
1376 | - | |||||||||||||||||||
1377 | The write channel is implicitly opened when start() is called. | - | ||||||||||||||||||
1378 | - | |||||||||||||||||||
1379 | \sa closeReadChannel() | - | ||||||||||||||||||
1380 | */ | - | ||||||||||||||||||
1381 | void QProcess::closeWriteChannel() | - | ||||||||||||||||||
1382 | { | - | ||||||||||||||||||
1383 | Q_D(QProcess); | - | ||||||||||||||||||
1384 | d->stdinChannel.closed = true; // closing | - | ||||||||||||||||||
1385 | if (d->stdinChannel.buffer.isEmpty())
| 32-170 | ||||||||||||||||||
1386 | d->closeWriteChannel(); executed 170 times by 6 tests: d->closeWriteChannel(); Executed by:
| 170 | ||||||||||||||||||
1387 | } executed 202 times by 8 tests: end of block Executed by:
| 202 | ||||||||||||||||||
1388 | - | |||||||||||||||||||
1389 | /*! | - | ||||||||||||||||||
1390 | \since 4.2 | - | ||||||||||||||||||
1391 | - | |||||||||||||||||||
1392 | Redirects the process' standard input to the file indicated by \a | - | ||||||||||||||||||
1393 | fileName. When an input redirection is in place, the QProcess | - | ||||||||||||||||||
1394 | object will be in read-only mode (calling write() will result in | - | ||||||||||||||||||
1395 | error). | - | ||||||||||||||||||
1396 | - | |||||||||||||||||||
1397 | To make the process read EOF right away, pass nullDevice() here. | - | ||||||||||||||||||
1398 | This is cleaner than using closeWriteChannel() before writing any | - | ||||||||||||||||||
1399 | data, because it can be set up prior to starting the process. | - | ||||||||||||||||||
1400 | - | |||||||||||||||||||
1401 | If the file \a fileName does not exist at the moment start() is | - | ||||||||||||||||||
1402 | called or is not readable, starting the process will fail. | - | ||||||||||||||||||
1403 | - | |||||||||||||||||||
1404 | Calling setStandardInputFile() after the process has started has no | - | ||||||||||||||||||
1405 | effect. | - | ||||||||||||||||||
1406 | - | |||||||||||||||||||
1407 | \sa setStandardOutputFile(), setStandardErrorFile(), | - | ||||||||||||||||||
1408 | setStandardOutputProcess() | - | ||||||||||||||||||
1409 | */ | - | ||||||||||||||||||
1410 | void QProcess::setStandardInputFile(const QString &fileName) | - | ||||||||||||||||||
1411 | { | - | ||||||||||||||||||
1412 | Q_D(QProcess); | - | ||||||||||||||||||
1413 | d->stdinChannel = fileName; | - | ||||||||||||||||||
1414 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
1415 | - | |||||||||||||||||||
1416 | /*! | - | ||||||||||||||||||
1417 | \since 4.2 | - | ||||||||||||||||||
1418 | - | |||||||||||||||||||
1419 | Redirects the process' standard output to the file \a | - | ||||||||||||||||||
1420 | fileName. When the redirection is in place, the standard output | - | ||||||||||||||||||
1421 | read channel is closed: reading from it using read() will always | - | ||||||||||||||||||
1422 | fail, as will readAllStandardOutput(). | - | ||||||||||||||||||
1423 | - | |||||||||||||||||||
1424 | To discard all standard output from the process, pass nullDevice() | - | ||||||||||||||||||
1425 | here. This is more efficient than simply never reading the standard | - | ||||||||||||||||||
1426 | output, as no QProcess buffers are filled. | - | ||||||||||||||||||
1427 | - | |||||||||||||||||||
1428 | If the file \a fileName doesn't exist at the moment start() is | - | ||||||||||||||||||
1429 | called, it will be created. If it cannot be created, the starting | - | ||||||||||||||||||
1430 | will fail. | - | ||||||||||||||||||
1431 | - | |||||||||||||||||||
1432 | If the file exists and \a mode is QIODevice::Truncate, the file | - | ||||||||||||||||||
1433 | will be truncated. Otherwise (if \a mode is QIODevice::Append), | - | ||||||||||||||||||
1434 | the file will be appended to. | - | ||||||||||||||||||
1435 | - | |||||||||||||||||||
1436 | Calling setStandardOutputFile() after the process has started has | - | ||||||||||||||||||
1437 | no effect. | - | ||||||||||||||||||
1438 | - | |||||||||||||||||||
1439 | \sa setStandardInputFile(), setStandardErrorFile(), | - | ||||||||||||||||||
1440 | setStandardOutputProcess() | - | ||||||||||||||||||
1441 | */ | - | ||||||||||||||||||
1442 | void QProcess::setStandardOutputFile(const QString &fileName, OpenMode mode) | - | ||||||||||||||||||
1443 | { | - | ||||||||||||||||||
1444 | Q_ASSERT(mode == Append || mode == Truncate); | - | ||||||||||||||||||
1445 | Q_D(QProcess); | - | ||||||||||||||||||
1446 | - | |||||||||||||||||||
1447 | d->stdoutChannel = fileName; | - | ||||||||||||||||||
1448 | d->stdoutChannel.append = mode == Append; | - | ||||||||||||||||||
1449 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||||||||
1450 | - | |||||||||||||||||||
1451 | /*! | - | ||||||||||||||||||
1452 | \since 4.2 | - | ||||||||||||||||||
1453 | - | |||||||||||||||||||
1454 | Redirects the process' standard error to the file \a | - | ||||||||||||||||||
1455 | fileName. When the redirection is in place, the standard error | - | ||||||||||||||||||
1456 | read channel is closed: reading from it using read() will always | - | ||||||||||||||||||
1457 | fail, as will readAllStandardError(). The file will be appended to | - | ||||||||||||||||||
1458 | if \a mode is Append, otherwise, it will be truncated. | - | ||||||||||||||||||
1459 | - | |||||||||||||||||||
1460 | See setStandardOutputFile() for more information on how the file | - | ||||||||||||||||||
1461 | is opened. | - | ||||||||||||||||||
1462 | - | |||||||||||||||||||
1463 | Note: if setProcessChannelMode() was called with an argument of | - | ||||||||||||||||||
1464 | QProcess::MergedChannels, this function has no effect. | - | ||||||||||||||||||
1465 | - | |||||||||||||||||||
1466 | \sa setStandardInputFile(), setStandardOutputFile(), | - | ||||||||||||||||||
1467 | setStandardOutputProcess() | - | ||||||||||||||||||
1468 | */ | - | ||||||||||||||||||
1469 | void QProcess::setStandardErrorFile(const QString &fileName, OpenMode mode) | - | ||||||||||||||||||
1470 | { | - | ||||||||||||||||||
1471 | Q_ASSERT(mode == Append || mode == Truncate); | - | ||||||||||||||||||
1472 | Q_D(QProcess); | - | ||||||||||||||||||
1473 | - | |||||||||||||||||||
1474 | d->stderrChannel = fileName; | - | ||||||||||||||||||
1475 | d->stderrChannel.append = mode == Append; | - | ||||||||||||||||||
1476 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
1477 | - | |||||||||||||||||||
1478 | /*! | - | ||||||||||||||||||
1479 | \since 4.2 | - | ||||||||||||||||||
1480 | - | |||||||||||||||||||
1481 | Pipes the standard output stream of this process to the \a | - | ||||||||||||||||||
1482 | destination process' standard input. | - | ||||||||||||||||||
1483 | - | |||||||||||||||||||
1484 | The following shell command: | - | ||||||||||||||||||
1485 | \snippet code/src_corelib_io_qprocess.cpp 2 | - | ||||||||||||||||||
1486 | - | |||||||||||||||||||
1487 | Can be accomplished with QProcess with the following code: | - | ||||||||||||||||||
1488 | \snippet code/src_corelib_io_qprocess.cpp 3 | - | ||||||||||||||||||
1489 | */ | - | ||||||||||||||||||
1490 | void QProcess::setStandardOutputProcess(QProcess *destination) | - | ||||||||||||||||||
1491 | { | - | ||||||||||||||||||
1492 | QProcessPrivate *dfrom = d_func(); | - | ||||||||||||||||||
1493 | QProcessPrivate *dto = destination->d_func(); | - | ||||||||||||||||||
1494 | dfrom->stdoutChannel.pipeTo(dto); | - | ||||||||||||||||||
1495 | dto->stdinChannel.pipeFrom(dfrom); | - | ||||||||||||||||||
1496 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||||||||
1497 | - | |||||||||||||||||||
1498 | #if defined(Q_OS_WIN) | - | ||||||||||||||||||
1499 | - | |||||||||||||||||||
1500 | /*! | - | ||||||||||||||||||
1501 | \since 4.7 | - | ||||||||||||||||||
1502 | - | |||||||||||||||||||
1503 | Returns the additional native command line arguments for the program. | - | ||||||||||||||||||
1504 | - | |||||||||||||||||||
1505 | \note This function is available only on the Windows platform. | - | ||||||||||||||||||
1506 | - | |||||||||||||||||||
1507 | \sa setNativeArguments() | - | ||||||||||||||||||
1508 | */ | - | ||||||||||||||||||
1509 | QString QProcess::nativeArguments() const | - | ||||||||||||||||||
1510 | { | - | ||||||||||||||||||
1511 | Q_D(const QProcess); | - | ||||||||||||||||||
1512 | return d->nativeArguments; | - | ||||||||||||||||||
1513 | } | - | ||||||||||||||||||
1514 | - | |||||||||||||||||||
1515 | /*! | - | ||||||||||||||||||
1516 | \since 4.7 | - | ||||||||||||||||||
1517 | \overload | - | ||||||||||||||||||
1518 | - | |||||||||||||||||||
1519 | Sets additional native command line \a arguments for the program. | - | ||||||||||||||||||
1520 | - | |||||||||||||||||||
1521 | On operating systems where the system API for passing command line | - | ||||||||||||||||||
1522 | \a arguments to a subprocess natively uses a single string, one can | - | ||||||||||||||||||
1523 | conceive command lines which cannot be passed via QProcess's portable | - | ||||||||||||||||||
1524 | list-based API. In such cases this function must be used to set a | - | ||||||||||||||||||
1525 | string which is \e appended to the string composed from the usual | - | ||||||||||||||||||
1526 | argument list, with a delimiting space. | - | ||||||||||||||||||
1527 | - | |||||||||||||||||||
1528 | \note This function is available only on the Windows platform. | - | ||||||||||||||||||
1529 | - | |||||||||||||||||||
1530 | \sa nativeArguments() | - | ||||||||||||||||||
1531 | */ | - | ||||||||||||||||||
1532 | void QProcess::setNativeArguments(const QString &arguments) | - | ||||||||||||||||||
1533 | { | - | ||||||||||||||||||
1534 | Q_D(QProcess); | - | ||||||||||||||||||
1535 | d->nativeArguments = arguments; | - | ||||||||||||||||||
1536 | } | - | ||||||||||||||||||
1537 | - | |||||||||||||||||||
1538 | #endif | - | ||||||||||||||||||
1539 | - | |||||||||||||||||||
1540 | /*! | - | ||||||||||||||||||
1541 | If QProcess has been assigned a working directory, this function returns | - | ||||||||||||||||||
1542 | the working directory that the QProcess will enter before the program has | - | ||||||||||||||||||
1543 | started. Otherwise, (i.e., no directory has been assigned,) an empty | - | ||||||||||||||||||
1544 | string is returned, and QProcess will use the application's current | - | ||||||||||||||||||
1545 | working directory instead. | - | ||||||||||||||||||
1546 | - | |||||||||||||||||||
1547 | \sa setWorkingDirectory() | - | ||||||||||||||||||
1548 | */ | - | ||||||||||||||||||
1549 | QString QProcess::workingDirectory() const | - | ||||||||||||||||||
1550 | { | - | ||||||||||||||||||
1551 | Q_D(const QProcess); | - | ||||||||||||||||||
1552 | return d->workingDirectory; executed 1 time by 1 test: return d->workingDirectory; Executed by:
| 1 | ||||||||||||||||||
1553 | } | - | ||||||||||||||||||
1554 | - | |||||||||||||||||||
1555 | /*! | - | ||||||||||||||||||
1556 | Sets the working directory to \a dir. QProcess will start the | - | ||||||||||||||||||
1557 | process in this directory. The default behavior is to start the | - | ||||||||||||||||||
1558 | process in the working directory of the calling process. | - | ||||||||||||||||||
1559 | - | |||||||||||||||||||
1560 | \note On QNX, this may cause all application threads to | - | ||||||||||||||||||
1561 | temporarily freeze. | - | ||||||||||||||||||
1562 | - | |||||||||||||||||||
1563 | \sa workingDirectory(), start() | - | ||||||||||||||||||
1564 | */ | - | ||||||||||||||||||
1565 | void QProcess::setWorkingDirectory(const QString &dir) | - | ||||||||||||||||||
1566 | { | - | ||||||||||||||||||
1567 | Q_D(QProcess); | - | ||||||||||||||||||
1568 | d->workingDirectory = dir; | - | ||||||||||||||||||
1569 | } executed 87 times by 4 tests: end of block Executed by:
| 87 | ||||||||||||||||||
1570 | - | |||||||||||||||||||
1571 | - | |||||||||||||||||||
1572 | /*! | - | ||||||||||||||||||
1573 | \deprecated | - | ||||||||||||||||||
1574 | Use processId() instead. | - | ||||||||||||||||||
1575 | - | |||||||||||||||||||
1576 | Returns the native process identifier for the running process, if | - | ||||||||||||||||||
1577 | available. If no process is currently running, \c 0 is returned. | - | ||||||||||||||||||
1578 | - | |||||||||||||||||||
1579 | \note Unlike \l processId(), pid() returns an integer on Unix and a pointer on Windows. | - | ||||||||||||||||||
1580 | - | |||||||||||||||||||
1581 | \sa Q_PID, processId() | - | ||||||||||||||||||
1582 | */ | - | ||||||||||||||||||
1583 | Q_PID QProcess::pid() const // ### Qt 6 remove or rename this method to processInformation() | - | ||||||||||||||||||
1584 | { | - | ||||||||||||||||||
1585 | Q_D(const QProcess); | - | ||||||||||||||||||
1586 | return d->pid; executed 1 time by 1 test: return d->pid; Executed by:
| 1 | ||||||||||||||||||
1587 | } | - | ||||||||||||||||||
1588 | - | |||||||||||||||||||
1589 | /*! | - | ||||||||||||||||||
1590 | \since 5.3 | - | ||||||||||||||||||
1591 | - | |||||||||||||||||||
1592 | Returns the native process identifier for the running process, if | - | ||||||||||||||||||
1593 | available. If no process is currently running, \c 0 is returned. | - | ||||||||||||||||||
1594 | */ | - | ||||||||||||||||||
1595 | qint64 QProcess::processId() const | - | ||||||||||||||||||
1596 | { | - | ||||||||||||||||||
1597 | Q_D(const QProcess); | - | ||||||||||||||||||
1598 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1599 | return d->pid ? d->pid->dwProcessId : 0; | - | ||||||||||||||||||
1600 | #else | - | ||||||||||||||||||
1601 | return d->pid; executed 15 times by 2 tests: return d->pid; Executed by:
| 15 | ||||||||||||||||||
1602 | #endif | - | ||||||||||||||||||
1603 | } | - | ||||||||||||||||||
1604 | - | |||||||||||||||||||
1605 | /*! \reimp | - | ||||||||||||||||||
1606 | - | |||||||||||||||||||
1607 | This function operates on the current read channel. | - | ||||||||||||||||||
1608 | - | |||||||||||||||||||
1609 | \sa readChannel(), setReadChannel() | - | ||||||||||||||||||
1610 | */ | - | ||||||||||||||||||
1611 | bool QProcess::canReadLine() const | - | ||||||||||||||||||
1612 | { | - | ||||||||||||||||||
1613 | Q_D(const QProcess); | - | ||||||||||||||||||
1614 | const QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError)
| 0-1 | ||||||||||||||||||
1615 | ? &d->stderrChannel.buffer | - | ||||||||||||||||||
1616 | : &d->stdoutChannel.buffer; | - | ||||||||||||||||||
1617 | return readBuffer->canReadLine() || QIODevice::canReadLine(); executed 1 time by 1 test: return readBuffer->canReadLine() || QIODevice::canReadLine(); Executed by:
| 0-1 | ||||||||||||||||||
1618 | } | - | ||||||||||||||||||
1619 | - | |||||||||||||||||||
1620 | /*! | - | ||||||||||||||||||
1621 | Closes all communication with the process and kills it. After calling this | - | ||||||||||||||||||
1622 | function, QProcess will no longer emit readyRead(), and data can no | - | ||||||||||||||||||
1623 | longer be read or written. | - | ||||||||||||||||||
1624 | */ | - | ||||||||||||||||||
1625 | void QProcess::close() | - | ||||||||||||||||||
1626 | { | - | ||||||||||||||||||
1627 | emit aboutToClose(); | - | ||||||||||||||||||
1628 | while (waitForBytesWritten(-1))
| 0-5 | ||||||||||||||||||
1629 | ; never executed: ; | 0 | ||||||||||||||||||
1630 | kill(); | - | ||||||||||||||||||
1631 | waitForFinished(-1); | - | ||||||||||||||||||
1632 | QIODevice::close(); | - | ||||||||||||||||||
1633 | } executed 5 times by 5 tests: end of block Executed by:
| 5 | ||||||||||||||||||
1634 | - | |||||||||||||||||||
1635 | /*! \reimp | - | ||||||||||||||||||
1636 | - | |||||||||||||||||||
1637 | Returns \c true if the process is not running, and no more data is available | - | ||||||||||||||||||
1638 | for reading; otherwise returns \c false. | - | ||||||||||||||||||
1639 | */ | - | ||||||||||||||||||
1640 | bool QProcess::atEnd() const | - | ||||||||||||||||||
1641 | { | - | ||||||||||||||||||
1642 | Q_D(const QProcess); | - | ||||||||||||||||||
1643 | const QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError)
| 0-61 | ||||||||||||||||||
1644 | ? &d->stderrChannel.buffer | - | ||||||||||||||||||
1645 | : &d->stdoutChannel.buffer; | - | ||||||||||||||||||
1646 | return QIODevice::atEnd() && (!isOpen() || readBuffer->isEmpty()); executed 61 times by 3 tests: return QIODevice::atEnd() && (!isOpen() || readBuffer->isEmpty()); Executed by:
| 0-61 | ||||||||||||||||||
1647 | } | - | ||||||||||||||||||
1648 | - | |||||||||||||||||||
1649 | /*! \reimp | - | ||||||||||||||||||
1650 | */ | - | ||||||||||||||||||
1651 | bool QProcess::isSequential() const | - | ||||||||||||||||||
1652 | { | - | ||||||||||||||||||
1653 | return true; executed 1508 times by 20 tests: return true; Executed by:
| 1508 | ||||||||||||||||||
1654 | } | - | ||||||||||||||||||
1655 | - | |||||||||||||||||||
1656 | /*! \reimp | - | ||||||||||||||||||
1657 | */ | - | ||||||||||||||||||
1658 | qint64 QProcess::bytesAvailable() const | - | ||||||||||||||||||
1659 | { | - | ||||||||||||||||||
1660 | Q_D(const QProcess); | - | ||||||||||||||||||
1661 | const QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError)
| 36-871 | ||||||||||||||||||
1662 | ? &d->stderrChannel.buffer | - | ||||||||||||||||||
1663 | : &d->stdoutChannel.buffer; | - | ||||||||||||||||||
1664 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1665 | qDebug("QProcess::bytesAvailable() == %i (%s)", readBuffer->size(), | - | ||||||||||||||||||
1666 | (d->processChannel == QProcess::StandardError) ? "stderr" : "stdout"); | - | ||||||||||||||||||
1667 | #endif | - | ||||||||||||||||||
1668 | return readBuffer->size() + QIODevice::bytesAvailable(); executed 907 times by 3 tests: return readBuffer->size() + QIODevice::bytesAvailable(); Executed by:
| 907 | ||||||||||||||||||
1669 | } | - | ||||||||||||||||||
1670 | - | |||||||||||||||||||
1671 | /*! \reimp | - | ||||||||||||||||||
1672 | */ | - | ||||||||||||||||||
1673 | qint64 QProcess::bytesToWrite() const | - | ||||||||||||||||||
1674 | { | - | ||||||||||||||||||
1675 | Q_D(const QProcess); | - | ||||||||||||||||||
1676 | qint64 size = d->stdinChannel.buffer.size(); | - | ||||||||||||||||||
1677 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1678 | size += d->pipeWriterBytesToWrite(); | - | ||||||||||||||||||
1679 | #endif | - | ||||||||||||||||||
1680 | return size; executed 288 times by 2 tests: return size; Executed by:
| 288 | ||||||||||||||||||
1681 | } | - | ||||||||||||||||||
1682 | - | |||||||||||||||||||
1683 | /*! | - | ||||||||||||||||||
1684 | Returns the type of error that occurred last. | - | ||||||||||||||||||
1685 | - | |||||||||||||||||||
1686 | \sa state() | - | ||||||||||||||||||
1687 | */ | - | ||||||||||||||||||
1688 | QProcess::ProcessError QProcess::error() const | - | ||||||||||||||||||
1689 | { | - | ||||||||||||||||||
1690 | Q_D(const QProcess); | - | ||||||||||||||||||
1691 | return d->processError; executed 774 times by 4 tests: return d->processError; Executed by:
| 774 | ||||||||||||||||||
1692 | } | - | ||||||||||||||||||
1693 | - | |||||||||||||||||||
1694 | /*! | - | ||||||||||||||||||
1695 | Returns the current state of the process. | - | ||||||||||||||||||
1696 | - | |||||||||||||||||||
1697 | \sa stateChanged(), error() | - | ||||||||||||||||||
1698 | */ | - | ||||||||||||||||||
1699 | QProcess::ProcessState QProcess::state() const | - | ||||||||||||||||||
1700 | { | - | ||||||||||||||||||
1701 | Q_D(const QProcess); | - | ||||||||||||||||||
1702 | return d->processState; executed 92 times by 5 tests: return d->processState; Executed by:
| 92 | ||||||||||||||||||
1703 | } | - | ||||||||||||||||||
1704 | - | |||||||||||||||||||
1705 | /*! | - | ||||||||||||||||||
1706 | \deprecated | - | ||||||||||||||||||
1707 | Sets the environment that QProcess will pass to the child process. | - | ||||||||||||||||||
1708 | The parameter \a environment is a list of key=value pairs. | - | ||||||||||||||||||
1709 | - | |||||||||||||||||||
1710 | For example, the following code adds the environment variable \c{TMPDIR}: | - | ||||||||||||||||||
1711 | - | |||||||||||||||||||
1712 | \snippet qprocess-environment/main.cpp 0 | - | ||||||||||||||||||
1713 | - | |||||||||||||||||||
1714 | \note This function is less efficient than the setProcessEnvironment() | - | ||||||||||||||||||
1715 | function. | - | ||||||||||||||||||
1716 | - | |||||||||||||||||||
1717 | \sa environment(), setProcessEnvironment(), systemEnvironment() | - | ||||||||||||||||||
1718 | */ | - | ||||||||||||||||||
1719 | void QProcess::setEnvironment(const QStringList &environment) | - | ||||||||||||||||||
1720 | { | - | ||||||||||||||||||
1721 | setProcessEnvironment(QProcessEnvironmentPrivate::fromList(environment)); | - | ||||||||||||||||||
1722 | } executed 137 times by 4 tests: end of block Executed by:
| 137 | ||||||||||||||||||
1723 | - | |||||||||||||||||||
1724 | /*! | - | ||||||||||||||||||
1725 | \deprecated | - | ||||||||||||||||||
1726 | Returns the environment that QProcess will pass to its child | - | ||||||||||||||||||
1727 | process, or an empty QStringList if no environment has been set | - | ||||||||||||||||||
1728 | using setEnvironment(). If no environment has been set, the | - | ||||||||||||||||||
1729 | environment of the calling process will be used. | - | ||||||||||||||||||
1730 | - | |||||||||||||||||||
1731 | \note The environment settings are ignored on Windows CE, | - | ||||||||||||||||||
1732 | as there is no concept of an environment. | - | ||||||||||||||||||
1733 | - | |||||||||||||||||||
1734 | \sa processEnvironment(), setEnvironment(), systemEnvironment() | - | ||||||||||||||||||
1735 | */ | - | ||||||||||||||||||
1736 | QStringList QProcess::environment() const | - | ||||||||||||||||||
1737 | { | - | ||||||||||||||||||
1738 | Q_D(const QProcess); | - | ||||||||||||||||||
1739 | return d->environment.toStringList(); executed 1 time by 1 test: return d->environment.toStringList(); Executed by:
| 1 | ||||||||||||||||||
1740 | } | - | ||||||||||||||||||
1741 | - | |||||||||||||||||||
1742 | /*! | - | ||||||||||||||||||
1743 | \since 4.6 | - | ||||||||||||||||||
1744 | Sets the \a environment that QProcess will pass to the child process. | - | ||||||||||||||||||
1745 | - | |||||||||||||||||||
1746 | For example, the following code adds the environment variable \c{TMPDIR}: | - | ||||||||||||||||||
1747 | - | |||||||||||||||||||
1748 | \snippet qprocess-environment/main.cpp 1 | - | ||||||||||||||||||
1749 | - | |||||||||||||||||||
1750 | Note how, on Windows, environment variable names are case-insensitive. | - | ||||||||||||||||||
1751 | - | |||||||||||||||||||
1752 | \sa processEnvironment(), QProcessEnvironment::systemEnvironment(), setEnvironment() | - | ||||||||||||||||||
1753 | */ | - | ||||||||||||||||||
1754 | void QProcess::setProcessEnvironment(const QProcessEnvironment &environment) | - | ||||||||||||||||||
1755 | { | - | ||||||||||||||||||
1756 | Q_D(QProcess); | - | ||||||||||||||||||
1757 | d->environment = environment; | - | ||||||||||||||||||
1758 | } executed 818 times by 6 tests: end of block Executed by:
| 818 | ||||||||||||||||||
1759 | - | |||||||||||||||||||
1760 | /*! | - | ||||||||||||||||||
1761 | \since 4.6 | - | ||||||||||||||||||
1762 | Returns the environment that QProcess will pass to its child | - | ||||||||||||||||||
1763 | process, or an empty object if no environment has been set using | - | ||||||||||||||||||
1764 | setEnvironment() or setProcessEnvironment(). If no environment has | - | ||||||||||||||||||
1765 | been set, the environment of the calling process will be used. | - | ||||||||||||||||||
1766 | - | |||||||||||||||||||
1767 | \note The environment settings are ignored on Windows CE, | - | ||||||||||||||||||
1768 | as there is no concept of an environment. | - | ||||||||||||||||||
1769 | - | |||||||||||||||||||
1770 | \sa setProcessEnvironment(), setEnvironment(), QProcessEnvironment::isEmpty() | - | ||||||||||||||||||
1771 | */ | - | ||||||||||||||||||
1772 | QProcessEnvironment QProcess::processEnvironment() const | - | ||||||||||||||||||
1773 | { | - | ||||||||||||||||||
1774 | Q_D(const QProcess); | - | ||||||||||||||||||
1775 | return d->environment; never executed: return d->environment; | 0 | ||||||||||||||||||
1776 | } | - | ||||||||||||||||||
1777 | - | |||||||||||||||||||
1778 | /*! | - | ||||||||||||||||||
1779 | Blocks until the process has started and the started() signal has | - | ||||||||||||||||||
1780 | been emitted, or until \a msecs milliseconds have passed. | - | ||||||||||||||||||
1781 | - | |||||||||||||||||||
1782 | Returns \c true if the process was started successfully; otherwise | - | ||||||||||||||||||
1783 | returns \c false (if the operation timed out or if an error | - | ||||||||||||||||||
1784 | occurred). | - | ||||||||||||||||||
1785 | - | |||||||||||||||||||
1786 | This function can operate without an event loop. It is | - | ||||||||||||||||||
1787 | useful when writing non-GUI applications and when performing | - | ||||||||||||||||||
1788 | I/O operations in a non-GUI thread. | - | ||||||||||||||||||
1789 | - | |||||||||||||||||||
1790 | \warning Calling this function from the main (GUI) thread | - | ||||||||||||||||||
1791 | might cause your user interface to freeze. | - | ||||||||||||||||||
1792 | - | |||||||||||||||||||
1793 | If msecs is -1, this function will not time out. | - | ||||||||||||||||||
1794 | - | |||||||||||||||||||
1795 | \note On some UNIX operating systems, this function may return true but | - | ||||||||||||||||||
1796 | the process may later report a QProcess::FailedToStart error. | - | ||||||||||||||||||
1797 | - | |||||||||||||||||||
1798 | \sa started(), waitForReadyRead(), waitForBytesWritten(), waitForFinished() | - | ||||||||||||||||||
1799 | */ | - | ||||||||||||||||||
1800 | bool QProcess::waitForStarted(int msecs) | - | ||||||||||||||||||
1801 | { | - | ||||||||||||||||||
1802 | Q_D(QProcess); | - | ||||||||||||||||||
1803 | if (d->processState == QProcess::Starting)
| 4-2246 | ||||||||||||||||||
1804 | return d->waitForStarted(msecs); executed 2246 times by 36 tests: return d->waitForStarted(msecs); Executed by:
| 2246 | ||||||||||||||||||
1805 | - | |||||||||||||||||||
1806 | return d->processState == QProcess::Running; executed 4 times by 1 test: return d->processState == QProcess::Running; Executed by:
| 4 | ||||||||||||||||||
1807 | } | - | ||||||||||||||||||
1808 | - | |||||||||||||||||||
1809 | /*! \reimp | - | ||||||||||||||||||
1810 | */ | - | ||||||||||||||||||
1811 | bool QProcess::waitForReadyRead(int msecs) | - | ||||||||||||||||||
1812 | { | - | ||||||||||||||||||
1813 | Q_D(QProcess); | - | ||||||||||||||||||
1814 | - | |||||||||||||||||||
1815 | if (d->processState == QProcess::NotRunning)
| 1-744 | ||||||||||||||||||
1816 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||||||||
1817 | if (d->processChannel == QProcess::StandardOutput && d->stdoutChannel.closed)
| 5-716 | ||||||||||||||||||
1818 | return false; executed 5 times by 1 test: return false; Executed by:
| 5 | ||||||||||||||||||
1819 | if (d->processChannel == QProcess::StandardError && d->stderrChannel.closed)
| 5-711 | ||||||||||||||||||
1820 | return false; executed 5 times by 1 test: return false; Executed by:
| 5 | ||||||||||||||||||
1821 | return d->waitForReadyRead(msecs); executed 734 times by 9 tests: return d->waitForReadyRead(msecs); Executed by:
| 734 | ||||||||||||||||||
1822 | } | - | ||||||||||||||||||
1823 | - | |||||||||||||||||||
1824 | /*! \reimp | - | ||||||||||||||||||
1825 | */ | - | ||||||||||||||||||
1826 | bool QProcess::waitForBytesWritten(int msecs) | - | ||||||||||||||||||
1827 | { | - | ||||||||||||||||||
1828 | Q_D(QProcess); | - | ||||||||||||||||||
1829 | if (d->processState == QProcess::NotRunning)
| 4-252 | ||||||||||||||||||
1830 | return false; executed 4 times by 4 tests: return false; Executed by:
| 4 | ||||||||||||||||||
1831 | if (d->processState == QProcess::Starting) {
| 105-147 | ||||||||||||||||||
1832 | QElapsedTimer stopWatch; | - | ||||||||||||||||||
1833 | stopWatch.start(); | - | ||||||||||||||||||
1834 | bool started = waitForStarted(msecs); | - | ||||||||||||||||||
1835 | if (!started)
| 5-100 | ||||||||||||||||||
1836 | return false; executed 100 times by 1 test: return false; Executed by:
| 100 | ||||||||||||||||||
1837 | msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); | - | ||||||||||||||||||
1838 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||||||||
1839 | - | |||||||||||||||||||
1840 | return d->waitForBytesWritten(msecs); executed 152 times by 2 tests: return d->waitForBytesWritten(msecs); Executed by:
| 152 | ||||||||||||||||||
1841 | } | - | ||||||||||||||||||
1842 | - | |||||||||||||||||||
1843 | /*! | - | ||||||||||||||||||
1844 | Blocks until the process has finished and the finished() signal | - | ||||||||||||||||||
1845 | has been emitted, or until \a msecs milliseconds have passed. | - | ||||||||||||||||||
1846 | - | |||||||||||||||||||
1847 | Returns \c true if the process finished; otherwise returns \c false (if | - | ||||||||||||||||||
1848 | the operation timed out, if an error occurred, or if this QProcess | - | ||||||||||||||||||
1849 | is already finished). | - | ||||||||||||||||||
1850 | - | |||||||||||||||||||
1851 | This function can operate without an event loop. It is | - | ||||||||||||||||||
1852 | useful when writing non-GUI applications and when performing | - | ||||||||||||||||||
1853 | I/O operations in a non-GUI thread. | - | ||||||||||||||||||
1854 | - | |||||||||||||||||||
1855 | \warning Calling this function from the main (GUI) thread | - | ||||||||||||||||||
1856 | might cause your user interface to freeze. | - | ||||||||||||||||||
1857 | - | |||||||||||||||||||
1858 | If msecs is -1, this function will not time out. | - | ||||||||||||||||||
1859 | - | |||||||||||||||||||
1860 | \sa finished(), waitForStarted(), waitForReadyRead(), waitForBytesWritten() | - | ||||||||||||||||||
1861 | */ | - | ||||||||||||||||||
1862 | bool QProcess::waitForFinished(int msecs) | - | ||||||||||||||||||
1863 | { | - | ||||||||||||||||||
1864 | Q_D(QProcess); | - | ||||||||||||||||||
1865 | if (d->processState == QProcess::NotRunning)
| 5-1823 | ||||||||||||||||||
1866 | return false; executed 5 times by 5 tests: return false; Executed by:
| 5 | ||||||||||||||||||
1867 | if (d->processState == QProcess::Starting) {
| 585-1238 | ||||||||||||||||||
1868 | QElapsedTimer stopWatch; | - | ||||||||||||||||||
1869 | stopWatch.start(); | - | ||||||||||||||||||
1870 | bool started = waitForStarted(msecs); | - | ||||||||||||||||||
1871 | if (!started)
| 117-468 | ||||||||||||||||||
1872 | return false; executed 117 times by 7 tests: return false; Executed by:
| 117 | ||||||||||||||||||
1873 | msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); | - | ||||||||||||||||||
1874 | } executed 468 times by 9 tests: end of block Executed by:
| 468 | ||||||||||||||||||
1875 | - | |||||||||||||||||||
1876 | return d->waitForFinished(msecs); executed 1706 times by 28 tests: return d->waitForFinished(msecs); Executed by:
| 1706 | ||||||||||||||||||
1877 | } | - | ||||||||||||||||||
1878 | - | |||||||||||||||||||
1879 | /*! | - | ||||||||||||||||||
1880 | Sets the current state of the QProcess to the \a state specified. | - | ||||||||||||||||||
1881 | - | |||||||||||||||||||
1882 | \sa state() | - | ||||||||||||||||||
1883 | */ | - | ||||||||||||||||||
1884 | void QProcess::setProcessState(ProcessState state) | - | ||||||||||||||||||
1885 | { | - | ||||||||||||||||||
1886 | Q_D(QProcess); | - | ||||||||||||||||||
1887 | if (d->processState == state)
| 2439-6715 | ||||||||||||||||||
1888 | return; executed 2439 times by 39 tests: return; Executed by:
| 2439 | ||||||||||||||||||
1889 | d->processState = state; | - | ||||||||||||||||||
1890 | emit stateChanged(state, QPrivateSignal()); | - | ||||||||||||||||||
1891 | } executed 6715 times by 38 tests: end of block Executed by:
| 6715 | ||||||||||||||||||
1892 | - | |||||||||||||||||||
1893 | /*! | - | ||||||||||||||||||
1894 | This function is called in the child process context just before the | - | ||||||||||||||||||
1895 | program is executed on Unix or \macos (i.e., after \c fork(), but before | - | ||||||||||||||||||
1896 | \c execve()). Reimplement this function to do last minute initialization | - | ||||||||||||||||||
1897 | of the child process. Example: | - | ||||||||||||||||||
1898 | - | |||||||||||||||||||
1899 | \snippet code/src_corelib_io_qprocess.cpp 4 | - | ||||||||||||||||||
1900 | - | |||||||||||||||||||
1901 | You cannot exit the process (by calling exit(), for instance) from | - | ||||||||||||||||||
1902 | this function. If you need to stop the program before it starts | - | ||||||||||||||||||
1903 | execution, your workaround is to emit finished() and then call | - | ||||||||||||||||||
1904 | exit(). | - | ||||||||||||||||||
1905 | - | |||||||||||||||||||
1906 | \warning This function is called by QProcess on Unix and \macos | - | ||||||||||||||||||
1907 | only. On Windows and QNX, it is not called. | - | ||||||||||||||||||
1908 | */ | - | ||||||||||||||||||
1909 | void QProcess::setupChildProcess() | - | ||||||||||||||||||
1910 | { | - | ||||||||||||||||||
1911 | } | - | ||||||||||||||||||
1912 | - | |||||||||||||||||||
1913 | /*! \reimp | - | ||||||||||||||||||
1914 | */ | - | ||||||||||||||||||
1915 | qint64 QProcess::readData(char *data, qint64 maxlen) | - | ||||||||||||||||||
1916 | { | - | ||||||||||||||||||
1917 | Q_D(QProcess); | - | ||||||||||||||||||
1918 | if (!maxlen)
| 1-4190 | ||||||||||||||||||
1919 | return 0; executed 1 time by 1 test: return 0; Executed by:
| 1 | ||||||||||||||||||
1920 | QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError)
| 1758-2432 | ||||||||||||||||||
1921 | ? &d->stderrChannel.buffer | - | ||||||||||||||||||
1922 | : &d->stdoutChannel.buffer; | - | ||||||||||||||||||
1923 | - | |||||||||||||||||||
1924 | if (maxlen == 1 && !readBuffer->isEmpty()) {
| 0-4190 | ||||||||||||||||||
1925 | int c = readBuffer->getChar(); | - | ||||||||||||||||||
1926 | if (c == -1) {
| 0 | ||||||||||||||||||
1927 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1928 | qDebug("QProcess::readData(%p \"%s\", %d) == -1", | - | ||||||||||||||||||
1929 | data, qt_prettyDebug(data, 1, maxlen).constData(), 1); | - | ||||||||||||||||||
1930 | #endif | - | ||||||||||||||||||
1931 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
1932 | } | - | ||||||||||||||||||
1933 | *data = (char) c; | - | ||||||||||||||||||
1934 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1935 | qDebug("QProcess::readData(%p \"%s\", %d) == 1", | - | ||||||||||||||||||
1936 | data, qt_prettyDebug(data, 1, maxlen).constData(), 1); | - | ||||||||||||||||||
1937 | #endif | - | ||||||||||||||||||
1938 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
1939 | } | - | ||||||||||||||||||
1940 | - | |||||||||||||||||||
1941 | qint64 bytesToRead = qMin(readBuffer->size(), maxlen); | - | ||||||||||||||||||
1942 | qint64 readSoFar = 0; | - | ||||||||||||||||||
1943 | while (readSoFar < bytesToRead) {
| 1641-4190 | ||||||||||||||||||
1944 | const char *ptr = readBuffer->readPointer(); | - | ||||||||||||||||||
1945 | qint64 bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar, | - | ||||||||||||||||||
1946 | readBuffer->nextDataBlockSize()); | - | ||||||||||||||||||
1947 | memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock); | - | ||||||||||||||||||
1948 | readSoFar += bytesToReadFromThisBlock; | - | ||||||||||||||||||
1949 | readBuffer->free(bytesToReadFromThisBlock); | - | ||||||||||||||||||
1950 | } executed 1641 times by 17 tests: end of block Executed by:
| 1641 | ||||||||||||||||||
1951 | - | |||||||||||||||||||
1952 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1953 | qDebug("QProcess::readData(%p \"%s\", %lld) == %lld", | - | ||||||||||||||||||
1954 | data, qt_prettyDebug(data, readSoFar, 16).constData(), maxlen, readSoFar); | - | ||||||||||||||||||
1955 | #endif | - | ||||||||||||||||||
1956 | if (!readSoFar && d->processState == QProcess::NotRunning)
| 355-2810 | ||||||||||||||||||
1957 | return -1; // EOF executed 2455 times by 16 tests: return -1; Executed by:
| 2455 | ||||||||||||||||||
1958 | return readSoFar; executed 1735 times by 17 tests: return readSoFar; Executed by:
| 1735 | ||||||||||||||||||
1959 | } | - | ||||||||||||||||||
1960 | - | |||||||||||||||||||
1961 | /*! \reimp | - | ||||||||||||||||||
1962 | */ | - | ||||||||||||||||||
1963 | qint64 QProcess::writeData(const char *data, qint64 len) | - | ||||||||||||||||||
1964 | { | - | ||||||||||||||||||
1965 | Q_D(QProcess); | - | ||||||||||||||||||
1966 | - | |||||||||||||||||||
1967 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
1968 | Q_UNUSED(data); | - | ||||||||||||||||||
1969 | Q_UNUSED(len); | - | ||||||||||||||||||
1970 | d->setErrorAndEmit(QProcess::WriteError); | - | ||||||||||||||||||
1971 | return -1; | - | ||||||||||||||||||
1972 | #endif | - | ||||||||||||||||||
1973 | - | |||||||||||||||||||
1974 | if (d->stdinChannel.closed) {
| 0-735 | ||||||||||||||||||
1975 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
1976 | qDebug("QProcess::writeData(%p \"%s\", %lld) == 0 (write channel closing)", | - | ||||||||||||||||||
1977 | data, qt_prettyDebug(data, len, 16).constData(), len); | - | ||||||||||||||||||
1978 | #endif | - | ||||||||||||||||||
1979 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
1980 | } | - | ||||||||||||||||||
1981 | - | |||||||||||||||||||
1982 | #if defined(Q_OS_WIN) | - | ||||||||||||||||||
1983 | if (!d->stdinWriteTrigger) { | - | ||||||||||||||||||
1984 | d->stdinWriteTrigger = new QTimer; | - | ||||||||||||||||||
1985 | d->stdinWriteTrigger->setSingleShot(true); | - | ||||||||||||||||||
1986 | QObjectPrivate::connect(d->stdinWriteTrigger, &QTimer::timeout, | - | ||||||||||||||||||
1987 | d, &QProcessPrivate::_q_canWrite); | - | ||||||||||||||||||
1988 | } | - | ||||||||||||||||||
1989 | #endif | - | ||||||||||||||||||
1990 | - | |||||||||||||||||||
1991 | if (len == 1) {
| 35-700 | ||||||||||||||||||
1992 | d->stdinChannel.buffer.putChar(*data); | - | ||||||||||||||||||
1993 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1994 | if (!d->stdinWriteTrigger->isActive()) | - | ||||||||||||||||||
1995 | d->stdinWriteTrigger->start(); | - | ||||||||||||||||||
1996 | #else | - | ||||||||||||||||||
1997 | if (d->stdinChannel.notifier)
| 0-35 | ||||||||||||||||||
1998 | d->stdinChannel.notifier->setEnabled(true); executed 35 times by 2 tests: d->stdinChannel.notifier->setEnabled(true); Executed by:
| 35 | ||||||||||||||||||
1999 | #endif | - | ||||||||||||||||||
2000 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
2001 | qDebug("QProcess::writeData(%p \"%s\", %lld) == 1 (written to buffer)", | - | ||||||||||||||||||
2002 | data, qt_prettyDebug(data, len, 16).constData(), len); | - | ||||||||||||||||||
2003 | #endif | - | ||||||||||||||||||
2004 | return 1; executed 35 times by 2 tests: return 1; Executed by:
| 35 | ||||||||||||||||||
2005 | } | - | ||||||||||||||||||
2006 | - | |||||||||||||||||||
2007 | char *dest = d->stdinChannel.buffer.reserve(len); | - | ||||||||||||||||||
2008 | memcpy(dest, data, len); | - | ||||||||||||||||||
2009 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
2010 | if (!d->stdinWriteTrigger->isActive()) | - | ||||||||||||||||||
2011 | d->stdinWriteTrigger->start(); | - | ||||||||||||||||||
2012 | #else | - | ||||||||||||||||||
2013 | if (d->stdinChannel.notifier)
| 0-700 | ||||||||||||||||||
2014 | d->stdinChannel.notifier->setEnabled(true); executed 700 times by 6 tests: d->stdinChannel.notifier->setEnabled(true); Executed by:
| 700 | ||||||||||||||||||
2015 | #endif | - | ||||||||||||||||||
2016 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
2017 | qDebug("QProcess::writeData(%p \"%s\", %lld) == %lld (written to buffer)", | - | ||||||||||||||||||
2018 | data, qt_prettyDebug(data, len, 16).constData(), len, len); | - | ||||||||||||||||||
2019 | #endif | - | ||||||||||||||||||
2020 | return len; executed 700 times by 6 tests: return len; Executed by:
| 700 | ||||||||||||||||||
2021 | } | - | ||||||||||||||||||
2022 | - | |||||||||||||||||||
2023 | /*! | - | ||||||||||||||||||
2024 | Regardless of the current read channel, this function returns all | - | ||||||||||||||||||
2025 | data available from the standard output of the process as a | - | ||||||||||||||||||
2026 | QByteArray. | - | ||||||||||||||||||
2027 | - | |||||||||||||||||||
2028 | \sa readyReadStandardOutput(), readAllStandardError(), readChannel(), setReadChannel() | - | ||||||||||||||||||
2029 | */ | - | ||||||||||||||||||
2030 | QByteArray QProcess::readAllStandardOutput() | - | ||||||||||||||||||
2031 | { | - | ||||||||||||||||||
2032 | ProcessChannel tmp = readChannel(); | - | ||||||||||||||||||
2033 | setReadChannel(StandardOutput); | - | ||||||||||||||||||
2034 | QByteArray data = readAll(); | - | ||||||||||||||||||
2035 | setReadChannel(tmp); | - | ||||||||||||||||||
2036 | return data; executed 580 times by 11 tests: return data; Executed by:
| 580 | ||||||||||||||||||
2037 | } | - | ||||||||||||||||||
2038 | - | |||||||||||||||||||
2039 | /*! | - | ||||||||||||||||||
2040 | Regardless of the current read channel, this function returns all | - | ||||||||||||||||||
2041 | data available from the standard error of the process as a | - | ||||||||||||||||||
2042 | QByteArray. | - | ||||||||||||||||||
2043 | - | |||||||||||||||||||
2044 | \sa readyReadStandardError(), readAllStandardOutput(), readChannel(), setReadChannel() | - | ||||||||||||||||||
2045 | */ | - | ||||||||||||||||||
2046 | QByteArray QProcess::readAllStandardError() | - | ||||||||||||||||||
2047 | { | - | ||||||||||||||||||
2048 | ProcessChannel tmp = readChannel(); | - | ||||||||||||||||||
2049 | setReadChannel(StandardError); | - | ||||||||||||||||||
2050 | QByteArray data = readAll(); | - | ||||||||||||||||||
2051 | setReadChannel(tmp); | - | ||||||||||||||||||
2052 | return data; executed 1662 times by 13 tests: return data; Executed by:
| 1662 | ||||||||||||||||||
2053 | } | - | ||||||||||||||||||
2054 | - | |||||||||||||||||||
2055 | /*! | - | ||||||||||||||||||
2056 | Starts the given \a program in a new process, passing the command line | - | ||||||||||||||||||
2057 | arguments in \a arguments. | - | ||||||||||||||||||
2058 | - | |||||||||||||||||||
2059 | The QProcess object will immediately enter the Starting state. If the | - | ||||||||||||||||||
2060 | process starts successfully, QProcess will emit started(); otherwise, | - | ||||||||||||||||||
2061 | errorOccurred() will be emitted. | - | ||||||||||||||||||
2062 | - | |||||||||||||||||||
2063 | \note Processes are started asynchronously, which means the started() | - | ||||||||||||||||||
2064 | and errorOccurred() signals may be delayed. Call waitForStarted() to make | - | ||||||||||||||||||
2065 | sure the process has started (or has failed to start) and those signals | - | ||||||||||||||||||
2066 | have been emitted. | - | ||||||||||||||||||
2067 | - | |||||||||||||||||||
2068 | \note No further splitting of the arguments is performed. | - | ||||||||||||||||||
2069 | - | |||||||||||||||||||
2070 | \b{Windows:} The arguments are quoted and joined into a command line | - | ||||||||||||||||||
2071 | that is compatible with the \c CommandLineToArgvW() Windows function. | - | ||||||||||||||||||
2072 | For programs that have different command line quoting requirements, | - | ||||||||||||||||||
2073 | you need to use setNativeArguments(). One notable program that does | - | ||||||||||||||||||
2074 | not follow the \c CommandLineToArgvW() rules is cmd.exe and, by | - | ||||||||||||||||||
2075 | consequence, all batch scripts. | - | ||||||||||||||||||
2076 | - | |||||||||||||||||||
2077 | The OpenMode is set to \a mode. | - | ||||||||||||||||||
2078 | - | |||||||||||||||||||
2079 | If the QProcess object is already running a process, a warning may be | - | ||||||||||||||||||
2080 | printed at the console, and the existing process will continue running | - | ||||||||||||||||||
2081 | unaffected. | - | ||||||||||||||||||
2082 | - | |||||||||||||||||||
2083 | \sa processId(), started(), waitForStarted(), setNativeArguments() | - | ||||||||||||||||||
2084 | */ | - | ||||||||||||||||||
2085 | void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode) | - | ||||||||||||||||||
2086 | { | - | ||||||||||||||||||
2087 | Q_D(QProcess); | - | ||||||||||||||||||
2088 | if (d->processState != NotRunning) {
| 0-2472 | ||||||||||||||||||
2089 | qWarning("QProcess::start: Process is already running"); | - | ||||||||||||||||||
2090 | return; never executed: return; | 0 | ||||||||||||||||||
2091 | } | - | ||||||||||||||||||
2092 | if (program.isEmpty()) {
| 1-2471 | ||||||||||||||||||
2093 | d->setErrorAndEmit(QProcess::FailedToStart, tr("No program defined")); | - | ||||||||||||||||||
2094 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||
2095 | } | - | ||||||||||||||||||
2096 | - | |||||||||||||||||||
2097 | d->program = program; | - | ||||||||||||||||||
2098 | d->arguments = arguments; | - | ||||||||||||||||||
2099 | - | |||||||||||||||||||
2100 | d->start(mode); | - | ||||||||||||||||||
2101 | } executed 2471 times by 38 tests: end of block Executed by:
| 2471 | ||||||||||||||||||
2102 | - | |||||||||||||||||||
2103 | /*! | - | ||||||||||||||||||
2104 | \since 5.1 | - | ||||||||||||||||||
2105 | \overload | - | ||||||||||||||||||
2106 | - | |||||||||||||||||||
2107 | Starts the program set by setProgram() with arguments set by setArguments(). | - | ||||||||||||||||||
2108 | The OpenMode is set to \a mode. | - | ||||||||||||||||||
2109 | - | |||||||||||||||||||
2110 | \sa open(), setProgram(), setArguments() | - | ||||||||||||||||||
2111 | */ | - | ||||||||||||||||||
2112 | void QProcess::start(OpenMode mode) | - | ||||||||||||||||||
2113 | { | - | ||||||||||||||||||
2114 | Q_D(QProcess); | - | ||||||||||||||||||
2115 | if (d->processState != NotRunning) {
| 0-2 | ||||||||||||||||||
2116 | qWarning("QProcess::start: Process is already running"); | - | ||||||||||||||||||
2117 | return; never executed: return; | 0 | ||||||||||||||||||
2118 | } | - | ||||||||||||||||||
2119 | if (d->program.isEmpty()) {
| 1 | ||||||||||||||||||
2120 | d->setErrorAndEmit(QProcess::FailedToStart, tr("No program defined")); | - | ||||||||||||||||||
2121 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||
2122 | } | - | ||||||||||||||||||
2123 | - | |||||||||||||||||||
2124 | d->start(mode); | - | ||||||||||||||||||
2125 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
2126 | - | |||||||||||||||||||
2127 | /*! | - | ||||||||||||||||||
2128 | Starts the program set by setProgram() with arguments set by setArguments(). | - | ||||||||||||||||||
2129 | The OpenMode is set to \a mode. | - | ||||||||||||||||||
2130 | - | |||||||||||||||||||
2131 | This method is an alias for start(), and exists only to fully implement | - | ||||||||||||||||||
2132 | the interface defined by QIODevice. | - | ||||||||||||||||||
2133 | - | |||||||||||||||||||
2134 | \sa start(), setProgram(), setArguments() | - | ||||||||||||||||||
2135 | */ | - | ||||||||||||||||||
2136 | bool QProcess::open(OpenMode mode) | - | ||||||||||||||||||
2137 | { | - | ||||||||||||||||||
2138 | Q_D(QProcess); | - | ||||||||||||||||||
2139 | if (d->processState != NotRunning) {
| 0-2 | ||||||||||||||||||
2140 | qWarning("QProcess::start: Process is already running"); | - | ||||||||||||||||||
2141 | return false; never executed: return false; | 0 | ||||||||||||||||||
2142 | } | - | ||||||||||||||||||
2143 | if (d->program.isEmpty()) {
| 1 | ||||||||||||||||||
2144 | qWarning("QProcess::start: program not set"); | - | ||||||||||||||||||
2145 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||||||||
2146 | } | - | ||||||||||||||||||
2147 | - | |||||||||||||||||||
2148 | d->start(mode); | - | ||||||||||||||||||
2149 | return true; executed 1 time by 1 test: return true; Executed by:
| 1 | ||||||||||||||||||
2150 | } | - | ||||||||||||||||||
2151 | - | |||||||||||||||||||
2152 | void QProcessPrivate::start(QIODevice::OpenMode mode) | - | ||||||||||||||||||
2153 | { | - | ||||||||||||||||||
2154 | Q_Q(QProcess); | - | ||||||||||||||||||
2155 | #if defined QPROCESS_DEBUG | - | ||||||||||||||||||
2156 | qDebug() << "QProcess::start(" << program << ',' << arguments << ',' << mode << ')'; | - | ||||||||||||||||||
2157 | #endif | - | ||||||||||||||||||
2158 | - | |||||||||||||||||||
2159 | stdinChannel.buffer.clear(); | - | ||||||||||||||||||
2160 | stdoutChannel.buffer.clear(); | - | ||||||||||||||||||
2161 | stderrChannel.buffer.clear(); | - | ||||||||||||||||||
2162 | - | |||||||||||||||||||
2163 | if (stdinChannel.type != QProcessPrivate::Channel::Normal)
| 5-2468 | ||||||||||||||||||
2164 | mode &= ~QIODevice::WriteOnly; // not open for writing executed 5 times by 1 test: mode &= ~QIODevice::WriteOnly; Executed by:
| 5 | ||||||||||||||||||
2165 | if (stdoutChannel.type != QProcessPrivate::Channel::Normal &&
| 9-2464 | ||||||||||||||||||
2166 | (stderrChannel.type != QProcessPrivate::Channel::Normal ||
| 0-9 | ||||||||||||||||||
2167 | processChannelMode == QProcess::MergedChannels))
| 3-6 | ||||||||||||||||||
2168 | mode &= ~QIODevice::ReadOnly; // not open for reading executed 3 times by 1 test: mode &= ~QIODevice::ReadOnly; Executed by:
| 3 | ||||||||||||||||||
2169 | if (mode == 0)
| 0-2473 | ||||||||||||||||||
2170 | mode = QIODevice::Unbuffered; never executed: mode = QIODevice::Unbuffered; | 0 | ||||||||||||||||||
2171 | #ifndef Q_OS_WINCE | - | ||||||||||||||||||
2172 | if ((mode & QIODevice::ReadOnly) == 0) {
| 4-2469 | ||||||||||||||||||
2173 | if (stdoutChannel.type == QProcessPrivate::Channel::Normal)
| 1-3 | ||||||||||||||||||
2174 | q->setStandardOutputFile(q->nullDevice()); executed 1 time by 1 test: q->setStandardOutputFile(q->nullDevice()); Executed by:
| 1 | ||||||||||||||||||
2175 | if (stderrChannel.type == QProcessPrivate::Channel::Normal
| 0-4 | ||||||||||||||||||
2176 | && processChannelMode != QProcess::MergedChannels)
| 1-3 | ||||||||||||||||||
2177 | q->setStandardErrorFile(q->nullDevice()); executed 1 time by 1 test: q->setStandardErrorFile(q->nullDevice()); Executed by:
| 1 | ||||||||||||||||||
2178 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
2179 | #endif | - | ||||||||||||||||||
2180 | - | |||||||||||||||||||
2181 | q->QIODevice::open(mode); | - | ||||||||||||||||||
2182 | - | |||||||||||||||||||
2183 | stdinChannel.closed = false; | - | ||||||||||||||||||
2184 | stdoutChannel.closed = false; | - | ||||||||||||||||||
2185 | stderrChannel.closed = false; | - | ||||||||||||||||||
2186 | - | |||||||||||||||||||
2187 | exitCode = 0; | - | ||||||||||||||||||
2188 | exitStatus = QProcess::NormalExit; | - | ||||||||||||||||||
2189 | processError = QProcess::UnknownError; | - | ||||||||||||||||||
2190 | errorString.clear(); | - | ||||||||||||||||||
2191 | startProcess(); | - | ||||||||||||||||||
2192 | } executed 2473 times by 38 tests: end of block Executed by:
| 2473 | ||||||||||||||||||
2193 | - | |||||||||||||||||||
2194 | - | |||||||||||||||||||
2195 | static QStringList parseCombinedArgString(const QString &program) | - | ||||||||||||||||||
2196 | { | - | ||||||||||||||||||
2197 | QStringList args; | - | ||||||||||||||||||
2198 | QString tmp; | - | ||||||||||||||||||
2199 | int quoteCount = 0; | - | ||||||||||||||||||
2200 | bool inQuote = false; | - | ||||||||||||||||||
2201 | - | |||||||||||||||||||
2202 | // handle quoting. tokens can be surrounded by double quotes | - | ||||||||||||||||||
2203 | // "hello world". three consecutive double quotes represent | - | ||||||||||||||||||
2204 | // the quote character itself. | - | ||||||||||||||||||
2205 | for (int i = 0; i < program.size(); ++i) {
| 895-19433 | ||||||||||||||||||
2206 | if (program.at(i) == QLatin1Char('"')) {
| 372-19061 | ||||||||||||||||||
2207 | ++quoteCount; | - | ||||||||||||||||||
2208 | if (quoteCount == 3) {
| 84-288 | ||||||||||||||||||
2209 | // third consecutive quote | - | ||||||||||||||||||
2210 | quoteCount = 0; | - | ||||||||||||||||||
2211 | tmp += program.at(i); | - | ||||||||||||||||||
2212 | } executed 84 times by 1 test: end of block Executed by:
| 84 | ||||||||||||||||||
2213 | continue; executed 372 times by 1 test: continue; Executed by:
| 372 | ||||||||||||||||||
2214 | } | - | ||||||||||||||||||
2215 | if (quoteCount) {
| 99-18962 | ||||||||||||||||||
2216 | if (quoteCount == 1)
| 6-93 | ||||||||||||||||||
2217 | inQuote = !inQuote; executed 93 times by 1 test: inQuote = !inQuote; Executed by:
| 93 | ||||||||||||||||||
2218 | quoteCount = 0; | - | ||||||||||||||||||
2219 | } executed 99 times by 1 test: end of block Executed by:
| 99 | ||||||||||||||||||
2220 | if (!inQuote && program.at(i).isSpace()) {
| 330-18167 | ||||||||||||||||||
2221 | if (!tmp.isEmpty()) {
| 13-317 | ||||||||||||||||||
2222 | args += tmp; | - | ||||||||||||||||||
2223 | tmp.clear(); | - | ||||||||||||||||||
2224 | } executed 317 times by 4 tests: end of block Executed by:
| 317 | ||||||||||||||||||
2225 | } else { executed 330 times by 4 tests: end of block Executed by:
| 330 | ||||||||||||||||||
2226 | tmp += program.at(i); | - | ||||||||||||||||||
2227 | } executed 18731 times by 16 tests: end of block Executed by:
| 18731 | ||||||||||||||||||
2228 | } | - | ||||||||||||||||||
2229 | if (!tmp.isEmpty())
| 10-885 | ||||||||||||||||||
2230 | args += tmp; executed 885 times by 16 tests: args += tmp; Executed by:
| 885 | ||||||||||||||||||
2231 | - | |||||||||||||||||||
2232 | return args; executed 895 times by 16 tests: return args; Executed by:
| 895 | ||||||||||||||||||
2233 | } | - | ||||||||||||||||||
2234 | - | |||||||||||||||||||
2235 | /*! | - | ||||||||||||||||||
2236 | \overload | - | ||||||||||||||||||
2237 | - | |||||||||||||||||||
2238 | Starts the command \a command in a new process. | - | ||||||||||||||||||
2239 | The OpenMode is set to \a mode. | - | ||||||||||||||||||
2240 | - | |||||||||||||||||||
2241 | \a command is a single string of text containing both the program name | - | ||||||||||||||||||
2242 | and its arguments. The arguments are separated by one or more spaces. | - | ||||||||||||||||||
2243 | For example: | - | ||||||||||||||||||
2244 | - | |||||||||||||||||||
2245 | \snippet code/src_corelib_io_qprocess.cpp 5 | - | ||||||||||||||||||
2246 | - | |||||||||||||||||||
2247 | Arguments containing spaces must be quoted to be correctly supplied to | - | ||||||||||||||||||
2248 | the new process. For example: | - | ||||||||||||||||||
2249 | - | |||||||||||||||||||
2250 | \snippet code/src_corelib_io_qprocess.cpp 6 | - | ||||||||||||||||||
2251 | - | |||||||||||||||||||
2252 | Literal quotes in the \a command string are represented by triple quotes. | - | ||||||||||||||||||
2253 | For example: | - | ||||||||||||||||||
2254 | - | |||||||||||||||||||
2255 | \snippet code/src_corelib_io_qprocess.cpp 7 | - | ||||||||||||||||||
2256 | - | |||||||||||||||||||
2257 | After the \a command string has been split and unquoted, this function | - | ||||||||||||||||||
2258 | behaves like the overload which takes the arguments as a string list. | - | ||||||||||||||||||
2259 | - | |||||||||||||||||||
2260 | You can disable this overload by defining \c | - | ||||||||||||||||||
2261 | QT_NO_PROCESS_COMBINED_ARGUMENT_START when you compile your applications. | - | ||||||||||||||||||
2262 | This can be useful if you want to ensure that you are not splitting arguments | - | ||||||||||||||||||
2263 | unintentionally, for example. In virtually all cases, using the other overload | - | ||||||||||||||||||
2264 | is the preferred method. | - | ||||||||||||||||||
2265 | - | |||||||||||||||||||
2266 | On operating systems where the system API for passing command line | - | ||||||||||||||||||
2267 | arguments to a subprocess natively uses a single string (Windows), one can | - | ||||||||||||||||||
2268 | conceive command lines which cannot be passed via QProcess's portable | - | ||||||||||||||||||
2269 | list-based API. In these rare cases you need to use setProgram() and | - | ||||||||||||||||||
2270 | setNativeArguments() instead of this function. | - | ||||||||||||||||||
2271 | - | |||||||||||||||||||
2272 | */ | - | ||||||||||||||||||
2273 | #if !defined(QT_NO_PROCESS_COMBINED_ARGUMENT_START) | - | ||||||||||||||||||
2274 | void QProcess::start(const QString &command, OpenMode mode) | - | ||||||||||||||||||
2275 | { | - | ||||||||||||||||||
2276 | QStringList args = parseCombinedArgString(command); | - | ||||||||||||||||||
2277 | if (args.isEmpty()) {
| 4-886 | ||||||||||||||||||
2278 | Q_D(QProcess); | - | ||||||||||||||||||
2279 | d->setErrorAndEmit(QProcess::FailedToStart, tr("No program defined")); | - | ||||||||||||||||||
2280 | return; executed 4 times by 1 test: return; Executed by:
| 4 | ||||||||||||||||||
2281 | } | - | ||||||||||||||||||
2282 | - | |||||||||||||||||||
2283 | QString prog = args.first(); | - | ||||||||||||||||||
2284 | args.removeFirst(); | - | ||||||||||||||||||
2285 | - | |||||||||||||||||||
2286 | start(prog, args, mode); | - | ||||||||||||||||||
2287 | } executed 886 times by 16 tests: end of block Executed by:
| 886 | ||||||||||||||||||
2288 | #endif | - | ||||||||||||||||||
2289 | - | |||||||||||||||||||
2290 | /*! | - | ||||||||||||||||||
2291 | \since 5.0 | - | ||||||||||||||||||
2292 | - | |||||||||||||||||||
2293 | Returns the program the process was last started with. | - | ||||||||||||||||||
2294 | - | |||||||||||||||||||
2295 | \sa start() | - | ||||||||||||||||||
2296 | */ | - | ||||||||||||||||||
2297 | QString QProcess::program() const | - | ||||||||||||||||||
2298 | { | - | ||||||||||||||||||
2299 | Q_D(const QProcess); | - | ||||||||||||||||||
2300 | return d->program; executed 2 times by 2 tests: return d->program; Executed by:
| 2 | ||||||||||||||||||
2301 | } | - | ||||||||||||||||||
2302 | - | |||||||||||||||||||
2303 | /*! | - | ||||||||||||||||||
2304 | \since 5.1 | - | ||||||||||||||||||
2305 | - | |||||||||||||||||||
2306 | Set the \a program to use when starting the process. | - | ||||||||||||||||||
2307 | This function must be called before start(). | - | ||||||||||||||||||
2308 | - | |||||||||||||||||||
2309 | \sa start(), setArguments(), program() | - | ||||||||||||||||||
2310 | */ | - | ||||||||||||||||||
2311 | void QProcess::setProgram(const QString &program) | - | ||||||||||||||||||
2312 | { | - | ||||||||||||||||||
2313 | Q_D(QProcess); | - | ||||||||||||||||||
2314 | if (d->processState != NotRunning) {
| 0-2 | ||||||||||||||||||
2315 | qWarning("QProcess::setProgram: Process is already running"); | - | ||||||||||||||||||
2316 | return; never executed: return; | 0 | ||||||||||||||||||
2317 | } | - | ||||||||||||||||||
2318 | d->program = program; | - | ||||||||||||||||||
2319 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
2320 | - | |||||||||||||||||||
2321 | /*! | - | ||||||||||||||||||
2322 | \since 5.0 | - | ||||||||||||||||||
2323 | - | |||||||||||||||||||
2324 | Returns the command line arguments the process was last started with. | - | ||||||||||||||||||
2325 | - | |||||||||||||||||||
2326 | \sa start() | - | ||||||||||||||||||
2327 | */ | - | ||||||||||||||||||
2328 | QStringList QProcess::arguments() const | - | ||||||||||||||||||
2329 | { | - | ||||||||||||||||||
2330 | Q_D(const QProcess); | - | ||||||||||||||||||
2331 | return d->arguments; executed 1 time by 1 test: return d->arguments; Executed by:
| 1 | ||||||||||||||||||
2332 | } | - | ||||||||||||||||||
2333 | - | |||||||||||||||||||
2334 | /*! | - | ||||||||||||||||||
2335 | \since 5.1 | - | ||||||||||||||||||
2336 | - | |||||||||||||||||||
2337 | Set the \a arguments to pass to the called program when starting the process. | - | ||||||||||||||||||
2338 | This function must be called before start(). | - | ||||||||||||||||||
2339 | - | |||||||||||||||||||
2340 | \sa start(), setProgram(), arguments() | - | ||||||||||||||||||
2341 | */ | - | ||||||||||||||||||
2342 | void QProcess::setArguments(const QStringList &arguments) | - | ||||||||||||||||||
2343 | { | - | ||||||||||||||||||
2344 | Q_D(QProcess); | - | ||||||||||||||||||
2345 | if (d->processState != NotRunning) {
| 0-1 | ||||||||||||||||||
2346 | qWarning("QProcess::setProgram: Process is already running"); | - | ||||||||||||||||||
2347 | return; never executed: return; | 0 | ||||||||||||||||||
2348 | } | - | ||||||||||||||||||
2349 | d->arguments = arguments; | - | ||||||||||||||||||
2350 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
2351 | - | |||||||||||||||||||
2352 | /*! | - | ||||||||||||||||||
2353 | Attempts to terminate the process. | - | ||||||||||||||||||
2354 | - | |||||||||||||||||||
2355 | The process may not exit as a result of calling this function (it is given | - | ||||||||||||||||||
2356 | the chance to prompt the user for any unsaved files, etc). | - | ||||||||||||||||||
2357 | - | |||||||||||||||||||
2358 | On Windows, terminate() posts a WM_CLOSE message to all top-level windows | - | ||||||||||||||||||
2359 | of the process and then to the main thread of the process itself. On Unix | - | ||||||||||||||||||
2360 | and \macos the \c SIGTERM signal is sent. | - | ||||||||||||||||||
2361 | - | |||||||||||||||||||
2362 | Console applications on Windows that do not run an event loop, or whose | - | ||||||||||||||||||
2363 | event loop does not handle the WM_CLOSE message, can only be terminated by | - | ||||||||||||||||||
2364 | calling kill(). | - | ||||||||||||||||||
2365 | - | |||||||||||||||||||
2366 | \sa kill() | - | ||||||||||||||||||
2367 | */ | - | ||||||||||||||||||
2368 | void QProcess::terminate() | - | ||||||||||||||||||
2369 | { | - | ||||||||||||||||||
2370 | Q_D(QProcess); | - | ||||||||||||||||||
2371 | d->terminateProcess(); | - | ||||||||||||||||||
2372 | } executed 12 times by 3 tests: end of block Executed by:
| 12 | ||||||||||||||||||
2373 | - | |||||||||||||||||||
2374 | /*! | - | ||||||||||||||||||
2375 | Kills the current process, causing it to exit immediately. | - | ||||||||||||||||||
2376 | - | |||||||||||||||||||
2377 | On Windows, kill() uses TerminateProcess, and on Unix and \macos, the | - | ||||||||||||||||||
2378 | SIGKILL signal is sent to the process. | - | ||||||||||||||||||
2379 | - | |||||||||||||||||||
2380 | \sa terminate() | - | ||||||||||||||||||
2381 | */ | - | ||||||||||||||||||
2382 | void QProcess::kill() | - | ||||||||||||||||||
2383 | { | - | ||||||||||||||||||
2384 | Q_D(QProcess); | - | ||||||||||||||||||
2385 | d->killProcess(); | - | ||||||||||||||||||
2386 | } executed 15 times by 7 tests: end of block Executed by:
| 15 | ||||||||||||||||||
2387 | - | |||||||||||||||||||
2388 | /*! | - | ||||||||||||||||||
2389 | Returns the exit code of the last process that finished. | - | ||||||||||||||||||
2390 | - | |||||||||||||||||||
2391 | This value is not valid unless exitStatus() returns NormalExit. | - | ||||||||||||||||||
2392 | */ | - | ||||||||||||||||||
2393 | int QProcess::exitCode() const | - | ||||||||||||||||||
2394 | { | - | ||||||||||||||||||
2395 | Q_D(const QProcess); | - | ||||||||||||||||||
2396 | return d->exitCode; executed 829 times by 12 tests: return d->exitCode; Executed by:
| 829 | ||||||||||||||||||
2397 | } | - | ||||||||||||||||||
2398 | - | |||||||||||||||||||
2399 | /*! | - | ||||||||||||||||||
2400 | \since 4.1 | - | ||||||||||||||||||
2401 | - | |||||||||||||||||||
2402 | Returns the exit status of the last process that finished. | - | ||||||||||||||||||
2403 | - | |||||||||||||||||||
2404 | On Windows, if the process was terminated with TerminateProcess() from | - | ||||||||||||||||||
2405 | another application, this function will still return NormalExit | - | ||||||||||||||||||
2406 | unless the exit code is less than 0. | - | ||||||||||||||||||
2407 | */ | - | ||||||||||||||||||
2408 | QProcess::ExitStatus QProcess::exitStatus() const | - | ||||||||||||||||||
2409 | { | - | ||||||||||||||||||
2410 | Q_D(const QProcess); | - | ||||||||||||||||||
2411 | return d->exitStatus; executed 972 times by 10 tests: return d->exitStatus; Executed by:
| 972 | ||||||||||||||||||
2412 | } | - | ||||||||||||||||||
2413 | - | |||||||||||||||||||
2414 | /*! | - | ||||||||||||||||||
2415 | Starts the program \a program with the arguments \a arguments in a | - | ||||||||||||||||||
2416 | new process, waits for it to finish, and then returns the exit | - | ||||||||||||||||||
2417 | code of the process. Any data the new process writes to the | - | ||||||||||||||||||
2418 | console is forwarded to the calling process. | - | ||||||||||||||||||
2419 | - | |||||||||||||||||||
2420 | The environment and working directory are inherited from the calling | - | ||||||||||||||||||
2421 | process. | - | ||||||||||||||||||
2422 | - | |||||||||||||||||||
2423 | Argument handling is identical to the respective start() overload. | - | ||||||||||||||||||
2424 | - | |||||||||||||||||||
2425 | If the process cannot be started, -2 is returned. If the process | - | ||||||||||||||||||
2426 | crashes, -1 is returned. Otherwise, the process' exit code is | - | ||||||||||||||||||
2427 | returned. | - | ||||||||||||||||||
2428 | - | |||||||||||||||||||
2429 | \sa start() | - | ||||||||||||||||||
2430 | */ | - | ||||||||||||||||||
2431 | int QProcess::execute(const QString &program, const QStringList &arguments) | - | ||||||||||||||||||
2432 | { | - | ||||||||||||||||||
2433 | QProcess process; | - | ||||||||||||||||||
2434 | process.setReadChannelMode(ForwardedChannels); | - | ||||||||||||||||||
2435 | process.start(program, arguments); | - | ||||||||||||||||||
2436 | if (!process.waitForFinished(-1) || process.error() == FailedToStart)
| 0-4 | ||||||||||||||||||
2437 | return -2; executed 4 times by 1 test: return -2; Executed by:
| 4 | ||||||||||||||||||
2438 | return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1; executed 1 time by 1 test: return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1; Executed by:
| 0-1 | ||||||||||||||||||
2439 | } | - | ||||||||||||||||||
2440 | - | |||||||||||||||||||
2441 | /*! | - | ||||||||||||||||||
2442 | \overload | - | ||||||||||||||||||
2443 | - | |||||||||||||||||||
2444 | Starts the program \a command in a new process, waits for it to finish, | - | ||||||||||||||||||
2445 | and then returns the exit code. | - | ||||||||||||||||||
2446 | - | |||||||||||||||||||
2447 | Argument handling is identical to the respective start() overload. | - | ||||||||||||||||||
2448 | - | |||||||||||||||||||
2449 | After the \a command string has been split and unquoted, this function | - | ||||||||||||||||||
2450 | behaves like the overload which takes the arguments as a string list. | - | ||||||||||||||||||
2451 | - | |||||||||||||||||||
2452 | \sa start() | - | ||||||||||||||||||
2453 | */ | - | ||||||||||||||||||
2454 | int QProcess::execute(const QString &command) | - | ||||||||||||||||||
2455 | { | - | ||||||||||||||||||
2456 | QProcess process; | - | ||||||||||||||||||
2457 | process.setReadChannelMode(ForwardedChannels); | - | ||||||||||||||||||
2458 | process.start(command); | - | ||||||||||||||||||
2459 | if (!process.waitForFinished(-1) || process.error() == FailedToStart)
| 0-4 | ||||||||||||||||||
2460 | return -2; executed 4 times by 3 tests: return -2; Executed by:
| 4 | ||||||||||||||||||
2461 | return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1; never executed: return process.exitStatus() == QProcess::NormalExit ? process.exitCode() : -1;
| 0 | ||||||||||||||||||
2462 | } | - | ||||||||||||||||||
2463 | - | |||||||||||||||||||
2464 | /*! | - | ||||||||||||||||||
2465 | Starts the program \a program with the arguments \a arguments in a | - | ||||||||||||||||||
2466 | new process, and detaches from it. Returns \c true on success; | - | ||||||||||||||||||
2467 | otherwise returns \c false. If the calling process exits, the | - | ||||||||||||||||||
2468 | detached process will continue to run unaffected. | - | ||||||||||||||||||
2469 | - | |||||||||||||||||||
2470 | Argument handling is identical to the respective start() overload. | - | ||||||||||||||||||
2471 | - | |||||||||||||||||||
2472 | \b{Unix:} The started process will run in its own session and act | - | ||||||||||||||||||
2473 | like a daemon. | - | ||||||||||||||||||
2474 | - | |||||||||||||||||||
2475 | The process will be started in the directory \a workingDirectory. | - | ||||||||||||||||||
2476 | If \a workingDirectory is empty, the working directory is inherited | - | ||||||||||||||||||
2477 | from the calling process. | - | ||||||||||||||||||
2478 | - | |||||||||||||||||||
2479 | \note On QNX, this may cause all application threads to | - | ||||||||||||||||||
2480 | temporarily freeze. | - | ||||||||||||||||||
2481 | - | |||||||||||||||||||
2482 | If the function is successful then *\a pid is set to the process | - | ||||||||||||||||||
2483 | identifier of the started process. | - | ||||||||||||||||||
2484 | - | |||||||||||||||||||
2485 | \sa start() | - | ||||||||||||||||||
2486 | */ | - | ||||||||||||||||||
2487 | bool QProcess::startDetached(const QString &program, | - | ||||||||||||||||||
2488 | const QStringList &arguments, | - | ||||||||||||||||||
2489 | const QString &workingDirectory, | - | ||||||||||||||||||
2490 | qint64 *pid) | - | ||||||||||||||||||
2491 | { | - | ||||||||||||||||||
2492 | return QProcessPrivate::startDetached(program, executed 1 time by 1 test: return QProcessPrivate::startDetached(program, arguments, workingDirectory, pid); Executed by:
| 1 | ||||||||||||||||||
2493 | arguments, executed 1 time by 1 test: return QProcessPrivate::startDetached(program, arguments, workingDirectory, pid); Executed by:
| 1 | ||||||||||||||||||
2494 | workingDirectory, executed 1 time by 1 test: return QProcessPrivate::startDetached(program, arguments, workingDirectory, pid); Executed by:
| 1 | ||||||||||||||||||
2495 | pid); executed 1 time by 1 test: return QProcessPrivate::startDetached(program, arguments, workingDirectory, pid); Executed by:
| 1 | ||||||||||||||||||
2496 | } | - | ||||||||||||||||||
2497 | - | |||||||||||||||||||
2498 | /*! | - | ||||||||||||||||||
2499 | \internal | - | ||||||||||||||||||
2500 | */ | - | ||||||||||||||||||
2501 | bool QProcess::startDetached(const QString &program, | - | ||||||||||||||||||
2502 | const QStringList &arguments) | - | ||||||||||||||||||
2503 | { | - | ||||||||||||||||||
2504 | return QProcessPrivate::startDetached(program, arguments); executed 1 time by 1 test: return QProcessPrivate::startDetached(program, arguments); Executed by:
| 1 | ||||||||||||||||||
2505 | } | - | ||||||||||||||||||
2506 | - | |||||||||||||||||||
2507 | /*! | - | ||||||||||||||||||
2508 | \overload | - | ||||||||||||||||||
2509 | - | |||||||||||||||||||
2510 | Starts the command \a command in a new process, and detaches from it. | - | ||||||||||||||||||
2511 | Returns \c true on success; otherwise returns \c false. | - | ||||||||||||||||||
2512 | - | |||||||||||||||||||
2513 | Argument handling is identical to the respective start() overload. | - | ||||||||||||||||||
2514 | - | |||||||||||||||||||
2515 | After the \a command string has been split and unquoted, this function | - | ||||||||||||||||||
2516 | behaves like the overload which takes the arguments as a string list. | - | ||||||||||||||||||
2517 | - | |||||||||||||||||||
2518 | \sa start(const QString &command, OpenMode mode) | - | ||||||||||||||||||
2519 | */ | - | ||||||||||||||||||
2520 | bool QProcess::startDetached(const QString &command) | - | ||||||||||||||||||
2521 | { | - | ||||||||||||||||||
2522 | QStringList args = parseCombinedArgString(command); | - | ||||||||||||||||||
2523 | if (args.isEmpty())
| 2-3 | ||||||||||||||||||
2524 | return false; executed 3 times by 1 test: return false; Executed by:
| 3 | ||||||||||||||||||
2525 | - | |||||||||||||||||||
2526 | QString prog = args.first(); | - | ||||||||||||||||||
2527 | args.removeFirst(); | - | ||||||||||||||||||
2528 | - | |||||||||||||||||||
2529 | return QProcessPrivate::startDetached(prog, args); executed 2 times by 1 test: return QProcessPrivate::startDetached(prog, args); Executed by:
| 2 | ||||||||||||||||||
2530 | } | - | ||||||||||||||||||
2531 | - | |||||||||||||||||||
2532 | QT_BEGIN_INCLUDE_NAMESPACE | - | ||||||||||||||||||
2533 | #if defined(Q_OS_MACX) | - | ||||||||||||||||||
2534 | # include <crt_externs.h> | - | ||||||||||||||||||
2535 | # define environ (*_NSGetEnviron()) | - | ||||||||||||||||||
2536 | #elif defined(Q_OS_WINCE) || defined(Q_OS_IOS) | - | ||||||||||||||||||
2537 | static char *qt_empty_environ[] = { 0 }; | - | ||||||||||||||||||
2538 | #define environ qt_empty_environ | - | ||||||||||||||||||
2539 | #elif !defined(Q_OS_WIN) | - | ||||||||||||||||||
2540 | extern char **environ; | - | ||||||||||||||||||
2541 | #endif | - | ||||||||||||||||||
2542 | QT_END_INCLUDE_NAMESPACE | - | ||||||||||||||||||
2543 | - | |||||||||||||||||||
2544 | /*! | - | ||||||||||||||||||
2545 | \since 4.1 | - | ||||||||||||||||||
2546 | - | |||||||||||||||||||
2547 | Returns the environment of the calling process as a list of | - | ||||||||||||||||||
2548 | key=value pairs. Example: | - | ||||||||||||||||||
2549 | - | |||||||||||||||||||
2550 | \snippet code/src_corelib_io_qprocess.cpp 8 | - | ||||||||||||||||||
2551 | - | |||||||||||||||||||
2552 | This function does not cache the system environment. Therefore, it's | - | ||||||||||||||||||
2553 | possible to obtain an updated version of the environment if low-level C | - | ||||||||||||||||||
2554 | library functions like \tt setenv or \tt putenv have been called. | - | ||||||||||||||||||
2555 | - | |||||||||||||||||||
2556 | However, note that repeated calls to this function will recreate the | - | ||||||||||||||||||
2557 | list of environment variables, which is a non-trivial operation. | - | ||||||||||||||||||
2558 | - | |||||||||||||||||||
2559 | \note For new code, it is recommended to use QProcessEnvironment::systemEnvironment() | - | ||||||||||||||||||
2560 | - | |||||||||||||||||||
2561 | \sa QProcessEnvironment::systemEnvironment(), setProcessEnvironment() | - | ||||||||||||||||||
2562 | */ | - | ||||||||||||||||||
2563 | QStringList QProcess::systemEnvironment() | - | ||||||||||||||||||
2564 | { | - | ||||||||||||||||||
2565 | QStringList tmp; | - | ||||||||||||||||||
2566 | char *entry = 0; | - | ||||||||||||||||||
2567 | int count = 0; | - | ||||||||||||||||||
2568 | while ((entry = environ[count++]))
| 55-1651 | ||||||||||||||||||
2569 | tmp << QString::fromLocal8Bit(entry); executed 1651 times by 4 tests: tmp << QString::fromLocal8Bit(entry); Executed by:
| 1651 | ||||||||||||||||||
2570 | return tmp; executed 55 times by 4 tests: return tmp; Executed by:
| 55 | ||||||||||||||||||
2571 | } | - | ||||||||||||||||||
2572 | - | |||||||||||||||||||
2573 | /*! | - | ||||||||||||||||||
2574 | \fn QProcessEnvironment QProcessEnvironment::systemEnvironment() | - | ||||||||||||||||||
2575 | - | |||||||||||||||||||
2576 | \since 4.6 | - | ||||||||||||||||||
2577 | - | |||||||||||||||||||
2578 | \brief The systemEnvironment function returns the environment of | - | ||||||||||||||||||
2579 | the calling process. | - | ||||||||||||||||||
2580 | - | |||||||||||||||||||
2581 | It is returned as a QProcessEnvironment. This function does not | - | ||||||||||||||||||
2582 | cache the system environment. Therefore, it's possible to obtain | - | ||||||||||||||||||
2583 | an updated version of the environment if low-level C library | - | ||||||||||||||||||
2584 | functions like \tt setenv or \tt putenv have been called. | - | ||||||||||||||||||
2585 | - | |||||||||||||||||||
2586 | However, note that repeated calls to this function will recreate the | - | ||||||||||||||||||
2587 | QProcessEnvironment object, which is a non-trivial operation. | - | ||||||||||||||||||
2588 | - | |||||||||||||||||||
2589 | \sa QProcess::systemEnvironment() | - | ||||||||||||||||||
2590 | */ | - | ||||||||||||||||||
2591 | - | |||||||||||||||||||
2592 | /*! | - | ||||||||||||||||||
2593 | \since 5.2 | - | ||||||||||||||||||
2594 | - | |||||||||||||||||||
2595 | \brief The null device of the operating system. | - | ||||||||||||||||||
2596 | - | |||||||||||||||||||
2597 | The returned file path uses native directory separators. | - | ||||||||||||||||||
2598 | - | |||||||||||||||||||
2599 | \sa QProcess::setStandardInputFile(), QProcess::setStandardOutputFile(), | - | ||||||||||||||||||
2600 | QProcess::setStandardErrorFile() | - | ||||||||||||||||||
2601 | */ | - | ||||||||||||||||||
2602 | QString QProcess::nullDevice() | - | ||||||||||||||||||
2603 | { | - | ||||||||||||||||||
2604 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
2605 | return QStringLiteral("\\\\.\\NUL"); | - | ||||||||||||||||||
2606 | #else | - | ||||||||||||||||||
2607 | return QStringLiteral("/dev/null"); executed 5 times by 1 test: return ([]() -> QString { enum { Size = sizeof(u"" "/dev/null")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "/dev/null" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); Executed by:
executed 5 times by 1 test: return qstring_literal_temp; Executed by:
| 5 | ||||||||||||||||||
2608 | #endif | - | ||||||||||||||||||
2609 | } | - | ||||||||||||||||||
2610 | - | |||||||||||||||||||
2611 | /*! | - | ||||||||||||||||||
2612 | \typedef Q_PID | - | ||||||||||||||||||
2613 | \relates QProcess | - | ||||||||||||||||||
2614 | - | |||||||||||||||||||
2615 | Typedef for the identifiers used to represent processes on the underlying | - | ||||||||||||||||||
2616 | platform. On Unix, this corresponds to \l qint64; on Windows, it | - | ||||||||||||||||||
2617 | corresponds to \c{_PROCESS_INFORMATION*}. | - | ||||||||||||||||||
2618 | - | |||||||||||||||||||
2619 | \sa QProcess::pid() | - | ||||||||||||||||||
2620 | */ | - | ||||||||||||||||||
2621 | - | |||||||||||||||||||
2622 | QT_END_NAMESPACE | - | ||||||||||||||||||
2623 | - | |||||||||||||||||||
2624 | #include "moc_qprocess.cpp" | - | ||||||||||||||||||
2625 | - | |||||||||||||||||||
2626 | #endif // QT_NO_PROCESS | - | ||||||||||||||||||
2627 | - | |||||||||||||||||||
Source code | Switch to Preprocessed file |