1 | /****************************************************************************
|
---|
2 | ** $Id: jprocess.cpp,v 1.4 2003/04/23 10:48:33 justin Exp $
|
---|
3 | **
|
---|
4 | ** Implementation of QProcess class
|
---|
5 | **
|
---|
6 | ** Created : 20000905
|
---|
7 | **
|
---|
8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
9 | **
|
---|
10 | ** This file is part of the kernel module of the Qt GUI Toolkit.
|
---|
11 | **
|
---|
12 | ** This file may be distributed under the terms of the Q Public License
|
---|
13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
15 | **
|
---|
16 | ** This file may be distributed and/or modified under the terms of the
|
---|
17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
19 | ** packaging of this file.
|
---|
20 | **
|
---|
21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
23 | ** Agreement provided with the Software.
|
---|
24 | **
|
---|
25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
27 | **
|
---|
28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
29 | ** information about Qt Commercial License Agreements.
|
---|
30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
32 | **
|
---|
33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
34 | ** not clear to you.
|
---|
35 | **
|
---|
36 | **********************************************************************/
|
---|
37 |
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <stdlib.h>
|
---|
40 |
|
---|
41 | #include "jprocess.h"
|
---|
42 |
|
---|
43 | #ifndef QT_NO_PROCESS
|
---|
44 |
|
---|
45 | #include "qapplication.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | //#define QT_QPROCESS_DEBUG
|
---|
49 |
|
---|
50 |
|
---|
51 | /*!
|
---|
52 | \class QProcess qprocess.h
|
---|
53 |
|
---|
54 | \brief The QProcess class is used to start external programs and
|
---|
55 | to communicate with them.
|
---|
56 |
|
---|
57 | \ingroup io
|
---|
58 | \ingroup misc
|
---|
59 | \mainclass
|
---|
60 |
|
---|
61 | You can write to the started program's standard input, and can
|
---|
62 | read the program's standard output and standard error. You can
|
---|
63 | pass command line arguments to the program either in the
|
---|
64 | constructor or with setArguments() or addArgument(). The program's
|
---|
65 | working directory can be set with setWorkingDirectory(). If you
|
---|
66 | need to set up environment variables pass them to the start() or
|
---|
67 | launch() functions (see below). The processExited() signal is
|
---|
68 | emitted if the program exits. The program's exit status is
|
---|
69 | available from exitStatus(), although you could simply call
|
---|
70 | normalExit() to see if the program terminated normally.
|
---|
71 |
|
---|
72 | There are two different ways to start a process. If you just want
|
---|
73 | to run a program, optionally passing data to its standard input at
|
---|
74 | the beginning, use one of the launch() functions. If you want full
|
---|
75 | control of the program's standard input (especially if you don't
|
---|
76 | know all the data you want to send to standard input at the
|
---|
77 | beginning), use the start() function.
|
---|
78 |
|
---|
79 | If you use start() you can write to the program's standard input
|
---|
80 | using writeToStdin() and you can close the standard input with
|
---|
81 | closeStdin(). The wroteToStdin() signal is emitted if the data
|
---|
82 | sent to standard input has been written. You can read from the
|
---|
83 | program's standard output using readStdout() or readLineStdout().
|
---|
84 | These functions return an empty QByteArray if there is no data to
|
---|
85 | read. The readyReadStdout() signal is emitted when there is data
|
---|
86 | available to be read from standard output. Standard error has a
|
---|
87 | set of functions that correspond to the standard output functions,
|
---|
88 | i.e. readStderr(), readLineStderr() and readyReadStderr().
|
---|
89 |
|
---|
90 | If you use one of the launch() functions the data you pass will be
|
---|
91 | sent to the program's standard input which will be closed once all
|
---|
92 | the data has been written. You should \e not use writeToStdin() or
|
---|
93 | closeStdin() if you use launch(). If you need to send data to the
|
---|
94 | program's standard input after it has started running use start()
|
---|
95 | instead of launch().
|
---|
96 |
|
---|
97 | Both start() and launch() can accept a string list of strings each
|
---|
98 | of which has the format, key=value, where the keys are the names
|
---|
99 | of environment variables.
|
---|
100 |
|
---|
101 | You can test to see if a program is running with isRunning(). The
|
---|
102 | program's process identifier is available from
|
---|
103 | processIdentifier(). If you want to terminate a running program
|
---|
104 | use tryTerminate(), but note that the program may ignore this. If
|
---|
105 | you \e really want to terminate the program, without it having any
|
---|
106 | chance to clean up, you can use kill().
|
---|
107 |
|
---|
108 | As an example, suppose we want to start the \c uic command (a Qt
|
---|
109 | command line tool used with \e{Qt Designer}) and perform some
|
---|
110 | operations on the output (the \c uic outputs the code it generates
|
---|
111 | to standard output by default). Suppose further that we want to
|
---|
112 | run the program on the file "small_dialog.ui" with the command
|
---|
113 | line options "-tr i18n". On the command line we would write:
|
---|
114 | \code
|
---|
115 | uic -tr i18n small_dialog.ui
|
---|
116 | \endcode
|
---|
117 |
|
---|
118 | \quotefile process/process.cpp
|
---|
119 |
|
---|
120 | A code snippet for this with the QProcess class might look like
|
---|
121 | this:
|
---|
122 |
|
---|
123 | \skipto UicManager::UicManager()
|
---|
124 | \printline UicManager::UicManager()
|
---|
125 | \printline {
|
---|
126 | \skipto proc = new QProcess( this );
|
---|
127 | \printline proc = new QProcess( this );
|
---|
128 | \skipto proc->addArgument( "uic" );
|
---|
129 | \printuntil this, SLOT(readFromStdout()) );
|
---|
130 | \skipto if ( !proc->start() ) {
|
---|
131 | \printuntil // error handling
|
---|
132 | \skipto }
|
---|
133 | \printline }
|
---|
134 | \printline }
|
---|
135 |
|
---|
136 | \skipto void UicManager::readFromStdout()
|
---|
137 | \printuntil // Bear in mind that the data might be output in chunks.
|
---|
138 | \skipto }
|
---|
139 | \printline }
|
---|
140 |
|
---|
141 | Although you may need quotes for a file named on the command line
|
---|
142 | (e.g. if it contains spaces) you shouldn't use extra quotes for
|
---|
143 | arguments passed to addArgument() or setArguments().
|
---|
144 |
|
---|
145 | The readyReadStdout() signal is emitted when there is new data on
|
---|
146 | standard output. This happens asynchronously: you don't know if
|
---|
147 | more data will arrive later.
|
---|
148 |
|
---|
149 | In the above example you could connect the processExited() signal
|
---|
150 | to the slot UicManager::readFromStdout() instead. If you do so,
|
---|
151 | you will be certain that all the data is available when the slot
|
---|
152 | is called. On the other hand, you must wait until the process has
|
---|
153 | finished before doing any processing.
|
---|
154 |
|
---|
155 | Note that if you are expecting a lot of output from the process,
|
---|
156 | you may hit platform-dependent limits to the pipe buffer size. The
|
---|
157 | solution is to make sure you connect to the output, e.g. the
|
---|
158 | readyReadStdout() and readyReadStderr() signals and read the data
|
---|
159 | as soon as it becomes available.
|
---|
160 |
|
---|
161 | Note: Under Windows there are certain problems starting 16-bit applications
|
---|
162 | and capture their output. Microsoft recommends to use an intermediate
|
---|
163 | application to start 16-bit applications. See Knowledge Base article \link
|
---|
164 | http://support.microsoft.com/default.aspx?scid=KB;en-us;q150956 Q150956
|
---|
165 | \endlink for details on this and example code for an intermediate
|
---|
166 | application.
|
---|
167 |
|
---|
168 | \sa QSocket
|
---|
169 | */
|
---|
170 |
|
---|
171 | /*!
|
---|
172 | \enum QProcess::Communication
|
---|
173 |
|
---|
174 | This enum type defines the communication channels connected to the
|
---|
175 | process.
|
---|
176 |
|
---|
177 | \value Stdin Data can be written to the process's standard input.
|
---|
178 |
|
---|
179 | \value Stdout Data can be read from the process's standard
|
---|
180 | output.
|
---|
181 |
|
---|
182 | \value Stderr Data can be read from the process's standard error.
|
---|
183 |
|
---|
184 | \value DupStderr Both the process's standard error output \e and
|
---|
185 | its standard output are written to its standard output. (Like
|
---|
186 | Unix's dup2().) This means that nothing is sent to the standard
|
---|
187 | error output. This is especially useful if your application
|
---|
188 | requires that the output on standard output and on standard error
|
---|
189 | must be read in the same order that they are produced. This is a
|
---|
190 | flag, so to activate it you must pass \c{Stdout|Stderr|DupStderr},
|
---|
191 | or \c{Stdin|Stdout|Stderr|DupStderr} if you want to provide input,
|
---|
192 | to the setCommunication() call.
|
---|
193 |
|
---|
194 | \sa setCommunication() communication()
|
---|
195 | */
|
---|
196 |
|
---|
197 | /*!
|
---|
198 | Constructs a QProcess object. The \a parent and \a name parameters
|
---|
199 | are passed to the QObject constructor.
|
---|
200 |
|
---|
201 | \sa setArguments() addArgument() start()
|
---|
202 | */
|
---|
203 | JProcess::JProcess( QObject *parent, const char *name )
|
---|
204 | : QObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ),
|
---|
205 | wroteToStdinConnected( FALSE ),
|
---|
206 | readStdoutCalled( FALSE ), readStderrCalled( FALSE ),
|
---|
207 | comms( Stdin|Stdout|Stderr )
|
---|
208 | {
|
---|
209 | init();
|
---|
210 | }
|
---|
211 |
|
---|
212 | /*!
|
---|
213 | Constructs a QProcess with \a arg0 as the command to be executed.
|
---|
214 | The \a parent and \a name parameters are passed to the QObject
|
---|
215 | constructor.
|
---|
216 |
|
---|
217 | The process is not started. You must call start() or launch() to
|
---|
218 | start the process.
|
---|
219 |
|
---|
220 | \sa setArguments() addArgument() start()
|
---|
221 | */
|
---|
222 | JProcess::JProcess( const QString& arg0, QObject *parent, const char *name )
|
---|
223 | : QObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ),
|
---|
224 | wroteToStdinConnected( FALSE ),
|
---|
225 | readStdoutCalled( FALSE ), readStderrCalled( FALSE ),
|
---|
226 | comms( Stdin|Stdout|Stderr )
|
---|
227 | {
|
---|
228 | init();
|
---|
229 | addArgument( arg0 );
|
---|
230 | }
|
---|
231 |
|
---|
232 | /*!
|
---|
233 | Constructs a QProcess with \a args as the arguments of the
|
---|
234 | process. The first element in the list is the command to be
|
---|
235 | executed. The other elements in the list are the arguments to this
|
---|
236 | command. The \a parent and \a name parameters are passed to the
|
---|
237 | QObject constructor.
|
---|
238 |
|
---|
239 | The process is not started. You must call start() or launch() to
|
---|
240 | start the process.
|
---|
241 |
|
---|
242 | \sa setArguments() addArgument() start()
|
---|
243 | */
|
---|
244 | JProcess::JProcess( const QStringList& args, QObject *parent, const char *name )
|
---|
245 | : QObject( parent, name ), ioRedirection( FALSE ), notifyOnExit( FALSE ),
|
---|
246 | wroteToStdinConnected( FALSE ),
|
---|
247 | readStdoutCalled( FALSE ), readStderrCalled( FALSE ),
|
---|
248 | comms( Stdin|Stdout|Stderr )
|
---|
249 | {
|
---|
250 | init();
|
---|
251 | setArguments( args );
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /*!
|
---|
256 | Returns the list of arguments that are set for the process.
|
---|
257 | Arguments can be specified with the constructor or with the
|
---|
258 | functions setArguments() and addArgument().
|
---|
259 |
|
---|
260 | Note that if you want to iterate over the list, you should iterate
|
---|
261 | over a copy, e.g.
|
---|
262 | \code
|
---|
263 | QStringList list = myProcess.arguments();
|
---|
264 | QStringList::Iterator it = list.begin();
|
---|
265 | while( it != list.end() ) {
|
---|
266 | myProcessing( *it );
|
---|
267 | ++it;
|
---|
268 | }
|
---|
269 | \endcode
|
---|
270 |
|
---|
271 | \sa setArguments() addArgument()
|
---|
272 | */
|
---|
273 | QStringList JProcess::arguments() const
|
---|
274 | {
|
---|
275 | return _arguments;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /*!
|
---|
279 | Clears the list of arguments that are set for the process.
|
---|
280 |
|
---|
281 | \sa setArguments() addArgument()
|
---|
282 | */
|
---|
283 | void JProcess::clearArguments()
|
---|
284 | {
|
---|
285 | _arguments.clear();
|
---|
286 | }
|
---|
287 |
|
---|
288 | /*!
|
---|
289 | Sets \a args as the arguments for the process. The first element
|
---|
290 | in the list is the command to be executed. The other elements in
|
---|
291 | the list are the arguments to the command. Any previous arguments
|
---|
292 | are deleted.
|
---|
293 |
|
---|
294 | QProcess does not perform argument substitutions; for example, if you
|
---|
295 | specify "*" or "$DISPLAY", these values are passed to the process
|
---|
296 | literally. If you want to have the same behavior as the shell
|
---|
297 | provides, you must do the substitutions yourself; i.e. instead of
|
---|
298 | specifying a "*" you must specify the list of all the filenames in
|
---|
299 | the current directory, and instead of "$DISPLAY" you must specify
|
---|
300 | the value of the environment variable \c DISPLAY.
|
---|
301 |
|
---|
302 | Note for Windows users. The standard Windows shells, e.g. \c
|
---|
303 | command.com and \c cmd.exe, do not perform file globbing, i.e.
|
---|
304 | they do not convert a "*" on the command line into a list of files
|
---|
305 | in the current directory. For this reason most Windows
|
---|
306 | applications implement their own file globbing, and as a result of
|
---|
307 | this, specifying an argument of "*" for a Windows application is
|
---|
308 | likely to result in the application performing a file glob and
|
---|
309 | ending up with a list of filenames.
|
---|
310 |
|
---|
311 | \sa arguments() addArgument()
|
---|
312 | */
|
---|
313 | void JProcess::setArguments( const QStringList& args )
|
---|
314 | {
|
---|
315 | _arguments = args;
|
---|
316 | }
|
---|
317 |
|
---|
318 | /*!
|
---|
319 | Adds \a arg to the end of the list of arguments.
|
---|
320 |
|
---|
321 | The first element in the list of arguments is the command to be
|
---|
322 | executed; the following elements are the command's arguments.
|
---|
323 |
|
---|
324 | \sa arguments() setArguments()
|
---|
325 | */
|
---|
326 | void JProcess::addArgument( const QString& arg )
|
---|
327 | {
|
---|
328 | _arguments.append( arg );
|
---|
329 | }
|
---|
330 |
|
---|
331 | #ifndef QT_NO_DIR
|
---|
332 | /*!
|
---|
333 | Returns the working directory that was set with
|
---|
334 | setWorkingDirectory(), or the current directory if none has been
|
---|
335 | explicitly set.
|
---|
336 |
|
---|
337 | \sa setWorkingDirectory() QDir::current()
|
---|
338 | */
|
---|
339 | QDir JProcess::workingDirectory() const
|
---|
340 | {
|
---|
341 | return workingDir;
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*!
|
---|
345 | Sets \a dir as the working directory for processes. This does not
|
---|
346 | affect running processes; only processes that are started
|
---|
347 | afterwards are affected.
|
---|
348 |
|
---|
349 | Setting the working directory is especially useful for processes
|
---|
350 | that try to access files with relative paths.
|
---|
351 |
|
---|
352 | \sa workingDirectory() start()
|
---|
353 | */
|
---|
354 | void JProcess::setWorkingDirectory( const QDir& dir )
|
---|
355 | {
|
---|
356 | workingDir = dir;
|
---|
357 | }
|
---|
358 | #endif //QT_NO_DIR
|
---|
359 |
|
---|
360 | /*!
|
---|
361 | Returns the communication required with the process, i.e. some
|
---|
362 | combination of the \c Communication flags.
|
---|
363 |
|
---|
364 | \sa setCommunication()
|
---|
365 | */
|
---|
366 | int JProcess::communication() const
|
---|
367 | {
|
---|
368 | return comms;
|
---|
369 | }
|
---|
370 |
|
---|
371 | /*!
|
---|
372 | Sets \a commFlags as the communication required with the process.
|
---|
373 |
|
---|
374 | \a commFlags is a bitwise OR of the flags defined by the \c
|
---|
375 | Communication enum.
|
---|
376 |
|
---|
377 | The default is \c{Stdin|Stdout|Stderr}.
|
---|
378 |
|
---|
379 | \sa communication()
|
---|
380 | */
|
---|
381 | void JProcess::setCommunication( int commFlags )
|
---|
382 | {
|
---|
383 | comms = commFlags;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /*!
|
---|
387 | Returns TRUE if the process has exited normally; otherwise returns
|
---|
388 | FALSE. This implies that this function returns FALSE if the
|
---|
389 | process is still running.
|
---|
390 |
|
---|
391 | \sa isRunning() exitStatus() processExited()
|
---|
392 | */
|
---|
393 | bool JProcess::normalExit() const
|
---|
394 | {
|
---|
395 | // isRunning() has the side effect that it determines the exit status!
|
---|
396 | if ( isRunning() )
|
---|
397 | return FALSE;
|
---|
398 | else
|
---|
399 | return exitNormal;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /*!
|
---|
403 | Returns the exit status of the process or 0 if the process is
|
---|
404 | still running. This function returns immediately and does not wait
|
---|
405 | until the process is finished.
|
---|
406 |
|
---|
407 | If normalExit() is FALSE (e.g. if the program was killed or
|
---|
408 | crashed), this function returns 0, so you should check the return
|
---|
409 | value of normalExit() before relying on this value.
|
---|
410 |
|
---|
411 | \sa normalExit() processExited()
|
---|
412 | */
|
---|
413 | int JProcess::exitStatus() const
|
---|
414 | {
|
---|
415 | // isRunning() has the side effect that it determines the exit status!
|
---|
416 | if ( isRunning() )
|
---|
417 | return 0;
|
---|
418 | else
|
---|
419 | return exitStat;
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /*!
|
---|
424 | Reads the data that the process has written to standard output.
|
---|
425 | When new data is written to standard output, the class emits the
|
---|
426 | signal readyReadStdout().
|
---|
427 |
|
---|
428 | If there is no data to read, this function returns a QByteArray of
|
---|
429 | size 0: it does not wait until there is something to read.
|
---|
430 |
|
---|
431 | \sa readyReadStdout() readLineStdout() readStderr() writeToStdin()
|
---|
432 | */
|
---|
433 | QByteArray JProcess::readStdout()
|
---|
434 | {
|
---|
435 | if ( readStdoutCalled ) {
|
---|
436 | return QByteArray();
|
---|
437 | }
|
---|
438 | readStdoutCalled = TRUE;
|
---|
439 |
|
---|
440 | QByteArray buf = bufStdout()->copy();
|
---|
441 | consumeBufStdout( -1 ); // consume everything
|
---|
442 |
|
---|
443 | readStdoutCalled = FALSE;
|
---|
444 | return buf;
|
---|
445 | }
|
---|
446 |
|
---|
447 | /*!
|
---|
448 | Reads the data that the process has written to standard error.
|
---|
449 | When new data is written to standard error, the class emits the
|
---|
450 | signal readyReadStderr().
|
---|
451 |
|
---|
452 | If there is no data to read, this function returns a QByteArray of
|
---|
453 | size 0: it does not wait until there is something to read.
|
---|
454 |
|
---|
455 | \sa readyReadStderr() readLineStderr() readStdout() writeToStdin()
|
---|
456 | */
|
---|
457 | QByteArray JProcess::readStderr()
|
---|
458 | {
|
---|
459 | if ( readStderrCalled ) {
|
---|
460 | return QByteArray();
|
---|
461 | }
|
---|
462 | readStderrCalled = TRUE;
|
---|
463 |
|
---|
464 | QByteArray buf = bufStderr()->copy();
|
---|
465 | consumeBufStderr( -1 ); // consume everything
|
---|
466 |
|
---|
467 | readStderrCalled = FALSE;
|
---|
468 | return buf;
|
---|
469 | }
|
---|
470 |
|
---|
471 | /*!
|
---|
472 | Reads a line of text from standard output, excluding any trailing
|
---|
473 | newline or carriage return characters, and returns it. Returns
|
---|
474 | QString::null if canReadLineStdout() returns FALSE.
|
---|
475 |
|
---|
476 | \sa canReadLineStdout() readyReadStdout() readStdout() readLineStderr()
|
---|
477 | */
|
---|
478 | QString JProcess::readLineStdout()
|
---|
479 | {
|
---|
480 | QByteArray a;
|
---|
481 | QString s;
|
---|
482 | if ( scanNewline( TRUE, &a ) ) {
|
---|
483 | if ( a.isEmpty() )
|
---|
484 | s = "";
|
---|
485 | else
|
---|
486 | s = QString( a );
|
---|
487 | } else if ( canReadLineStdout() ) {
|
---|
488 | s = QString( *bufStdout() );
|
---|
489 | consumeBufStdout( -1 ); // consume everything
|
---|
490 | }
|
---|
491 | return s;
|
---|
492 | }
|
---|
493 |
|
---|
494 | /*!
|
---|
495 | Reads a line of text from standard error, excluding any trailing
|
---|
496 | newline or carriage return characters and returns it. Returns
|
---|
497 | QString::null if canReadLineStderr() returns FALSE.
|
---|
498 |
|
---|
499 | \sa canReadLineStderr() readyReadStderr() readStderr() readLineStdout()
|
---|
500 | */
|
---|
501 | QString JProcess::readLineStderr()
|
---|
502 | {
|
---|
503 | QByteArray a;
|
---|
504 | QString s;
|
---|
505 | if ( scanNewline( FALSE, &a ) ) {
|
---|
506 | if ( a.isEmpty() )
|
---|
507 | s = "";
|
---|
508 | else
|
---|
509 | s = QString( a );
|
---|
510 | } else if ( canReadLineStderr() ) {
|
---|
511 | s = QString( *bufStderr() );
|
---|
512 | consumeBufStderr( -1 ); // consume everything
|
---|
513 | }
|
---|
514 | return s;
|
---|
515 | }
|
---|
516 |
|
---|
517 | /*
|
---|
518 | This private function scans for any occurrence of \n or \r\n in the
|
---|
519 | buffer \e buf. It stores the text in the byte array \a store if it is
|
---|
520 | non-null.
|
---|
521 | */
|
---|
522 | bool JProcess::scanNewline( bool stdOut, QByteArray *store )
|
---|
523 | {
|
---|
524 | QByteArray *buf;
|
---|
525 | if ( stdOut )
|
---|
526 | buf = bufStdout();
|
---|
527 | else
|
---|
528 | buf = bufStderr();
|
---|
529 | uint n = buf->size();
|
---|
530 | uint i;
|
---|
531 | for ( i=0; i<n; i++ ) {
|
---|
532 | if ( buf->at(i) == '\n' ) {
|
---|
533 | break;
|
---|
534 | }
|
---|
535 | }
|
---|
536 | if ( i >= n )
|
---|
537 | return FALSE;
|
---|
538 |
|
---|
539 | if ( store ) {
|
---|
540 | uint lineLength = i;
|
---|
541 | if ( lineLength>0 && buf->at(lineLength-1) == '\r' )
|
---|
542 | lineLength--; // (if there are two \r, let one stay)
|
---|
543 | store->resize( lineLength );
|
---|
544 | memcpy( store->data(), buf->data(), lineLength );
|
---|
545 | if ( stdOut )
|
---|
546 | consumeBufStdout( i+1 );
|
---|
547 | else
|
---|
548 | consumeBufStderr( i+1 );
|
---|
549 | }
|
---|
550 | return TRUE;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /*!
|
---|
554 | \fn void QProcess::launchFinished()
|
---|
555 |
|
---|
556 | This signal is emitted when the process was started with launch().
|
---|
557 | If the start was successful, this signal is emitted after all the
|
---|
558 | data has been written to standard input. If the start failed, then
|
---|
559 | this signal is emitted immediately.
|
---|
560 |
|
---|
561 | This signal is especially useful if you want to know when you can
|
---|
562 | safely delete the QProcess object when you are not intrested in
|
---|
563 | reading from standard output or standard error.
|
---|
564 |
|
---|
565 | \sa launch() QObject::deleteLater()
|
---|
566 | */
|
---|
567 |
|
---|
568 | /*!
|
---|
569 | Runs the process and writes the data \a buf to the process's
|
---|
570 | standard input. If all the data is written to standard input,
|
---|
571 | standard input is closed. The command is searched for in the path
|
---|
572 | for executable programs; you can also use an absolute path in the
|
---|
573 | command itself.
|
---|
574 |
|
---|
575 | If \a env is null, then the process is started with the same
|
---|
576 | environment as the starting process. If \a env is non-null, then
|
---|
577 | the values in the string list are interpreted as environment
|
---|
578 | setttings of the form \c {key=value} and the process is started
|
---|
579 | with these environment settings. For convenience, there is a small
|
---|
580 | exception to this rule under Unix: if \a env does not contain any
|
---|
581 | settings for the environment variable \c LD_LIBRARY_PATH, then
|
---|
582 | this variable is inherited from the starting process.
|
---|
583 |
|
---|
584 | Returns TRUE if the process could be started; otherwise returns
|
---|
585 | FALSE.
|
---|
586 |
|
---|
587 | Note that you should not use the slots writeToStdin() and
|
---|
588 | closeStdin() on processes started with launch(), since the result
|
---|
589 | is not well-defined. If you need these slots, use start() instead.
|
---|
590 |
|
---|
591 | The process may or may not read the \a buf data sent to its
|
---|
592 | standard input.
|
---|
593 |
|
---|
594 | You can call this function even when a process that was started
|
---|
595 | with this instance is still running. Be aware that if you do this
|
---|
596 | the standard input of the process that was launched first will be
|
---|
597 | closed, with any pending data being deleted, and the process will
|
---|
598 | be left to run out of your control. Similarly, if the process
|
---|
599 | could not be started the standard input will be closed and the
|
---|
600 | pending data deleted. (On operating systems that have zombie
|
---|
601 | processes, Qt will also wait() on the old process.)
|
---|
602 |
|
---|
603 | The object emits the signal launchFinished() when this function
|
---|
604 | call is finished. If the start was successful, this signal is
|
---|
605 | emitted after all the data has been written to standard input. If
|
---|
606 | the start failed, then this signal is emitted immediately.
|
---|
607 |
|
---|
608 | \sa start() launchFinished();
|
---|
609 | */
|
---|
610 | bool JProcess::launch( const QByteArray& buf, QStringList *env )
|
---|
611 | {
|
---|
612 | if ( start( env ) ) {
|
---|
613 | if ( !buf.isEmpty() ) {
|
---|
614 | connect( this, SIGNAL(wroteToStdin()),
|
---|
615 | this, SLOT(closeStdinLaunch()) );
|
---|
616 | writeToStdin( buf );
|
---|
617 | } else {
|
---|
618 | closeStdin();
|
---|
619 | emit launchFinished();
|
---|
620 | }
|
---|
621 | return TRUE;
|
---|
622 | } else {
|
---|
623 | emit launchFinished();
|
---|
624 | return FALSE;
|
---|
625 | }
|
---|
626 | }
|
---|
627 |
|
---|
628 | /*!
|
---|
629 | \overload
|
---|
630 |
|
---|
631 | The data \a buf is written to standard input with writeToStdin()
|
---|
632 | using the QString::local8Bit() representation of the strings.
|
---|
633 | */
|
---|
634 | bool JProcess::launch( const QString& buf, QStringList *env )
|
---|
635 | {
|
---|
636 | if ( start( env ) ) {
|
---|
637 | if ( !buf.isEmpty() ) {
|
---|
638 | connect( this, SIGNAL(wroteToStdin()),
|
---|
639 | this, SLOT(closeStdinLaunch()) );
|
---|
640 | writeToStdin( buf );
|
---|
641 | } else {
|
---|
642 | closeStdin();
|
---|
643 | emit launchFinished();
|
---|
644 | }
|
---|
645 | return TRUE;
|
---|
646 | } else {
|
---|
647 | emit launchFinished();
|
---|
648 | return FALSE;
|
---|
649 | }
|
---|
650 | }
|
---|
651 |
|
---|
652 | /*
|
---|
653 | This private slot is used by the launch() functions to close standard input.
|
---|
654 | */
|
---|
655 | void JProcess::closeStdinLaunch()
|
---|
656 | {
|
---|
657 | disconnect( this, SIGNAL(wroteToStdin()),
|
---|
658 | this, SLOT(closeStdinLaunch()) );
|
---|
659 | closeStdin();
|
---|
660 | emit launchFinished();
|
---|
661 | }
|
---|
662 |
|
---|
663 |
|
---|
664 | /*!
|
---|
665 | \fn void QProcess::readyReadStdout()
|
---|
666 |
|
---|
667 | This signal is emitted when the process has written data to
|
---|
668 | standard output. You can read the data with readStdout().
|
---|
669 |
|
---|
670 | Note that this signal is only emitted when there is new data and
|
---|
671 | not when there is old, but unread data. In the slot connected to
|
---|
672 | this signal, you should always read everything that is available
|
---|
673 | at that moment to make sure that you don't lose any data.
|
---|
674 |
|
---|
675 | \sa readStdout() readLineStdout() readyReadStderr()
|
---|
676 | */
|
---|
677 |
|
---|
678 | /*!
|
---|
679 | \fn void QProcess::readyReadStderr()
|
---|
680 |
|
---|
681 | This signal is emitted when the process has written data to
|
---|
682 | standard error. You can read the data with readStderr().
|
---|
683 |
|
---|
684 | Note that this signal is only emitted when there is new data and
|
---|
685 | not when there is old, but unread data. In the slot connected to
|
---|
686 | this signal, you should always read everything that is available
|
---|
687 | at that moment to make sure that you don't lose any data.
|
---|
688 |
|
---|
689 | \sa readStderr() readLineStderr() readyReadStdout()
|
---|
690 | */
|
---|
691 |
|
---|
692 | /*!
|
---|
693 | \fn void QProcess::processExited()
|
---|
694 |
|
---|
695 | This signal is emitted when the process has exited.
|
---|
696 |
|
---|
697 | \sa isRunning() normalExit() exitStatus() start() launch()
|
---|
698 | */
|
---|
699 |
|
---|
700 | /*!
|
---|
701 | \fn void QProcess::wroteToStdin()
|
---|
702 |
|
---|
703 | This signal is emitted if the data sent to standard input (via
|
---|
704 | writeToStdin()) was actually written to the process. This does not
|
---|
705 | imply that the process really read the data, since this class only
|
---|
706 | detects when it was able to write the data to the operating
|
---|
707 | system. But it is now safe to close standard input without losing
|
---|
708 | pending data.
|
---|
709 |
|
---|
710 | \sa writeToStdin() closeStdin()
|
---|
711 | */
|
---|
712 |
|
---|
713 |
|
---|
714 | /*!
|
---|
715 | \overload
|
---|
716 |
|
---|
717 | The string \a buf is handled as text using the
|
---|
718 | QString::local8Bit() representation.
|
---|
719 | */
|
---|
720 | void JProcess::writeToStdin( const QString& buf )
|
---|
721 | {
|
---|
722 | QByteArray tmp = buf.local8Bit();
|
---|
723 | tmp.resize( buf.length() );
|
---|
724 | writeToStdin( tmp );
|
---|
725 | }
|
---|
726 |
|
---|
727 |
|
---|
728 | /*
|
---|
729 | * Under Windows the implementation is not so nice: it is not that easy to
|
---|
730 | * detect when one of the signals should be emitted; therefore there are some
|
---|
731 | * timers that query the information.
|
---|
732 | * To keep it a little efficient, use the timers only when they are needed.
|
---|
733 | * They are needed, if you are interested in the signals. So use
|
---|
734 | * connectNotify() and disconnectNotify() to keep track of your interest.
|
---|
735 | */
|
---|
736 | /*! \reimp
|
---|
737 | */
|
---|
738 | void JProcess::connectNotify( const char * signal )
|
---|
739 | {
|
---|
740 | #if defined(QT_QPROCESS_DEBUG)
|
---|
741 | qDebug( "QProcess::connectNotify(): signal %s has been connected", signal );
|
---|
742 | #endif
|
---|
743 | if ( !ioRedirection )
|
---|
744 | if ( qstrcmp( signal, SIGNAL(readyReadStdout()) )==0 ||
|
---|
745 | qstrcmp( signal, SIGNAL(readyReadStderr()) )==0
|
---|
746 | ) {
|
---|
747 | #if defined(QT_QPROCESS_DEBUG)
|
---|
748 | qDebug( "QProcess::connectNotify(): set ioRedirection to TRUE" );
|
---|
749 | #endif
|
---|
750 | setIoRedirection( TRUE );
|
---|
751 | return;
|
---|
752 | }
|
---|
753 | if ( !notifyOnExit && qstrcmp( signal, SIGNAL(processExited()) )==0 ) {
|
---|
754 | #if defined(QT_QPROCESS_DEBUG)
|
---|
755 | qDebug( "QProcess::connectNotify(): set notifyOnExit to TRUE" );
|
---|
756 | #endif
|
---|
757 | setNotifyOnExit( TRUE );
|
---|
758 | return;
|
---|
759 | }
|
---|
760 | if ( !wroteToStdinConnected && qstrcmp( signal, SIGNAL(wroteToStdin()) )==0 ) {
|
---|
761 | #if defined(QT_QPROCESS_DEBUG)
|
---|
762 | qDebug( "QProcess::connectNotify(): set wroteToStdinConnected to TRUE" );
|
---|
763 | #endif
|
---|
764 | setWroteStdinConnected( TRUE );
|
---|
765 | return;
|
---|
766 | }
|
---|
767 | }
|
---|
768 |
|
---|
769 | /*! \reimp
|
---|
770 | */
|
---|
771 | void JProcess::disconnectNotify( const char * )
|
---|
772 | {
|
---|
773 | if ( ioRedirection &&
|
---|
774 | receivers( SIGNAL(readyReadStdout()) ) ==0 &&
|
---|
775 | receivers( SIGNAL(readyReadStderr()) ) ==0
|
---|
776 | ) {
|
---|
777 | #if defined(QT_QPROCESS_DEBUG)
|
---|
778 | qDebug( "QProcess::disconnectNotify(): set ioRedirection to FALSE" );
|
---|
779 | #endif
|
---|
780 | setIoRedirection( FALSE );
|
---|
781 | }
|
---|
782 | if ( notifyOnExit && receivers( SIGNAL(processExited()) ) == 0 ) {
|
---|
783 | #if defined(QT_QPROCESS_DEBUG)
|
---|
784 | qDebug( "QProcess::disconnectNotify(): set notifyOnExit to FALSE" );
|
---|
785 | #endif
|
---|
786 | setNotifyOnExit( FALSE );
|
---|
787 | }
|
---|
788 | if ( wroteToStdinConnected && receivers( SIGNAL(wroteToStdin()) ) == 0 ) {
|
---|
789 | #if defined(QT_QPROCESS_DEBUG)
|
---|
790 | qDebug( "QProcess::disconnectNotify(): set wroteToStdinConnected to FALSE" );
|
---|
791 | #endif
|
---|
792 | setWroteStdinConnected( FALSE );
|
---|
793 | }
|
---|
794 | }
|
---|
795 |
|
---|
796 | #endif // QT_NO_PROCESS
|
---|