Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for a snapshot of the develop branch, built from commit b8adfe0e57.
PrevUpHomeNext

Struct shell

boost::process::v2::shell — Utility to parse commands.

Synopsis

// In header: <boost/process/v2/shell.hpp>


struct shell {
  // types
  typedef char          char_type;
  typedef const char ** args_type;

  // construct/copy/destruct
  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(shell &&) noexcept;
  shell & operator=(const shell &) = delete;
  shell & operator=(shell &&) noexcept;
  ~shell();

  // public member functions
  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;

  // private member functions
  void parse_();
};

Description

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 construct/copy/destruct

  1. shell() = default;
  2. template<typename Char, typename Traits> 
      shell(basic_string_view< Char, Traits > input);
  3. shell(basic_cstring_ref< char_type > input);
  4. shell(basic_string_view< typename std::conditional< std::is_same< char_type, char >::value, wchar_t, char >::type > input);
  5. shell(const shell &) = delete;
  6. shell(shell && lhs) noexcept;
  7. shell & operator=(const shell &) = delete;
  8. shell & operator=(shell && lhs) noexcept;
  9. ~shell();

shell public member functions

  1. int argc() const;
  2. char_type ** argv() const;
  3. char_type ** begin() const;
  4. char_type ** end() const;
  5. bool empty() const;
  6. std::size_t size() const;
  7. args_type args() const;
    Native representation of the arguments to be used - excluding the executable.
  8. template<typename Environment = environment::current_view> 
      filesystem::path exe(Environment && env = environment::current()) const;

shell private member functions

  1. void parse_();

PrevUpHomeNext