shell.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2006, 2007 Julio M. Merino Vidal
  2. // Copyright (c) 2008 Ilya Sokolov, Boris Schaeling
  3. // Copyright (c) 2009 Boris Schaeling
  4. // Copyright (c) 2010 Felipe Tanus, Boris Schaeling
  5. // Copyright (c) 2011, 2012 Jeff Flinn, Boris Schaeling
  6. // Copyright (c) 2016 Klemens D. Morgenstern
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_PROCESS_SHELL_PATH_HPP
  11. #define BOOST_PROCESS_SHELL_PATH_HPP
  12. #include <boost/process/detail/config.hpp>
  13. #include <boost/process/detail/traits/wchar_t.hpp>
  14. #if defined(BOOST_POSIX_API)
  15. #include <boost/process/detail/posix/shell_path.hpp>
  16. #elif defined(BOOST_WINDOWS_API)
  17. #include <boost/process/detail/windows/shell_path.hpp>
  18. #endif
  19. /** \file boost/process/shell.hpp
  20. *
  21. * Header which provides the shell property. This provides the
  22. * property to launch a process through the system shell.
  23. * It also allows the user to obtain the shell-path via shell().
  24. \xmlonly
  25. <programlisting>
  26. namespace boost {
  27. namespace process {
  28. <emphasis>unspecified</emphasis> <globalname alt="boost::process::shell">shell</globalname>;
  29. }
  30. }
  31. </programlisting>
  32. \endxmlonly
  33. */
  34. namespace boost { namespace process { namespace detail {
  35. struct shell_
  36. {
  37. constexpr shell_() {}
  38. boost::filesystem::path operator()() const
  39. {
  40. return boost::process::detail::api::shell_path();
  41. }
  42. boost::filesystem::path operator()(std::error_code & ec) const noexcept
  43. {
  44. return boost::process::detail::api::shell_path(ec);
  45. }
  46. };
  47. template<>
  48. struct is_wchar_t<shell_> : is_wchar_t<boost::filesystem::path>
  49. {
  50. };
  51. }
  52. /**
  53. The shell property enables to launch a program through the shell of the system.
  54. \code{.cpp}
  55. system("gcc", shell);
  56. \endcode
  57. The shell argument goes without any expression. The operator() is overloaded, to
  58. obtain the path of the system shell.
  59. \code{.cpp}
  60. auto shell_cmd = shell();
  61. //avoid exceptions
  62. std::error_code ec;
  63. shell_cmd = shell(ec);
  64. \endcode
  65. \attention Launching through the shell will NOT provide proper error handling, i.e.
  66. you will get an error via the return code.
  67. \attention Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of `shell` is strongly discouraged in cases where the command string is constructed from external input:
  68. */
  69. constexpr ::boost::process::detail::shell_ shell;
  70. }}
  71. #endif