Commit a81c4bbd authored by Peter Hutterer's avatar Peter Hutterer
Browse files

process: add Start(program, vector<char*>)



Same thing as the va_list but takes a std::vector of arguments instead.
Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: default avatarChase Douglas <chase.douglas@canonical.com>
parent 9bf895eb
......@@ -90,6 +90,22 @@ class Process {
*/
Process();
/**
* Starts a program as a child process.
*
* See 'man execvp' for further information on the elements in
* the vector.
*
* @param program The program to start.
* @param args Vector of arguments passed to the program.
*
* @throws std::runtime_error on failure.
*
* @post If successful: Child process forked and program started.
* @post If successful: Subsequent calls to Pid() return child process pid.
*/
void Start(const std::string& program, const std::vector<std::string> &args);
/**
* Starts a program as a child process.
*
......
......@@ -48,7 +48,7 @@ xorg::testing::Process::Process() : d_(new Private) {
d_->pid = -1;
}
void xorg::testing::Process::Start(const std::string& program, va_list args) {
void xorg::testing::Process::Start(const std::string &program, const std::vector<std::string> &argv) {
if (d_->pid != -1)
throw std::runtime_error("Attempting to start an already started process");
......@@ -61,18 +61,31 @@ void xorg::testing::Process::Start(const std::string& program, va_list args) {
close(1);
close(2);
std::vector<char*> argv;
std::vector<char*> args;
std::vector<std::string>::const_iterator it;
do
argv.push_back(va_arg(args, char*));
while (argv.back());
for (it = argv.begin(); it != argv.end(); it++)
if (!it->empty())
args.push_back(strdup(it->c_str()));
args.push_back(NULL);
execvp(program.c_str(), &argv[0]);
execvp(program.c_str(), &args[0]);
throw std::runtime_error("Failed to start process");
}
}
void xorg::testing::Process::Start(const std::string& program, va_list args) {
std::vector<std::string> argv;
do {
std::string arg(va_arg(args, char*));
argv.push_back(arg);
} while (!argv.back().empty());
Start(program, argv);
}
void xorg::testing::Process::Start(const std::string& program, ...) {
va_list list;
va_start(list, program);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment