OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / utils / qtcprocess.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef QTCPROCESS_H
34 #define QTCPROCESS_H
35
36 #include <QtCore/QProcess>
37
38 #include "utils_global.h"
39
40 #include "environment.h"
41
42 namespace Utils {
43 class AbstractMacroExpander;
44
45 class QTCREATOR_UTILS_EXPORT QtcProcess : public QProcess
46 {
47     Q_OBJECT
48
49 public:
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; }
55     void start();
56
57     enum SplitError {
58         SplitOk = 0, //! All went just fine
59         BadQuoting, //! Command contains quoting errors
60         FoundMeta //! Command contains complex shell constructs
61     };
62
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);
69 #ifdef Q_OS_WIN
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);
83 #else
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);
93 #endif
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);
104
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.
108      */
109     class QTCREATOR_UTILS_EXPORT ArgIterator {
110     public:
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.
113         bool next();
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.
119         void deleteArg();
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);
123     private:
124         QString *m_str, m_value;
125         int m_pos, m_prev;
126         bool m_simple;
127     };
128
129     class QTCREATOR_UTILS_EXPORT ConstArgIterator {
130     public:
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(); }
135     private:
136         QString m_str;
137         ArgIterator m_ait;
138     };
139
140 private:
141     QString m_command;
142     QString m_arguments;
143     Environment m_environment;
144     bool m_haveEnv;
145 };
146
147 }
148
149 #endif // QTCPROCESS_H