• Skip to content
  • Skip to link menu
Brand

API Documentation

  1. KDE API Reference
  2. The KDE Frameworks
  3. KCoreAddons
  • KDE Home
  • Contact Us

Quick Links

Skip menu "KCoreAddons"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Dependencies
  • Related Pages

Class Picker

About

Addons to QtCore

Maintainer
Michael Pyne
Supported platforms
Android, FreeBSD, Linux, MacOSX, Windows
Community
IRC: #kde-devel on Freenode
Mailing list: kde-frameworks-devel
Use with CMake
find_package(KF5CoreAddons)
target_link_libraries(yourapp KF5::CoreAddons)
Use with QMake
QT += KCoreAddons 
Clone
git clone git://anongit.kde.org/kcoreaddons.git
Browse source
KCoreAddons on cgit.kde.org

KCoreAddons

  • frameworks
  • frameworks
  • kcoreaddons
  • src
  • lib
  • io
kprocess.cpp
1 /*
2  This file is part of the KDE libraries
3 
4  Copyright (C) 2007 Oswald Buddenhagen <[email protected]>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kprocess_p.h"
23 
24 #include <qstandardpaths.h>
25 #include <qplatformdefs.h>
26 #include <kshell.h>
27 #ifdef Q_OS_WIN
28 # include <qt_windows.h>
29 # include <kshell_p.h>
30 #endif
31 
32 #include <qfile.h>
33 
35 // public member functions //
37 
38 KProcess::KProcess(QObject *parent) :
39  QProcess(parent),
40  d_ptr(new KProcessPrivate(this))
41 {
42  setOutputChannelMode(ForwardedChannels);
43 }
44 
45 KProcess::KProcess(KProcessPrivate *d, QObject *parent) :
46  QProcess(parent),
47  d_ptr(d)
48 {
49  d_ptr->q_ptr = this;
50  setOutputChannelMode(ForwardedChannels);
51 }
52 
53 KProcess::~KProcess()
54 {
55  delete d_ptr;
56 }
57 
58 void KProcess::setOutputChannelMode(OutputChannelMode mode)
59 {
60  QProcess::setProcessChannelMode(static_cast<ProcessChannelMode>(mode));
61 }
62 
63 KProcess::OutputChannelMode KProcess::outputChannelMode() const
64 {
65  return static_cast<OutputChannelMode>(QProcess::processChannelMode());
66 }
67 
68 void KProcess::setNextOpenMode(QIODevice::OpenMode mode)
69 {
70  Q_D(KProcess);
71 
72  d->openMode = mode;
73 }
74 
75 #define DUMMYENV "_KPROCESS_DUMMY_="
76 
77 void KProcess::clearEnvironment()
78 {
79  setEnvironment(QStringList { QStringLiteral(DUMMYENV) });
80 }
81 
82 void KProcess::setEnv(const QString &name, const QString &value, bool overwrite)
83 {
84  QStringList env = environment();
85  if (env.isEmpty()) {
86  env = systemEnvironment();
87  env.removeAll(QStringLiteral(DUMMYENV));
88  }
89  QString fname(name);
90  fname.append(QLatin1Char('='));
91  for (QStringList::Iterator it = env.begin(); it != env.end(); ++it)
92  if ((*it).startsWith(fname)) {
93  if (overwrite) {
94  *it = fname.append(value);
95  setEnvironment(env);
96  }
97  return;
98  }
99  env.append(fname.append(value));
100  setEnvironment(env);
101 }
102 
103 void KProcess::unsetEnv(const QString &name)
104 {
105  QStringList env = environment();
106  if (env.isEmpty()) {
107  env = systemEnvironment();
108  env.removeAll(QStringLiteral(DUMMYENV));
109  }
110  QString fname(name);
111  fname.append(QLatin1Char('='));
112  for (QStringList::Iterator it = env.begin(); it != env.end(); ++it)
113  if ((*it).startsWith(fname)) {
114  env.erase(it);
115  if (env.isEmpty()) {
116  env.append(QStringLiteral(DUMMYENV));
117  }
118  setEnvironment(env);
119  return;
120  }
121 }
122 
123 void KProcess::setProgram(const QString &exe, const QStringList &args)
124 {
125  Q_D(KProcess);
126 
127  d->prog = exe;
128  d->args = args;
129 #ifdef Q_OS_WIN
130  setNativeArguments(QString());
131 #endif
132 }
133 
134 void KProcess::setProgram(const QStringList &argv)
135 {
136  Q_D(KProcess);
137 
138  Q_ASSERT(!argv.isEmpty());
139  d->args = argv;
140  d->prog = d->args.takeFirst();
141 #ifdef Q_OS_WIN
142  setNativeArguments(QString());
143 #endif
144 }
145 
146 KProcess &KProcess::operator<<(const QString &arg)
147 {
148  Q_D(KProcess);
149 
150  if (d->prog.isEmpty()) {
151  d->prog = arg;
152  } else {
153  d->args << arg;
154  }
155  return *this;
156 }
157 
158 KProcess &KProcess::operator<<(const QStringList &args)
159 {
160  Q_D(KProcess);
161 
162  if (d->prog.isEmpty()) {
163  setProgram(args);
164  } else {
165  d->args << args;
166  }
167  return *this;
168 }
169 
170 void KProcess::clearProgram()
171 {
172  Q_D(KProcess);
173 
174  d->prog.clear();
175  d->args.clear();
176 #ifdef Q_OS_WIN
177  setNativeArguments(QString());
178 #endif
179 }
180 
181 void KProcess::setShellCommand(const QString &cmd)
182 {
183  Q_D(KProcess);
184 
185  KShell::Errors err;
186  d->args = KShell::splitArgs(
187  cmd, KShell::AbortOnMeta | KShell::TildeExpand, &err);
188  if (err == KShell::NoError && !d->args.isEmpty()) {
189  d->prog = QStandardPaths::findExecutable(d->args[0]);
190  if (!d->prog.isEmpty()) {
191  d->args.removeFirst();
192 #ifdef Q_OS_WIN
193  setNativeArguments(QString());
194 #endif
195  return;
196  }
197  }
198 
199  d->args.clear();
200 
201 #ifdef Q_OS_UNIX
202 // #ifdef NON_FREE // ... as they ship non-POSIX /bin/sh
203 # if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__GNU__) && !defined(__APPLE__)
204  // If /bin/sh is a symlink, we can be pretty sure that it points to a
205  // POSIX shell - the original bourne shell is about the only non-POSIX
206  // shell still in use and it is always installed natively as /bin/sh.
207  d->prog = QFile::symLinkTarget(QStringLiteral("/bin/sh"));
208  if (d->prog.isEmpty()) {
209  // Try some known POSIX shells.
210  d->prog = QStandardPaths::findExecutable(QStringLiteral("ksh"));
211  if (d->prog.isEmpty()) {
212  d->prog = QStandardPaths::findExecutable(QStringLiteral("ash"));
213  if (d->prog.isEmpty()) {
214  d->prog = QStandardPaths::findExecutable(QStringLiteral("bash"));
215  if (d->prog.isEmpty()) {
216  d->prog = QStandardPaths::findExecutable(QStringLiteral("zsh"));
217  if (d->prog.isEmpty())
218  // We're pretty much screwed, to be honest ...
219  {
220  d->prog = QStringLiteral("/bin/sh");
221  }
222  }
223  }
224  }
225  }
226 # else
227  d->prog = QStringLiteral("/bin/sh");
228 # endif
229 
230  d->args << QStringLiteral("-c") << cmd;
231 #else // Q_OS_UNIX
232  // KMacroExpander::expandMacrosShellQuote(), KShell::quoteArg() and
233  // KShell::joinArgs() may generate these for security reasons.
234  setEnv(PERCENT_VARIABLE, QStringLiteral("%"));
235 
236 #ifndef _WIN32_WCE
237  WCHAR sysdir[MAX_PATH + 1];
238  UINT size = GetSystemDirectoryW(sysdir, MAX_PATH + 1);
239  d->prog = QString::fromUtf16((const ushort *) sysdir, size);
240  d->prog += QLatin1String("\\cmd.exe");
241  setNativeArguments(QLatin1String("/V:OFF /S /C \"") + cmd + QLatin1Char('"'));
242 #else
243  d->prog = QStringLiteral("\\windows\\cmd.exe");
244  setNativeArguments(QStringLiteral("/S /C \"") + cmd + QLatin1Char('"'));
245 #endif
246 #endif
247 }
248 
249 QStringList KProcess::program() const
250 {
251  Q_D(const KProcess);
252 
253  QStringList argv = d->args;
254  argv.prepend(d->prog);
255  return argv;
256 }
257 
258 void KProcess::start()
259 {
260  Q_D(KProcess);
261 
262  QProcess::start(d->prog, d->args, d->openMode);
263 }
264 
265 int KProcess::execute(int msecs)
266 {
267  start();
268  if (!waitForFinished(msecs)) {
269  kill();
270  waitForFinished(-1);
271  return -2;
272  }
273  return (exitStatus() == QProcess::NormalExit) ? exitCode() : -1;
274 }
275 
276 // static
277 int KProcess::execute(const QString &exe, const QStringList &args, int msecs)
278 {
279  KProcess p;
280  p.setProgram(exe, args);
281  return p.execute(msecs);
282 }
283 
284 // static
285 int KProcess::execute(const QStringList &argv, int msecs)
286 {
287  KProcess p;
288  p.setProgram(argv);
289  return p.execute(msecs);
290 }
291 
292 int KProcess::startDetached()
293 {
294  Q_D(KProcess);
295 
296  qint64 pid;
297  if (!QProcess::startDetached(d->prog, d->args, workingDirectory(), &pid)) {
298  return 0;
299  }
300  return static_cast<int>(pid);
301 }
302 
303 // static
304 int KProcess::startDetached(const QString &exe, const QStringList &args)
305 {
306  qint64 pid;
307  if (!QProcess::startDetached(exe, args, QString(), &pid)) {
308  return 0;
309  }
310  return static_cast<int>(pid);
311 }
312 
313 // static
314 int KProcess::startDetached(const QStringList &argv)
315 {
316  QStringList args = argv;
317  QString prog = args.takeFirst();
318  return startDetached(prog, args);
319 }
320 
321 int KProcess::pid() const
322 {
323 #ifdef Q_OS_UNIX
324  return static_cast<int>(QProcess::pid());
325 #else
326  return QProcess::pid() ? QProcess::pid()->dwProcessId : 0;
327 #endif
328 }
329 
330 #include "moc_kprocess.cpp"
KProcess::setNextOpenMode
void setNextOpenMode(QIODevice::OpenMode mode)
Set the QIODevice open mode the process will be opened in.
Definition: kprocess.cpp:68
KProcess::setShellCommand
void setShellCommand(const QString &cmd)
Set a command to execute through a shell (a POSIX sh on *NIX and cmd.exe on Windows).
Definition: kprocess.cpp:181
QString::append
QString & append(QChar ch)
KProcess
Child process invocation, monitoring and control.
Definition: kprocess.h:44
QProcess::kill
void kill()
QProcess::systemEnvironment
QStringList systemEnvironment()
QProcess::startDetached
bool startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory, qint64 *pid)
QStandardPaths::findExecutable
QString findExecutable(const QString &executableName, const QStringList &paths)
KProcess::operator<<
KProcess & operator<<(const QString &arg)
Append an element to the command line argument list for this process.
Definition: kprocess.cpp:146
QIODevice::OpenMode
typedef OpenMode
QList::erase
iterator erase(iterator pos)
KProcess::setOutputChannelMode
void setOutputChannelMode(OutputChannelMode mode)
Set how to handle the output channels of the child process.
Definition: kprocess.cpp:58
KProcess::KProcess
KProcess(QObject *parent=nullptr)
Constructor.
Definition: kprocess.cpp:38
QProcess::processChannelMode
ProcessChannelMode processChannelMode() const
QList::append
void append(const T &value)
QIODevice::size
virtual qint64 size() const
KProcess::program
QStringList program() const
Obtain the currently set program and arguments.
Definition: kprocess.cpp:249
QString::fromUtf16
QString fromUtf16(const ushort *unicode, int size)
QProcess
QObject
KProcess::clearProgram
void clearProgram()
Clear the program and command line argument list.
Definition: kprocess.cpp:170
KProcess::setEnv
void setEnv(const QString &name, const QString &value, bool overwrite=true)
Adds the variable name to the process&#39; environment.
Definition: kprocess.cpp:82
QFile::symLinkTarget
QString symLinkTarget() const
QList::isEmpty
bool isEmpty() const
QList::removeAll
int removeAll(const T &value)
KProcess::clearEnvironment
void clearEnvironment()
Empties the process&#39; environment.
Definition: kprocess.cpp:77
KShell::Errors
Errors
Status codes from splitArgs()
Definition: kshell.h:87
QList::Iterator
typedef Iterator
QString
KShell::splitArgs
KCOREADDONS_EXPORT QStringList splitArgs(const QString &cmd, Options flags=NoOptions, Errors *err=nullptr)
Splits cmd according to system shell word splitting and quoting rules.
Definition: kshell_unix.cpp:71
KProcess::unsetEnv
void unsetEnv(const QString &name)
Removes the variable name from the process&#39; environment.
Definition: kprocess.cpp:103
QStringList
KProcess::d_ptr
KProcessPrivate *const d_ptr
Definition: kprocess.h:328
KProcess::start
void start()
Start the process.
Definition: kprocess.cpp:258
QList::end
iterator end()
QProcess::pid
Q_PID pid() const
QLatin1Char
QProcess::setProcessChannelMode
void setProcessChannelMode(ProcessChannelMode mode)
QProcess::setEnvironment
void setEnvironment(const QStringList &environment)
KProcess::ForwardedChannels
Both standard output and standard error are forwarded to the parent process&#39; respective channel...
Definition: kprocess.h:61
QList::takeFirst
T takeFirst()
QLatin1String
QProcess::workingDirectory
QString workingDirectory() const
KShell::NoError
Success.
Definition: kshell.h:91
KProcess::execute
int execute(int msecs=-1)
Start the process, wait for it to finish, and return the exit code.
Definition: kprocess.cpp:265
QList::prepend
void prepend(const T &value)
KProcess::pid
int pid() const
Obtain the process&#39; ID as known to the system.
Definition: kprocess.cpp:321
QProcess::environment
QStringList environment() const
KProcess::~KProcess
~KProcess() override
Destructor.
Definition: kprocess.cpp:53
QObject::parent
QObject * parent() const
QProcess::setNativeArguments
void setNativeArguments(const QString &arguments)
KProcess::startDetached
int startDetached()
Start the process and detach from it.
Definition: kprocess.cpp:292
QProcess::exitStatus
QProcess::ExitStatus exitStatus() const
QList::begin
iterator begin()
QProcess::exitCode
int exitCode() const
QProcess::start
void start(const QString &program, const QStringList &arguments, QFlags< QIODevice::OpenModeFlag > mode)
KShell::AbortOnMeta
Put the parser into full shell mode and bail out if a too complex construct is encountered.
Definition: kshell.h:80
KShell::TildeExpand
Perform tilde expansion.
Definition: kshell.h:49
KProcess::OutputChannelMode
OutputChannelMode
Modes in which the output channels can be opened.
Definition: kprocess.h:54
KProcess::outputChannelMode
OutputChannelMode outputChannelMode() const
Query how the output channels of the child process are handled.
Definition: kprocess.cpp:63
KProcess::setProgram
void setProgram(const QString &exe, const QStringList &args=QStringList())
Set the program and the command line arguments.
Definition: kprocess.cpp:123
QProcess::waitForFinished
bool waitForFinished(int msecs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Sun Dec 15 2019 02:33:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal