basic_cmd.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_DETAIL_BASIC_CMD_HPP_
  6. #define BOOST_PROCESS_DETAIL_BASIC_CMD_HPP_
  7. #include <boost/process/detail/config.hpp>
  8. #include <boost/process/detail/handler_base.hpp>
  9. #include <boost/process/detail/traits/cmd_or_exe.hpp>
  10. #include <boost/process/detail/traits/wchar_t.hpp>
  11. #if defined( BOOST_WINDOWS_API )
  12. #include <boost/process/detail/windows/basic_cmd.hpp>
  13. #include <boost/process/detail/windows/cmd.hpp>
  14. #elif defined( BOOST_POSIX_API )
  15. #include <boost/process/detail/posix/basic_cmd.hpp>
  16. #include <boost/process/detail/posix/cmd.hpp>
  17. #endif
  18. #include <boost/process/shell.hpp>
  19. #include <iterator>
  20. namespace boost { namespace process { namespace detail {
  21. template<typename Char>
  22. struct exe_setter_
  23. {
  24. typedef Char value_type;
  25. typedef std::basic_string<Char> string_type;
  26. string_type exe_;
  27. exe_setter_(string_type && str) : exe_(std::move(str)) {}
  28. exe_setter_(const string_type & str) : exe_(str) {}
  29. };
  30. template<> struct is_wchar_t<exe_setter_<wchar_t>> : std::true_type {};
  31. template<>
  32. struct char_converter<char, exe_setter_<wchar_t>>
  33. {
  34. static exe_setter_<char> conv(const exe_setter_<wchar_t> & in)
  35. {
  36. return {::boost::process::detail::convert(in.exe_)};
  37. }
  38. };
  39. template<>
  40. struct char_converter<wchar_t, exe_setter_<char>>
  41. {
  42. static exe_setter_<wchar_t> conv(const exe_setter_<char> & in)
  43. {
  44. return {::boost::process::detail::convert(in.exe_)};
  45. }
  46. };
  47. template <typename Char, bool Append >
  48. struct arg_setter_
  49. {
  50. using value_type = Char;
  51. using string_type = std::basic_string<value_type>;
  52. std::vector<string_type> _args;
  53. typedef typename std::vector<string_type>::iterator iterator;
  54. typedef typename std::vector<string_type>::const_iterator const_iterator;
  55. template<typename Iterator>
  56. arg_setter_(Iterator && begin, Iterator && end) : _args(begin, end) {}
  57. template<typename Range>
  58. arg_setter_(Range && str) :
  59. _args(std::begin(str),
  60. std::end(str)) {}
  61. iterator begin() {return _args.begin();}
  62. iterator end() {return _args.end();}
  63. const_iterator begin() const {return _args.begin();}
  64. const_iterator end() const {return _args.end();}
  65. arg_setter_(string_type & str) : _args{{str}} {}
  66. arg_setter_(string_type && s) : _args({std::move(s)}) {}
  67. arg_setter_(const string_type & s) : _args({s}) {}
  68. arg_setter_(const value_type* s) : _args({std::move(s)}) {}
  69. template<std::size_t Size>
  70. arg_setter_(const value_type (&s) [Size]) : _args({s}) {}
  71. };
  72. template<> struct is_wchar_t<arg_setter_<wchar_t, true >> : std::true_type {};
  73. template<> struct is_wchar_t<arg_setter_<wchar_t, false>> : std::true_type {};
  74. template<>
  75. struct char_converter<char, arg_setter_<wchar_t, true>>
  76. {
  77. static arg_setter_<char, true> conv(const arg_setter_<wchar_t, true> & in)
  78. {
  79. std::vector<std::string> vec(in._args.size());
  80. std::transform(in._args.begin(), in._args.end(), vec.begin(),
  81. [](const std::wstring & ws)
  82. {
  83. return ::boost::process::detail::convert(ws);
  84. });
  85. return {vec};
  86. }
  87. };
  88. template<>
  89. struct char_converter<wchar_t, arg_setter_<char, true>>
  90. {
  91. static arg_setter_<wchar_t, true> conv(const arg_setter_<char, true> & in)
  92. {
  93. std::vector<std::wstring> vec(in._args.size());
  94. std::transform(in._args.begin(), in._args.end(), vec.begin(),
  95. [](const std::string & ws)
  96. {
  97. return ::boost::process::detail::convert(ws);
  98. });
  99. return {vec};
  100. }
  101. };
  102. template<>
  103. struct char_converter<char, arg_setter_<wchar_t, false>>
  104. {
  105. static arg_setter_<char, false> conv(const arg_setter_<wchar_t, false> & in)
  106. {
  107. std::vector<std::string> vec(in._args.size());
  108. std::transform(in._args.begin(), in._args.end(), vec.begin(),
  109. [](const std::wstring & ws)
  110. {
  111. return ::boost::process::detail::convert(ws);
  112. });
  113. return {vec}; }
  114. };
  115. template<>
  116. struct char_converter<wchar_t, arg_setter_<char, false>>
  117. {
  118. static arg_setter_<wchar_t, false> conv(const arg_setter_<char, false> & in)
  119. {
  120. std::vector<std::wstring> vec(in._args.size());
  121. std::transform(in._args.begin(), in._args.end(), vec.begin(),
  122. [](const std::string & ws)
  123. {
  124. return ::boost::process::detail::convert(ws);
  125. });
  126. return {vec};
  127. }
  128. };
  129. using api::exe_cmd_init;
  130. template<typename Char>
  131. struct exe_builder
  132. {
  133. //set by path, because that will not be interpreted as a cmd
  134. bool not_cmd = false;
  135. bool shell = false;
  136. using string_type = std::basic_string<Char>;
  137. string_type exe;
  138. std::vector<string_type> args;
  139. void operator()(const boost::filesystem::path & data)
  140. {
  141. not_cmd = true;
  142. if (exe.empty())
  143. exe = data.native();
  144. else
  145. args.push_back(data.native());
  146. }
  147. void operator()(const string_type & data)
  148. {
  149. if (exe.empty())
  150. exe = data;
  151. else
  152. args.push_back(data);
  153. }
  154. void operator()(const Char* data)
  155. {
  156. if (exe.empty())
  157. exe = data;
  158. else
  159. args.push_back(data);
  160. }
  161. void operator()(shell_) {shell = true;}
  162. void operator()(std::vector<string_type> && data)
  163. {
  164. if (data.empty())
  165. return;
  166. auto itr = std::make_move_iterator(data.begin());
  167. auto end = std::make_move_iterator(data.end());
  168. if (exe.empty())
  169. {
  170. exe = *itr;
  171. itr++;
  172. }
  173. args.insert(args.end(), itr, end);
  174. }
  175. void operator()(const std::vector<string_type> & data)
  176. {
  177. if (data.empty())
  178. return;
  179. auto itr = data.begin();
  180. auto end = data.end();
  181. if (exe.empty())
  182. {
  183. exe = *itr;
  184. itr++;
  185. }
  186. args.insert(args.end(), itr, end);
  187. }
  188. void operator()(exe_setter_<Char> && data)
  189. {
  190. not_cmd = true;
  191. exe = std::move(data.exe_);
  192. }
  193. void operator()(const exe_setter_<Char> & data)
  194. {
  195. not_cmd = true;
  196. exe = data.exe_;
  197. }
  198. void operator()(arg_setter_<Char, false> && data)
  199. {
  200. args.assign(
  201. std::make_move_iterator(data._args.begin()),
  202. std::make_move_iterator(data._args.end()));
  203. }
  204. void operator()(arg_setter_<Char, true> && data)
  205. {
  206. args.insert(args.end(),
  207. std::make_move_iterator(data._args.begin()),
  208. std::make_move_iterator(data._args.end()));
  209. }
  210. void operator()(const arg_setter_<Char, false> & data)
  211. {
  212. args.assign(data._args.begin(), data._args.end());
  213. }
  214. void operator()(const arg_setter_<Char, true> & data)
  215. {
  216. args.insert(args.end(), data._args.begin(), data._args.end());
  217. }
  218. api::exe_cmd_init<Char> get_initializer()
  219. {
  220. if (not_cmd || !args.empty())
  221. {
  222. if (shell)
  223. return api::exe_cmd_init<Char>::exe_args_shell(std::move(exe), std::move(args));
  224. else
  225. return api::exe_cmd_init<Char>::exe_args(std::move(exe), std::move(args));
  226. }
  227. else
  228. if (shell)
  229. return api::exe_cmd_init<Char>::cmd_shell(std::move(exe));
  230. else
  231. return api::exe_cmd_init<Char>::cmd(std::move(exe));
  232. }
  233. typedef api::exe_cmd_init<Char> result_type;
  234. };
  235. template<>
  236. struct initializer_builder<cmd_or_exe_tag<char>>
  237. {
  238. typedef exe_builder<char> type;
  239. };
  240. template<>
  241. struct initializer_builder<cmd_or_exe_tag<wchar_t>>
  242. {
  243. typedef exe_builder<wchar_t> type;
  244. };
  245. }}}
  246. #endif /* BOOST_PROCESS_DETAIL_EXE_BUILDER_HPP_ */