source: ogBrowser-Git/qtermwidget/lib/kprocess.cpp @ c0cec9d

jenkinsmain
Last change on this file since c0cec9d was fedf2a2, checked in by Vadim Troshchinskiy Shmelev <vtroshchinskiy@…>, 18 months ago

Update Qtermwidget to Qt6 version
Remove build files

  • Property mode set to 100644
File size: 7.9 KB
Line 
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#include "kprocess.h"
31
32#include <qfile.h>
33
34/////////////////////////////
35// public member functions //
36/////////////////////////////
37
38KProcess::KProcess(QObject *parent) :
39    QProcess(parent),
40    d_ptr(new KProcessPrivate(this))
41{
42    setOutputChannelMode(ForwardedChannels);
43}
44
45KProcess::KProcess(KProcessPrivate *d, QObject *parent) :
46    QProcess(parent),
47    d_ptr(d)
48{
49    d_ptr->q_ptr = this;
50    setOutputChannelMode(ForwardedChannels);
51}
52
53KProcess::~KProcess() = default;
54
55void KProcess::setOutputChannelMode(OutputChannelMode mode)
56{
57    QProcess::setProcessChannelMode(static_cast<ProcessChannelMode>(mode));
58}
59
60KProcess::OutputChannelMode KProcess::outputChannelMode() const
61{
62    return static_cast<OutputChannelMode>(QProcess::processChannelMode());
63}
64
65void KProcess::setNextOpenMode(QIODevice::OpenMode mode)
66{
67    Q_D(KProcess);
68
69    d->openMode = mode;
70}
71
72#define DUMMYENV "_KPROCESS_DUMMY_="
73
74void KProcess::clearEnvironment()
75{
76    setEnvironment(QStringList() << QString::fromLatin1(DUMMYENV));
77}
78
79void KProcess::setEnv(const QString &name, const QString &value, bool overwrite)
80{
81    QStringList env = environment();
82    if (env.isEmpty()) {
83        env = systemEnvironment();
84        env.removeAll(QString::fromLatin1(DUMMYENV));
85    }
86    QString fname(name);
87    fname.append(QLatin1Char('='));
88    for (QStringList::Iterator it = env.begin(); it != env.end(); ++it)
89        if ((*it).startsWith(fname)) {
90            if (overwrite) {
91                *it = fname.append(value);
92                setEnvironment(env);
93            }
94            return;
95        }
96    env.append(fname.append(value));
97    setEnvironment(env);
98}
99
100void KProcess::unsetEnv(const QString &name)
101{
102    QStringList env = environment();
103    if (env.isEmpty()) {
104        env = systemEnvironment();
105        env.removeAll(QString::fromLatin1(DUMMYENV));
106    }
107    QString fname(name);
108    fname.append(QLatin1Char('='));
109    for (QStringList::Iterator it = env.begin(); it != env.end(); ++it)
110        if ((*it).startsWith(fname)) {
111            env.erase(it);
112            if (env.isEmpty())
113                env.append(QString::fromLatin1(DUMMYENV));
114            setEnvironment(env);
115            return;
116        }
117}
118
119void KProcess::setProgram(const QString &exe, const QStringList &args)
120{
121    Q_D(KProcess);
122
123    d->prog = exe;
124    d->args = args;
125#ifdef Q_OS_WIN
126    setNativeArguments(QString());
127#endif
128}
129
130void KProcess::setProgram(const QStringList &argv)
131{
132    Q_D(KProcess);
133
134    Q_ASSERT( !argv.isEmpty() );
135    d->args = argv;
136    d->prog = d->args.takeFirst();
137#ifdef Q_OS_WIN
138    setNativeArguments(QString());
139#endif
140}
141
142KProcess &KProcess::operator<<(const QString &arg)
143{
144    Q_D(KProcess);
145
146    if (d->prog.isEmpty())
147        d->prog = arg;
148    else
149        d->args << arg;
150    return *this;
151}
152
153KProcess &KProcess::operator<<(const QStringList &args)
154{
155    Q_D(KProcess);
156
157    if (d->prog.isEmpty())
158        setProgram(args);
159    else
160        d->args << args;
161    return *this;
162}
163
164void KProcess::clearProgram()
165{
166    Q_D(KProcess);
167
168    d->prog.clear();
169    d->args.clear();
170#ifdef Q_OS_WIN
171    setNativeArguments(QString());
172#endif
173}
174
175#if 0
176void KProcess::setShellCommand(const QString &cmd)
177{
178    Q_D(KProcess);
179
180    KShell::Errors err;
181    d->args = KShell::splitArgs(
182            cmd, KShell::AbortOnMeta | KShell::TildeExpand, &err);
183    if (err == KShell::NoError && !d->args.isEmpty()) {
184        d->prog = KStandardDirs::findExe(d->args[0]);
185        if (!d->prog.isEmpty()) {
186            d->args.removeFirst();
187#ifdef Q_OS_WIN
188            setNativeArguments(QString());
189#endif
190            return;
191        }
192    }
193
194    d->args.clear();
195
196#ifdef Q_OS_UNIX
197// #ifdef NON_FREE // ... as they ship non-POSIX /bin/sh
198# if !defined(__linux__) && !defined(__FreeBSD__) && !defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__) && !defined(__GNU__)
199    // If /bin/sh is a symlink, we can be pretty sure that it points to a
200    // POSIX shell - the original bourne shell is about the only non-POSIX
201    // shell still in use and it is always installed natively as /bin/sh.
202    d->prog = QFile::symLinkTarget(QString::fromLatin1("/bin/sh"));
203    if (d->prog.isEmpty()) {
204        // Try some known POSIX shells.
205        d->prog = KStandardDirs::findExe(QString::fromLatin1("ksh"));
206        if (d->prog.isEmpty()) {
207            d->prog = KStandardDirs::findExe(QString::fromLatin1("ash"));
208            if (d->prog.isEmpty()) {
209                d->prog = KStandardDirs::findExe(QString::fromLatin1("bash"));
210                if (d->prog.isEmpty()) {
211                    d->prog = KStandardDirs::findExe(QString::fromLatin1("zsh"));
212                    if (d->prog.isEmpty())
213                        // We're pretty much screwed, to be honest ...
214                        d->prog = QString::fromLatin1("/bin/sh");
215                }
216            }
217        }
218    }
219# else
220    d->prog = QString::fromLatin1("/bin/sh");
221# endif
222
223    d->args << QString::fromLatin1("-c") << cmd;
224#else // Q_OS_UNIX
225    // KMacroExpander::expandMacrosShellQuote(), KShell::quoteArg() and
226    // KShell::joinArgs() may generate these for security reasons.
227    setEnv(PERCENT_VARIABLE, QLatin1String("%"));
228
229#ifndef _WIN32_WCE
230    WCHAR sysdir[MAX_PATH + 1];
231    UINT size = GetSystemDirectoryW(sysdir, MAX_PATH + 1);
232    d->prog = QString::fromUtf16((const ushort *) sysdir, size);
233    d->prog += QLatin1String("\\cmd.exe");
234    setNativeArguments(QLatin1String("/V:OFF /S /C \"") + cmd + QLatin1Char('"'));
235#else
236    d->prog = QLatin1String("\\windows\\cmd.exe");
237    setNativeArguments(QLatin1String("/S /C \"") + cmd + QLatin1Char('"'));
238#endif
239#endif
240}
241#endif
242QStringList KProcess::program() const
243{
244    Q_D(const KProcess);
245
246    QStringList argv = d->args;
247    argv.prepend(d->prog);
248    return argv;
249}
250
251void KProcess::start()
252{
253    Q_D(KProcess);
254
255    QProcess::start(d->prog, d->args, d->openMode);
256}
257
258int KProcess::execute(int msecs)
259{
260    start();
261    if (!waitForFinished(msecs)) {
262        kill();
263        waitForFinished(-1);
264        return -2;
265    }
266    return (exitStatus() == QProcess::NormalExit) ? exitCode() : -1;
267}
268
269// static
270int KProcess::execute(const QString &exe, const QStringList &args, int msecs)
271{
272    KProcess p;
273    p.setProgram(exe, args);
274    return p.execute(msecs);
275}
276
277// static
278int KProcess::execute(const QStringList &argv, int msecs)
279{
280    KProcess p;
281    p.setProgram(argv);
282    return p.execute(msecs);
283}
284
285int KProcess::startDetached()
286{
287    Q_D(KProcess);
288
289    qint64 pid;
290    if (!QProcess::startDetached(d->prog, d->args, workingDirectory(), &pid))
291        return 0;
292    return static_cast<int>(pid);
293}
294
295// static
296int KProcess::startDetached(const QString &exe, const QStringList &args)
297{
298    qint64 pid;
299    if (!QProcess::startDetached(exe, args, QString(), &pid))
300        return 0;
301    return static_cast<int>(pid);
302}
303
304// static
305int KProcess::startDetached(const QStringList &argv)
306{
307    QStringList args = argv;
308    QString prog = args.takeFirst();
309    return startDetached(prog, args);
310}
Note: See TracBrowser for help on using the repository browser.