Commit 43bdab62 authored by Peter Hutterer's avatar Peter Hutterer
Browse files

process: require NULL as last argument to Start()



And handle empty arguments and NULL_terminated arguments. This is a better
API than require the user to pass empty strings, especially since in some
cases an empty string may be a valid argument.
Signed-off-by: Peter Hutterer's avatarPeter Hutterer <peter.hutterer@who-t.net>
Reviewed-by: default avatarChase Douglas <chase.douglas@canonical.com>
parent 98bacb5f
......@@ -30,6 +30,7 @@
#define XORG_GTEST_PROCESS_H
#include <stdarg.h>
#include <X11/Xfuncproto.h> /* for _X_SENTINEL */
#include <memory>
#include <string>
......@@ -114,7 +115,7 @@ class Process {
*
* @param program The program to start.
* @param args Variadic list of arguments passed to the program. This list
* must end in a zero-length string ("", not NULL).
* must end with NULL.
*
* @throws std::runtime_error on failure.
*
......@@ -127,7 +128,7 @@ class Process {
* Starts a program as a child process.
*
* Takes a variadic list of arguments passed to the program. This list
* must end in a zero-length string ("", not NULL).
* must end with NULL.
* See 'man execvp' for further information on the variadic argument list.
*
* @param program The program to start.
......@@ -137,7 +138,7 @@ class Process {
* @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, ...);
void Start(const std::string& program, ...) _X_SENTINEL(0);
/**
* Terminates (SIGTERM) this child process and waits a given timeout for
......
......@@ -69,8 +69,7 @@ void xorg::testing::Process::Start(const std::string &program, const std::vector
args.push_back(strdup(program.c_str()));
for (it = argv.begin(); it != argv.end(); it++)
if (!it->empty())
args.push_back(strdup(it->c_str()));
args.push_back(strdup(it->c_str()));
args.push_back(NULL);
execvp(program.c_str(), &args[0]);
......@@ -82,10 +81,14 @@ void xorg::testing::Process::Start(const std::string &program, const std::vector
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());
if (args) {
char *arg;
do {
arg = va_arg(args, char*);
if (arg)
argv.push_back(std::string(arg));
} while (arg);
}
Start(program, argv);
}
......
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