1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
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 Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
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 | |
49 | QT_BEGIN_HEADER |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | QT_MODULE(Core) |
54 | |
55 | #ifndef QT_NO_PROCESS |
56 | |
57 | #if (!defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)) || defined(qdoc) |
58 | typedef qint64 Q_PID; |
59 | #else |
60 | QT_END_NAMESPACE |
61 | typedef struct _PROCESS_INFORMATION *Q_PID; |
62 | QT_BEGIN_NAMESPACE |
63 | #endif |
64 | |
65 | class QProcessPrivate; |
66 | class QProcessEnvironmentPrivate; |
67 | |
68 | class Q_CORE_EXPORT QProcessEnvironment |
69 | { |
70 | public: |
71 | QProcessEnvironment(); |
72 | QProcessEnvironment(const QProcessEnvironment &other); |
73 | ~QProcessEnvironment(); |
74 | QProcessEnvironment &operator=(const QProcessEnvironment &other); |
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 | |
96 | private: |
97 | friend class QProcessPrivate; |
98 | friend class QProcessEnvironmentPrivate; |
99 | QSharedDataPointer<QProcessEnvironmentPrivate> d; |
100 | }; |
101 | |
102 | class Q_CORE_EXPORT QProcess : public QIODevice |
103 | { |
104 | Q_OBJECT |
105 | public: |
106 | enum ProcessError { |
107 | FailedToStart, //### file not found, resource error |
108 | Crashed, |
109 | Timedout, |
110 | ReadError, |
111 | WriteError, |
112 | UnknownError |
113 | }; |
114 | enum ProcessState { |
115 | NotRunning, |
116 | Starting, |
117 | Running |
118 | }; |
119 | enum ProcessChannel { |
120 | StandardOutput, |
121 | StandardError |
122 | }; |
123 | enum ProcessChannelMode { |
124 | SeparateChannels, |
125 | MergedChannels, |
126 | ForwardedChannels |
127 | }; |
128 | enum ExitStatus { |
129 | NormalExit, |
130 | CrashExit |
131 | }; |
132 | |
133 | explicit QProcess(QObject *parent = 0); |
134 | virtual ~QProcess(); |
135 | |
136 | void start(const QString &program, const QStringList &arguments, OpenMode mode = ReadWrite); |
137 | void start(const QString &program, OpenMode mode = ReadWrite); |
138 | |
139 | ProcessChannelMode readChannelMode() const; |
140 | void setReadChannelMode(ProcessChannelMode mode); |
141 | ProcessChannelMode processChannelMode() const; |
142 | void setProcessChannelMode(ProcessChannelMode mode); |
143 | |
144 | ProcessChannel readChannel() const; |
145 | void setReadChannel(ProcessChannel channel); |
146 | |
147 | void closeReadChannel(ProcessChannel channel); |
148 | void closeWriteChannel(); |
149 | |
150 | void setStandardInputFile(const QString &fileName); |
151 | void setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate); |
152 | void setStandardErrorFile(const QString &fileName, OpenMode mode = Truncate); |
153 | void setStandardOutputProcess(QProcess *destination); |
154 | |
155 | #if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN) |
156 | QString nativeArguments() const; |
157 | void setNativeArguments(const QString &arguments); |
158 | #endif |
159 | |
160 | QString workingDirectory() const; |
161 | void setWorkingDirectory(const QString &dir); |
162 | |
163 | void setEnvironment(const QStringList &environment); |
164 | QStringList environment() const; |
165 | void setProcessEnvironment(const QProcessEnvironment &environment); |
166 | QProcessEnvironment processEnvironment() const; |
167 | |
168 | QProcess::ProcessError error() const; |
169 | QProcess::ProcessState state() const; |
170 | |
171 | // #### Qt 5: Q_PID is a pointer on Windows and a value on Unix |
172 | Q_PID pid() const; |
173 | |
174 | bool waitForStarted(int msecs = 30000); |
175 | bool waitForReadyRead(int msecs = 30000); |
176 | bool waitForBytesWritten(int msecs = 30000); |
177 | bool waitForFinished(int msecs = 30000); |
178 | |
179 | QByteArray readAllStandardOutput(); |
180 | QByteArray readAllStandardError(); |
181 | |
182 | int exitCode() const; |
183 | QProcess::ExitStatus exitStatus() const; |
184 | |
185 | // QIODevice |
186 | qint64 bytesAvailable() const; |
187 | qint64 bytesToWrite() const; |
188 | bool isSequential() const; |
189 | bool canReadLine() const; |
190 | void close(); |
191 | bool atEnd() const; |
192 | |
193 | static int execute(const QString &program, const QStringList &arguments); |
194 | static int execute(const QString &program); |
195 | |
196 | static bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, |
197 | qint64 *pid = 0); |
198 | static bool startDetached(const QString &program, const QStringList &arguments); |
199 | static bool startDetached(const QString &program); |
200 | |
201 | static QStringList systemEnvironment(); |
202 | |
203 | public Q_SLOTS: |
204 | void terminate(); |
205 | void kill(); |
206 | |
207 | Q_SIGNALS: |
208 | void started(); |
209 | void finished(int exitCode); |
210 | void finished(int exitCode, QProcess::ExitStatus exitStatus); |
211 | void error(QProcess::ProcessError error); |
212 | void stateChanged(QProcess::ProcessState state); |
213 | |
214 | void readyReadStandardOutput(); |
215 | void readyReadStandardError(); |
216 | |
217 | protected: |
218 | void setProcessState(ProcessState state); |
219 | |
220 | virtual void setupChildProcess(); |
221 | |
222 | // QIODevice |
223 | qint64 readData(char *data, qint64 maxlen); |
224 | qint64 writeData(const char *data, qint64 len); |
225 | |
226 | private: |
227 | Q_DECLARE_PRIVATE(QProcess) |
228 | Q_DISABLE_COPY(QProcess) |
229 | |
230 | Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardOutput()) |
231 | Q_PRIVATE_SLOT(d_func(), bool _q_canReadStandardError()) |
232 | Q_PRIVATE_SLOT(d_func(), bool _q_canWrite()) |
233 | Q_PRIVATE_SLOT(d_func(), bool _q_startupNotification()) |
234 | Q_PRIVATE_SLOT(d_func(), bool _q_processDied()) |
235 | Q_PRIVATE_SLOT(d_func(), void _q_notified()) |
236 | friend class QProcessManager; |
237 | }; |
238 | |
239 | #endif // QT_NO_PROCESS |
240 | |
241 | QT_END_NAMESPACE |
242 | |
243 | QT_END_HEADER |
244 | |
245 | #endif // QPROCESS_H |
246 | |