]> git.tdb.fi Git - libs/core.git/blob - source/core/process.h
Use vectors for storage in Poller
[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 #include "noncopyable.h"
9
10 namespace Msp {
11
12 /**
13 Provides an interface for running and controlling programs.
14
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.
19
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.
24 */
25 class Process: private NonCopyable
26 {
27 public:
28         typedef std::vector<std::string> Arguments;
29
30 private:
31         struct Private;
32
33         Private *priv;
34         FS::Path work_dir;
35         bool redirect;
36         IO::Base *cin;
37         IO::Base *cout;
38         IO::Base *cerr;
39         bool running;
40         bool finished;
41         unsigned exit_code;
42
43         static Process *_self;
44
45         Process(const Private &);
46         void init();
47 public:
48         Process();
49         ~Process();
50
51         /** Returns an object referring to the current process. */
52         static Process &self();
53 private:
54         static void platform_get_self_info(Private &);
55
56 public:
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 &);
60
61         /** Redirects console input from an I/O object. */
62         void redirect_cin(IO::Base &);
63
64         /** Redirects console output to an I/O object. */
65         void redirect_cout(IO::Base &);
66
67         /** Redirects error output to an I/O object. */
68         void redirect_cerr(IO::Base &);
69 private:
70         void do_redirect(IO::Base *&, IO::Base &);
71
72 public:
73         /** Executes a command, searching for it in the standard locations. */
74         void execute(const std::string &, const Arguments &);
75
76         /** Executes a command specified by a full path. */
77         void execute(const FS::Path &, const Arguments &);
78
79 private:
80         void execute(const std::string &, bool, const Arguments &);
81
82 public:
83         bool is_running() const { return running; }
84         bool has_finished() const { return finished; }
85
86         /** Checks the status of a running process.  If block is true, waits for the
87         process to finish. */
88         bool wait(bool block = true);
89
90         /** Returns the exit code for a finished process. */
91         unsigned get_exit_code() const;
92
93         /** Terminates the process. */
94         void terminate();
95
96         /** Brutally murders the process.  It is not given a chance to terminate
97         gracefully. */
98         void kill();
99
100         /** Sends the process an interrupt signal, as if ^C was pressed. */
101         void interrupt();
102 };
103
104 } // namespace Msp
105
106 #endif