1/*
2 Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
18*/
19
20#ifndef SHELLCOMMAND_H
21#define SHELLCOMMAND_H
22
23// Qt
24#include <QtCore/QStringList>
25
26// Konsole
27#include "konsole_export.h"
28
29namespace Konsole
30{
31/**
32 * A class to parse and extract information about shell commands.
33 *
34 * ShellCommand can be used to:
35 *
36 * <ul>
37 * <li>Take a command-line (eg "/bin/sh -c /path/to/my/script") and split it
38 * into its component parts (eg. the command "/bin/sh" and the arguments
39 * "-c","/path/to/my/script")
40 * </li>
41 * <li>Take a command and a list of arguments and combine them to
42 * form a complete command line.
43 * </li>
44 * <li>Determine whether the binary specified by a command exists in the
45 * user's PATH.
46 * </li>
47 * <li>Determine whether a command-line specifies the execution of
48 * another command as the root user using su/sudo etc.
49 * </li>
50 * </ul>
51 */
52class KONSOLEPRIVATE_EXPORT ShellCommand
53{
54public:
55 /**
56 * Constructs a ShellCommand from a command line.
57 *
58 * @param aCommand The command line to parse.
59 */
60 explicit ShellCommand(const QString& aCommand);
61 /**
62 * Constructs a ShellCommand with the specified @p aCommand and @p aArguments.
63 */
64 ShellCommand(const QString& aCommand, const QStringList& aArguments);
65
66 /** Returns the command. */
67 QString command() const;
68 /** Returns the arguments. */
69 QStringList arguments() const;
70
71 /**
72 * Returns the full command line.
73 */
74 QString fullCommand() const;
75
76 /** Expands environment variables in @p text .*/
77 static QString expand(const QString& text);
78
79 /** Expands environment variables in each string in @p list. */
80 static QStringList expand(const QStringList& items);
81
82
83 static bool isValidEnvCharacter(const QChar& ch);
84
85 static bool isValidLeadingEnvCharacter(const QChar& ch);
86
87private:
88 static bool expandEnv(QString& text);
89
90 QStringList _arguments;
91};
92}
93
94#endif // SHELLCOMMAND_H
95
96