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