1 | /* |
---|
2 | * This file is a part of QTerminal - http://gitorious.org/qterminal |
---|
3 | * |
---|
4 | * This file was un-linked from KDE and modified |
---|
5 | * by Maxim Bourmistrov <maxim@unixconn.com> |
---|
6 | * |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | This file is part of the KDE libraries |
---|
11 | |
---|
12 | Copyright (C) 2007 Oswald Buddenhagen <ossi@kde.org> |
---|
13 | |
---|
14 | This library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU Library General Public |
---|
16 | License as published by the Free Software Foundation; either |
---|
17 | version 2 of the License, or (at your option) any later version. |
---|
18 | |
---|
19 | This library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | Library General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU Library General Public License |
---|
25 | along with this library; see the file COPYING.LIB. If not, write to |
---|
26 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
27 | Boston, MA 02110-1301, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef KPROCESS_H |
---|
31 | #define KPROCESS_H |
---|
32 | |
---|
33 | //#include <kdecore_export.h> |
---|
34 | |
---|
35 | #include <QProcess> |
---|
36 | |
---|
37 | #include <memory> |
---|
38 | |
---|
39 | class KProcessPrivate; |
---|
40 | |
---|
41 | /** |
---|
42 | * \class KProcess kprocess.h <KProcess> |
---|
43 | * |
---|
44 | * Child process invocation, monitoring and control. |
---|
45 | * |
---|
46 | * This class extends QProcess by some useful functionality, overrides |
---|
47 | * some defaults with saner values and wraps parts of the API into a more |
---|
48 | * accessible one. |
---|
49 | * This is the preferred way of spawning child processes in KDE; don't |
---|
50 | * use QProcess directly. |
---|
51 | * |
---|
52 | * @author Oswald Buddenhagen <ossi@kde.org> |
---|
53 | **/ |
---|
54 | class KProcess : public QProcess |
---|
55 | { |
---|
56 | Q_OBJECT |
---|
57 | Q_DECLARE_PRIVATE(KProcess) |
---|
58 | |
---|
59 | public: |
---|
60 | |
---|
61 | /** |
---|
62 | * Modes in which the output channels can be opened. |
---|
63 | */ |
---|
64 | enum OutputChannelMode { |
---|
65 | SeparateChannels = QProcess::SeparateChannels, |
---|
66 | /**< Standard output and standard error are handled by KProcess |
---|
67 | as separate channels */ |
---|
68 | MergedChannels = QProcess::MergedChannels, |
---|
69 | /**< Standard output and standard error are handled by KProcess |
---|
70 | as one channel */ |
---|
71 | ForwardedChannels = QProcess::ForwardedChannels, |
---|
72 | /**< Both standard output and standard error are forwarded |
---|
73 | to the parent process' respective channel */ |
---|
74 | OnlyStdoutChannel = QProcess::ForwardedErrorChannel, |
---|
75 | /**< Only standard output is handled; standard error is forwarded */ |
---|
76 | OnlyStderrChannel = QProcess::ForwardedOutputChannel |
---|
77 | /**< Only standard error is handled; standard output is forwarded */ |
---|
78 | }; |
---|
79 | |
---|
80 | /** |
---|
81 | * Constructor |
---|
82 | */ |
---|
83 | explicit KProcess(QObject *parent = nullptr); |
---|
84 | |
---|
85 | /** |
---|
86 | * Destructor |
---|
87 | */ |
---|
88 | ~KProcess() override; |
---|
89 | |
---|
90 | /** |
---|
91 | * Set how to handle the output channels of the child process. |
---|
92 | * |
---|
93 | * The default is ForwardedChannels, which is unlike in QProcess. |
---|
94 | * Do not request more than you actually handle, as this output is |
---|
95 | * simply lost otherwise. |
---|
96 | * |
---|
97 | * This function must be called before starting the process. |
---|
98 | * |
---|
99 | * @param mode the output channel handling mode |
---|
100 | */ |
---|
101 | void setOutputChannelMode(OutputChannelMode mode); |
---|
102 | |
---|
103 | /** |
---|
104 | * Query how the output channels of the child process are handled. |
---|
105 | * |
---|
106 | * @return the output channel handling mode |
---|
107 | */ |
---|
108 | OutputChannelMode outputChannelMode() const; |
---|
109 | |
---|
110 | /** |
---|
111 | * Set the QIODevice open mode the process will be opened in. |
---|
112 | * |
---|
113 | * This function must be called before starting the process, obviously. |
---|
114 | * |
---|
115 | * @param mode the open mode. Note that this mode is automatically |
---|
116 | * "reduced" according to the channel modes and redirections. |
---|
117 | * The default is QIODevice::ReadWrite. |
---|
118 | */ |
---|
119 | void setNextOpenMode(QIODevice::OpenMode mode); |
---|
120 | |
---|
121 | /** |
---|
122 | * Adds the variable @p name to the process' environment. |
---|
123 | * |
---|
124 | * This function must be called before starting the process. |
---|
125 | * |
---|
126 | * @param name the name of the environment variable |
---|
127 | * @param value the new value for the environment variable |
---|
128 | * @param overwrite if @c false and the environment variable is already |
---|
129 | * set, the old value will be preserved |
---|
130 | */ |
---|
131 | void setEnv(const QString &name, const QString &value, bool overwrite = true); |
---|
132 | |
---|
133 | /** |
---|
134 | * Removes the variable @p name from the process' environment. |
---|
135 | * |
---|
136 | * This function must be called before starting the process. |
---|
137 | * |
---|
138 | * @param name the name of the environment variable |
---|
139 | */ |
---|
140 | void unsetEnv(const QString &name); |
---|
141 | |
---|
142 | /** |
---|
143 | * Empties the process' environment. |
---|
144 | * |
---|
145 | * Note that LD_LIBRARY_PATH/DYLD_LIBRARY_PATH is automatically added |
---|
146 | * on *NIX. |
---|
147 | * |
---|
148 | * This function must be called before starting the process. |
---|
149 | */ |
---|
150 | void clearEnvironment(); |
---|
151 | |
---|
152 | /** |
---|
153 | * Set the program and the command line arguments. |
---|
154 | * |
---|
155 | * This function must be called before starting the process, obviously. |
---|
156 | * |
---|
157 | * @param exe the program to execute |
---|
158 | * @param args the command line arguments for the program, |
---|
159 | * one per list element |
---|
160 | */ |
---|
161 | void setProgram(const QString &exe, const QStringList &args = QStringList()); |
---|
162 | |
---|
163 | /** |
---|
164 | * @overload |
---|
165 | * |
---|
166 | * @param argv the program to execute and the command line arguments |
---|
167 | * for the program, one per list element |
---|
168 | */ |
---|
169 | void setProgram(const QStringList &argv); |
---|
170 | |
---|
171 | /** |
---|
172 | * Append an element to the command line argument list for this process. |
---|
173 | * |
---|
174 | * If no executable is set yet, it will be set instead. |
---|
175 | * |
---|
176 | * For example, doing an "ls -l /usr/local/bin" can be achieved by: |
---|
177 | * \code |
---|
178 | * KProcess p; |
---|
179 | * p << "ls" << "-l" << "/usr/local/bin"; |
---|
180 | * ... |
---|
181 | * \endcode |
---|
182 | * |
---|
183 | * This function must be called before starting the process, obviously. |
---|
184 | * |
---|
185 | * @param arg the argument to add |
---|
186 | * @return a reference to this KProcess |
---|
187 | */ |
---|
188 | KProcess &operator<<(const QString& arg); |
---|
189 | |
---|
190 | /** |
---|
191 | * @overload |
---|
192 | * |
---|
193 | * @param args the arguments to add |
---|
194 | * @return a reference to this KProcess |
---|
195 | */ |
---|
196 | KProcess &operator<<(const QStringList& args); |
---|
197 | |
---|
198 | /** |
---|
199 | * Clear the program and command line argument list. |
---|
200 | */ |
---|
201 | void clearProgram(); |
---|
202 | |
---|
203 | /** |
---|
204 | * Set a command to execute through a shell (a POSIX sh on *NIX |
---|
205 | * and cmd.exe on Windows). |
---|
206 | * |
---|
207 | * Using this for anything but user-supplied commands is usually a bad |
---|
208 | * idea, as the command's syntax depends on the platform. |
---|
209 | * Redirections including pipes, etc. are better handled by the |
---|
210 | * respective functions provided by QProcess. |
---|
211 | * |
---|
212 | * If KProcess determines that the command does not really need a |
---|
213 | * shell, it will transparently execute it without one for performance |
---|
214 | * reasons. |
---|
215 | * |
---|
216 | * This function must be called before starting the process, obviously. |
---|
217 | * |
---|
218 | * @param cmd the command to execute through a shell. |
---|
219 | * The caller must make sure that all filenames etc. are properly |
---|
220 | * quoted when passed as argument. Failure to do so often results in |
---|
221 | * serious security holes. See KShell::quoteArg(). |
---|
222 | */ |
---|
223 | void setShellCommand(const QString &cmd); |
---|
224 | |
---|
225 | /** |
---|
226 | * Obtain the currently set program and arguments. |
---|
227 | * |
---|
228 | * @return a list, the first element being the program, the remaining ones |
---|
229 | * being command line arguments to the program. |
---|
230 | */ |
---|
231 | QStringList program() const; |
---|
232 | |
---|
233 | /** |
---|
234 | * Start the process. |
---|
235 | * |
---|
236 | * @see QProcess::start(const QString &, const QStringList &, OpenMode) |
---|
237 | */ |
---|
238 | void start(); |
---|
239 | |
---|
240 | /** |
---|
241 | * Start the process, wait for it to finish, and return the exit code. |
---|
242 | * |
---|
243 | * This method is roughly equivalent to the sequence: |
---|
244 | * <code> |
---|
245 | * start(); |
---|
246 | * waitForFinished(msecs); |
---|
247 | * return exitCode(); |
---|
248 | * </code> |
---|
249 | * |
---|
250 | * Unlike the other execute() variants this method is not static, |
---|
251 | * so the process can be parametrized properly and talked to. |
---|
252 | * |
---|
253 | * @param msecs time to wait for process to exit before killing it |
---|
254 | * @return -2 if the process could not be started, -1 if it crashed, |
---|
255 | * otherwise its exit code |
---|
256 | */ |
---|
257 | int execute(int msecs = -1); |
---|
258 | |
---|
259 | /** |
---|
260 | * @overload |
---|
261 | * |
---|
262 | * @param exe the program to execute |
---|
263 | * @param args the command line arguments for the program, |
---|
264 | * one per list element |
---|
265 | * @param msecs time to wait for process to exit before killing it |
---|
266 | * @return -2 if the process could not be started, -1 if it crashed, |
---|
267 | * otherwise its exit code |
---|
268 | */ |
---|
269 | static int execute(const QString &exe, const QStringList &args = QStringList(), int msecs = -1); |
---|
270 | |
---|
271 | /** |
---|
272 | * @overload |
---|
273 | * |
---|
274 | * @param argv the program to execute and the command line arguments |
---|
275 | * for the program, one per list element |
---|
276 | * @param msecs time to wait for process to exit before killing it |
---|
277 | * @return -2 if the process could not be started, -1 if it crashed, |
---|
278 | * otherwise its exit code |
---|
279 | */ |
---|
280 | static int execute(const QStringList &argv, int msecs = -1); |
---|
281 | |
---|
282 | /** |
---|
283 | * Start the process and detach from it. See QProcess::startDetached() |
---|
284 | * for details. |
---|
285 | * |
---|
286 | * Unlike the other startDetached() variants this method is not static, |
---|
287 | * so the process can be parametrized properly. |
---|
288 | * @note Currently, only the setProgram()/setShellCommand() and |
---|
289 | * setWorkingDirectory() parametrizations are supported. |
---|
290 | * |
---|
291 | * The KProcess object may be re-used immediately after calling this |
---|
292 | * function. |
---|
293 | * |
---|
294 | * @return the PID of the started process or 0 on error |
---|
295 | */ |
---|
296 | int startDetached(); |
---|
297 | |
---|
298 | /** |
---|
299 | * @overload |
---|
300 | * |
---|
301 | * @param exe the program to start |
---|
302 | * @param args the command line arguments for the program, |
---|
303 | * one per list element |
---|
304 | * @return the PID of the started process or 0 on error |
---|
305 | */ |
---|
306 | static int startDetached(const QString &exe, const QStringList &args = QStringList()); |
---|
307 | |
---|
308 | /** |
---|
309 | * @overload |
---|
310 | * |
---|
311 | * @param argv the program to start and the command line arguments |
---|
312 | * for the program, one per list element |
---|
313 | * @return the PID of the started process or 0 on error |
---|
314 | */ |
---|
315 | static int startDetached(const QStringList &argv); |
---|
316 | |
---|
317 | protected: |
---|
318 | /** |
---|
319 | * @internal |
---|
320 | */ |
---|
321 | KProcess(KProcessPrivate *d, QObject *parent); |
---|
322 | |
---|
323 | /** |
---|
324 | * @internal |
---|
325 | */ |
---|
326 | std::unique_ptr<KProcessPrivate> const d_ptr; |
---|
327 | |
---|
328 | private: |
---|
329 | // hide those |
---|
330 | #if QT_VERSION < 0x060000 |
---|
331 | using QProcess::setReadChannelMode; |
---|
332 | using QProcess::readChannelMode; |
---|
333 | #endif |
---|
334 | using QProcess::setProcessChannelMode; |
---|
335 | using QProcess::processChannelMode; |
---|
336 | }; |
---|
337 | |
---|
338 | /* ----------- kprocess_p.h ---------------- */ |
---|
339 | class KProcessPrivate { |
---|
340 | |
---|
341 | Q_DECLARE_PUBLIC(KProcess) |
---|
342 | |
---|
343 | protected: |
---|
344 | KProcessPrivate(KProcess *qq) : |
---|
345 | openMode(QIODevice::ReadWrite), |
---|
346 | q_ptr(qq) |
---|
347 | { |
---|
348 | } |
---|
349 | |
---|
350 | QString prog; |
---|
351 | QStringList args; |
---|
352 | QIODevice::OpenMode openMode; |
---|
353 | |
---|
354 | KProcess *q_ptr; |
---|
355 | }; |
---|
356 | /* ------------------------------------------- */ |
---|
357 | #endif |
---|
358 | |
---|