]> git.tdb.fi Git - builder.git/blob - source/externaltask.h
Support redirecting ExternalTask's stdin/stdout from/to a file
[builder.git] / source / externaltask.h
1 #ifndef EXTERNALTASK_H_
2 #define EXTERNALTASK_H_
3
4 #include <string>
5 #include <vector>
6 #include <msp/core/process.h>
7 #include <msp/fs/path.h>
8 #include <msp/io/pipe.h>
9 #include "task.h"
10
11 /**
12 Runs an external command.  A zero exit status is translated to a SUCCESS status
13 for the task, and anything else is treated as an error.  Output can optionally
14 be captured.
15 */
16 class ExternalTask: public Task
17 {
18 public:
19         enum StreamAction
20         {
21                 PASSTHROUGH,  //< Do not touch the stream
22                 CAPTURE,      //< Capture the stream
23                 REDIRECT,     //< Redirect the stream to/from a file
24                 IGNORE        //< Redirect the stream to oblivion
25         };
26
27         typedef Msp::Process::Arguments Arguments;
28
29 private:
30         Arguments argv;
31         Msp::FS::Path work_dir;
32         Msp::Process *process;
33         int exit_code;
34         Msp::FS::Path stdin_file;
35         StreamAction stdout_action;
36         Msp::FS::Path stdout_file;
37         StreamAction stderr_action;
38         Msp::IO::Pipe *capture_pipe;
39         std::string output;
40
41 public:
42         /** Creates an ExternalTask with an argument array and an optional working
43         directory.  The first element of the argument array should be the command
44         name.  If the working directory is not specified, no chdir is done. */
45         ExternalTask(const Arguments &, const Msp::FS::Path & = Msp::FS::Path());
46
47         virtual ~ExternalTask();
48
49         virtual std::string get_command() const;
50         virtual void start();
51         virtual Status check();
52         virtual Status wait();
53 private:
54         Status do_wait(bool);
55
56 public:
57         /// Redirect stdin from a file.  Has no effect after the task is started.
58         void set_stdin(const Msp::FS::Path &);
59
60         /// Sets destination for stdout.  Has no effect after the task is started.
61         void set_stdout(StreamAction);
62
63         /// Redirect stdout to a file.  Has no effect after the task is started.
64         void set_stdout(const Msp::FS::Path &);
65
66         /// Sets destination for stderr.  Has no effect after the task is started.
67         void set_stderr(StreamAction);
68
69         /** Returns captured output, if any.  This may be called while the task is
70         still running, but it will always return all output. */
71         const std::string &get_output() const { return output; }
72
73         /** Executes a command and captures its output.  Stderr is ignored, but if
74         the command exits with a nonzero status, an exception is thrown. */
75         static std::string run_and_capture_output(const Arguments &, const Msp::FS::Path & = Msp::FS::Path());
76 };
77
78 #endif