...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::process::v2::shell — Utility to parse commands.
// In header: <boost/process/v2/shell.hpp> struct shell { // types typedef char char_type; typedef const char ** args_type; // public member functions shell() = default; template<typename Char, typename Traits> shell(basic_string_view< Char, Traits >); shell(basic_cstring_ref< char_type >); shell(basic_string_view< typename std::conditional< std::is_same< char_type, char >::value, wchar_t, char >::type >); shell(const shell &) = delete; shell & operator=(const shell &) = delete; shell(shell &&) noexcept; shell & operator=(shell &&) noexcept; int argc() const; char_type ** argv() const; char_type ** begin() const; char_type ** end() const; bool empty() const; std::size_t size() const; args_type args() const; template<typename Environment = environment::current_view> filesystem::path exe(Environment && = environment::current()) const; ~shell(); // private member functions void parse_(); };
This utility class parses command lines into tokens and allows users to executed based on textual inputs.
In v1, this was possible directly when starting a process, but has been removed based on the security risks associated with this.
By making the shell parsing explicitly, it encourages a user to run a sanity check on the executable before launching it.
Example.
asio::io_context ctx; auto cmd = shell("my-app --help"); auto exe = cmd.exe(); check_if_malicious(exe); process proc{ctx, exe, cmd.args()};
shell
public member functionsshell() = default;
template<typename Char, typename Traits> shell(basic_string_view< Char, Traits > input);
shell(basic_cstring_ref< char_type > input);
shell(basic_string_view< typename std::conditional< std::is_same< char_type, char >::value, wchar_t, char >::type > input);
shell(const shell &) = delete;
shell & operator=(const shell &) = delete;
shell(shell && lhs) noexcept;
shell & operator=(shell && lhs) noexcept;
int argc() const;
char_type ** argv() const;
char_type ** begin() const;
char_type ** end() const;
bool empty() const;
std::size_t size() const;
args_type args() const;Native representation of the arguments to be used - excluding the executable.
template<typename Environment = environment::current_view> filesystem::path exe(Environment && env = environment::current()) const;
~shell();