File indexing completed on 2024-04-21 05:51:27

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef SHELLCOMMAND_H
0008 #define SHELLCOMMAND_H
0009 
0010 // Qt
0011 #include <QStringList>
0012 
0013 namespace Konsole
0014 {
0015 /**
0016  * A class to parse and extract information about shell commands.
0017  *
0018  * ShellCommand can be used to:
0019  *
0020  * <ul>
0021  *      <li>Take a command-line (eg "/bin/sh -c /path/to/my/script") and split it
0022  *          into its component parts (eg. the command "/bin/sh" and the arguments
0023  *          "-c","/path/to/my/script")
0024  *      </li>
0025  *      <li>Take a command and a list of arguments and combine them to
0026  *          form a complete command line.
0027  *      </li>
0028  *      <li>Determine whether the binary specified by a command exists in the
0029  *          user's PATH.
0030  *      </li>
0031  *      <li>Determine whether a command-line specifies the execution of
0032  *          another command as the root user using su/sudo etc.
0033  *      </li>
0034  * </ul>
0035  */
0036 class ShellCommand
0037 {
0038 public:
0039     /**
0040      * Constructs a ShellCommand from a command line.
0041      *
0042      * @param aCommand The command line to parse.
0043      */
0044     explicit ShellCommand(const QString &aCommand);
0045     /**
0046      * Constructs a ShellCommand with the specified @p aCommand and @p aArguments.
0047      */
0048     ShellCommand(const QString &aCommand, const QStringList &aArguments);
0049 
0050     /** Returns the command. */
0051     QString command() const;
0052     /** Returns the arguments. */
0053     QStringList arguments() const;
0054 
0055     /**
0056      * Returns the full command line.
0057      */
0058     QString fullCommand() const;
0059 
0060     /** Expands environment variables in @p text .*/
0061     static QString expand(const QString &text);
0062 
0063     /** Expands environment variables in each string in @p list. */
0064     static QStringList expand(const QStringList &items);
0065 
0066     static bool isValidEnvCharacter(const QChar &ch);
0067 
0068     static bool isValidLeadingEnvCharacter(const QChar &ch);
0069 
0070 private:
0071     static bool expandEnv(QString &text);
0072 
0073     QStringList _arguments;
0074 };
0075 }
0076 
0077 #endif // SHELLCOMMAND_H