author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 22 | 79de32ba3296 |
child 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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> |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
91 |
#include <qelapsedtimer.h> |
0 | 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 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
278 |
return d == other.d || (d && other.d && d->hash == other.d->hash); |
0 | 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 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
// d detaches from null |
0 | 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 Users |
|
546 |
||
547 |
Some Windows commands (for example, \c dir) are not provided by |
|
548 |
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 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
554 |
\section1 Symbian Platform Security Requirements |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
555 |
|
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
556 |
On Symbian, processes which use the functions kill() or terminate() |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
557 |
must have the \c PowerMgmt platform security capability. If the client |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
558 |
process lacks this capability, these functions will fail. |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
559 |
|
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
560 |
Platform security capabilities are added via the |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
561 |
\l{qmake-variable-reference.html#target-capability}{TARGET.CAPABILITY} |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
562 |
qmake variable. |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
563 |
|
0 | 564 |
\sa QBuffer, QFile, QTcpSocket |
565 |
*/ |
|
566 |
||
567 |
/*! |
|
568 |
\enum QProcess::ProcessChannel |
|
569 |
||
570 |
This enum describes the process channels used by the running process. |
|
571 |
Pass one of these values to setReadChannel() to set the |
|
572 |
current read channel of QProcess. |
|
573 |
||
574 |
\value StandardOutput The standard output (stdout) of the running |
|
575 |
process. |
|
576 |
||
577 |
\value StandardError The standard error (stderr) of the running |
|
578 |
process. |
|
579 |
||
580 |
\sa setReadChannel() |
|
581 |
*/ |
|
582 |
||
583 |
/*! |
|
584 |
\enum QProcess::ProcessChannelMode |
|
585 |
||
586 |
This enum describes the process channel modes of QProcess. Pass |
|
587 |
one of these values to setProcessChannelMode() to set the |
|
588 |
current read channel mode. |
|
589 |
||
590 |
\value SeparateChannels QProcess manages the output of the |
|
591 |
running process, keeping standard output and standard error data |
|
592 |
in separate internal buffers. You can select the QProcess's |
|
593 |
current read channel by calling setReadChannel(). This is the |
|
594 |
default channel mode of QProcess. |
|
595 |
||
596 |
\value MergedChannels QProcess merges the output of the running |
|
597 |
process into the standard output channel (\c stdout). The |
|
598 |
standard error channel (\c stderr) will not receive any data. The |
|
599 |
standard output and standard error data of the running process |
|
600 |
are interleaved. |
|
601 |
||
602 |
\value ForwardedChannels QProcess forwards the output of the |
|
603 |
running process onto the main process. Anything the child process |
|
604 |
writes to its standard output and standard error will be written |
|
605 |
to the standard output and standard error of the main process. |
|
606 |
||
607 |
\sa setProcessChannelMode() |
|
608 |
*/ |
|
609 |
||
610 |
/*! |
|
611 |
\enum QProcess::ProcessError |
|
612 |
||
613 |
This enum describes the different types of errors that are |
|
614 |
reported by QProcess. |
|
615 |
||
616 |
\value FailedToStart The process failed to start. Either the |
|
617 |
invoked program is missing, or you may have insufficient |
|
618 |
permissions to invoke the program. |
|
619 |
||
620 |
\value Crashed The process crashed some time after starting |
|
621 |
successfully. |
|
622 |
||
623 |
\value Timedout The last waitFor...() function timed out. The |
|
624 |
state of QProcess is unchanged, and you can try calling |
|
625 |
waitFor...() again. |
|
626 |
||
627 |
\value WriteError An error occurred when attempting to write to the |
|
628 |
process. For example, the process may not be running, or it may |
|
629 |
have closed its input channel. |
|
630 |
||
631 |
\value ReadError An error occurred when attempting to read from |
|
632 |
the process. For example, the process may not be running. |
|
633 |
||
634 |
\value UnknownError An unknown error occurred. This is the default |
|
635 |
return value of error(). |
|
636 |
||
637 |
\sa error() |
|
638 |
*/ |
|
639 |
||
640 |
/*! |
|
641 |
\enum QProcess::ProcessState |
|
642 |
||
643 |
This enum describes the different states of QProcess. |
|
644 |
||
645 |
\value NotRunning The process is not running. |
|
646 |
||
647 |
\value Starting The process is starting, but the program has not |
|
648 |
yet been invoked. |
|
649 |
||
650 |
\value Running The process is running and is ready for reading and |
|
651 |
writing. |
|
652 |
||
653 |
\sa state() |
|
654 |
*/ |
|
655 |
||
656 |
/*! |
|
657 |
\enum QProcess::ExitStatus |
|
658 |
||
659 |
This enum describes the different exit statuses of QProcess. |
|
660 |
||
661 |
\value NormalExit The process exited normally. |
|
662 |
||
663 |
\value CrashExit The process crashed. |
|
664 |
||
665 |
\sa exitStatus() |
|
666 |
*/ |
|
667 |
||
668 |
/*! |
|
669 |
\fn void QProcess::error(QProcess::ProcessError error) |
|
670 |
||
671 |
This signal is emitted when an error occurs with the process. The |
|
672 |
specified \a error describes the type of error that occurred. |
|
673 |
*/ |
|
674 |
||
675 |
/*! |
|
676 |
\fn void QProcess::started() |
|
677 |
||
678 |
This signal is emitted by QProcess when the process has started, |
|
679 |
and state() returns \l Running. |
|
680 |
*/ |
|
681 |
||
682 |
/*! |
|
683 |
\fn void QProcess::stateChanged(QProcess::ProcessState newState) |
|
684 |
||
685 |
This signal is emitted whenever the state of QProcess changes. The |
|
686 |
\a newState argument is the state QProcess changed to. |
|
687 |
*/ |
|
688 |
||
689 |
/*! |
|
690 |
\fn void QProcess::finished(int exitCode) |
|
691 |
\obsolete |
|
692 |
\overload |
|
693 |
||
694 |
Use finished(int exitCode, QProcess::ExitStatus status) instead. |
|
695 |
*/ |
|
696 |
||
697 |
/*! |
|
698 |
\fn void QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus) |
|
699 |
||
700 |
This signal is emitted when the process finishes. \a exitCode is the exit |
|
701 |
code of the process, and \a exitStatus is the exit status. After the |
|
702 |
process has finished, the buffers in QProcess are still intact. You can |
|
703 |
still read any data that the process may have written before it finished. |
|
704 |
||
705 |
\sa exitStatus() |
|
706 |
*/ |
|
707 |
||
708 |
/*! |
|
709 |
\fn void QProcess::readyReadStandardOutput() |
|
710 |
||
711 |
This signal is emitted when the process has made new data |
|
712 |
available through its standard output channel (\c stdout). It is |
|
713 |
emitted regardless of the current \l{readChannel()}{read channel}. |
|
714 |
||
715 |
\sa readAllStandardOutput(), readChannel() |
|
716 |
*/ |
|
717 |
||
718 |
/*! |
|
719 |
\fn void QProcess::readyReadStandardError() |
|
720 |
||
721 |
This signal is emitted when the process has made new data |
|
722 |
available through its standard error channel (\c stderr). It is |
|
723 |
emitted regardless of the current \l{readChannel()}{read |
|
724 |
channel}. |
|
725 |
||
726 |
\sa readAllStandardError(), readChannel() |
|
727 |
*/ |
|
728 |
||
729 |
/*! \internal |
|
730 |
*/ |
|
731 |
QProcessPrivate::QProcessPrivate() |
|
732 |
{ |
|
733 |
processChannel = QProcess::StandardOutput; |
|
734 |
processChannelMode = QProcess::SeparateChannels; |
|
735 |
processError = QProcess::UnknownError; |
|
736 |
processState = QProcess::NotRunning; |
|
737 |
pid = 0; |
|
738 |
sequenceNumber = 0; |
|
739 |
exitCode = 0; |
|
740 |
exitStatus = QProcess::NormalExit; |
|
741 |
startupSocketNotifier = 0; |
|
742 |
deathNotifier = 0; |
|
743 |
notifier = 0; |
|
744 |
pipeWriter = 0; |
|
745 |
childStartedPipe[0] = INVALID_Q_PIPE; |
|
746 |
childStartedPipe[1] = INVALID_Q_PIPE; |
|
747 |
deathPipe[0] = INVALID_Q_PIPE; |
|
748 |
deathPipe[1] = INVALID_Q_PIPE; |
|
749 |
exitCode = 0; |
|
750 |
crashed = false; |
|
751 |
dying = false; |
|
752 |
emittedReadyRead = false; |
|
753 |
emittedBytesWritten = false; |
|
754 |
#ifdef Q_WS_WIN |
|
755 |
pipeWriter = 0; |
|
756 |
processFinishedNotifier = 0; |
|
757 |
#endif // Q_WS_WIN |
|
758 |
#ifdef Q_OS_UNIX |
|
759 |
serial = 0; |
|
760 |
#endif |
|
761 |
#ifdef Q_OS_SYMBIAN |
|
762 |
symbianProcess = NULL; |
|
763 |
processLaunched = false; |
|
764 |
#endif |
|
765 |
} |
|
766 |
||
767 |
/*! \internal |
|
768 |
*/ |
|
769 |
QProcessPrivate::~QProcessPrivate() |
|
770 |
{ |
|
771 |
if (stdinChannel.process) |
|
772 |
stdinChannel.process->stdoutChannel.clear(); |
|
773 |
if (stdoutChannel.process) |
|
774 |
stdoutChannel.process->stdinChannel.clear(); |
|
775 |
} |
|
776 |
||
777 |
/*! \internal |
|
778 |
*/ |
|
779 |
void QProcessPrivate::cleanup() |
|
780 |
{ |
|
781 |
q_func()->setProcessState(QProcess::NotRunning); |
|
782 |
#ifdef Q_OS_WIN |
|
783 |
if (pid) { |
|
784 |
CloseHandle(pid->hThread); |
|
785 |
CloseHandle(pid->hProcess); |
|
786 |
delete pid; |
|
787 |
pid = 0; |
|
788 |
} |
|
789 |
if (processFinishedNotifier) { |
|
790 |
processFinishedNotifier->setEnabled(false); |
|
791 |
qDeleteInEventHandler(processFinishedNotifier); |
|
792 |
processFinishedNotifier = 0; |
|
793 |
} |
|
794 |
||
795 |
#endif |
|
796 |
pid = 0; |
|
797 |
sequenceNumber = 0; |
|
798 |
dying = false; |
|
799 |
||
800 |
if (stdoutChannel.notifier) { |
|
801 |
stdoutChannel.notifier->setEnabled(false); |
|
802 |
qDeleteInEventHandler(stdoutChannel.notifier); |
|
803 |
stdoutChannel.notifier = 0; |
|
804 |
} |
|
805 |
if (stderrChannel.notifier) { |
|
806 |
stderrChannel.notifier->setEnabled(false); |
|
807 |
qDeleteInEventHandler(stderrChannel.notifier); |
|
808 |
stderrChannel.notifier = 0; |
|
809 |
} |
|
810 |
if (stdinChannel.notifier) { |
|
811 |
stdinChannel.notifier->setEnabled(false); |
|
812 |
qDeleteInEventHandler(stdinChannel.notifier); |
|
813 |
stdinChannel.notifier = 0; |
|
814 |
} |
|
815 |
if (startupSocketNotifier) { |
|
816 |
startupSocketNotifier->setEnabled(false); |
|
817 |
qDeleteInEventHandler(startupSocketNotifier); |
|
818 |
startupSocketNotifier = 0; |
|
819 |
} |
|
820 |
if (deathNotifier) { |
|
821 |
deathNotifier->setEnabled(false); |
|
822 |
qDeleteInEventHandler(deathNotifier); |
|
823 |
deathNotifier = 0; |
|
824 |
} |
|
825 |
if (notifier) { |
|
826 |
qDeleteInEventHandler(notifier); |
|
827 |
notifier = 0; |
|
828 |
} |
|
829 |
destroyPipe(stdoutChannel.pipe); |
|
830 |
destroyPipe(stderrChannel.pipe); |
|
831 |
destroyPipe(stdinChannel.pipe); |
|
832 |
destroyPipe(childStartedPipe); |
|
833 |
destroyPipe(deathPipe); |
|
834 |
#ifdef Q_OS_UNIX |
|
835 |
serial = 0; |
|
836 |
#endif |
|
837 |
#ifdef Q_OS_SYMBIAN |
|
838 |
if (symbianProcess) { |
|
839 |
symbianProcess->Close(); |
|
840 |
delete symbianProcess; |
|
841 |
symbianProcess = NULL; |
|
842 |
} |
|
843 |
#endif |
|
844 |
} |
|
845 |
||
846 |
/*! \internal |
|
847 |
*/ |
|
848 |
bool QProcessPrivate::_q_canReadStandardOutput() |
|
849 |
{ |
|
850 |
Q_Q(QProcess); |
|
851 |
qint64 available = bytesAvailableFromStdout(); |
|
852 |
if (available == 0) { |
|
853 |
if (stdoutChannel.notifier) |
|
854 |
stdoutChannel.notifier->setEnabled(false); |
|
855 |
destroyPipe(stdoutChannel.pipe); |
|
856 |
#if defined QPROCESS_DEBUG |
|
857 |
qDebug("QProcessPrivate::canReadStandardOutput(), 0 bytes available"); |
|
858 |
#endif |
|
859 |
return false; |
|
860 |
} |
|
861 |
||
862 |
char *ptr = outputReadBuffer.reserve(available); |
|
863 |
qint64 readBytes = readFromStdout(ptr, available); |
|
864 |
if (readBytes == -1) { |
|
865 |
processError = QProcess::ReadError; |
|
866 |
q->setErrorString(QProcess::tr("Error reading from process")); |
|
867 |
emit q->error(processError); |
|
868 |
#if defined QPROCESS_DEBUG |
|
869 |
qDebug("QProcessPrivate::canReadStandardOutput(), failed to read from the process"); |
|
870 |
#endif |
|
871 |
return false; |
|
872 |
} |
|
873 |
#if defined QPROCESS_DEBUG |
|
874 |
qDebug("QProcessPrivate::canReadStandardOutput(), read %d bytes from the process' output", |
|
875 |
int(readBytes)); |
|
876 |
#endif |
|
877 |
||
878 |
if (stdoutChannel.closed) { |
|
879 |
outputReadBuffer.chop(readBytes); |
|
880 |
return false; |
|
881 |
} |
|
882 |
||
883 |
outputReadBuffer.chop(available - readBytes); |
|
884 |
||
885 |
bool didRead = false; |
|
886 |
if (readBytes == 0) { |
|
887 |
if (stdoutChannel.notifier) |
|
888 |
stdoutChannel.notifier->setEnabled(false); |
|
889 |
} else if (processChannel == QProcess::StandardOutput) { |
|
890 |
didRead = true; |
|
891 |
if (!emittedReadyRead) { |
|
892 |
emittedReadyRead = true; |
|
893 |
emit q->readyRead(); |
|
894 |
emittedReadyRead = false; |
|
895 |
} |
|
896 |
} |
|
897 |
emit q->readyReadStandardOutput(); |
|
898 |
return didRead; |
|
899 |
} |
|
900 |
||
901 |
/*! \internal |
|
902 |
*/ |
|
903 |
bool QProcessPrivate::_q_canReadStandardError() |
|
904 |
{ |
|
905 |
Q_Q(QProcess); |
|
906 |
qint64 available = bytesAvailableFromStderr(); |
|
907 |
if (available == 0) { |
|
908 |
if (stderrChannel.notifier) |
|
909 |
stderrChannel.notifier->setEnabled(false); |
|
910 |
destroyPipe(stderrChannel.pipe); |
|
911 |
return false; |
|
912 |
} |
|
913 |
||
914 |
char *ptr = errorReadBuffer.reserve(available); |
|
915 |
qint64 readBytes = readFromStderr(ptr, available); |
|
916 |
if (readBytes == -1) { |
|
917 |
processError = QProcess::ReadError; |
|
918 |
q->setErrorString(QProcess::tr("Error reading from process")); |
|
919 |
emit q->error(processError); |
|
920 |
return false; |
|
921 |
} |
|
922 |
if (stderrChannel.closed) { |
|
923 |
errorReadBuffer.chop(readBytes); |
|
924 |
return false; |
|
925 |
} |
|
926 |
||
927 |
errorReadBuffer.chop(available - readBytes); |
|
928 |
||
929 |
bool didRead = false; |
|
930 |
if (readBytes == 0) { |
|
931 |
if (stderrChannel.notifier) |
|
932 |
stderrChannel.notifier->setEnabled(false); |
|
933 |
} else if (processChannel == QProcess::StandardError) { |
|
934 |
didRead = true; |
|
935 |
if (!emittedReadyRead) { |
|
936 |
emittedReadyRead = true; |
|
937 |
emit q->readyRead(); |
|
938 |
emittedReadyRead = false; |
|
939 |
} |
|
940 |
} |
|
941 |
emit q->readyReadStandardError(); |
|
942 |
return didRead; |
|
943 |
} |
|
944 |
||
945 |
/*! \internal |
|
946 |
*/ |
|
947 |
bool QProcessPrivate::_q_canWrite() |
|
948 |
{ |
|
949 |
Q_Q(QProcess); |
|
950 |
if (stdinChannel.notifier) |
|
951 |
stdinChannel.notifier->setEnabled(false); |
|
952 |
||
953 |
if (writeBuffer.isEmpty()) { |
|
954 |
#if defined QPROCESS_DEBUG |
|
955 |
qDebug("QProcessPrivate::canWrite(), not writing anything (empty write buffer)."); |
|
956 |
#endif |
|
957 |
return false; |
|
958 |
} |
|
959 |
||
960 |
qint64 written = writeToStdin(writeBuffer.readPointer(), |
|
961 |
writeBuffer.nextDataBlockSize()); |
|
962 |
if (written < 0) { |
|
963 |
destroyPipe(stdinChannel.pipe); |
|
964 |
processError = QProcess::WriteError; |
|
965 |
q->setErrorString(QProcess::tr("Error writing to process")); |
|
966 |
#if defined(QPROCESS_DEBUG) && !defined(Q_OS_WINCE) |
|
967 |
qDebug("QProcessPrivate::canWrite(), failed to write (%s)", strerror(errno)); |
|
968 |
#endif |
|
969 |
emit q->error(processError); |
|
970 |
return false; |
|
971 |
} |
|
972 |
||
973 |
#if defined QPROCESS_DEBUG |
|
974 |
qDebug("QProcessPrivate::canWrite(), wrote %d bytes to the process input", int(written)); |
|
975 |
#endif |
|
976 |
||
977 |
writeBuffer.free(written); |
|
978 |
if (!emittedBytesWritten) { |
|
979 |
emittedBytesWritten = true; |
|
980 |
emit q->bytesWritten(written); |
|
981 |
emittedBytesWritten = false; |
|
982 |
} |
|
983 |
if (stdinChannel.notifier && !writeBuffer.isEmpty()) |
|
984 |
stdinChannel.notifier->setEnabled(true); |
|
985 |
if (writeBuffer.isEmpty() && stdinChannel.closed) |
|
986 |
closeWriteChannel(); |
|
987 |
return true; |
|
988 |
} |
|
989 |
||
990 |
/*! \internal |
|
991 |
*/ |
|
992 |
bool QProcessPrivate::_q_processDied() |
|
993 |
{ |
|
994 |
Q_Q(QProcess); |
|
995 |
#if defined QPROCESS_DEBUG |
|
996 |
qDebug("QProcessPrivate::_q_processDied()"); |
|
997 |
#endif |
|
998 |
#ifdef Q_OS_UNIX |
|
999 |
if (!waitForDeadChild()) |
|
1000 |
return false; |
|
1001 |
#endif |
|
1002 |
#ifdef Q_OS_WIN |
|
1003 |
if (processFinishedNotifier) |
|
1004 |
processFinishedNotifier->setEnabled(false); |
|
1005 |
#endif |
|
1006 |
||
1007 |
// the process may have died before it got a chance to report that it was |
|
1008 |
// either running or stopped, so we will call _q_startupNotification() and |
|
1009 |
// give it a chance to emit started() or error(FailedToStart). |
|
1010 |
if (processState == QProcess::Starting) { |
|
1011 |
if (!_q_startupNotification()) |
|
1012 |
return true; |
|
1013 |
} |
|
1014 |
||
1015 |
if (dying) { |
|
1016 |
// at this point we know the process is dead. prevent |
|
1017 |
// reentering this slot recursively by calling waitForFinished() |
|
1018 |
// or opening a dialog inside slots connected to the readyRead |
|
1019 |
// signals emitted below. |
|
1020 |
return true; |
|
1021 |
} |
|
1022 |
dying = true; |
|
1023 |
||
1024 |
// in case there is data in the pipe line and this slot by chance |
|
1025 |
// got called before the read notifications, call these two slots |
|
1026 |
// so the data is made available before the process dies. |
|
1027 |
_q_canReadStandardOutput(); |
|
1028 |
_q_canReadStandardError(); |
|
1029 |
||
1030 |
findExitCode(); |
|
1031 |
||
1032 |
if (crashed) { |
|
1033 |
exitStatus = QProcess::CrashExit; |
|
1034 |
processError = QProcess::Crashed; |
|
1035 |
q->setErrorString(QProcess::tr("Process crashed")); |
|
1036 |
emit q->error(processError); |
|
1037 |
} |
|
1038 |
||
1039 |
bool wasRunning = (processState == QProcess::Running); |
|
1040 |
||
1041 |
cleanup(); |
|
1042 |
||
1043 |
if (wasRunning) { |
|
1044 |
// we received EOF now: |
|
1045 |
emit q->readChannelFinished(); |
|
1046 |
// in the future: |
|
1047 |
//emit q->standardOutputClosed(); |
|
1048 |
//emit q->standardErrorClosed(); |
|
1049 |
||
1050 |
emit q->finished(exitCode); |
|
1051 |
emit q->finished(exitCode, exitStatus); |
|
1052 |
} |
|
1053 |
#if defined QPROCESS_DEBUG |
|
1054 |
qDebug("QProcessPrivate::_q_processDied() process is dead"); |
|
1055 |
#endif |
|
1056 |
return true; |
|
1057 |
} |
|
1058 |
||
1059 |
/*! \internal |
|
1060 |
*/ |
|
1061 |
bool QProcessPrivate::_q_startupNotification() |
|
1062 |
{ |
|
1063 |
Q_Q(QProcess); |
|
1064 |
#if defined QPROCESS_DEBUG |
|
1065 |
qDebug("QProcessPrivate::startupNotification()"); |
|
1066 |
#endif |
|
1067 |
||
1068 |
if (startupSocketNotifier) |
|
1069 |
startupSocketNotifier->setEnabled(false); |
|
1070 |
if (processStarted()) { |
|
1071 |
q->setProcessState(QProcess::Running); |
|
1072 |
emit q->started(); |
|
1073 |
return true; |
|
1074 |
} |
|
1075 |
||
1076 |
q->setProcessState(QProcess::NotRunning); |
|
1077 |
processError = QProcess::FailedToStart; |
|
1078 |
emit q->error(processError); |
|
1079 |
#ifdef Q_OS_UNIX |
|
1080 |
// make sure the process manager removes this entry |
|
1081 |
waitForDeadChild(); |
|
1082 |
findExitCode(); |
|
1083 |
#endif |
|
1084 |
cleanup(); |
|
1085 |
return false; |
|
1086 |
} |
|
1087 |
||
1088 |
/*! \internal |
|
1089 |
*/ |
|
1090 |
void QProcessPrivate::closeWriteChannel() |
|
1091 |
{ |
|
1092 |
#if defined QPROCESS_DEBUG |
|
1093 |
qDebug("QProcessPrivate::closeWriteChannel()"); |
|
1094 |
#endif |
|
1095 |
if (stdinChannel.notifier) { |
|
1096 |
extern void qDeleteInEventHandler(QObject *o); |
|
1097 |
stdinChannel.notifier->setEnabled(false); |
|
1098 |
if (stdinChannel.notifier) { |
|
1099 |
qDeleteInEventHandler(stdinChannel.notifier); |
|
1100 |
stdinChannel.notifier = 0; |
|
1101 |
} |
|
1102 |
} |
|
1103 |
#ifdef Q_OS_WIN |
|
1104 |
// ### Find a better fix, feeding the process little by little |
|
1105 |
// instead. |
|
1106 |
flushPipeWriter(); |
|
1107 |
#endif |
|
1108 |
destroyPipe(stdinChannel.pipe); |
|
1109 |
} |
|
1110 |
||
1111 |
/*! |
|
1112 |
Constructs a QProcess object with the given \a parent. |
|
1113 |
*/ |
|
1114 |
QProcess::QProcess(QObject *parent) |
|
1115 |
: QIODevice(*new QProcessPrivate, parent) |
|
1116 |
{ |
|
1117 |
#if defined QPROCESS_DEBUG |
|
1118 |
qDebug("QProcess::QProcess(%p)", parent); |
|
1119 |
#endif |
|
1120 |
} |
|
1121 |
||
1122 |
/*! |
|
1123 |
Destructs the QProcess object, i.e., killing the process. |
|
1124 |
||
1125 |
Note that this function will not return until the process is |
|
1126 |
terminated. |
|
1127 |
*/ |
|
1128 |
QProcess::~QProcess() |
|
1129 |
{ |
|
1130 |
Q_D(QProcess); |
|
1131 |
if (d->processState != NotRunning) { |
|
1132 |
qWarning("QProcess: Destroyed while process is still running."); |
|
1133 |
kill(); |
|
1134 |
waitForFinished(); |
|
1135 |
} |
|
1136 |
#ifdef Q_OS_UNIX |
|
1137 |
// make sure the process manager removes this entry |
|
1138 |
d->findExitCode(); |
|
1139 |
#endif |
|
1140 |
d->cleanup(); |
|
1141 |
} |
|
1142 |
||
1143 |
/*! |
|
1144 |
\obsolete |
|
1145 |
Returns the read channel mode of the QProcess. This function is |
|
1146 |
equivalent to processChannelMode() |
|
1147 |
||
1148 |
\sa processChannelMode() |
|
1149 |
*/ |
|
1150 |
QProcess::ProcessChannelMode QProcess::readChannelMode() const |
|
1151 |
{ |
|
1152 |
return processChannelMode(); |
|
1153 |
} |
|
1154 |
||
1155 |
/*! |
|
1156 |
\obsolete |
|
1157 |
||
1158 |
Use setProcessChannelMode(\a mode) instead. |
|
1159 |
||
1160 |
\sa setProcessChannelMode() |
|
1161 |
*/ |
|
1162 |
void QProcess::setReadChannelMode(ProcessChannelMode mode) |
|
1163 |
{ |
|
1164 |
setProcessChannelMode(mode); |
|
1165 |
} |
|
1166 |
||
1167 |
/*! |
|
1168 |
\since 4.2 |
|
1169 |
||
1170 |
Returns the channel mode of the QProcess standard output and |
|
1171 |
standard error channels. |
|
1172 |
||
1173 |
\sa setProcessChannelMode(), ProcessChannelMode, setReadChannel() |
|
1174 |
*/ |
|
1175 |
QProcess::ProcessChannelMode QProcess::processChannelMode() const |
|
1176 |
{ |
|
1177 |
Q_D(const QProcess); |
|
1178 |
return d->processChannelMode; |
|
1179 |
} |
|
1180 |
||
1181 |
/*! |
|
1182 |
\since 4.2 |
|
1183 |
||
1184 |
Sets the channel mode of the QProcess standard output and standard |
|
1185 |
error channels to the \a mode specified. |
|
1186 |
This mode will be used the next time start() is called. For example: |
|
1187 |
||
1188 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 0 |
|
1189 |
||
1190 |
\sa processChannelMode(), ProcessChannelMode, setReadChannel() |
|
1191 |
*/ |
|
1192 |
void QProcess::setProcessChannelMode(ProcessChannelMode mode) |
|
1193 |
{ |
|
1194 |
Q_D(QProcess); |
|
1195 |
d->processChannelMode = mode; |
|
1196 |
} |
|
1197 |
||
1198 |
/*! |
|
1199 |
Returns the current read channel of the QProcess. |
|
1200 |
||
1201 |
\sa setReadChannel() |
|
1202 |
*/ |
|
1203 |
QProcess::ProcessChannel QProcess::readChannel() const |
|
1204 |
{ |
|
1205 |
Q_D(const QProcess); |
|
1206 |
return d->processChannel; |
|
1207 |
} |
|
1208 |
||
1209 |
/*! |
|
1210 |
Sets the current read channel of the QProcess to the given \a |
|
1211 |
channel. The current input channel is used by the functions |
|
1212 |
read(), readAll(), readLine(), and getChar(). It also determines |
|
1213 |
which channel triggers QProcess to emit readyRead(). |
|
1214 |
||
1215 |
\sa readChannel() |
|
1216 |
*/ |
|
1217 |
void QProcess::setReadChannel(ProcessChannel channel) |
|
1218 |
{ |
|
1219 |
Q_D(QProcess); |
|
1220 |
if (d->processChannel != channel) { |
|
1221 |
QByteArray buf = d->buffer.readAll(); |
|
1222 |
if (d->processChannel == QProcess::StandardOutput) { |
|
1223 |
for (int i = buf.size() - 1; i >= 0; --i) |
|
1224 |
d->outputReadBuffer.ungetChar(buf.at(i)); |
|
1225 |
} else { |
|
1226 |
for (int i = buf.size() - 1; i >= 0; --i) |
|
1227 |
d->errorReadBuffer.ungetChar(buf.at(i)); |
|
1228 |
} |
|
1229 |
} |
|
1230 |
d->processChannel = channel; |
|
1231 |
} |
|
1232 |
||
1233 |
/*! |
|
1234 |
Closes the read channel \a channel. After calling this function, |
|
1235 |
QProcess will no longer receive data on the channel. Any data that |
|
1236 |
has already been received is still available for reading. |
|
1237 |
||
1238 |
Call this function to save memory, if you are not interested in |
|
1239 |
the output of the process. |
|
1240 |
||
1241 |
\sa closeWriteChannel(), setReadChannel() |
|
1242 |
*/ |
|
1243 |
void QProcess::closeReadChannel(ProcessChannel channel) |
|
1244 |
{ |
|
1245 |
Q_D(QProcess); |
|
1246 |
||
1247 |
if (channel == StandardOutput) |
|
1248 |
d->stdoutChannel.closed = true; |
|
1249 |
else |
|
1250 |
d->stderrChannel.closed = true; |
|
1251 |
} |
|
1252 |
||
1253 |
/*! |
|
1254 |
Schedules the write channel of QProcess to be closed. The channel |
|
1255 |
will close once all data has been written to the process. After |
|
1256 |
calling this function, any attempts to write to the process will |
|
1257 |
fail. |
|
1258 |
||
1259 |
Closing the write channel is necessary for programs that read |
|
1260 |
input data until the channel has been closed. For example, the |
|
1261 |
program "more" is used to display text data in a console on both |
|
1262 |
Unix and Windows. But it will not display the text data until |
|
1263 |
QProcess's write channel has been closed. Example: |
|
1264 |
||
1265 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 1 |
|
1266 |
||
1267 |
The write channel is implicitly opened when start() is called. |
|
1268 |
||
1269 |
\sa closeReadChannel() |
|
1270 |
*/ |
|
1271 |
void QProcess::closeWriteChannel() |
|
1272 |
{ |
|
1273 |
Q_D(QProcess); |
|
1274 |
d->stdinChannel.closed = true; // closing |
|
1275 |
if (d->writeBuffer.isEmpty()) |
|
1276 |
d->closeWriteChannel(); |
|
1277 |
} |
|
1278 |
||
1279 |
/*! |
|
1280 |
\since 4.2 |
|
1281 |
||
1282 |
Redirects the process' standard input to the file indicated by \a |
|
1283 |
fileName. When an input redirection is in place, the QProcess |
|
1284 |
object will be in read-only mode (calling write() will result in |
|
1285 |
error). |
|
1286 |
||
1287 |
If the file \a fileName does not exist at the moment start() is |
|
1288 |
called or is not readable, starting the process will fail. |
|
1289 |
||
1290 |
Calling setStandardInputFile() after the process has started has no |
|
1291 |
effect. |
|
1292 |
||
1293 |
\sa setStandardOutputFile(), setStandardErrorFile(), |
|
1294 |
setStandardOutputProcess() |
|
1295 |
*/ |
|
1296 |
void QProcess::setStandardInputFile(const QString &fileName) |
|
1297 |
{ |
|
1298 |
Q_D(QProcess); |
|
1299 |
d->stdinChannel = fileName; |
|
1300 |
} |
|
1301 |
||
1302 |
/*! |
|
1303 |
\since 4.2 |
|
1304 |
||
1305 |
Redirects the process' standard output to the file \a |
|
1306 |
fileName. When the redirection is in place, the standard output |
|
1307 |
read channel is closed: reading from it using read() will always |
|
1308 |
fail, as will readAllStandardOutput(). |
|
1309 |
||
1310 |
If the file \a fileName doesn't exist at the moment start() is |
|
1311 |
called, it will be created. If it cannot be created, the starting |
|
1312 |
will fail. |
|
1313 |
||
1314 |
If the file exists and \a mode is QIODevice::Truncate, the file |
|
1315 |
will be truncated. Otherwise (if \a mode is QIODevice::Append), |
|
1316 |
the file will be appended to. |
|
1317 |
||
1318 |
Calling setStandardOutputFile() after the process has started has |
|
1319 |
no effect. |
|
1320 |
||
1321 |
\sa setStandardInputFile(), setStandardErrorFile(), |
|
1322 |
setStandardOutputProcess() |
|
1323 |
*/ |
|
1324 |
void QProcess::setStandardOutputFile(const QString &fileName, OpenMode mode) |
|
1325 |
{ |
|
1326 |
Q_ASSERT(mode == Append || mode == Truncate); |
|
1327 |
Q_D(QProcess); |
|
1328 |
||
1329 |
d->stdoutChannel = fileName; |
|
1330 |
d->stdoutChannel.append = mode == Append; |
|
1331 |
} |
|
1332 |
||
1333 |
/*! |
|
1334 |
\since 4.2 |
|
1335 |
||
1336 |
Redirects the process' standard error to the file \a |
|
1337 |
fileName. When the redirection is in place, the standard error |
|
1338 |
read channel is closed: reading from it using read() will always |
|
1339 |
fail, as will readAllStandardError(). The file will be appended to |
|
1340 |
if \a mode is Append, otherwise, it will be truncated. |
|
1341 |
||
1342 |
See setStandardOutputFile() for more information on how the file |
|
1343 |
is opened. |
|
1344 |
||
1345 |
Note: if setProcessChannelMode() was called with an argument of |
|
1346 |
QProcess::MergedChannels, this function has no effect. |
|
1347 |
||
1348 |
\sa setStandardInputFile(), setStandardOutputFile(), |
|
1349 |
setStandardOutputProcess() |
|
1350 |
*/ |
|
1351 |
void QProcess::setStandardErrorFile(const QString &fileName, OpenMode mode) |
|
1352 |
{ |
|
1353 |
Q_ASSERT(mode == Append || mode == Truncate); |
|
1354 |
Q_D(QProcess); |
|
1355 |
||
1356 |
d->stderrChannel = fileName; |
|
1357 |
d->stderrChannel.append = mode == Append; |
|
1358 |
} |
|
1359 |
||
1360 |
/*! |
|
1361 |
\since 4.2 |
|
1362 |
||
1363 |
Pipes the standard output stream of this process to the \a |
|
1364 |
destination process' standard input. |
|
1365 |
||
1366 |
The following shell command: |
|
1367 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 2 |
|
1368 |
||
1369 |
Can be accomplished with QProcesses with the following code: |
|
1370 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 3 |
|
1371 |
*/ |
|
1372 |
void QProcess::setStandardOutputProcess(QProcess *destination) |
|
1373 |
{ |
|
1374 |
QProcessPrivate *dfrom = d_func(); |
|
1375 |
QProcessPrivate *dto = destination->d_func(); |
|
1376 |
dfrom->stdoutChannel.pipeTo(dto); |
|
1377 |
dto->stdinChannel.pipeFrom(dfrom); |
|
1378 |
} |
|
1379 |
||
1380 |
/*! |
|
1381 |
If QProcess has been assigned a working directory, this function returns |
|
1382 |
the working directory that the QProcess will enter before the program has |
|
1383 |
started. Otherwise, (i.e., no directory has been assigned,) an empty |
|
1384 |
string is returned, and QProcess will use the application's current |
|
1385 |
working directory instead. |
|
1386 |
||
1387 |
\sa setWorkingDirectory() |
|
1388 |
*/ |
|
1389 |
QString QProcess::workingDirectory() const |
|
1390 |
{ |
|
1391 |
Q_D(const QProcess); |
|
1392 |
return d->workingDirectory; |
|
1393 |
} |
|
1394 |
||
1395 |
/*! |
|
1396 |
Sets the working directory to \a dir. QProcess will start the |
|
1397 |
process in this directory. The default behavior is to start the |
|
1398 |
process in the working directory of the calling process. |
|
1399 |
||
1400 |
\note The working directory setting is ignored on Symbian; |
|
1401 |
the private directory of the process is considered its working |
|
1402 |
directory. |
|
1403 |
||
1404 |
\sa workingDirectory(), start() |
|
1405 |
*/ |
|
1406 |
void QProcess::setWorkingDirectory(const QString &dir) |
|
1407 |
{ |
|
1408 |
Q_D(QProcess); |
|
1409 |
d->workingDirectory = dir; |
|
1410 |
} |
|
1411 |
||
1412 |
/*! |
|
1413 |
Returns the native process identifier for the running process, if |
|
1414 |
available. If no process is currently running, 0 is returned. |
|
1415 |
*/ |
|
1416 |
Q_PID QProcess::pid() const |
|
1417 |
{ |
|
1418 |
Q_D(const QProcess); |
|
1419 |
return d->pid; |
|
1420 |
} |
|
1421 |
||
1422 |
/*! \reimp |
|
1423 |
||
1424 |
This function operates on the current read channel. |
|
1425 |
||
1426 |
\sa readChannel(), setReadChannel() |
|
1427 |
*/ |
|
1428 |
bool QProcess::canReadLine() const |
|
1429 |
{ |
|
1430 |
Q_D(const QProcess); |
|
1431 |
const QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError) |
|
1432 |
? &d->errorReadBuffer |
|
1433 |
: &d->outputReadBuffer; |
|
1434 |
return readBuffer->canReadLine() || QIODevice::canReadLine(); |
|
1435 |
} |
|
1436 |
||
1437 |
/*! |
|
1438 |
Closes all communication with the process and kills it. After calling this |
|
1439 |
function, QProcess will no longer emit readyRead(), and data can no |
|
1440 |
longer be read or written. |
|
1441 |
*/ |
|
1442 |
void QProcess::close() |
|
1443 |
{ |
|
1444 |
emit aboutToClose(); |
|
1445 |
while (waitForBytesWritten(-1)) |
|
1446 |
; |
|
1447 |
kill(); |
|
1448 |
waitForFinished(-1); |
|
1449 |
QIODevice::close(); |
|
1450 |
} |
|
1451 |
||
1452 |
/*! \reimp |
|
1453 |
||
1454 |
Returns true if the process is not running, and no more data is available |
|
1455 |
for reading; otherwise returns false. |
|
1456 |
*/ |
|
1457 |
bool QProcess::atEnd() const |
|
1458 |
{ |
|
1459 |
Q_D(const QProcess); |
|
1460 |
const QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError) |
|
1461 |
? &d->errorReadBuffer |
|
1462 |
: &d->outputReadBuffer; |
|
1463 |
return QIODevice::atEnd() && (!isOpen() || readBuffer->isEmpty()); |
|
1464 |
} |
|
1465 |
||
1466 |
/*! \reimp |
|
1467 |
*/ |
|
1468 |
bool QProcess::isSequential() const |
|
1469 |
{ |
|
1470 |
return true; |
|
1471 |
} |
|
1472 |
||
1473 |
/*! \reimp |
|
1474 |
*/ |
|
1475 |
qint64 QProcess::bytesAvailable() const |
|
1476 |
{ |
|
1477 |
Q_D(const QProcess); |
|
1478 |
const QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError) |
|
1479 |
? &d->errorReadBuffer |
|
1480 |
: &d->outputReadBuffer; |
|
1481 |
#if defined QPROCESS_DEBUG |
|
1482 |
qDebug("QProcess::bytesAvailable() == %i (%s)", readBuffer->size(), |
|
1483 |
(d->processChannel == QProcess::StandardError) ? "stderr" : "stdout"); |
|
1484 |
#endif |
|
1485 |
return readBuffer->size() + QIODevice::bytesAvailable(); |
|
1486 |
} |
|
1487 |
||
1488 |
/*! \reimp |
|
1489 |
*/ |
|
1490 |
qint64 QProcess::bytesToWrite() const |
|
1491 |
{ |
|
1492 |
Q_D(const QProcess); |
|
1493 |
qint64 size = d->writeBuffer.size(); |
|
1494 |
#ifdef Q_OS_WIN |
|
1495 |
size += d->pipeWriterBytesToWrite(); |
|
1496 |
#endif |
|
1497 |
return size; |
|
1498 |
} |
|
1499 |
||
1500 |
/*! |
|
1501 |
Returns the type of error that occurred last. |
|
1502 |
||
1503 |
\sa state() |
|
1504 |
*/ |
|
1505 |
QProcess::ProcessError QProcess::error() const |
|
1506 |
{ |
|
1507 |
Q_D(const QProcess); |
|
1508 |
return d->processError; |
|
1509 |
} |
|
1510 |
||
1511 |
/*! |
|
1512 |
Returns the current state of the process. |
|
1513 |
||
1514 |
\sa stateChanged(), error() |
|
1515 |
*/ |
|
1516 |
QProcess::ProcessState QProcess::state() const |
|
1517 |
{ |
|
1518 |
Q_D(const QProcess); |
|
1519 |
return d->processState; |
|
1520 |
} |
|
1521 |
||
1522 |
/*! |
|
1523 |
\deprecated |
|
1524 |
Sets the environment that QProcess will use when starting a process to the |
|
1525 |
\a environment specified which consists of a list of key=value pairs. |
|
1526 |
||
1527 |
For example, the following code adds the \c{C:\\BIN} directory to the list of |
|
1528 |
executable paths (\c{PATHS}) on Windows: |
|
1529 |
||
1530 |
\snippet doc/src/snippets/qprocess-environment/main.cpp 0 |
|
1531 |
||
1532 |
\note This function is less efficient than the setProcessEnvironment() |
|
1533 |
function. |
|
1534 |
||
1535 |
\sa environment(), setProcessEnvironment(), systemEnvironment() |
|
1536 |
*/ |
|
1537 |
void QProcess::setEnvironment(const QStringList &environment) |
|
1538 |
{ |
|
1539 |
setProcessEnvironment(QProcessEnvironmentPrivate::fromList(environment)); |
|
1540 |
} |
|
1541 |
||
1542 |
/*! |
|
1543 |
\deprecated |
|
1544 |
Returns the environment that QProcess will use when starting a |
|
1545 |
process, or an empty QStringList if no environment has been set |
|
1546 |
using setEnvironment() or setEnvironmentHash(). If no environment |
|
1547 |
has been set, the environment of the calling process will be used. |
|
1548 |
||
1549 |
\note The environment settings are ignored on Windows CE and Symbian, |
|
1550 |
as there is no concept of an environment. |
|
1551 |
||
1552 |
\sa processEnvironment(), setEnvironment(), systemEnvironment() |
|
1553 |
*/ |
|
1554 |
QStringList QProcess::environment() const |
|
1555 |
{ |
|
1556 |
Q_D(const QProcess); |
|
1557 |
return d->environment.toStringList(); |
|
1558 |
} |
|
1559 |
||
1560 |
/*! |
|
1561 |
\since 4.6 |
|
1562 |
Sets the environment that QProcess will use when starting a process to the |
|
1563 |
\a environment object. |
|
1564 |
||
1565 |
For example, the following code adds the \c{C:\\BIN} directory to the list of |
|
1566 |
executable paths (\c{PATHS}) on Windows and sets \c{TMPDIR}: |
|
1567 |
||
1568 |
\snippet doc/src/snippets/qprocess-environment/main.cpp 1 |
|
1569 |
||
1570 |
Note how, on Windows, environment variable names are case-insensitive. |
|
1571 |
||
1572 |
\sa processEnvironment(), QProcessEnvironment::systemEnvironment(), setEnvironment() |
|
1573 |
*/ |
|
1574 |
void QProcess::setProcessEnvironment(const QProcessEnvironment &environment) |
|
1575 |
{ |
|
1576 |
Q_D(QProcess); |
|
1577 |
d->environment = environment; |
|
1578 |
} |
|
1579 |
||
1580 |
/*! |
|
1581 |
\since 4.6 |
|
1582 |
Returns the environment that QProcess will use when starting a |
|
1583 |
process, or an empty object if no environment has been set using |
|
1584 |
setEnvironment() or setProcessEnvironment(). If no environment has |
|
1585 |
been set, the environment of the calling process will be used. |
|
1586 |
||
1587 |
\note The environment settings are ignored on Windows CE, |
|
1588 |
as there is no concept of an environment. |
|
1589 |
||
1590 |
\sa setProcessEnvironment(), setEnvironment(), QProcessEnvironment::isEmpty() |
|
1591 |
*/ |
|
1592 |
QProcessEnvironment QProcess::processEnvironment() const |
|
1593 |
{ |
|
1594 |
Q_D(const QProcess); |
|
1595 |
return d->environment; |
|
1596 |
} |
|
1597 |
||
1598 |
/*! |
|
1599 |
Blocks until the process has started and the started() signal has |
|
1600 |
been emitted, or until \a msecs milliseconds have passed. |
|
1601 |
||
1602 |
Returns true if the process was started successfully; otherwise |
|
1603 |
returns false (if the operation timed out or if an error |
|
1604 |
occurred). |
|
1605 |
||
1606 |
This function can operate without an event loop. It is |
|
1607 |
useful when writing non-GUI applications and when performing |
|
1608 |
I/O operations in a non-GUI thread. |
|
1609 |
||
1610 |
\warning Calling this function from the main (GUI) thread |
|
1611 |
might cause your user interface to freeze. |
|
1612 |
||
1613 |
If msecs is -1, this function will not time out. |
|
1614 |
||
1615 |
\sa started(), waitForReadyRead(), waitForBytesWritten(), waitForFinished() |
|
1616 |
*/ |
|
1617 |
bool QProcess::waitForStarted(int msecs) |
|
1618 |
{ |
|
1619 |
Q_D(QProcess); |
|
1620 |
if (d->processState == QProcess::Starting) { |
|
1621 |
if (!d->waitForStarted(msecs)) |
|
1622 |
return false; |
|
1623 |
setProcessState(QProcess::Running); |
|
1624 |
emit started(); |
|
1625 |
} |
|
1626 |
return d->processState == QProcess::Running; |
|
1627 |
} |
|
1628 |
||
1629 |
/*! \reimp |
|
1630 |
*/ |
|
1631 |
bool QProcess::waitForReadyRead(int msecs) |
|
1632 |
{ |
|
1633 |
Q_D(QProcess); |
|
1634 |
||
1635 |
if (d->processState == QProcess::NotRunning) |
|
1636 |
return false; |
|
1637 |
if (d->processChannel == QProcess::StandardOutput && d->stdoutChannel.closed) |
|
1638 |
return false; |
|
1639 |
if (d->processChannel == QProcess::StandardError && d->stderrChannel.closed) |
|
1640 |
return false; |
|
1641 |
return d->waitForReadyRead(msecs); |
|
1642 |
} |
|
1643 |
||
1644 |
/*! \reimp |
|
1645 |
*/ |
|
1646 |
bool QProcess::waitForBytesWritten(int msecs) |
|
1647 |
{ |
|
1648 |
Q_D(QProcess); |
|
1649 |
if (d->processState == QProcess::NotRunning) |
|
1650 |
return false; |
|
1651 |
if (d->processState == QProcess::Starting) { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
1652 |
QElapsedTimer stopWatch; |
0 | 1653 |
stopWatch.start(); |
1654 |
bool started = waitForStarted(msecs); |
|
1655 |
if (!started) |
|
1656 |
return false; |
|
1657 |
if (msecs != -1) |
|
1658 |
msecs -= stopWatch.elapsed(); |
|
1659 |
} |
|
1660 |
||
1661 |
return d->waitForBytesWritten(msecs); |
|
1662 |
} |
|
1663 |
||
1664 |
/*! |
|
1665 |
Blocks until the process has finished and the finished() signal |
|
1666 |
has been emitted, or until \a msecs milliseconds have passed. |
|
1667 |
||
1668 |
Returns true if the process finished; otherwise returns false (if |
|
1669 |
the operation timed out, if an error occurred, or if this QProcess |
|
1670 |
is already finished). |
|
1671 |
||
1672 |
This function can operate without an event loop. It is |
|
1673 |
useful when writing non-GUI applications and when performing |
|
1674 |
I/O operations in a non-GUI thread. |
|
1675 |
||
1676 |
\warning Calling this function from the main (GUI) thread |
|
1677 |
might cause your user interface to freeze. |
|
1678 |
||
1679 |
If msecs is -1, this function will not time out. |
|
1680 |
||
1681 |
\sa finished(), waitForStarted(), waitForReadyRead(), waitForBytesWritten() |
|
1682 |
*/ |
|
1683 |
bool QProcess::waitForFinished(int msecs) |
|
1684 |
{ |
|
1685 |
Q_D(QProcess); |
|
1686 |
if (d->processState == QProcess::NotRunning) |
|
1687 |
return false; |
|
1688 |
if (d->processState == QProcess::Starting) { |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
1689 |
QElapsedTimer stopWatch; |
0 | 1690 |
stopWatch.start(); |
1691 |
bool started = waitForStarted(msecs); |
|
1692 |
if (!started) |
|
1693 |
return false; |
|
1694 |
if (msecs != -1) |
|
1695 |
msecs -= stopWatch.elapsed(); |
|
1696 |
} |
|
1697 |
||
1698 |
return d->waitForFinished(msecs); |
|
1699 |
} |
|
1700 |
||
1701 |
/*! |
|
1702 |
Sets the current state of the QProcess to the \a state specified. |
|
1703 |
||
1704 |
\sa state() |
|
1705 |
*/ |
|
1706 |
void QProcess::setProcessState(ProcessState state) |
|
1707 |
{ |
|
1708 |
Q_D(QProcess); |
|
1709 |
if (d->processState == state) |
|
1710 |
return; |
|
1711 |
d->processState = state; |
|
1712 |
emit stateChanged(state); |
|
1713 |
} |
|
1714 |
||
1715 |
/*! |
|
1716 |
This function is called in the child process context just before the |
|
1717 |
program is executed on Unix or Mac OS X (i.e., after \e fork(), but before |
|
1718 |
\e execve()). Reimplement this function to do last minute initialization |
|
1719 |
of the child process. Example: |
|
1720 |
||
1721 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 4 |
|
1722 |
||
1723 |
You cannot exit the process (by calling exit(), for instance) from |
|
1724 |
this function. If you need to stop the program before it starts |
|
1725 |
execution, your workaround is to emit finished() and then call |
|
1726 |
exit(). |
|
1727 |
||
1728 |
\warning This function is called by QProcess on Unix and Mac OS X |
|
1729 |
only. On Windows, it is not called. |
|
1730 |
*/ |
|
1731 |
void QProcess::setupChildProcess() |
|
1732 |
{ |
|
1733 |
} |
|
1734 |
||
1735 |
/*! \reimp |
|
1736 |
*/ |
|
1737 |
qint64 QProcess::readData(char *data, qint64 maxlen) |
|
1738 |
{ |
|
1739 |
Q_D(QProcess); |
|
1740 |
QRingBuffer *readBuffer = (d->processChannel == QProcess::StandardError) |
|
1741 |
? &d->errorReadBuffer |
|
1742 |
: &d->outputReadBuffer; |
|
1743 |
||
1744 |
if (maxlen == 1 && !readBuffer->isEmpty()) { |
|
1745 |
int c = readBuffer->getChar(); |
|
1746 |
if (c == -1) { |
|
1747 |
#if defined QPROCESS_DEBUG |
|
1748 |
qDebug("QProcess::readData(%p \"%s\", %d) == -1", |
|
1749 |
data, qt_prettyDebug(data, 1, maxlen).constData(), 1); |
|
1750 |
#endif |
|
1751 |
return -1; |
|
1752 |
} |
|
1753 |
*data = (char) c; |
|
1754 |
#if defined QPROCESS_DEBUG |
|
1755 |
qDebug("QProcess::readData(%p \"%s\", %d) == 1", |
|
1756 |
data, qt_prettyDebug(data, 1, maxlen).constData(), 1); |
|
1757 |
#endif |
|
1758 |
return 1; |
|
1759 |
} |
|
1760 |
||
1761 |
qint64 bytesToRead = qint64(qMin(readBuffer->size(), (int)maxlen)); |
|
1762 |
qint64 readSoFar = 0; |
|
1763 |
while (readSoFar < bytesToRead) { |
|
1764 |
const char *ptr = readBuffer->readPointer(); |
|
1765 |
int bytesToReadFromThisBlock = qMin<qint64>(bytesToRead - readSoFar, |
|
1766 |
readBuffer->nextDataBlockSize()); |
|
1767 |
memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock); |
|
1768 |
readSoFar += bytesToReadFromThisBlock; |
|
1769 |
readBuffer->free(bytesToReadFromThisBlock); |
|
1770 |
} |
|
1771 |
||
1772 |
#if defined QPROCESS_DEBUG |
|
1773 |
qDebug("QProcess::readData(%p \"%s\", %lld) == %lld", |
|
1774 |
data, qt_prettyDebug(data, readSoFar, 16).constData(), maxlen, readSoFar); |
|
1775 |
#endif |
|
1776 |
if (!readSoFar && d->processState == QProcess::NotRunning) |
|
1777 |
return -1; // EOF |
|
1778 |
return readSoFar; |
|
1779 |
} |
|
1780 |
||
1781 |
/*! \reimp |
|
1782 |
*/ |
|
1783 |
qint64 QProcess::writeData(const char *data, qint64 len) |
|
1784 |
{ |
|
1785 |
Q_D(QProcess); |
|
1786 |
||
1787 |
#if defined(Q_OS_WINCE) |
|
1788 |
Q_UNUSED(data); |
|
1789 |
Q_UNUSED(len); |
|
1790 |
d->processError = QProcess::WriteError; |
|
1791 |
setErrorString(tr("Error writing to process")); |
|
1792 |
emit error(d->processError); |
|
1793 |
return -1; |
|
1794 |
#endif |
|
1795 |
||
1796 |
if (d->stdinChannel.closed) { |
|
1797 |
#if defined QPROCESS_DEBUG |
|
1798 |
qDebug("QProcess::writeData(%p \"%s\", %lld) == 0 (write channel closing)", |
|
1799 |
data, qt_prettyDebug(data, len, 16).constData(), len); |
|
1800 |
#endif |
|
1801 |
return 0; |
|
1802 |
} |
|
1803 |
||
1804 |
if (len == 1) { |
|
1805 |
d->writeBuffer.putChar(*data); |
|
1806 |
if (d->stdinChannel.notifier) |
|
1807 |
d->stdinChannel.notifier->setEnabled(true); |
|
1808 |
#if defined QPROCESS_DEBUG |
|
1809 |
qDebug("QProcess::writeData(%p \"%s\", %lld) == 1 (written to buffer)", |
|
1810 |
data, qt_prettyDebug(data, len, 16).constData(), len); |
|
1811 |
#endif |
|
1812 |
return 1; |
|
1813 |
} |
|
1814 |
||
1815 |
char *dest = d->writeBuffer.reserve(len); |
|
1816 |
memcpy(dest, data, len); |
|
1817 |
if (d->stdinChannel.notifier) |
|
1818 |
d->stdinChannel.notifier->setEnabled(true); |
|
1819 |
#if defined QPROCESS_DEBUG |
|
1820 |
qDebug("QProcess::writeData(%p \"%s\", %lld) == %lld (written to buffer)", |
|
1821 |
data, qt_prettyDebug(data, len, 16).constData(), len, len); |
|
1822 |
#endif |
|
1823 |
return len; |
|
1824 |
} |
|
1825 |
||
1826 |
/*! |
|
1827 |
Regardless of the current read channel, this function returns all |
|
1828 |
data available from the standard output of the process as a |
|
1829 |
QByteArray. |
|
1830 |
||
1831 |
\sa readyReadStandardOutput(), readAllStandardError(), readChannel(), setReadChannel() |
|
1832 |
*/ |
|
1833 |
QByteArray QProcess::readAllStandardOutput() |
|
1834 |
{ |
|
1835 |
ProcessChannel tmp = readChannel(); |
|
1836 |
setReadChannel(StandardOutput); |
|
1837 |
QByteArray data = readAll(); |
|
1838 |
setReadChannel(tmp); |
|
1839 |
return data; |
|
1840 |
} |
|
1841 |
||
1842 |
/*! |
|
1843 |
Regardless of the current read channel, this function returns all |
|
1844 |
data available from the standard error of the process as a |
|
1845 |
QByteArray. |
|
1846 |
||
1847 |
\sa readyReadStandardError(), readAllStandardOutput(), readChannel(), setReadChannel() |
|
1848 |
*/ |
|
1849 |
QByteArray QProcess::readAllStandardError() |
|
1850 |
{ |
|
1851 |
ProcessChannel tmp = readChannel(); |
|
1852 |
setReadChannel(StandardError); |
|
1853 |
QByteArray data = readAll(); |
|
1854 |
setReadChannel(tmp); |
|
1855 |
return data; |
|
1856 |
} |
|
1857 |
||
1858 |
/*! |
|
1859 |
Starts the program \a program in a new process, if one is not already |
|
1860 |
running, passing the command line arguments in \a arguments. The OpenMode |
|
1861 |
is set to \a mode. |
|
1862 |
||
1863 |
The QProcess object will immediately enter the Starting state. If the |
|
1864 |
process starts successfully, QProcess will emit started(); otherwise, |
|
1865 |
error() will be emitted. If the QProcess object is already running a |
|
1866 |
process, a warning may be printed at the console, and the existing |
|
1867 |
process will continue running. |
|
1868 |
||
1869 |
\note Arguments that contain spaces are not passed to the |
|
1870 |
process as separate arguments. |
|
1871 |
||
1872 |
\note Processes are started asynchronously, which means the started() |
|
1873 |
and error() signals may be delayed. Call waitForStarted() to make |
|
1874 |
sure the process has started (or has failed to start) and those signals |
|
1875 |
have been emitted. |
|
1876 |
||
1877 |
\bold{Windows:} Arguments that contain spaces are wrapped in quotes. |
|
1878 |
||
1879 |
\sa pid(), started(), waitForStarted() |
|
1880 |
*/ |
|
1881 |
void QProcess::start(const QString &program, const QStringList &arguments, OpenMode mode) |
|
1882 |
{ |
|
1883 |
Q_D(QProcess); |
|
1884 |
if (d->processState != NotRunning) { |
|
1885 |
qWarning("QProcess::start: Process is already running"); |
|
1886 |
return; |
|
1887 |
} |
|
1888 |
||
1889 |
#if defined QPROCESS_DEBUG |
|
1890 |
qDebug() << "QProcess::start(" << program << ',' << arguments << ',' << mode << ')'; |
|
1891 |
#endif |
|
1892 |
||
1893 |
d->outputReadBuffer.clear(); |
|
1894 |
d->errorReadBuffer.clear(); |
|
1895 |
||
1896 |
if (d->stdinChannel.type != QProcessPrivate::Channel::Normal) |
|
1897 |
mode &= ~WriteOnly; // not open for writing |
|
1898 |
if (d->stdoutChannel.type != QProcessPrivate::Channel::Normal && |
|
1899 |
(d->stderrChannel.type != QProcessPrivate::Channel::Normal || |
|
1900 |
d->processChannelMode == MergedChannels)) |
|
1901 |
mode &= ~ReadOnly; // not open for reading |
|
1902 |
if (mode == 0) |
|
1903 |
mode = Unbuffered; |
|
1904 |
QIODevice::open(mode); |
|
1905 |
||
1906 |
d->stdinChannel.closed = false; |
|
1907 |
d->stdoutChannel.closed = false; |
|
1908 |
d->stderrChannel.closed = false; |
|
1909 |
||
1910 |
d->program = program; |
|
1911 |
d->arguments = arguments; |
|
1912 |
||
1913 |
d->exitCode = 0; |
|
1914 |
d->exitStatus = NormalExit; |
|
1915 |
d->processError = QProcess::UnknownError; |
|
1916 |
d->errorString.clear(); |
|
1917 |
d->startProcess(); |
|
1918 |
} |
|
1919 |
||
1920 |
||
1921 |
static QStringList parseCombinedArgString(const QString &program) |
|
1922 |
{ |
|
1923 |
QStringList args; |
|
1924 |
QString tmp; |
|
1925 |
int quoteCount = 0; |
|
1926 |
bool inQuote = false; |
|
1927 |
||
1928 |
// handle quoting. tokens can be surrounded by double quotes |
|
1929 |
// "hello world". three consecutive double quotes represent |
|
1930 |
// the quote character itself. |
|
1931 |
for (int i = 0; i < program.size(); ++i) { |
|
1932 |
if (program.at(i) == QLatin1Char('"')) { |
|
1933 |
++quoteCount; |
|
1934 |
if (quoteCount == 3) { |
|
1935 |
// third consecutive quote |
|
1936 |
quoteCount = 0; |
|
1937 |
tmp += program.at(i); |
|
1938 |
} |
|
1939 |
continue; |
|
1940 |
} |
|
1941 |
if (quoteCount) { |
|
1942 |
if (quoteCount == 1) |
|
1943 |
inQuote = !inQuote; |
|
1944 |
quoteCount = 0; |
|
1945 |
} |
|
1946 |
if (!inQuote && program.at(i).isSpace()) { |
|
1947 |
if (!tmp.isEmpty()) { |
|
1948 |
args += tmp; |
|
1949 |
tmp.clear(); |
|
1950 |
} |
|
1951 |
} else { |
|
1952 |
tmp += program.at(i); |
|
1953 |
} |
|
1954 |
} |
|
1955 |
if (!tmp.isEmpty()) |
|
1956 |
args += tmp; |
|
1957 |
||
1958 |
return args; |
|
1959 |
} |
|
1960 |
||
1961 |
/*! |
|
1962 |
\overload |
|
1963 |
||
1964 |
Starts the program \a program in a new process, if one is not already |
|
1965 |
running. \a program is a single string of text containing both the |
|
1966 |
program name and its arguments. The arguments are separated by one or |
|
1967 |
more spaces. For example: |
|
1968 |
||
1969 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 5 |
|
1970 |
||
1971 |
The \a program string can also contain quotes, to ensure that arguments |
|
1972 |
containing spaces are correctly supplied to the new process. For example: |
|
1973 |
||
1974 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 6 |
|
1975 |
||
1976 |
If the QProcess object is already running a process, a warning may be |
|
1977 |
printed at the console, and the existing process will continue running. |
|
1978 |
||
1979 |
Note that, on Windows, quotes need to be both escaped and quoted. |
|
1980 |
For example, the above code would be specified in the following |
|
1981 |
way to ensure that \c{"My Documents"} is used as the argument to |
|
1982 |
the \c dir executable: |
|
1983 |
||
1984 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 7 |
|
1985 |
||
1986 |
The OpenMode is set to \a mode. |
|
1987 |
*/ |
|
1988 |
void QProcess::start(const QString &program, OpenMode mode) |
|
1989 |
{ |
|
1990 |
QStringList args = parseCombinedArgString(program); |
|
1991 |
if (args.isEmpty()) { |
|
1992 |
Q_D(QProcess); |
|
1993 |
d->processError = QProcess::FailedToStart; |
|
1994 |
setErrorString(tr("No program defined")); |
|
1995 |
emit error(d->processError); |
|
1996 |
return; |
|
1997 |
} |
|
1998 |
||
1999 |
QString prog = args.first(); |
|
2000 |
args.removeFirst(); |
|
2001 |
||
2002 |
start(prog, args, mode); |
|
2003 |
} |
|
2004 |
||
2005 |
/*! |
|
2006 |
Attempts to terminate the process. |
|
2007 |
||
2008 |
The process may not exit as a result of calling this function (it is given |
|
2009 |
the chance to prompt the user for any unsaved files, etc). |
|
2010 |
||
2011 |
On Windows, terminate() posts a WM_CLOSE message to all toplevel windows |
|
2012 |
of the process and then to the main thread of the process itself. On Unix |
|
2013 |
and Mac OS X the SIGTERM signal is sent. |
|
2014 |
||
2015 |
Console applications on Windows that do not run an event loop, or whose |
|
2016 |
event loop does not handle the WM_CLOSE message, can only be terminated by |
|
2017 |
calling kill(). |
|
2018 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
2019 |
On Symbian, this function requires platform security capability |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
2020 |
\c PowerMgmt. If absent, the process will panic with KERN-EXEC 46. |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
2021 |
|
0 | 2022 |
\note Terminating running processes from other processes will typically |
2023 |
cause a panic in Symbian due to platform security. |
|
2024 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
2025 |
\sa {Symbian Platform Security Requirements} |
0 | 2026 |
\sa kill() |
2027 |
*/ |
|
2028 |
void QProcess::terminate() |
|
2029 |
{ |
|
2030 |
Q_D(QProcess); |
|
2031 |
d->terminateProcess(); |
|
2032 |
} |
|
2033 |
||
2034 |
/*! |
|
2035 |
Kills the current process, causing it to exit immediately. |
|
2036 |
||
2037 |
On Windows, kill() uses TerminateProcess, and on Unix and Mac OS X, the |
|
2038 |
SIGKILL signal is sent to the process. |
|
2039 |
||
22
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
2040 |
On Symbian, this function requires platform security capability |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
2041 |
\c PowerMgmt. If absent, the process will panic with KERN-EXEC 46. |
79de32ba3296
Revision: 201017
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
2042 |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
22
diff
changeset
|
2043 |
\sa {Symbian Platform Security Requirements} |
0 | 2044 |
\sa terminate() |
2045 |
*/ |
|
2046 |
void QProcess::kill() |
|
2047 |
{ |
|
2048 |
Q_D(QProcess); |
|
2049 |
d->killProcess(); |
|
2050 |
} |
|
2051 |
||
2052 |
/*! |
|
2053 |
Returns the exit code of the last process that finished. |
|
2054 |
*/ |
|
2055 |
int QProcess::exitCode() const |
|
2056 |
{ |
|
2057 |
Q_D(const QProcess); |
|
2058 |
return d->exitCode; |
|
2059 |
} |
|
2060 |
||
2061 |
/*! |
|
2062 |
\since 4.1 |
|
2063 |
||
2064 |
Returns the exit status of the last process that finished. |
|
2065 |
||
2066 |
On Windows, if the process was terminated with TerminateProcess() |
|
2067 |
from another application this function will still return NormalExit |
|
2068 |
unless the exit code is less than 0. |
|
2069 |
*/ |
|
2070 |
QProcess::ExitStatus QProcess::exitStatus() const |
|
2071 |
{ |
|
2072 |
Q_D(const QProcess); |
|
2073 |
return d->exitStatus; |
|
2074 |
} |
|
2075 |
||
2076 |
/*! |
|
2077 |
Starts the program \a program with the arguments \a arguments in a |
|
2078 |
new process, waits for it to finish, and then returns the exit |
|
2079 |
code of the process. Any data the new process writes to the |
|
2080 |
console is forwarded to the calling process. |
|
2081 |
||
2082 |
The environment and working directory are inherited by the calling |
|
2083 |
process. |
|
2084 |
||
2085 |
On Windows, arguments that contain spaces are wrapped in quotes. |
|
2086 |
*/ |
|
2087 |
int QProcess::execute(const QString &program, const QStringList &arguments) |
|
2088 |
{ |
|
2089 |
QProcess process; |
|
2090 |
process.setReadChannelMode(ForwardedChannels); |
|
2091 |
process.start(program, arguments); |
|
2092 |
process.waitForFinished(-1); |
|
2093 |
return process.exitCode(); |
|
2094 |
} |
|
2095 |
||
2096 |
/*! |
|
2097 |
\overload |
|
2098 |
||
2099 |
Starts the program \a program in a new process. \a program is a |
|
2100 |
single string of text containing both the program name and its |
|
2101 |
arguments. The arguments are separated by one or more spaces. |
|
2102 |
*/ |
|
2103 |
int QProcess::execute(const QString &program) |
|
2104 |
{ |
|
2105 |
QProcess process; |
|
2106 |
process.setReadChannelMode(ForwardedChannels); |
|
2107 |
process.start(program); |
|
2108 |
process.waitForFinished(-1); |
|
2109 |
return process.exitCode(); |
|
2110 |
} |
|
2111 |
||
2112 |
/*! |
|
2113 |
Starts the program \a program with the arguments \a arguments in a |
|
2114 |
new process, and detaches from it. Returns true on success; |
|
2115 |
otherwise returns false. If the calling process exits, the |
|
2116 |
detached process will continue to live. |
|
2117 |
||
2118 |
Note that arguments that contain spaces are not passed to the |
|
2119 |
process as separate arguments. |
|
2120 |
||
2121 |
\bold{Unix:} The started process will run in its own session and act |
|
2122 |
like a daemon. |
|
2123 |
||
2124 |
\bold{Windows:} Arguments that contain spaces are wrapped in quotes. |
|
2125 |
The started process will run as a regular standalone process. |
|
2126 |
||
2127 |
The process will be started in the directory \a workingDirectory. |
|
2128 |
||
2129 |
If the function is successful then *\a pid is set to the process |
|
2130 |
identifier of the started process. |
|
2131 |
*/ |
|
2132 |
bool QProcess::startDetached(const QString &program, |
|
2133 |
const QStringList &arguments, |
|
2134 |
const QString &workingDirectory, |
|
2135 |
qint64 *pid) |
|
2136 |
{ |
|
2137 |
return QProcessPrivate::startDetached(program, |
|
2138 |
arguments, |
|
2139 |
workingDirectory, |
|
2140 |
pid); |
|
2141 |
} |
|
2142 |
||
2143 |
/*! |
|
2144 |
Starts the program \a program with the given \a arguments in a |
|
2145 |
new process, and detaches from it. Returns true on success; |
|
2146 |
otherwise returns false. If the calling process exits, the |
|
2147 |
detached process will continue to live. |
|
2148 |
||
2149 |
\note Arguments that contain spaces are not passed to the |
|
2150 |
process as separate arguments. |
|
2151 |
||
2152 |
\bold{Unix:} The started process will run in its own session and act |
|
2153 |
like a daemon. |
|
2154 |
||
2155 |
\bold{Windows:} Arguments that contain spaces are wrapped in quotes. |
|
2156 |
The started process will run as a regular standalone process. |
|
2157 |
*/ |
|
2158 |
bool QProcess::startDetached(const QString &program, |
|
2159 |
const QStringList &arguments) |
|
2160 |
{ |
|
2161 |
return QProcessPrivate::startDetached(program, arguments); |
|
2162 |
} |
|
2163 |
||
2164 |
/*! |
|
2165 |
\overload |
|
2166 |
||
2167 |
Starts the program \a program in a new process. \a program is a |
|
2168 |
single string of text containing both the program name and its |
|
2169 |
arguments. The arguments are separated by one or more spaces. |
|
2170 |
||
2171 |
The \a program string can also contain quotes, to ensure that arguments |
|
2172 |
containing spaces are correctly supplied to the new process. |
|
2173 |
*/ |
|
2174 |
bool QProcess::startDetached(const QString &program) |
|
2175 |
{ |
|
2176 |
QStringList args = parseCombinedArgString(program); |
|
2177 |
if (args.isEmpty()) |
|
2178 |
return false; |
|
2179 |
||
2180 |
QString prog = args.first(); |
|
2181 |
args.removeFirst(); |
|
2182 |
||
2183 |
return QProcessPrivate::startDetached(prog, args); |
|
2184 |
} |
|
2185 |
||
2186 |
QT_BEGIN_INCLUDE_NAMESPACE |
|
2187 |
#ifdef Q_OS_MAC |
|
2188 |
# include <crt_externs.h> |
|
2189 |
# define environ (*_NSGetEnviron()) |
|
2190 |
#elif defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
|
2191 |
static char *qt_empty_environ[] = { 0 }; |
|
2192 |
#define environ qt_empty_environ |
|
2193 |
#elif !defined(Q_OS_WIN) |
|
2194 |
extern char **environ; |
|
2195 |
#endif |
|
2196 |
QT_END_INCLUDE_NAMESPACE |
|
2197 |
||
2198 |
/*! |
|
2199 |
\since 4.1 |
|
2200 |
||
2201 |
Returns the environment of the calling process as a list of |
|
2202 |
key=value pairs. Example: |
|
2203 |
||
2204 |
\snippet doc/src/snippets/code/src_corelib_io_qprocess.cpp 8 |
|
2205 |
||
2206 |
This function does not cache the system environment. Therefore, it's |
|
2207 |
possible to obtain an updated version of the environment if low-level C |
|
2208 |
library functions like \tt setenv ot \tt putenv have been called. |
|
2209 |
||
2210 |
However, note that repeated calls to this function will recreate the |
|
2211 |
list of environment variables, which is a non-trivial operation. |
|
2212 |
||
2213 |
\note For new code, it is recommended to use QProcessEvironment::systemEnvironment() |
|
2214 |
||
2215 |
\sa QProcessEnvironment::systemEnvironment(), environment(), setEnvironment() |
|
2216 |
*/ |
|
2217 |
QStringList QProcess::systemEnvironment() |
|
2218 |
{ |
|
2219 |
QStringList tmp; |
|
2220 |
char *entry = 0; |
|
2221 |
int count = 0; |
|
2222 |
while ((entry = environ[count++])) |
|
2223 |
tmp << QString::fromLocal8Bit(entry); |
|
2224 |
return tmp; |
|
2225 |
} |
|
2226 |
||
2227 |
/*! |
|
2228 |
\since 4.6 |
|
2229 |
||
2230 |
\brief The systemEnvironment function returns the environment of |
|
2231 |
the calling process. |
|
2232 |
||
2233 |
It is returned as a QProcessEnvironment. This function does not |
|
2234 |
cache the system environment. Therefore, it's possible to obtain |
|
2235 |
an updated version of the environment if low-level C library |
|
2236 |
functions like \tt setenv ot \tt putenv have been called. |
|
2237 |
||
2238 |
However, note that repeated calls to this function will recreate the |
|
2239 |
QProcessEnvironment object, which is a non-trivial operation. |
|
2240 |
||
2241 |
\sa QProcess::systemEnvironment() |
|
2242 |
*/ |
|
2243 |
QProcessEnvironment QProcessEnvironment::systemEnvironment() |
|
2244 |
{ |
|
2245 |
QProcessEnvironment env; |
|
2246 |
const char *entry; |
|
2247 |
for (int count = 0; (entry = environ[count]); ++count) { |
|
2248 |
const char *equal = strchr(entry, '='); |
|
2249 |
if (!equal) |
|
2250 |
continue; |
|
2251 |
||
2252 |
QByteArray name(entry, equal - entry); |
|
2253 |
QByteArray value(equal + 1); |
|
2254 |
env.insert(QString::fromLocal8Bit(name), QString::fromLocal8Bit(value)); |
|
2255 |
} |
|
2256 |
return env; |
|
2257 |
} |
|
2258 |
||
2259 |
/*! |
|
2260 |
\typedef Q_PID |
|
2261 |
\relates QProcess |
|
2262 |
||
2263 |
Typedef for the identifiers used to represent processes on the underlying |
|
2264 |
platform. On Unix and Symbian, this corresponds to \l qint64; on Windows, it |
|
2265 |
corresponds to \c{_PROCESS_INFORMATION*}. |
|
2266 |
||
2267 |
\sa QProcess::pid() |
|
2268 |
*/ |
|
2269 |
||
2270 |
QT_END_NAMESPACE |
|
2271 |
||
2272 |
#include "moc_qprocess.cpp" |
|
2273 |
||
2274 |
#endif // QT_NO_PROCESS |
|
2275 |