io.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_IO_HPP_
  6. #define BOOST_PROCESS_IO_HPP_
  7. #include <iosfwd>
  8. #include <cstdio>
  9. #include <functional>
  10. #include <utility>
  11. #include <boost/process/detail/config.hpp>
  12. #include <boost/process/pipe.hpp>
  13. #include <future>
  14. #if defined(BOOST_POSIX_API)
  15. #include <boost/process/detail/posix/asio_fwd.hpp>
  16. #include <boost/process/detail/posix/close_in.hpp>
  17. #include <boost/process/detail/posix/close_out.hpp>
  18. #include <boost/process/detail/posix/null_in.hpp>
  19. #include <boost/process/detail/posix/null_out.hpp>
  20. #include <boost/process/detail/posix/file_in.hpp>
  21. #include <boost/process/detail/posix/file_out.hpp>
  22. #include <boost/process/detail/posix/pipe_in.hpp>
  23. #include <boost/process/detail/posix/pipe_out.hpp>
  24. #elif defined(BOOST_WINDOWS_API)
  25. #include <boost/process/detail/windows/asio_fwd.hpp>
  26. #include <boost/process/detail/windows/close_in.hpp>
  27. #include <boost/process/detail/windows/close_out.hpp>
  28. #include <boost/process/detail/windows/null_in.hpp>
  29. #include <boost/process/detail/windows/null_out.hpp>
  30. #include <boost/process/detail/windows/file_in.hpp>
  31. #include <boost/process/detail/windows/file_out.hpp>
  32. #include <boost/process/detail/windows/pipe_in.hpp>
  33. #include <boost/process/detail/windows/pipe_out.hpp>
  34. #endif
  35. /** \file boost/process/io.hpp
  36. *
  37. * Header which provides the io properties. It provides the following properties:
  38. *
  39. \xmlonly
  40. <programlisting>
  41. namespace boost {
  42. namespace process {
  43. <emphasis>unspecified</emphasis> <globalname alt="boost::process::close">close</globalname>;
  44. <emphasis>unspecified</emphasis> <globalname alt="boost::process::null">null</globalname>;
  45. <emphasis>unspecified</emphasis> <globalname alt="boost::process::std_in">std_in</globalname>;
  46. <emphasis>unspecified</emphasis> <globalname alt="boost::process::std_out">std_out</globalname>;
  47. <emphasis>unspecified</emphasis> <globalname alt="boost::process::std_err">std_err</globalname>;
  48. }
  49. }
  50. </programlisting>
  51. \endxmlonly
  52. \par File I/O
  53. The library allows full redirection of streams to files as shown below.
  54. \code{.cpp}
  55. boost::process::filesystem::path log = "my_log_file.txt";
  56. boost::process::filesystem::path input = "input.txt";
  57. boost::process::filesystem::path output = "output.txt";
  58. system("my_prog", std_out>output, std_in<input, std_err>log);
  59. \endcode
  60. \par Synchronous Pipe I/O
  61. Another way is to communicate through pipes.
  62. \code{.cpp}
  63. pstream str;
  64. child c("my_prog", std_out > str);
  65. int i;
  66. str >> i;
  67. \endcode
  68. Note that the pipe may also be used between several processes, like this:
  69. \code{.cpp}
  70. pipe p;
  71. child c1("nm", "a.out", std_out>p);
  72. child c2("c++filt", std_in<p);
  73. \endcode
  74. \par Asynchronous I/O
  75. Utilizing `boost.asio` asynchronous I/O is provided.
  76. \code
  77. boost::asio::io_context ios;
  78. std::future<std::string> output;
  79. system("ls", std_out > output, ios);
  80. auto res = fut.get();
  81. \endcode
  82. \note `boost/process/async.hpp` must also be included for this to work.
  83. \par Closing
  84. Stream can be closed, so nothing can be read or written.
  85. \code{.cpp}
  86. system("foo", std_in.close());
  87. \endcode
  88. \par Null
  89. Streams can be redirected to null, which means, that written date will be
  90. discarded and read data will only contain `EOF`.
  91. \code{.cpp}
  92. system("b2", std_out > null);
  93. \endcode
  94. *
  95. */
  96. namespace boost { namespace process { namespace detail {
  97. template<typename T> using is_streambuf = typename std::is_same<T, boost::asio::streambuf>::type;
  98. template<typename T> using is_const_buffer =
  99. std::integral_constant<bool,
  100. std::is_same< boost::asio::const_buffer, T>::value |
  101. std::is_base_of<boost::asio::const_buffer, T>::value
  102. >;
  103. template<typename T> using is_mutable_buffer =
  104. std::integral_constant<bool,
  105. std::is_same< boost::asio::mutable_buffer, T>::value |
  106. std::is_base_of<boost::asio::mutable_buffer, T>::value
  107. >;
  108. struct null_t {constexpr null_t() = default;};
  109. struct close_t;
  110. template<class>
  111. struct std_in_
  112. {
  113. constexpr std_in_() = default;
  114. api::close_in close() const {return api::close_in(); }
  115. api::close_in operator=(const close_t &) const {return api::close_in();}
  116. api::close_in operator<(const close_t &) const {return api::close_in();}
  117. api::null_in null() const {return api::null_in();}
  118. api::null_in operator=(const null_t &) const {return api::null_in();}
  119. api::null_in operator<(const null_t &) const {return api::null_in();}
  120. api::file_in operator=(const boost::process::filesystem::path &p) const {return p;}
  121. api::file_in operator=(const std::string & p) const {return p;}
  122. api::file_in operator=(const std::wstring &p) const {return p;}
  123. api::file_in operator=(const char * p) const {return p;}
  124. api::file_in operator=(const wchar_t * p) const {return p;}
  125. api::file_in operator<(const boost::process::filesystem::path &p) const {return p;}
  126. api::file_in operator<(const std::string &p) const {return p;}
  127. api::file_in operator<(const std::wstring &p) const {return p;}
  128. api::file_in operator<(const char*p) const {return p;}
  129. api::file_in operator<(const wchar_t * p) const {return p;}
  130. api::file_in operator=(FILE * f) const {return f;}
  131. api::file_in operator<(FILE * f) const {return f;}
  132. template<typename Char, typename Traits> api::pipe_in operator=(basic_pipe<Char, Traits> & p) const {return p;}
  133. template<typename Char, typename Traits> api::pipe_in operator<(basic_pipe<Char, Traits> & p) const {return p;}
  134. template<typename Char, typename Traits> api::pipe_in operator=(basic_opstream<Char, Traits> & p) const {return p.pipe();}
  135. template<typename Char, typename Traits> api::pipe_in operator<(basic_opstream<Char, Traits> & p) const {return p.pipe();}
  136. template<typename Char, typename Traits> api::pipe_in operator=(basic_pstream <Char, Traits> & p) const {return p.pipe();}
  137. template<typename Char, typename Traits> api::pipe_in operator<(basic_pstream <Char, Traits> & p) const {return p.pipe();}
  138. api::async_pipe_in operator=(async_pipe & p) const {return p;}
  139. api::async_pipe_in operator<(async_pipe & p) const {return p;}
  140. template<typename T, typename = typename std::enable_if<
  141. is_const_buffer<T>::value || is_mutable_buffer<T>::value
  142. >::type>
  143. api::async_in_buffer<const T> operator=(const T & buf) const {return buf;}
  144. template<typename T, typename = typename std::enable_if<is_streambuf<T>::value>::type >
  145. api::async_in_buffer<T> operator=(T & buf) const {return buf;}
  146. template<typename T, typename = typename std::enable_if<
  147. is_const_buffer<T>::value || is_mutable_buffer<T>::value
  148. >::type>
  149. api::async_in_buffer<const T> operator<(const T & buf) const {return buf;}
  150. template<typename T, typename = typename std::enable_if<is_streambuf<T>::value>::type >
  151. api::async_in_buffer<T> operator<(T & buf) const {return buf;}
  152. };
  153. //-1 == empty.
  154. //1 == stdout
  155. //2 == stderr
  156. template<int p1, int p2 = -1>
  157. struct std_out_
  158. {
  159. constexpr std_out_() = default;
  160. api::close_out<p1,p2> close() const {return api::close_out<p1,p2>(); }
  161. api::close_out<p1,p2> operator=(const close_t &) const {return api::close_out<p1,p2>();}
  162. api::close_out<p1,p2> operator>(const close_t &) const {return api::close_out<p1,p2>();}
  163. api::null_out<p1,p2> null() const {return api::null_out<p1,p2>();}
  164. api::null_out<p1,p2> operator=(const null_t &) const {return api::null_out<p1,p2>();}
  165. api::null_out<p1,p2> operator>(const null_t &) const {return api::null_out<p1,p2>();}
  166. api::file_out<p1,p2> operator=(const boost::process::filesystem::path &p) const {return api::file_out<p1,p2>(p);}
  167. api::file_out<p1,p2> operator=(const std::string &p) const {return api::file_out<p1,p2>(p);}
  168. api::file_out<p1,p2> operator=(const std::wstring &p) const {return api::file_out<p1,p2>(p);}
  169. api::file_out<p1,p2> operator=(const char * p) const {return api::file_out<p1,p2>(p);}
  170. api::file_out<p1,p2> operator=(const wchar_t * p) const {return api::file_out<p1,p2>(p);}
  171. api::file_out<p1,p2> operator>(const boost::process::filesystem::path &p) const {return api::file_out<p1,p2>(p);}
  172. api::file_out<p1,p2> operator>(const std::string &p) const {return api::file_out<p1,p2>(p);}
  173. api::file_out<p1,p2> operator>(const std::wstring &p) const {return api::file_out<p1,p2>(p);}
  174. api::file_out<p1,p2> operator>(const char * p) const {return api::file_out<p1,p2>(p);}
  175. api::file_out<p1,p2> operator>(const wchar_t * p) const {return api::file_out<p1,p2>(p);}
  176. api::file_out<p1,p2> operator=(FILE * f) const {return f;}
  177. api::file_out<p1,p2> operator>(FILE * f) const {return f;}
  178. template<typename Char, typename Traits> api::pipe_out<p1,p2> operator=(basic_pipe<Char, Traits> & p) const {return p;}
  179. template<typename Char, typename Traits> api::pipe_out<p1,p2> operator>(basic_pipe<Char, Traits> & p) const {return p;}
  180. template<typename Char, typename Traits> api::pipe_out<p1,p2> operator=(basic_ipstream<Char, Traits> & p) const {return p.pipe();}
  181. template<typename Char, typename Traits> api::pipe_out<p1,p2> operator>(basic_ipstream<Char, Traits> & p) const {return p.pipe();}
  182. template<typename Char, typename Traits> api::pipe_out<p1,p2> operator=(basic_pstream <Char, Traits> & p) const {return p.pipe();}
  183. template<typename Char, typename Traits> api::pipe_out<p1,p2> operator>(basic_pstream <Char, Traits> & p) const {return p.pipe();}
  184. api::async_pipe_out<p1, p2> operator=(async_pipe & p) const {return p;}
  185. api::async_pipe_out<p1, p2> operator>(async_pipe & p) const {return p;}
  186. api::async_out_buffer<p1, p2, const asio::mutable_buffer> operator=(const asio::mutable_buffer & buf) const {return buf;}
  187. api::async_out_buffer<p1, p2, const asio::mutable_buffers_1> operator=(const asio::mutable_buffers_1 & buf) const {return buf;}
  188. api::async_out_buffer<p1, p2, asio::streambuf> operator=(asio::streambuf & os) const {return os ;}
  189. api::async_out_buffer<p1, p2, const asio::mutable_buffer> operator>(const asio::mutable_buffer & buf) const {return buf;}
  190. api::async_out_buffer<p1, p2, const asio::mutable_buffers_1> operator>(const asio::mutable_buffers_1 & buf) const {return buf;}
  191. api::async_out_buffer<p1, p2, asio::streambuf> operator>(asio::streambuf & os) const {return os ;}
  192. api::async_out_future<p1,p2, std::string> operator=(std::future<std::string> & fut) const { return fut;}
  193. api::async_out_future<p1,p2, std::string> operator>(std::future<std::string> & fut) const { return fut;}
  194. api::async_out_future<p1,p2, std::vector<char>> operator=(std::future<std::vector<char>> & fut) const { return fut;}
  195. api::async_out_future<p1,p2, std::vector<char>> operator>(std::future<std::vector<char>> & fut) const { return fut;}
  196. template<int pin, typename = typename std::enable_if<
  197. (((p1 == 1) && (pin == 2)) ||
  198. ((p1 == 2) && (pin == 1)))
  199. && (p2 == -1)>::type>
  200. constexpr std_out_<1, 2> operator& (const std_out_<pin>&) const
  201. {
  202. return std_out_<1, 2> ();
  203. }
  204. };
  205. struct close_t
  206. {
  207. constexpr close_t() = default;
  208. template<int T, int U>
  209. api::close_out<T,U> operator()(std_out_<T,U>) {return api::close_out<T,U>();}
  210. };
  211. }
  212. ///This constant is a utility to allow syntax like `std_out > close` for closing I/O streams.
  213. constexpr boost::process::detail::close_t close;
  214. ///This constant is a utility to redirect streams to the null-device.
  215. constexpr boost::process::detail::null_t null;
  216. /**
  217. This property allows to set the input stream for the child process.
  218. \section stdin_details Details
  219. \subsection stdin_file File Input
  220. The file I/O simple redirects the stream to a file, for which the possible types are
  221. - `boost::process::filesystem::path`
  222. - `std::basic_string<char_type>`
  223. - `const char_type*`
  224. - `FILE*`
  225. with `char_type` being either `char` or `wchar_t`.
  226. FILE* is explicitly added, so the process can easily redirect the output stream
  227. of the child to another output stream of the process. That is:
  228. \code{.cpp}
  229. system("ls", std_in < stdin);
  230. \endcode
  231. \warning If the launching and the child process use the input, this leads to undefined behaviour.
  232. A syntax like `system("ls", std_out > std::cerr)` is not possible, due to the C++
  233. implementation not providing access to the handle.
  234. The valid expressions for this property are
  235. \code{.cpp}
  236. std_in < file;
  237. std_in = file;
  238. \endcode
  239. \subsection stdin_pipe Pipe Input
  240. As explained in the corresponding section, the boost.process library provides a
  241. @ref boost::process::async_pipe "async_pipe" class which can be
  242. used to communicate with child processes.
  243. \note Technically the @ref boost::process::async_pipe "async_pipe"
  244. works synchronous here, since no asio implementation is used by the library here.
  245. The async-operation will then however not end if the process is finished, since
  246. the pipe remains open. You can use the async_close function with on_exit to fix that.
  247. Valid expressions with pipes are these:
  248. \code{.cpp}
  249. std_in < pipe;
  250. std_in = pipe;
  251. \endcode
  252. Where the valid types for `pipe` are the following:
  253. - `basic_pipe`
  254. - `async_pipe`
  255. - `basic_opstream`
  256. - `basic_pstream`
  257. Note that the pipe may also be used between several processes, like this:
  258. \code{.cpp}
  259. pipe p;
  260. child c1("nm", "a.out", std_out>p);
  261. child c2("c++filt", std_in<p);
  262. \endcode
  263. \subsection stdin_async_pipe Asynchronous Pipe Input
  264. Asynchronous Pipe I/O classifies communication which has automatically handling
  265. of the asynchronous operations by the process library. This means, that a pipe will be
  266. constructed, the async_read/-write will be automatically started, and that the
  267. end of the child process will also close the pipe.
  268. Valid types for pipe I/O are the following:
  269. - `boost::asio::const_buffer` \xmlonly <footnote><para> Constructed with <code>boost::asio::buffer</code></para></footnote> \endxmlonly
  270. - `boost::asio::mutable_buffer` \xmlonly <footnote><para> Constructed with <code>boost::asio::buffer</code></para></footnote> \endxmlonly
  271. - `boost::asio::streambuf`
  272. Valid expressions with pipes are these:
  273. \code{.cpp}
  274. std_in < buffer;
  275. std_in = buffer;
  276. std_out > buffer;
  277. std_out = buffer;
  278. std_err > buffer;
  279. std_err = buffer;
  280. (std_out & std_err) > buffer;
  281. (std_out & std_err) = buffer;
  282. \endcode
  283. \note It is also possible to get a future for std_in, by chaining another `std::future<void>` onto it,
  284. so you can wait for the input to be completed. It looks like this:
  285. \code{.cpp}
  286. std::future<void> fut;
  287. boost::asio::io_context ios;
  288. std::string data;
  289. child c("prog", std_in < buffer(data) > fut, ios);
  290. fut.get();
  291. \endcode
  292. \note `boost::asio::buffer` is also available in the `boost::process` namespace.
  293. \warning This feature requires `boost/process/async.hpp` to be included and a reference to `boost::asio::io_context` to be passed to the launching function.
  294. \subsection stdin_close Close
  295. The input stream can be closed, so it cannot be read from. This will lead to an error when attempted.
  296. This can be achieved by the following syntax.
  297. \code{.cpp}
  298. std_in < close;
  299. std_in = close;
  300. std_in.close();
  301. \endcode
  302. \subsection stdin_null Null
  303. The input stream can be redirected to read from the null-device, which means that only `EOF` is read.
  304. The syntax to achieve that has the following variants:
  305. \code{.cpp}
  306. std_in < null;
  307. std_in = null;
  308. std_in.null();
  309. \endcode
  310. */
  311. constexpr boost::process::detail::std_in_<void> std_in;
  312. /**
  313. This property allows to set the output stream for the child process.
  314. \note The Semantic is the same as for \xmlonly <globalname alt="boost::process::std_err">std_err</globalname> \endxmlonly
  315. \note `std_err` and `std_out` can be combined into one stream, with the `operator &`, i.e. `std_out & std_err`.
  316. \section stdout_details Details
  317. \subsection stdout_file File Input
  318. The file I/O simple redirects the stream to a file, for which the possible types are
  319. - `boost::process::filesystem::path`
  320. - `std::basic_string<char_type>`
  321. - `const char_type*`
  322. - `FILE*`
  323. with `char_type` being either `char` or `wchar_t`.
  324. FILE* is explicitly added, so the process can easily redirect the output stream
  325. of the child to another output stream of the process. That is:
  326. \code{.cpp}
  327. system("ls", std_out < stdin);
  328. \endcode
  329. \warning If the launching and the child process use the input, this leads to undefined behaviour.
  330. A syntax like `system("ls", std_out > std::cerr)` is not possible, due to the C++
  331. implementation not providing access to the handle.
  332. The valid expressions for this property are
  333. \code{.cpp}
  334. std_out < file;
  335. std_out = file;
  336. \endcode
  337. \subsection stdout_pipe Pipe Output
  338. As explained in the corresponding section, the boost.process library provides a
  339. @ref boost::process::async_pipe "async_pipe" class which can be
  340. used to communicate with child processes.
  341. \note Technically the @ref boost::process::async_pipe "async_pipe"
  342. works like a synchronous pipe here, since no asio implementation is used by the library here.
  343. The asynchronous operation will then however not end if the process is finished, since
  344. the pipe remains open. You can use the async_close function with on_exit to fix that.
  345. Valid expressions with pipes are these:
  346. \code{.cpp}
  347. std_out > pipe;
  348. std_out = pipe;
  349. \endcode
  350. Where the valid types for `pipe` are the following:
  351. - `basic_pipe`
  352. - `async_pipe`
  353. - `basic_ipstream`
  354. - `basic_pstream`
  355. Note that the pipe may also be used between several processes, like this:
  356. \code{.cpp}
  357. pipe p;
  358. child c1("nm", "a.out", std_out>p);
  359. child c2("c++filt", std_in<p);
  360. \endcode
  361. \subsection stdout_async_pipe Asynchronous Pipe Output
  362. Asynchronous Pipe I/O classifies communication which has automatically handling
  363. of the async operations by the process library. This means, that a pipe will be
  364. constructed, the async_read/-write will be automatically started, and that the
  365. end of the child process will also close the pipe.
  366. Valid types for pipe I/O are the following:
  367. - `boost::asio::mutable_buffer` \xmlonly <footnote><para> Constructed with <code>boost::asio::buffer</code></para></footnote> \endxmlonly
  368. - `boost::asio::streambuf`
  369. - `std::future<std::vector<char>>`
  370. - `std::future<std::string>`
  371. Valid expressions with pipes are these:
  372. \code{.cpp}
  373. std_out > buffer;
  374. std_out = buffer;
  375. std_err > buffer;
  376. std_err = buffer;
  377. (std_out & std_err) > buffer;
  378. (std_out & std_err) = buffer;
  379. \endcode
  380. \note `boost::asio::buffer` is also available in the `boost::process` namespace.
  381. \warning This feature requires `boost/process/async.hpp` to be included and a reference to `boost::asio::io_context` to be passed to the launching function.
  382. \subsection stdout_close Close
  383. The out stream can be closed, so it cannot be write from.
  384. This will lead to an error when attempted.
  385. This can be achieved by the following syntax.
  386. \code{.cpp}
  387. std_out > close;
  388. std_out = close;
  389. std_out.close();
  390. \endcode
  391. \subsection stdout_null Null
  392. The output stream can be redirected to write to the null-device,
  393. which means that all output is discarded.
  394. The syntax to achieve that has the following variants:
  395. \code{.cpp}
  396. std_out > null;
  397. std_out = null;
  398. std_out.null();
  399. \endcode
  400. */
  401. constexpr boost::process::detail::std_out_<1> std_out;
  402. /**This property allows setting the `stderr` stream. The semantic and syntax is the same as for
  403. * \xmlonly <globalname alt="boost::process::std_out">std_out</globalname> \endxmlonly .
  404. */
  405. constexpr boost::process::detail::std_out_<2> std_err;
  406. }}
  407. #endif /* INCLUDE_BOOST_PROCESS_IO_HPP_ */