mirror of
https://github.com/arun11299/cpp-subprocess.git
synced 2025-08-05 04:46:21 -04:00
Major issue resolved execv args was not terminated with nullptr
This commit is contained in:
parent
f409d50c8d
commit
aab30d19bf
@ -542,6 +542,9 @@ public:
|
|||||||
vargs_ = util::split(cmd_args);
|
vargs_ = util::split(cmd_args);
|
||||||
init_args(std::forward<Args>(args)...);
|
init_args(std::forward<Args>(args)...);
|
||||||
|
|
||||||
|
// Setup the communication channels of the Popen class
|
||||||
|
stream_.setup_comm_channels();
|
||||||
|
|
||||||
if (!defer_process_start_) execute_process();
|
if (!defer_process_start_) execute_process();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -551,6 +554,9 @@ public:
|
|||||||
vargs_.insert(vargs_.end(), cmd_args.begin(), cmd_args.end());
|
vargs_.insert(vargs_.end(), cmd_args.begin(), cmd_args.end());
|
||||||
init_args(std::forward<Args>(args)...);
|
init_args(std::forward<Args>(args)...);
|
||||||
|
|
||||||
|
// Setup the communication channels of the Popen class
|
||||||
|
stream_.setup_comm_channels();
|
||||||
|
|
||||||
if (!defer_process_start_) execute_process();
|
if (!defer_process_start_) execute_process();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -640,8 +646,9 @@ void Popen::init_args(F&& farg, Args&&... args)
|
|||||||
|
|
||||||
void Popen::populate_c_argv()
|
void Popen::populate_c_argv()
|
||||||
{
|
{
|
||||||
cargv_.reserve(vargs_.size());
|
cargv_.reserve(vargs_.size() + 1);
|
||||||
for (auto& arg : vargs_) cargv_.push_back(&arg[0]);
|
for (auto& arg : vargs_) cargv_.push_back(&arg[0]);
|
||||||
|
cargv_.push_back(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Popen::start_process() throw (CalledProcessError, OSError)
|
void Popen::start_process() throw (CalledProcessError, OSError)
|
||||||
@ -767,8 +774,6 @@ void Popen::execute_process() throw (CalledProcessError, OSError)
|
|||||||
throw exp;
|
throw exp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the communication channels of the Popen class
|
|
||||||
stream_.setup_comm_channels();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1087,6 +1092,22 @@ namespace detail
|
|||||||
return Popen(std::forward<F>(farg), std::forward<Args>(args)...).wait();
|
return Popen(std::forward<F>(farg), std::forward<Args>(args)...).wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void pipeline_impl(std::vector<Popen>& cmds) { /* EMPTY IMPL */ }
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
void pipeline_impl(std::vector<Popen>& cmds,
|
||||||
|
const std::string& cmd,
|
||||||
|
Args&&... args)
|
||||||
|
{
|
||||||
|
if (cmds.size() == 0) {
|
||||||
|
cmds.emplace_back(cmd, output{PIPE}, defer_spawn{true});
|
||||||
|
} else {
|
||||||
|
cmds.emplace_back(cmd, input{cmds.back().output()}, output{PIPE}, defer_spawn{true});
|
||||||
|
}
|
||||||
|
|
||||||
|
pipeline_impl(cmds, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
@ -1115,4 +1136,15 @@ OutBuffer check_output(const std::string& arg, Args&&... args)
|
|||||||
|
|
||||||
// Piping Support
|
// Piping Support
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
// Args expected to be std::initializer_list<const char*>
|
||||||
|
OutBuffer pipeline(Args&&... args)
|
||||||
|
{
|
||||||
|
std::vector<Popen> pcmds;
|
||||||
|
detail::pipeline_impl(pcmds, std::forward<Args>(args)...);
|
||||||
|
|
||||||
|
for (auto& p : pcmds) p.start_process();
|
||||||
|
return (pcmds.back().communicate().first);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -21,8 +21,15 @@ void test_piping()
|
|||||||
std::cout << res.buf.data() << std::endl;
|
std::cout << res.buf.data() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_easy_piping()
|
||||||
|
{
|
||||||
|
auto res = pipeline("cat ../subprocess.hpp", "grep Args", "grep template");
|
||||||
|
std::cout << res.buf.data() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
test_input();
|
//test_input();
|
||||||
test_piping();
|
//test_piping();
|
||||||
|
test_easy_piping();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user