source: trunk/src/qt3support/other/q3process.cpp@ 134

Last change on this file since 134 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 30.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the Qt3Support module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "q3process.h"
43
44#ifndef QT_NO_PROCESS
45
46#include "qapplication.h"
47#include "private/q3membuf_p.h"
48
49#include <stdio.h>
50#include <stdlib.h>
51
52QT_BEGIN_NAMESPACE
53
54//#define QT_Q3PROCESS_DEBUG
55
56
57/*!
58 \class Q3Process
59
60 \brief The Q3Process class is used to start external programs and
61 to communicate with them.
62
63 \compat
64
65 You can write to the started program's standard input, and can
66 read the program's standard output and standard error. You can
67 pass command line arguments to the program either in the
68 constructor or with setArguments() or addArgument(). The program's
69 working directory can be set with setWorkingDirectory(). If you
70 need to set up environment variables pass them to the start() or
71 launch() functions (see below). The processExited() signal is
72 emitted if the program exits. The program's exit status is
73 available from exitStatus(), although you could simply call
74 normalExit() to see if the program terminated normally.
75
76 There are two different ways to start a process. If you just want
77 to run a program, optionally passing data to its standard input at
78 the beginning, use one of the launch() functions. If you want full
79 control of the program's standard input (especially if you don't
80 know all the data you want to send to standard input at the
81 beginning), use the start() function.
82
83 If you use start() you can write to the program's standard input
84 using writeToStdin() and you can close the standard input with
85 closeStdin(). The wroteToStdin() signal is emitted if the data
86 sent to standard input has been written. You can read from the
87 program's standard output using readStdout() or readLineStdout().
88 These functions return an empty QByteArray if there is no data to
89 read. The readyReadStdout() signal is emitted when there is data
90 available to be read from standard output. Standard error has a
91 set of functions that correspond to the standard output functions,
92 i.e. readStderr(), readLineStderr() and readyReadStderr().
93
94 If you use one of the launch() functions the data you pass will be
95 sent to the program's standard input which will be closed once all
96 the data has been written. You should \e not use writeToStdin() or
97 closeStdin() if you use launch(). If you need to send data to the
98 program's standard input after it has started running use start()
99 instead of launch().
100
101 Both start() and launch() can accept a string list of strings each
102 of which has the format, key=value, where the keys are the names
103 of environment variables.
104
105 You can test to see if a program is running with isRunning(). The
106 program's process identifier is available from
107 processIdentifier(). If you want to terminate a running program
108 use tryTerminate(), but note that the program may ignore this. If
109 you \e really want to terminate the program, without it having any
110 chance to clean up, you can use kill().
111
112 Although you may need quotes for a file named on the command line
113 (e.g. if it contains spaces) you shouldn't use extra quotes for
114 arguments passed to addArgument() or setArguments().
115
116 The readyReadStdout() signal is emitted when there is new data on
117 standard output. This happens asynchronously: you don't know if
118 more data will arrive later.
119
120 In the above example you could connect the processExited() signal
121 to the slot UicManager::readFromStdout() instead. If you do so,
122 you will be certain that all the data is available when the slot
123 is called. On the other hand, you must wait until the process has
124 finished before doing any processing.
125
126 Note that if you are expecting a lot of output from the process,
127 you may hit platform-dependent limits to the pipe buffer size. The
128 solution is to make sure you connect to the output, e.g. the
129 readyReadStdout() and readyReadStderr() signals and read the data
130 as soon as it becomes available.
131
132 Please note that Q3Process does not emulate a shell. This means that
133 Q3Process does not do any expansion of arguments: a '*' is passed as a '*'
134 to the program and is \e not replaced by all the files, a '$HOME' is also
135 passed literally and is \e not replaced by the environment variable HOME
136 and the special characters for IO redirection ('>', '|', etc.) are also
137 passed literally and do \e not have the special meaning as they have in a
138 shell.
139
140 Also note that Q3Process does not emulate a terminal. This means that
141 certain programs which need direct terminal control, do not work as
142 expected with Q3Process. Such programs include console email programs (like
143 pine and mutt) but also programs which require the user to enter a password
144 (like su and ssh).
145
146 \section1 Notes for Windows users
147
148 Some Windows commands, for example, \c dir, are not provided by
149 separate applications, but by the command interpreter.
150 If you attempt to use Q3Process to execute these commands directly
151 it won't work. One possible solution is to execute the command
152 interpreter itself (\c cmd.exe on some Windows systems), and ask
153 the interpreter to execute the desired command.
154
155 Under Windows there are certain problems starting 16-bit applications
156 and capturing their output. Microsoft recommends using an intermediate
157 application to start 16-bit applications.
158
159 \sa Q3Socket
160*/
161
162/*!
163 \enum Q3Process::Communication
164
165 This enum type defines the communication channels connected to the
166 process.
167
168 \value Stdin Data can be written to the process's standard input.
169
170 \value Stdout Data can be read from the process's standard
171 output.
172
173 \value Stderr Data can be read from the process's standard error.
174
175 \value DupStderr Both the process's standard error output \e and
176 its standard output are written to its standard output. (Like
177 Unix's dup2().) This means that nothing is sent to the standard
178 error output. This is especially useful if your application
179 requires that the output on standard output and on standard error
180 must be read in the same order that they are produced. This is a
181 flag, so to activate it you must pass \c{Stdout|Stderr|DupStderr},
182 or \c{Stdin|Stdout|Stderr|DupStderr} if you want to provide input,
183 to the setCommunication() call.
184
185 \sa setCommunication() communication()
186*/