basic_cmd.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_POSIX_BASIC_CMD_HPP_
  6. #define BOOST_PROCESS_DETAIL_POSIX_BASIC_CMD_HPP_
  7. #include <boost/process/detail/posix/handler.hpp>
  8. #include <boost/process/detail/posix/cmd.hpp>
  9. #include <boost/algorithm/string/replace.hpp>
  10. #include <boost/process/shell.hpp>
  11. #include <boost/algorithm/string/trim.hpp>
  12. #include <boost/algorithm/string/join.hpp>
  13. #include <string>
  14. #include <vector>
  15. namespace boost
  16. {
  17. namespace process
  18. {
  19. namespace detail
  20. {
  21. namespace posix
  22. {
  23. inline std::string build_cmd_shell(const std::string & exe, std::vector<std::string> && data)
  24. {
  25. std::string st = exe;
  26. for (auto & arg : data)
  27. {
  28. boost::replace_all(arg, "\"", "\\\"");
  29. auto it = std::find(arg.begin(), arg.end(), ' ');//contains space?
  30. if (it != arg.end())//ok, contains spaces.
  31. {
  32. //the first one is put directly onto the output,
  33. //because then I don't have to copy the whole string
  34. arg.insert(arg.begin(), '"' );
  35. arg += '"'; //that is the post one.
  36. }
  37. if (!st.empty())//first one does not need a preceding space
  38. st += ' ';
  39. st += arg;
  40. }
  41. return st ;
  42. }
  43. inline std::vector<std::string> build_args(const std::string & data)
  44. {
  45. std::vector<std::string> st;
  46. typedef std::string::const_iterator itr_t;
  47. //normal quotes outside can be stripped, inside ones marked as \" will be replaced.
  48. auto make_entry = [](const itr_t & begin, const itr_t & end)
  49. {
  50. std::string data;
  51. if ((*begin == '"') && (*(end-1) == '"'))
  52. data.assign(begin+1, end-1);
  53. else
  54. data.assign(begin, end);
  55. boost::replace_all(data, "\\\"", "\"");
  56. return data;
  57. };
  58. bool in_quote = false;
  59. auto part_beg = data.cbegin();
  60. auto itr = data.cbegin();
  61. for (; itr != data.cend(); itr++)
  62. {
  63. if (*itr == '"')
  64. in_quote ^= true;
  65. if (!in_quote && (*itr == ' '))
  66. {
  67. //alright, got a space
  68. if ((itr != data.cbegin()) && (*(itr -1) != ' ' ))
  69. st.push_back(make_entry(part_beg, itr));
  70. part_beg = itr+1;
  71. }
  72. }
  73. if (part_beg != itr)
  74. st.emplace_back(make_entry(part_beg, itr));
  75. return st;
  76. }
  77. template<typename Char>
  78. struct exe_cmd_init;
  79. template<>
  80. struct exe_cmd_init<char> : boost::process::detail::api::handler_base_ext
  81. {
  82. exe_cmd_init(const exe_cmd_init & ) = delete;
  83. exe_cmd_init(exe_cmd_init && ) = default;
  84. exe_cmd_init(std::string && exe, std::vector<std::string> && args)
  85. : exe(std::move(exe)), args(std::move(args)) {};
  86. template <class Executor>
  87. void on_setup(Executor& exec)
  88. {
  89. if (exe.empty()) //cmd style
  90. {
  91. if (args.empty())
  92. exec.exe = "";
  93. else
  94. exec.exe = args.front().c_str();
  95. exec.cmd_style = true;
  96. }
  97. else
  98. exec.exe = &exe.front();
  99. cmd_impl = make_cmd();
  100. exec.cmd_line = cmd_impl.data();
  101. }
  102. static exe_cmd_init exe_args(std::string && exe, std::vector<std::string> && args) {return exe_cmd_init(std::move(exe), std::move(args));}
  103. static exe_cmd_init cmd (std::string && cmd)
  104. {
  105. auto args = build_args(cmd);
  106. return exe_cmd_init({}, std::move(args));
  107. }
  108. static exe_cmd_init exe_args_shell(std::string&& exe, std::vector<std::string> && args)
  109. {
  110. auto cmd = build_cmd_shell(std::move(exe), std::move(args));
  111. std::vector<std::string> args_ = {"-c", std::move(cmd)};
  112. std::string sh = shell().string();
  113. return exe_cmd_init(std::move(sh), std::move(args_));
  114. }
  115. static exe_cmd_init cmd_shell(std::string&& cmd)
  116. {
  117. std::vector<std::string> args = {"-c", cmd};
  118. std::string sh = shell().string();
  119. return exe_cmd_init(
  120. std::move(sh),
  121. {std::move(args)});
  122. }
  123. private:
  124. inline std::vector<char*> make_cmd();
  125. std::string exe;
  126. std::vector<std::string> args;
  127. std::vector<char*> cmd_impl;
  128. };
  129. std::vector<char*> exe_cmd_init<char>::make_cmd()
  130. {
  131. // any string must be writable.
  132. static char empty_string[1] = "";
  133. std::vector<char*> vec;
  134. if (!exe.empty())
  135. vec.push_back(exe.empty() ? empty_string : &exe.front());
  136. if (!args.empty()) {
  137. for (auto & v : args)
  138. vec.push_back(v.empty() ? empty_string : &v.front());
  139. }
  140. vec.push_back(nullptr);
  141. return vec;
  142. }
  143. }}}}
  144. #endif