1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
36 #include <QtCore/QProcess>
38 #include "utils_global.h"
40 #include "environment.h"
43 class AbstractMacroExpander;
45 class QTCREATOR_UTILS_EXPORT QtcProcess : public QProcess
50 QtcProcess(QObject *parent = 0) : QProcess(parent), m_haveEnv(false) {}
51 void setEnvironment(const Environment &env)
52 { m_environment = env; m_haveEnv = true; }
53 void setCommand(const QString &command, const QString &arguments)
54 { m_command = command; m_arguments = arguments; }
58 SplitOk = 0, //! All went just fine
59 BadQuoting, //! Command contains quoting errors
60 FoundMeta //! Command contains complex shell constructs
63 //! Quote a single argument for usage in a unix shell command
64 static QString quoteArgUnix(const QString &arg);
65 //! Quote a single argument and append it to a unix shell command
66 static void addArgUnix(QString *args, const QString &arg);
67 //! Join an argument list into a unix shell command
68 static QString joinArgsUnix(const QStringList &args);
70 //! Quote a single argument for usage in a shell command
71 static QString quoteArg(const QString &arg);
72 //! Quote a single argument and append it to a shell command
73 static void addArg(QString *args, const QString &arg);
74 //! Join an argument list into a shell command
75 static QString joinArgs(const QStringList &args);
76 //! Prepare argument of a shell command for feeding into QProcess
77 static QString prepareArgs(const QString &cmd, SplitError *err,
78 const Environment *env = 0, const QString *pwd = 0);
79 //! Prepare a shell command for feeding into QProcess
80 static void prepareCommand(const QString &command, const QString &arguments,
81 QString *outCmd, QString *outArgs,
82 const Environment *env = 0, const QString *pwd = 0);
84 static QString quoteArg(const QString &arg) { return quoteArgUnix(arg); }
85 static void addArg(QString *args, const QString &arg) { addArgUnix(args, arg); }
86 static QString joinArgs(const QStringList &args) { return joinArgsUnix(args); }
87 static QStringList prepareArgs(const QString &cmd, SplitError *err,
88 const Environment *env = 0, const QString *pwd = 0)
89 { return splitArgs(cmd, true, err, env, pwd); }
90 static bool prepareCommand(const QString &command, const QString &arguments,
91 QString *outCmd, QStringList *outArgs,
92 const Environment *env = 0, const QString *pwd = 0);
94 //! Quote and append each argument to a shell command
95 static void addArgs(QString *args, const QStringList &inArgs);
96 //! Append already quoted arguments to a shell command
97 static void addArgs(QString *args, const QString &inArgs);
98 //! Split a shell command into separate arguments. ArgIterator is usually a better choice.
99 static QStringList splitArgs(const QString &cmd, bool abortOnMeta = false, SplitError *err = 0,
100 const Environment *env = 0, const QString *pwd = 0);
101 //! Safely replace the expandos in a shell command
102 static bool expandMacros(QString *cmd, AbstractMacroExpander *mx);
103 static QString expandMacros(const QString &str, AbstractMacroExpander *mx);
105 /*! Iterate over arguments from a command line.
106 * Assumes that the name of the actual command is *not* part of the line.
107 * Terminates after the first command if the command line is complex.
109 class QTCREATOR_UTILS_EXPORT ArgIterator {
111 ArgIterator(QString *str) : m_str(str), m_pos(0), m_prev(-1) {}
112 //! Get the next argument. Returns false on encountering end of first command.
114 //! True iff the argument is a plain string, possibly after unquoting.
115 bool isSimple() const { return m_simple; }
116 //! Return the string value of the current argument if it is simple, otherwise empty.
117 QString value() const { return m_value; }
118 //! Delete the last argument fetched via next() from the command line.
120 //! Insert argument into the command line after the last one fetched via next().
121 //! This may be used before the first call to next() to insert at the front.
122 void appendArg(const QString &str);
124 QString *m_str, m_value;
129 class QTCREATOR_UTILS_EXPORT ConstArgIterator {
131 ConstArgIterator(const QString &str) : m_str(str), m_ait(&m_str) {}
132 bool next() { return m_ait.next(); }
133 bool isSimple() const { return m_ait.isSimple(); }
134 QString value() const { return m_ait.value(); }
143 Environment m_environment;
149 #endif // QTCPROCESS_H