4 #include <msp/fs/dir.h>
5 #include <msp/io/console.h>
6 #include <msp/io/file.h>
7 #include <msp/io/print.h>
8 #include <msp/time/units.h>
9 #include "externaltask.h"
14 ExternalTask::ExternalTask(const Arguments &a, const FS::Path &wd):
19 stdin_action(PASSTHROUGH),
20 stdout_action(PASSTHROUGH),
21 stderr_action(PASSTHROUGH),
25 throw invalid_argument("ExternalTask::ExternalTask");
28 ExternalTask::~ExternalTask()
33 string ExternalTask::get_command() const
36 for(vector<string>::const_iterator i=argv.begin(); i!=argv.end(); ++i)
41 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
43 if(*j=='"' || *j=='\'' || *j==' ' || *j=='\\' || *j=='&')
49 if(stdin_action==REDIRECT)
52 cmd += stdin_file.str();
55 if(stdout_action==REDIRECT)
58 cmd += stdout_file.str();
64 void ExternalTask::start()
66 IO::File *devnull = 0;
68 IO::File *outfile = 0;
72 process = new Process;
74 if(stdin_action==IGNORE || stdout_action==IGNORE || stderr_action==IGNORE)
76 devnull = new IO::File("/dev/null", IO::M_RDWR);
77 if(stdin_action==IGNORE)
78 process->redirect_cin(*devnull);
79 if(stdout_action==IGNORE)
80 process->redirect_cout(*devnull);
81 if(stderr_action==IGNORE)
82 process->redirect_cerr(*devnull);
85 if(stdout_action==REDIRECT)
87 outfile = new IO::File((work_dir/stdout_file).str(), IO::M_WRITE);
88 process->redirect_cout(*outfile);
91 if(stdout_action==CAPTURE || stderr_action==CAPTURE)
93 capture_pipe = new IO::Pipe;
94 if(stdout_action==CAPTURE)
95 process->redirect_cout(*capture_pipe);
96 if(stderr_action==CAPTURE)
97 process->redirect_cerr(*capture_pipe);
100 if(stdin_action==REDIRECT)
102 infile = new IO::File((work_dir/stdin_file).str());
103 process->redirect_cin(*infile);
106 if(!work_dir.empty())
107 process->set_working_directory(work_dir);
109 Process::Arguments args(argv.begin()+1, argv.end());
110 process->execute(argv.front(), args);
112 capture_pipe->set_mode(IO::M_READ);
119 Task::Status ExternalTask::check()
121 return do_wait(false);
124 Task::Status ExternalTask::wait()
126 return do_wait(true);
129 Task::Status ExternalTask::do_wait(bool block)
133 if(process->wait(block))
135 exit_code = process->get_exit_code();
140 // Do this after waiting to avoid a race condition
141 while(capture_pipe && IO::poll(*capture_pipe, IO::P_INPUT, Time::zero))
144 unsigned len = capture_pipe->read(buf, sizeof(buf));
146 output.append(buf, len);
154 signal_finished.emit(!exit_code);
157 return exit_code ? ERROR : SUCCESS;
160 void ExternalTask::set_stdin(const FS::Path &f)
162 stdin_action = REDIRECT;
166 void ExternalTask::set_stdout(StreamAction a)
169 throw invalid_argument("ExternalTask::set_stdout");
173 void ExternalTask::set_stdout(const FS::Path &f)
175 stdout_action = REDIRECT;
179 void ExternalTask::set_stderr(StreamAction a)
182 throw invalid_argument("ExternalTask::set_stdout");
186 string ExternalTask::run_and_capture_output(const Arguments &argv, const FS::Path &wd, bool capture_stderr)
188 ExternalTask task(argv, wd);
189 task.stdin_action = IGNORE;
190 task.set_stdout(CAPTURE);
191 task.set_stderr(capture_stderr ? CAPTURE : IGNORE);
193 if(task.wait()!=SUCCESS)
194 throw runtime_error(format("%s failed", argv.front()));
195 return task.get_output();