3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Angus Leeming
8 * \author Enrico Forestieri
9 * \author Peter Kuemmel
11 * Full author contact details are available in file CREDITS.
16 #include "support/debug.h"
17 #include "support/lstrings.h"
18 #include "support/qstring_helpers.h"
19 #include "support/Systemcall.h"
20 #include "support/SystemcallPrivate.h"
21 #include "support/os.h"
30 #include <QCoreApplication>
36 struct Sleep : QThread
38 static void millisec(unsigned long ms)
40 QThread::usleep(ms * 1000);
55 int Systemcall::startscript(Starttype how, string const & what)
57 string command = what;
59 if (how == DontWait) {
60 switch (os::shell()) {
65 command = "start /min " + command;
70 return ::system(command.c_str());
77 string const parsecmd(string const & cmd, string & outfile)
82 for (size_t i = 0; i < cmd.length(); ++i) {
84 if (c == '"' && !escaped)
86 else if (c == '\\' && !escaped)
88 else if (c == '>' && !(inquote || escaped)) {
89 outfile = trim(cmd.substr(i + 1), " \"");
90 return trim(cmd.substr(0, i));
102 int Systemcall::startscript(Starttype how, string const & what)
105 QString cmd = toqstr(parsecmd(what, outfile));
106 SystemcallPrivate d(outfile);
108 bool processEvents = false;
111 if (!d.waitWhile(SystemcallPrivate::Starting, processEvents, 3000)) {
112 LYXERR0("QProcess " << cmd << " did not start!");
113 LYXERR0("error " << d.errorMessage());
117 if (how == DontWait) {
118 QProcess* released = d.releaseProcess();
119 (void) released; // TODO who deletes it?
123 if (!d.waitWhile(SystemcallPrivate::Running, processEvents, 180000)) {
124 LYXERR0("QProcess " << cmd << " did not finished!");
125 LYXERR0("error " << d.errorMessage());
126 LYXERR0("status " << d.exitStatusMessage());
130 int const exit_code = d.exitCode();
132 LYXERR0("QProcess " << cmd << " finished!");
133 LYXERR0("error " << exit_code << ": " << d.errorMessage());
140 SystemcallPrivate::SystemcallPrivate(const std::string& of) :
141 proc_(new QProcess), outindex_(0),
142 errindex_(0), showout_(false), showerr_(false), outfile(of)
144 if (!outfile.empty()) {
145 // Check whether we have to simply throw away the output.
146 if (outfile != os::nulldev())
147 proc_->setStandardOutputFile(toqstr(outfile));
148 } else if (os::is_terminal(os::STDOUT))
150 if (os::is_terminal(os::STDERR))
153 connect(proc_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
154 connect(proc_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
155 connect(proc_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
156 connect(proc_, SIGNAL(started()), this, SLOT(processStarted()));
157 connect(proc_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
162 void SystemcallPrivate::startProcess(const QString& cmd)
165 state = SystemcallPrivate::Starting;
171 void SystemcallPrivate::waitAndProcessEvents()
173 Sleep::millisec(100);
174 QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
178 bool SystemcallPrivate::waitWhile(State waitwhile, bool processEvents, int timeout)
183 // Block GUI while waiting,
184 // relay on QProcess' wait functions
185 if (!processEvents) {
186 if (waitwhile == Starting)
187 return proc_->waitForStarted(timeout);
188 if (waitwhile == Running)
189 return proc_->waitForFinished(timeout);
193 // process events while waiting, no timeout
195 while (state == waitwhile && state != Error) {
196 waitAndProcessEvents();
198 return state != Error;
201 // process events while waiting whith timeout
204 while (state == waitwhile && state != Error && timer.elapsed() < timeout) {
205 waitAndProcessEvents();
207 return (state != Error) && (timer.elapsed() < timeout);
212 SystemcallPrivate::~SystemcallPrivate()
217 outdata_[outindex_] = '\0';
223 errdata_[errindex_] = '\0';
233 void SystemcallPrivate::flush()
236 // If the output has been redirected, we write it all at once.
237 // Even if we are not running in a terminal, the output could go
238 // to some log file, for example ~/.xsession-errors on *nix.
239 if (!os::is_terminal(os::STDOUT) && outfile.empty())
240 cout << fromqstr(QString::fromLocal8Bit(
241 proc_->readAllStandardOutput().data()));
242 if (!os::is_terminal(os::STDERR))
243 cerr << fromqstr(QString::fromLocal8Bit(
244 proc_->readAllStandardError().data()));
248 void SystemcallPrivate::stdOut()
250 if (proc_ && showout_) {
252 proc_->setReadChannel(QProcess::StandardOutput);
253 while (proc_->getChar(&c)) {
254 outdata_[outindex_++] = c;
255 if (c == '\n' || outindex_ + 1 == bufsize_) {
256 outdata_[outindex_] = '\0';
265 void SystemcallPrivate::stdErr()
267 if (proc_ && showerr_) {
269 proc_->setReadChannel(QProcess::StandardError);
270 while (proc_->getChar(&c)) {
271 errdata_[errindex_++] = c;
272 if (c == '\n' || errindex_ + 1 == bufsize_) {
273 errdata_[errindex_] = '\0';
282 void SystemcallPrivate::processError(QProcess::ProcessError err)
288 QString SystemcallPrivate::errorMessage() const
291 return "No QProcess available";
294 switch (proc_->error()) {
295 case QProcess::FailedToStart:
296 message = "The process failed to start. Either the invoked program is missing, "
297 "or you may have insufficient permissions to invoke the program.";
299 case QProcess::Crashed:
300 message = "The process crashed some time after starting successfully.";
302 case QProcess::Timedout:
303 message = "The process timed out. It might be restarted automatically.";
305 case QProcess::WriteError:
306 message = "An error occurred when attempting to write to the process-> For example, "
307 "the process may not be running, or it may have closed its input channel.";
309 case QProcess::ReadError:
310 message = "An error occurred when attempting to read from the process-> For example, "
311 "the process may not be running.";
313 case QProcess::UnknownError:
315 message = "An unknown error occured.";
322 void SystemcallPrivate::processStarted()
325 // why do we get two started signals?
326 //disconnect(proc_, SIGNAL(started()), this, SLOT(processStarted()));
330 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus status)
336 QString SystemcallPrivate::exitStatusMessage() const
339 return "No QProcess available";
342 switch (proc_->exitStatus()) {
343 case QProcess::NormalExit:
344 message = "The process exited normally.";
346 case QProcess::CrashExit:
347 message = "The process crashed.";
350 message = "Unknown exit state.";
356 int SystemcallPrivate::exitCode()
361 return proc_->exitCode();
365 QProcess* SystemcallPrivate::releaseProcess()
367 QProcess* released = proc_;
373 void SystemcallPrivate::killProcess()
378 void SystemcallPrivate::killProcess(QProcess * p)
382 p->closeReadChannel(QProcess::StandardOutput);
383 p->closeReadChannel(QProcess::StandardError);
391 #include "moc_SystemcallPrivate.cpp"
394 } // namespace support