source: trunk/src/corelib/io/qprocess.h

Last change on this file was 913, checked in by Dmitry A. Kuminov, 14 years ago

OS/2: QProcess: New I/O pipe notification mechanism.

This remake is to fix a number of problems such as hangs
and truncated data during interprocess communication using
redirection of standard I/O channels through the QIODevice
interface provided by QProcess. See #199.

File size: 7.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QPROCESS_H
43#define QPROCESS_H
44
45#include <QtCore/qiodevice.h>
46#include <QtCore/qstringlist.h>
47#include <QtCore/qshareddata.h>
48
49QT_BEGIN_HEADER
50
51QT_BEGIN_NAMESPACE
52
53QT_MODULE(Core)
54
55#ifndef QT_NO_PROCESS
56
57#if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc)
58typedef qint64 Q_PID;
59#elif defined(Q_OS_OS2)
60typedef int Q_PID;
61#else
62QT_END_NAMESPACE
63typedef struct _PROCESS_INFORMATION *Q_PID;
64QT_BEGIN_NAMESPACE
65#endif
66
67class QProcessPrivate;
68class QProcessEnvironmentPrivate;
69
70class Q_CORE_EXPORT QProcessEnvironment
71{
72public:
73 QProcessEnvironment();
74 QProcessEnvironment(const QProcessEnvironment &other);
75 ~QProcessEnvironment();
76 QProcessEnvironment &operator=(const QProcessEnvironment &other);
77
78 bool operator==(const QProcessEnvironment &other) const;
79 inline bool operator!=(const QProcessEnvironment &other) const
80 { return !(*this == other); }
81
82 bool isEmpty() const;
83 void clear();
84
85 bool contains(const QString &name) const;
86 void insert(const QString &name, const QString &value);
87 void remove(const QString &name);
88 QString value(const QString &name, const QString &defaultValue = QString()) const;
89
90 QStringList toStringList() const;
91
92 static QProcessEnvironment systemEnvironment();
93
94private:
95 friend class QProcessPrivate;
96 friend class QProcessEnvironmentPrivate;
97 QSharedDataPointer<QProcessEnvironmentPrivate> d;
98};
99
100class Q_CORE_EXPORT QProcess : public QIODevice
101{
102 Q_OBJECT
103public:
104 enum ProcessError {
105 FailedToStart, //### file not found, resource error
106 Crashed,
107 Timedout,
108 ReadError,
109 WriteError,
110 UnknownError
111 };
112 enum ProcessState {
113 NotRunning,
114 Starting,
115 Running
116 };
117 enum ProcessChannel {
118 StandardOutput,
119 StandardError
120 };
121 enum ProcessChannelMode {
122 SeparateChannels,
123 MergedChannels,
124 ForwardedChannels
125 };
126 enum ExitStatus {
127 NormalExit,
128 CrashExit
129 };
130
131 explicit QProcess(QObject *parent = 0);
132 virtual ~QProcess();
133
134 void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite);
135 void start(const QString &program, OpenMode mode = ReadWrite);
136
137 ProcessChannelMode readChannelMode() const;
138 void setReadChannelMode(ProcessChannelMode mode);
139 ProcessChannelMode processChannelMode() const;
140 void setProcessChannelMode(ProcessChannelMode mode);
141
142 ProcessChannel readChannel() const;
143 void setReadChannel(ProcessChannel channel);
144
145 void closeReadChannel(ProcessChannel channel);
146 void closeWriteChannel();
147
148 void setStandardInputFile(const QString &fileName);
149 void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
150 void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
151 void setStandardOutputProcess(QProcess *destination);
152
153#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
154 QString nativeArguments() const;
155 void setNativeArguments(const QString &arguments);
156#endif
157
158 QString workingDirectory() const;
159 void setWorkingDirectory(const QString &dir);
160
161 void setEnvironment(const QStringList &environment);
162 QStringList environment() const;
163 void setProcessEnvironment(const QProcessEnvironment &environment);
164 QProcessEnvironment processEnvironment() const;
165
166 QProcess::ProcessError error() const;
167 QProcess::ProcessState state() const;
168
169 // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
170 Q_PID pid() const;
171
172 bool waitForStarted(int msecs = 30000);
173 bool waitForReadyRead(int msecs = 30000);
174 bool waitForBytesWritten(int msecs = 30000);
175 bool waitForFinished(int msecs = 30000);
176
177 QByteArray readAllStandardOutput();
178 QByteArray readAllStandardError();
179
180 int exitCode() const;
181 QProcess::ExitStatus exitStatus() const;
182
183 // QIODevice
184 qint64 bytesAvailable() const;
185 qint64 bytesToWrite() const;
186 bool isSequential() const;
187 bool canReadLine() const;
188 void close();
189 bool atEnd() const;
190
191 static int execute(const QString &program, const QStringList &arguments);
192 static int execute(const QString &program);
193
194 static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory,
195 qint64 *pid = 0);
196 static bool startDetached(const QString &program, const QStringList &arguments);
197 static bool startDetached(const QString &program);
198
199 static QStringList systemEnvironment();
200
201public Q_SLOTS:
202 void terminate();
203 void kill();
204
205Q_SIGNALS:
206 void started();
207 void finished(int exitCode);
208 void finished(int exitCode, QProcess::ExitStatus exitStatus);
209 void error(QProcess::ProcessError error);
210 void stateChanged(QProcess::ProcessState state);
211
212 void readyReadStandardOutput();
213 void readyReadStandardError();
214
215protected:
216 void setProcessState(ProcessState state);
217
218 virtual void setupChildProcess();
219
220 // QIODevice
221 qint64 readData(char *data, qint64 maxlen);
222 qint64 writeData(const char *data, qint64 len);
223
224private:
225 Q_DECLARE_PRIVATE(QProcess)
226 Q_DISABLE_COPY(QProcess)
227
228 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
229 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
230 Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
231 Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
232 Q_PRIVATE_SLOT(d_func(), bool _q_processDied())
233#if defined(Q_OS_OS2)
234 Q_PRIVATE_SLOT(d_func(), void _q_notified(int))
235#else
236 Q_PRIVATE_SLOT(d_func(), void _q_notified())
237#endif
238 friend class QProcessManager;
239};
240
241#endif // QT_NO_PROCESS
242
243QT_END_NAMESPACE
244
245QT_END_HEADER
246
247#endif // QPROCESS_H
Note: See TracBrowser for help on using the repository browser.