1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QPROCESS_H
41#define QPROCESS_H
42
43#include <QtCore/qiodevice.h>
44#include <QtCore/qstringlist.h>
45#include <QtCore/qshareddata.h>
46
47#include <functional>
48
49QT_REQUIRE_CONFIG(processenvironment);
50
51#ifdef Q_OS_WIN
52typedef struct _PROCESS_INFORMATION *Q_PID;
53#endif
54
55#if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
56typedef struct _SECURITY_ATTRIBUTES Q_SECURITY_ATTRIBUTES;
57typedef struct _STARTUPINFOW Q_STARTUPINFO;
58#endif
59
60QT_BEGIN_NAMESPACE
61
62class QProcessPrivate;
63class QProcessEnvironmentPrivate;
64
65#ifndef Q_OS_WIN
66typedef qint64 Q_PID;
67#endif
68
69class Q_CORE_EXPORT QProcessEnvironment
70{
71public:
72 QProcessEnvironment();
73 QProcessEnvironment(const QProcessEnvironment &other);
74 ~QProcessEnvironment();
75 QProcessEnvironment &operator=(QProcessEnvironment && other) noexcept { swap(other); return *this; }
76 QProcessEnvironment &operator=(const QProcessEnvironment &other);
77
78 void swap(QProcessEnvironment &other) noexcept { qSwap(d, other.d); }
79
80 bool operator==(const QProcessEnvironment &other) const;
81 inline bool operator!=(const QProcessEnvironment &other) const
82 { return !(*this == other); }
83
84 bool isEmpty() const;
85 void clear();
86
87 bool contains(const QString &name) const;
88 void insert(const QString &name, const QString &value);
89 void remove(const QString &name);
90 QString value(const QString &name, const QString &defaultValue = QString()) const;
91
92 QStringList toStringList() const;
93
94 QStringList keys() const;
95
96 void insert(const QProcessEnvironment &e);
97
98 static QProcessEnvironment systemEnvironment();
99
100private:
101 friend class QProcessPrivate;
102 friend class QProcessEnvironmentPrivate;
103 QSharedDataPointer<QProcessEnvironmentPrivate> d;
104};
105
106Q_DECLARE_SHARED(QProcessEnvironment)
107
108#if QT_CONFIG(process)
109
110class Q_CORE_EXPORT QProcess : public QIODevice
111{
112 Q_OBJECT
113public:
114 enum ProcessError {
115 FailedToStart, //### file not found, resource error
116 Crashed,
117 Timedout,
118 ReadError,
119 WriteError,
120 UnknownError
121 };
122 Q_ENUM(ProcessError)
123
124 enum ProcessState {
125 NotRunning,
126 Starting,
127 Running
128 };
129 Q_ENUM(ProcessState)
130
131 enum ProcessChannel {
132 StandardOutput,
133 StandardError
134 };
135 Q_ENUM(ProcessChannel)
136
137 enum ProcessChannelMode {
138 SeparateChannels,
139 MergedChannels,
140 ForwardedChannels,
141 ForwardedOutputChannel,
142 ForwardedErrorChannel
143 };
144 Q_ENUM(ProcessChannelMode)
145
146 enum InputChannelMode {
147 ManagedInputChannel,
148 ForwardedInputChannel
149 };
150 Q_ENUM(InputChannelMode)
151
152 enum ExitStatus {
153 NormalExit,
154 CrashExit
155 };
156 Q_ENUM(ExitStatus)
157
158 explicit QProcess(QObject *parent = nullptr);
159 virtual ~QProcess();
160
161 void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite);
162#if !defined(QT_NO_PROCESS_COMBINED_ARGUMENT_START)
163 void start(const QString &command, OpenMode mode = ReadWrite);
164#endif
165 void start(OpenMode mode = ReadWrite);
166 bool startDetached(qint64 *pid = nullptr);
167 bool open(OpenMode mode = ReadWrite) override;
168
169 QString program() const;
170 void setProgram(const QString &program);
171
172 QStringList arguments() const;
173 void setArguments(const QStringList & arguments);
174
175#if QT_DEPRECATED_SINCE(5, 13)
176 QT_DEPRECATED_X("Use QProcess::processChannelMode() instead")
177 ProcessChannelMode readChannelMode() const;
178 QT_DEPRECATED_X("Use QProcess::setProcessChannelMode() instead")
179 void setReadChannelMode(ProcessChannelMode mode);
180#endif
181 ProcessChannelMode processChannelMode() const;
182 void setProcessChannelMode(ProcessChannelMode mode);
183 InputChannelMode inputChannelMode() const;
184 void setInputChannelMode(InputChannelMode mode);
185
186 ProcessChannel readChannel() const;
187 void setReadChannel(ProcessChannel channel);
188
189 void closeReadChannel(ProcessChannel channel);
190 void closeWriteChannel();
191
192 void setStandardInputFile(const QString &fileName);
193 void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
194 void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
195 void setStandardOutputProcess(QProcess *destination);
196
197#if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
198 QString nativeArguments() const;
199 void setNativeArguments(const QString &arguments);
200 struct CreateProcessArguments
201 {
202 const wchar_t *applicationName;
203 wchar_t *arguments;
204 Q_SECURITY_ATTRIBUTES *processAttributes;
205 Q_SECURITY_ATTRIBUTES *threadAttributes;
206 bool inheritHandles;
207 unsigned long flags;
208 void *environment;
209 const wchar_t *currentDirectory;
210 Q_STARTUPINFO *startupInfo;
211 Q_PID processInformation;
212 };
213 typedef std::function<void(CreateProcessArguments *)> CreateProcessArgumentModifier;
214 CreateProcessArgumentModifier createProcessArgumentsModifier() const;
215 void setCreateProcessArgumentsModifier(CreateProcessArgumentModifier modifier);
216#endif // Q_OS_WIN || Q_CLANG_QDOC
217
218 QString workingDirectory() const;
219 void setWorkingDirectory(const QString &dir);
220
221 void setEnvironment(const QStringList &environment);
222 QStringList environment() const;
223 void setProcessEnvironment(const QProcessEnvironment &environment);
224 QProcessEnvironment processEnvironment() const;
225
226 QProcess::ProcessError error() const;
227 QProcess::ProcessState state() const;
228
229 // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix
230 Q_PID pid() const;
231 qint64 processId() const;
232
233 bool waitForStarted(int msecs = 30000);
234 bool waitForReadyRead(int msecs = 30000) override;
235 bool waitForBytesWritten(int msecs = 30000) override;
236 bool waitForFinished(int msecs = 30000);
237
238 QByteArray readAllStandardOutput();
239 QByteArray readAllStandardError();
240
241 int exitCode() const;
242 QProcess::ExitStatus exitStatus() const;
243
244 // QIODevice
245 qint64 bytesAvailable() const override; // ### Qt6: remove trivial override
246 qint64 bytesToWrite() const override;
247 bool isSequential() const override;
248 bool canReadLine() const override; // ### Qt6: remove trivial override
249 void close() override;
250 bool atEnd() const override; // ### Qt6: remove trivial override
251
252 static int execute(const QString &program, const QStringList &arguments);
253 static int execute(const QString &command);
254
255 static bool startDetached(const QString &program, const QStringList &arguments,
256 const QString &workingDirectory
257#if defined(Q_QDOC)
258 = QString()
259#endif
260 , qint64 *pid = nullptr);
261#if !defined(Q_QDOC)
262 static bool startDetached(const QString &program, const QStringList &arguments); // ### Qt6: merge overloads
263#endif
264 static bool startDetached(const QString &command);
265
266 static QStringList systemEnvironment();
267
268 static QString nullDevice();
269
270public Q_SLOTS:
271 void terminate();
272 void kill();
273
274Q_SIGNALS:
275 void started(QPrivateSignal);
276#if QT_DEPRECATED_SINCE(5, 13)
277 QT_DEPRECATED_X("Use QProcess::finished(int, QProcess::ExitStatus) instead")
278 void finished(int exitCode); // ### Qt 6: merge the two signals with a default value
279#endif
280 void finished(int exitCode, QProcess::ExitStatus exitStatus);
281#if QT_DEPRECATED_SINCE(5,6)
282 void error(QProcess::ProcessError error);
283#endif
284 void errorOccurred(QProcess::ProcessError error);
285 void stateChanged(QProcess::ProcessState state, QPrivateSignal);
286
287 void readyReadStandardOutput(QPrivateSignal);
288 void readyReadStandardError(QPrivateSignal);
289
290protected:
291 void setProcessState(ProcessState state);
292
293 virtual void setupChildProcess();
294
295 // QIODevice
296 qint64 readData(char *data, qint64 maxlen) override;
297 qint64 writeData(const char *data, qint64 len) override;
298
299private:
300 Q_DECLARE_PRIVATE(QProcess)
301 Q_DISABLE_COPY(QProcess)
302
303 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
304 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
305 Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
306 Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
307 Q_PRIVATE_SLOT(d_func(), bool _q_processDied())
308 friend class QProcessManager;
309};
310
311#endif // QT_CONFIG(process)
312
313QT_END_NAMESPACE
314
315#endif // QPROCESS_H
316