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 stdout_action(PASSTHROUGH),
20 stderr_action(PASSTHROUGH),
24 throw invalid_argument("ExternalTask::ExternalTask");
27 ExternalTask::~ExternalTask()
32 string ExternalTask::get_command() const
35 for(vector<string>::const_iterator i=argv.begin(); i!=argv.end(); ++i)
40 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
42 if(*j=='"' || *j=='\'' || *j==' ' || *j=='\\' || *j=='&')
48 if(!stdin_file.empty())
51 cmd += stdin_file.str();
54 if(stdout_action==REDIRECT)
57 cmd += stdout_file.str();
63 void ExternalTask::start()
65 IO::File *devnull = 0;
67 IO::File *outfile = 0;
71 process = new Process;
73 if(stdout_action==IGNORE || stderr_action==IGNORE)
75 devnull = new IO::File("/dev/null", IO::M_WRITE);
76 if(stdout_action==IGNORE)
77 process->redirect_cout(*devnull);
78 if(stderr_action==IGNORE)
79 process->redirect_cerr(*devnull);
82 if(stdout_action==REDIRECT)
84 outfile = new IO::File((work_dir/stdout_file).str(), IO::M_WRITE);
85 process->redirect_cout(*outfile);
88 if(stdout_action==CAPTURE || stderr_action==CAPTURE)
90 capture_pipe = new IO::Pipe;
91 if(stdout_action==CAPTURE)
92 process->redirect_cout(*capture_pipe);
93 if(stderr_action==CAPTURE)
94 process->redirect_cerr(*capture_pipe);
97 if(!stdin_file.empty())
99 infile = new IO::File((work_dir/stdin_file).str());
100 process->redirect_cin(*infile);
103 if(!work_dir.empty())
104 process->set_working_directory(work_dir);
106 Process::Arguments args(argv.begin()+1, argv.end());
107 process->execute(argv.front(), args);
109 capture_pipe->set_mode(IO::M_READ);
116 Task::Status ExternalTask::check()
118 return do_wait(false);
121 Task::Status ExternalTask::wait()
123 return do_wait(true);
126 Task::Status ExternalTask::do_wait(bool block)
130 if(process->wait(block))
132 exit_code = process->get_exit_code();
137 // Do this after waiting to avoid a race condition
138 while(capture_pipe && IO::poll(*capture_pipe, IO::P_INPUT, Time::zero))
141 unsigned len = capture_pipe->read(buf, sizeof(buf));
143 output.append(buf, len);
151 signal_finished.emit(!exit_code);
154 return exit_code ? ERROR : SUCCESS;
157 void ExternalTask::set_stdin(const FS::Path &f)
162 void ExternalTask::set_stdout(StreamAction a)
165 throw invalid_argument("ExternalTask::set_stdout");
169 void ExternalTask::set_stdout(const FS::Path &f)
171 stdout_action = REDIRECT;
175 void ExternalTask::set_stderr(StreamAction a)
178 throw invalid_argument("ExternalTask::set_stdout");
182 string ExternalTask::run_and_capture_output(const Arguments &argv, const FS::Path &wd)
184 ExternalTask task(argv, wd);
185 task.set_stdout(CAPTURE);
186 task.set_stderr(IGNORE);
188 if(task.wait()!=SUCCESS)
189 throw runtime_error(format("%s failed", argv.front()));
190 return task.get_output();