summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@who-t.net>2012-06-28 10:09:47 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2012-07-12 08:10:44 +1000
commita81c4bbd964e13c597282f9e00da8106a5d62ba9 (patch)
tree1e3211f07282f4f12d54a778e920130acbee8f91
parent9bf895eb5979da2ab732765fae5d5deb965c5e0b (diff)
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 <peter.hutterer@who-t.net> Reviewed-by: Chase Douglas <chase.douglas@canonical.com>
-rw-r--r--include/xorg/gtest/xorg-gtest-process.h16
-rw-r--r--src/process.cpp25
2 files changed, 35 insertions, 6 deletions
diff --git a/include/xorg/gtest/xorg-gtest-process.h b/include/xorg/gtest/xorg-gtest-process.h
index 47aefcf..0b721ce 100644
--- a/include/xorg/gtest/xorg-gtest-process.h
+++ b/include/xorg/gtest/xorg-gtest-process.h
@@ -93,6 +93,22 @@ class 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.
+ *
* See 'man execvp' for further information on the variadic argument list.
*
* @param program The program to start.
diff --git a/src/process.cpp b/src/process.cpp
index e79b694..7b60afc 100644
--- a/src/process.cpp
+++ b/src/process.cpp
@@ -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);