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#if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
52struct _PROCESS_INFORMATION;
53struct _SECURITY_ATTRIBUTES;
54struct _STARTUPINFOW;
55using Q_PROCESS_INFORMATION = _PROCESS_INFORMATION;
56using Q_SECURITY_ATTRIBUTES = _SECURITY_ATTRIBUTES;
57using Q_STARTUPINFO = _STARTUPINFOW;
58#endif
59
60QT_BEGIN_NAMESPACE
61
62class QProcessPrivate;
63class QProcessEnvironmentPrivate;
64
65class Q_CORE_EXPORT QProcessEnvironment
66{
67public:
68 QProcessEnvironment();
69 QProcessEnvironment(const QProcessEnvironment &other);
70 ~QProcessEnvironment();
71 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QProcessEnvironment)
72 QProcessEnvironment &operator=(const QProcessEnvironment &other);
73
74 void swap(QProcessEnvironment &other) noexcept { qSwap(d, other.d); }
75
76 bool operator==(const QProcessEnvironment &other) const;
77 inline bool operator!=(const QProcessEnvironment &other) const
78 { return !(*this == other); }
79
80 bool isEmpty() const;
81 void clear();
82
83 bool contains(const QString &name) const;
84 void insert(const QString &name, const QString &value);
85 void remove(const QString &name);
86 QString value(const QString &name, const QString &defaultValue = QString()) const;
87
88 QStringList toStringList() const;
89
90 QStringList keys() const;
91
92 void insert(const QProcessEnvironment &e);
93
94 static QProcessEnvironment systemEnvironment();
95
96private:
97 friend class QProcessPrivate;
98 friend class QProcessEnvironmentPrivate;
99 QSharedDataPointer<QProcessEnvironmentPrivate> d;
100};
101
102Q_DECLARE_SHARED(QProcessEnvironment)
103
104#if QT_CONFIG(process)
105
106class Q_CORE_EXPORT QProcess : public QIODevice
107{
108 Q_OBJECT
109public:
110 enum ProcessError {
111 FailedToStart,
112 Crashed,
113 Timedout,
114 ReadError,
115 WriteError,
116 UnknownError
117 };
118 Q_ENUM(ProcessError)
119
120 enum ProcessState {
121 NotRunning,
122 Starting,
123 Running
124 };
125 Q_ENUM(ProcessState)
126
127 enum ProcessChannel {
128 StandardOutput,
129 StandardError
130 };
131 Q_ENUM(ProcessChannel)
132
133 enum ProcessChannelMode {
134 SeparateChannels,
135 MergedChannels,
136 ForwardedChannels,
137 ForwardedOutputChannel,
138 ForwardedErrorChannel
139 };
140 Q_ENUM(ProcessChannelMode)
141
142 enum InputChannelMode {
143 ManagedInputChannel,
144 ForwardedInputChannel
145 };
146 Q_ENUM(InputChannelMode)
147
148 enum ExitStatus {
149 NormalExit,
150 CrashExit
151 };
152 Q_ENUM(ExitStatus)
153
154 explicit QProcess(QObject *parent = nullptr);
155 virtual ~QProcess();
156
157 void start(const QString &program, const QStringList &arguments = {}, OpenMode mode = ReadWrite);
158 void start(OpenMode mode = ReadWrite);
159 void startCommand(const QString &command, OpenMode mode = ReadWrite);
160 bool startDetached(qint64 *pid = nullptr);
161 bool open(OpenMode mode = ReadWrite) override;
162
163 QString program() const;
164 void setProgram(const QString &program);
165
166 QStringList arguments() const;
167 void setArguments(const QStringList & arguments);
168
169 ProcessChannelMode processChannelMode() const;
170 void setProcessChannelMode(ProcessChannelMode mode);
171 InputChannelMode inputChannelMode() const;
172 void setInputChannelMode(InputChannelMode mode);
173
174 ProcessChannel readChannel() const;
175 void setReadChannel(ProcessChannel channel);
176
177 void closeReadChannel(ProcessChannel channel);
178 void closeWriteChannel();
179
180 void setStandardInputFile(const QString &fileName);
181 void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate);
182 void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate);
183 void setStandardOutputProcess(QProcess *destination);
184
185#if defined(Q_OS_WIN) || defined(Q_CLANG_QDOC)
186 QString nativeArguments() const;
187 void setNativeArguments(const QString &arguments);
188 struct CreateProcessArguments
189 {
190 const wchar_t *applicationName;
191 wchar_t *arguments;
192 Q_SECURITY_ATTRIBUTES *processAttributes;
193 Q_SECURITY_ATTRIBUTES *threadAttributes;
194 bool inheritHandles;
195 unsigned long flags;
196 void *environment;
197 const wchar_t *currentDirectory;
198 Q_STARTUPINFO *startupInfo;
199 Q_PROCESS_INFORMATION *processInformation;
200 };
201 typedef std::function<void(CreateProcessArguments *)> CreateProcessArgumentModifier;
202 CreateProcessArgumentModifier createProcessArgumentsModifier() const;
203 void setCreateProcessArgumentsModifier(CreateProcessArgumentModifier modifier);
204#endif // Q_OS_WIN || Q_CLANG_QDOC
205#if defined(Q_OS_UNIX) || defined(Q_CLANG_QDOC)
206 std::function<void(void)> childProcessModifier() const;
207 void setChildProcessModifier(const std::function<void(void)> &modifier);
208#endif
209
210 QString workingDirectory() const;
211 void setWorkingDirectory(const QString &dir);
212
213 void setEnvironment(const QStringList &environment);
214 QStringList environment() const;
215 void setProcessEnvironment(const QProcessEnvironment &environment);
216 QProcessEnvironment processEnvironment() const;
217
218 QProcess::ProcessError error() const;
219 QProcess::ProcessState state() const;
220
221 qint64 processId() const;
222
223 bool waitForStarted(int msecs = 30000);
224 bool waitForReadyRead(int msecs = 30000) override;
225 bool waitForBytesWritten(int msecs = 30000) override;
226 bool waitForFinished(int msecs = 30000);
227
228 QByteArray readAllStandardOutput();
229 QByteArray readAllStandardError();
230
231 int exitCode() const;
232 QProcess::ExitStatus exitStatus() const;
233
234 // QIODevice
235 qint64 bytesToWrite() const override;
236 bool isSequential() const override;
237 void close() override;
238
239 static int execute(const QString &program, const QStringList &arguments = {});
240 static bool startDetached(const QString &program, const QStringList &arguments = {},
241 const QString &workingDirectory = QString(), qint64 *pid = nullptr);
242
243 static QStringList systemEnvironment();
244
245 static QString nullDevice();
246
247 static QStringList splitCommand(QStringView command);
248
249public Q_SLOTS:
250 void terminate();
251 void kill();
252
253Q_SIGNALS:
254 void started(QPrivateSignal);
255 void finished(int exitCode, QProcess::ExitStatus exitStatus = NormalExit);
256 void errorOccurred(QProcess::ProcessError error);
257 void stateChanged(QProcess::ProcessState state, QPrivateSignal);
258
259 void readyReadStandardOutput(QPrivateSignal);
260 void readyReadStandardError(QPrivateSignal);
261
262protected:
263 void setProcessState(ProcessState state);
264
265 // QIODevice
266 qint64 readData(char *data, qint64 maxlen) override;
267 qint64 writeData(const char *data, qint64 len) override;
268
269private:
270 Q_DECLARE_PRIVATE(QProcess)
271 Q_DISABLE_COPY(QProcess)
272
273#if QT_VERSION < QT_VERSION_CHECK(7,0,0)
274 // ### Qt7: Remove this struct and the virtual function; they're here only
275 // to cause build errors in Qt 5 code that wasn't updated to Qt 6's
276 // setChildProcessModifier()
277 struct Use_setChildProcessModifier_Instead {};
278 QT_DEPRECATED_X("Use setChildProcessModifier() instead")
279 virtual Use_setChildProcessModifier_Instead setupChildProcess();
280#endif
281
282 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput())
283 Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError())
284 Q_PRIVATE_SLOT(d_func(), bool _q_canWrite())
285 Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification())
286 Q_PRIVATE_SLOT(d_func(), void _q_processDied())
287};
288
289#endif // QT_CONFIG(process)
290
291QT_END_NAMESPACE
292
293#endif // QPROCESS_H
294

source code of qtbase/src/corelib/io/qprocess.h