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;
35 bool redirect = false;
40 bool finished = false;
41 unsigned exit_code = 0;
43 static Process *_self;
45 Process(const Private &);
50 /** Returns an object referring to the current process. */
51 static Process &self();
53 static void platform_get_self_info(Private &);
56 /** Sets the working directory for the new process. By default, the working
57 directory is not changed. */
58 void set_working_directory(const FS::Path &);
60 /** Redirects console input from an I/O object. */
61 void redirect_cin(IO::Base &);
63 /** Redirects console output to an I/O object. */
64 void redirect_cout(IO::Base &);
66 /** Redirects error output to an I/O object. */
67 void redirect_cerr(IO::Base &);
69 void do_redirect(IO::Base *&, IO::Base &);
72 /** Executes a command, searching for it in the standard locations. */
73 void execute(const std::string &, const Arguments &);
75 /** Executes a command specified by a full path. */
76 void execute(const FS::Path &, const Arguments &);
79 void execute(const std::string &, bool, const Arguments &);
82 bool is_running() const { return running; }
83 bool has_finished() const { return finished; }
85 /** Checks the status of a running process. If block is true, waits for the
87 bool wait(bool block = true);
89 /** Returns the exit code for a finished process. */
90 unsigned get_exit_code() const;
92 /** Terminates the process. */
95 /** Brutally murders the process. It is not given a chance to terminate
99 /** Sends the process an interrupt signal, as if ^C was pressed. */