2 #include <msp/fs/dir.h>
3 #include <msp/io/console.h>
4 #include <msp/io/file.h>
5 #include <msp/io/print.h>
6 #include <msp/time/timedelta.h>
7 #include "externaltask.h"
12 ExternalTask::ExternalTask(const Arguments &a, const FS::Path &wd):
17 stdin_action(PASSTHROUGH),
18 stdout_action(PASSTHROUGH),
19 stderr_action(PASSTHROUGH),
23 throw invalid_argument("ExternalTask::ExternalTask");
26 ExternalTask::~ExternalTask()
31 string ExternalTask::get_command() const
34 for(vector<string>::const_iterator i=argv.begin(); i!=argv.end(); ++i)
39 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
41 if(*j=='"' || *j=='\'' || *j==' ' || *j=='\\' || *j=='&')
47 if(stdin_action==REDIRECT)
50 cmd += stdin_file.str();
53 if(stdout_action==REDIRECT)
56 cmd += stdout_file.str();
62 void ExternalTask::start()
64 IO::File *devnull = 0;
66 IO::File *outfile = 0;
70 process = new Process;
72 if(stdin_action==IGNORE || stdout_action==IGNORE || stderr_action==IGNORE)
75 devnull = new IO::File("nul", IO::M_RDWR);
77 devnull = new IO::File("/dev/null", IO::M_RDWR);
79 if(stdin_action==IGNORE)
80 process->redirect_cin(*devnull);
81 if(stdout_action==IGNORE)
82 process->redirect_cout(*devnull);
83 if(stderr_action==IGNORE)
84 process->redirect_cerr(*devnull);
87 if(stdout_action==REDIRECT)
89 outfile = new IO::File((work_dir/stdout_file).str(), IO::M_WRITE);
90 process->redirect_cout(*outfile);
93 if(stdout_action==CAPTURE || stderr_action==CAPTURE)
95 capture_pipe = new IO::Pipe;
96 if(stdout_action==CAPTURE)
97 process->redirect_cout(*capture_pipe);
98 if(stderr_action==CAPTURE)
99 process->redirect_cerr(*capture_pipe);
102 if(stdin_action==REDIRECT)
104 infile = new IO::File((work_dir/stdin_file).str());
105 process->redirect_cin(*infile);
108 if(!work_dir.empty())
109 process->set_working_directory(work_dir);
111 Process::Arguments args(argv.begin()+1, argv.end());
112 process->execute(argv.front(), args);
114 capture_pipe->set_mode(IO::M_READ);
121 Task::Status ExternalTask::check()
123 return do_wait(false);
126 Task::Status ExternalTask::wait()
128 return do_wait(true);
131 Task::Status ExternalTask::do_wait(bool block)
135 if(process->wait(block && !capture_pipe))
137 exit_code = process->get_exit_code();
142 // Do this after waiting to avoid a race condition
143 while(capture_pipe && IO::poll(*capture_pipe, IO::P_INPUT, 10*Time::msec))
146 unsigned len = capture_pipe->read(buf, sizeof(buf));
148 output.append(buf, len);
159 signal_finished.emit(!exit_code);
162 return exit_code ? ERROR : SUCCESS;
165 void ExternalTask::set_stdin(const FS::Path &f)
167 stdin_action = REDIRECT;
171 void ExternalTask::set_stdout(StreamAction a)
174 throw invalid_argument("ExternalTask::set_stdout");
178 void ExternalTask::set_stdout(const FS::Path &f)
180 stdout_action = REDIRECT;
184 void ExternalTask::set_stderr(StreamAction a)
187 throw invalid_argument("ExternalTask::set_stdout");
191 string ExternalTask::run_and_capture_output(const Arguments &argv, const FS::Path &wd, bool capture_stderr)
193 ExternalTask task(argv, wd);
194 task.stdin_action = IGNORE;
195 task.set_stdout(CAPTURE);
196 task.set_stderr(capture_stderr ? CAPTURE : IGNORE);
198 if(task.wait()!=SUCCESS)
199 throw runtime_error(format("%s failed", argv.front()));
200 return task.get_output();