Some C++ subprocess handling classes.
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <cpp-subprocess/locatebinary.h>
#include <cpp-subprocess/operating_system.h>
#include <cstdlib>
namespace ps = polysquare::subprocess;
char *paths = ::getenv ("PATH");
std::vector <std::string> paths;
boost::split (paths, boost::is_any_of (";"), boost::token_compress_on);
std::string binary;
try
{
binary = ps::locateBinary ("echo", paths, ps::MakeOperatingSystem ());
}
catch (std::runtime_error const &error)
{
std::cerr << error.what ();
}
#include <cpp-subprocess/launch.h>
#include <cpp-subprocess/operating_system.h>
namespace ps = polysquare::subprocess;
int stderr[2], stdout[2];
pipe (stderr);
pipe (stdout);
pid_t pid = 0;
try
{
char const **argv = {
"hello",
nullptr
};
pid = ps::launchBinary ("/usr/bin/echo"
argv,
environ,
stderr[1],
stdout[1],
ps::MakeOperatingSystem ());
}
catch (std::exception const &error)
{
std::cerr << error.what ();
};
stderr[0]
and stdout[0]
can be used with read
to read the output of the subprocess.
#include <cpp-subprocess/launch.h>
try
{
ps::waitForProcessEnd (pid, ps::MakeOperatingSystem ());
}
catch (std::runtime_error const &error)
{
std::cerr << error.what ();
}
#include <cpp-subprocess/launch.h>
#include <cpp-subprocess/operating_system.h>
namespace ps = polysquare::subprocess;
int stderr[2], stdout[2];
pipe (stderr);
pipe (stdout);
pid_t pid = 0;
try
{
char const **argv = {
"hello",
nullptr
};
pid = ps::launchBinaryAndWaitForReturn ("/usr/bin/echo"
argv,
environ,
stderr[1],
stdout[1],
ps::MakeOperatingSystem ());
}
catch (std::exception const &error)
{
std::cerr << error.what ();
};
#include <cpp-subprocess/readfd.h>
try
{
auto os = ps::MakeOperatingSystem ()
std::vector <string> lines = ps::ReadFDToLines (fd, os);
}