diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2014-12-05 13:26:29 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2014-12-05 13:26:29 +0200 |
commit | 985e8f5f28da87be779b80942577f088321024af (patch) | |
tree | 811473dc2c89d755e46cfa28711c3ce8569a1556 /build/process | |
parent | 0ba8af59dbc3e7419a9ef24c6d4d466d6d64862c (diff) |
Add support for starting processes, getting file timestamps
g++-4.9 -std=c++11 -I.. -o bd bd.cxx process.cxx timestamp.cxx
Diffstat (limited to 'build/process')
-rw-r--r-- | build/process | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/build/process b/build/process new file mode 100644 index 0000000..0b45846 --- /dev/null +++ b/build/process @@ -0,0 +1,67 @@ +// file : build/process -*- C++ -*- +// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef BUILD_PROCESS +#define BUILD_PROCESS + +#ifndef _WIN32 +# include <sys/types.h> // pid_t +#endif + +#include <cassert> +#include <system_error> + +namespace build +{ + + struct process_error: std::system_error + { + process_error (int e, bool child) + : system_error (e, std::system_category ()), child_ (child) {} + + bool + child () const {return child_;} + + private: + bool child_; + }; + + struct process + { + // Start another process using the specified command line. Connect the + // newly created process' stdin to out_fd. Also if connect_* are true, + // connect the created process' stdout and stderr to in_*fd. Throw + // process_error if anything goes wrong. + // + // Note that some of the exceptions (e.g., if exec() failed) can be + // thrown in the child version of us. + // + process (char const* args[], + bool connect_stdin = false, + bool connect_stderr = false, + bool connect_stdout = false); + + // Wait for the process to terminate. Return true if the process + // terminated normally and with the zero exit status. Throw + // process_error if anything goes wrong. + // + bool + wait (); + + ~process () {assert (id == 0);} + +#ifndef _WIN32 + typedef pid_t id_type; +#else + typedef void* id_type; // Win32 HANDLE. +#endif + + id_type id; + int out_fd; // Write to this fd to send to the new process' stdin. + int in_efd; // Read from this fd to receive from the new process' stderr. + int in_ofd; // Read from this fd to receive from the new process' stdout. + }; +} + +#endif // BUILD_PROCESS |