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