basic_cmd.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 += '"'; //thats the post one.
  36. }
  37. if (!st.empty())//first one does not need a preceeding 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. exec.exe = args.front().c_str();
  92. exec.cmd_style = true;
  93. }
  94. else
  95. exec.exe = &exe.front();
  96. cmd_impl = make_cmd();
  97. exec.cmd_line = cmd_impl.data();
  98. }
  99. static exe_cmd_init exe_args(std::string && exe, std::vector<std::string> && args) {return exe_cmd_init(std::move(exe), std::move(args));}
  100. static exe_cmd_init cmd (std::string && cmd)
  101. {
  102. auto args = build_args(cmd);
  103. return exe_cmd_init({}, std::move(args));
  104. }
  105. static exe_cmd_init exe_args_shell(std::string&& exe, std::vector<std::string> && args)
  106. {
  107. auto cmd = build_cmd_shell(std::move(exe), std::move(args));
  108. std::vector<std::string> args_ = {"-c", std::move(cmd)};
  109. std::string sh = shell().string();
  110. return exe_cmd_init(std::move(sh), std::move(args_));
  111. }
  112. static exe_cmd_init cmd_shell(std::string&& cmd)
  113. {
  114. std::vector<std::string> args = {"-c", "\"" + cmd + "\""};
  115. std::string sh = shell().string();
  116. return exe_cmd_init(
  117. std::move(sh),
  118. {std::move(args)});
  119. }
  120. private:
  121. inline std::vector<char*> make_cmd();
  122. std::string exe;
  123. std::vector<std::string> args;
  124. std::vector<char*> cmd_impl;
  125. };
  126. std::vector<char*> exe_cmd_init<char>::make_cmd()
  127. {
  128. std::vector<char*> vec;
  129. if (!exe.empty())
  130. vec.push_back(&exe.front());
  131. if (!args.empty()) {
  132. for (auto & v : args)
  133. vec.push_back(&v.front());
  134. }
  135. vec.push_back(nullptr);
  136. return vec;
  137. }
  138. }}}}
  139. #endif