1 #ifndef MSP_CORE_PROCESS_H_
2 #define MSP_CORE_PROCESS_H_
6 #include <msp/fs/path.h>
7 #include <msp/io/base.h>
8 #include "noncopyable.h"
13 Provides an interface for running and controlling programs.
15 The creation of a new system-level process does not happen immediately when a
16 Process object is created, but only when exexute is called. If called on the
17 object obtained with self(), the process is replaced with a new program
18 immediately. In particular, destructors are not invoked.
20 Output redirections can be specified before calling execute. To feed input to
21 the process or capture its output, use an IO::Pipe. Redirections performed on
22 the self object take effect immediately. It is recommended to perform such
23 redirections directly on the Console objects.
25 class Process: private NonCopyable
28 typedef std::vector<std::string> Arguments;
43 static Process *_self;
45 Process(const Private &);
51 /** Returns an object referring to the current process. */
52 static Process &self();
54 static void platform_get_self_info(Private &);
57 /** Sets the working directory for the new process. By default, the working
58 directory is not changed. */
59 void set_working_directory(const FS::Path &);
61 /** Redirects console input from an I/O object. */
62 void redirect_cin(IO::Base &);
64 /** Redirects console output to an I/O object. */
65 void redirect_cout(IO::Base &);
67 /** Redirects error output to an I/O object. */
68 void redirect_cerr(IO::Base &);
70 void do_redirect(IO::Base *&, IO::Base &);
73 /** Executes a command, searching for it in the standard locations. */
74 void execute(const std::string &, const Arguments &);
76 /** Executes a command specified by a full path. */
77 void execute(const FS::Path &, const Arguments &);
80 void execute(const std::string &, bool, const Arguments &);
83 bool is_running() const { return running; }
84 bool has_finished() const { return finished; }
86 /** Checks the status of a running process. If block is true, waits for the
88 bool wait(bool block = true);
90 /** Returns the exit code for a finished process. */
91 unsigned get_exit_code() const;
93 /** Terminates the process. */
96 /** Brutally murders the process. It is not given a chance to terminate
100 /** Sends the process an interrupt signal, as if ^C was pressed. */