]> git.tdb.fi Git - libs/core.git/blob - source/core/process.h
Add a Process class for running and interfacing with other programs
[libs/core.git] / source / core / process.h
1 #ifndef MSP_CORE_PROCESS_H_
2 #define MSP_CORE_PROCESS_H_
3
4 #include <string>
5 #include <vector>
6 #include <msp/fs/path.h>
7 #include <msp/io/base.h>
8
9 namespace Msp {
10
11 /**
12 Provides an interface for running and controlling programs.
13
14 The creation of a new system-level process does not happen immediately when a
15 Process object is created, but only when exexute is called.  If called on the
16 object obtained with self(), the process is replaced with a new program
17 immediately.  In particular, destructors are not invoked.
18
19 Output redirections can be specified before calling execute.  To feed input to
20 the process or capture its output, use an IO::Pipe.  Redirections performed on
21 the self object take effect immediately.  It is recommended to perform such
22 redirections directly on the Console objects.
23 */
24 class Process
25 {
26 public:
27         typedef std::vector<std::string> Arguments;
28
29 private:
30         struct Private;
31
32         Private *priv;
33         FS::Path work_dir;
34         bool redirect;
35         IO::Base *cin;
36         IO::Base *cout;
37         IO::Base *cerr;
38         bool running;
39         bool finished;
40         unsigned exit_code;
41
42         static Process *_self;
43
44         Process(const Private &);
45         void init();
46 public:
47         Process();
48         ~Process();
49
50         /** Returns an object referring to the current process. */
51         static Process &self();
52 private:
53         static void platform_get_self_info(Private &);
54
55 public:
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 &);
59
60         /** Redirects console input from an I/O object. */
61         void redirect_cin(IO::Base &);
62
63         /** Redirects console output to an I/O object. */
64         void redirect_cout(IO::Base &);
65
66         /** Redirects error output to an I/O object. */
67         void redirect_cerr(IO::Base &);
68 private:
69         void do_redirect(IO::Base *&, IO::Base &);
70
71 public:
72         /** Executes a command, searching for it in the standard locations. */
73         void execute(const std::string &, const Arguments &);
74
75         /** Executes a command specified by a full path. */
76         void execute(const FS::Path &, const Arguments &);
77
78 private:
79         void execute(const std::string &, bool, const Arguments &);
80
81 public:
82         bool is_running() const { return running; }
83         bool has_finished() const { return finished; }
84
85         /** Checks the status of a running process.  If block is true, waits for the
86         process to finish. */
87         bool wait(bool block = true);
88
89         /** Returns the exit code for a finished process. */
90         unsigned get_exit_code() const;
91
92         /** Terminates the process. */
93         void terminate();
94
95         /** Brutally murders the process.  It is not given a chance to terminate
96         gracefully. */
97         void kill();
98
99         /** Sends the process an interrupt signal, as if ^C was pressed. */
100         void interrupt();
101 };
102
103 } // namespace Msp
104
105 #endif