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, bool process_events)
105 QString cmd = toqstr(parsecmd(what, outfile));
106 SystemcallPrivate d(outfile);
110 if (!d.waitWhile(SystemcallPrivate::Starting, process_events, 3000)) {
111 LYXERR0("QProcess " << cmd << " did not start!");
112 LYXERR0("error " << d.errorMessage());
116 if (how == DontWait) {
117 QProcess* released = d.releaseProcess();
118 (void) released; // TODO who deletes it?
122 if (!d.waitWhile(SystemcallPrivate::Running, process_events, 180000)) {
123 LYXERR0("QProcess " << cmd << " did not finished!");
124 LYXERR0("error " << d.errorMessage());
125 LYXERR0("status " << d.exitStatusMessage());
129 int const exit_code = d.exitCode();
131 LYXERR0("QProcess " << cmd << " finished!");
132 LYXERR0("error " << exit_code << ": " << d.errorMessage());
139 SystemcallPrivate::SystemcallPrivate(const std::string& of) :
140 proc_(new QProcess), outindex_(0), errindex_(0),
141 outfile(of), showout_(false), showerr_(false), process_events(false)
143 if (!outfile.empty()) {
144 // Check whether we have to simply throw away the output.
145 if (outfile != os::nulldev())
146 proc_->setStandardOutputFile(toqstr(outfile));
147 } else if (os::is_terminal(os::STDOUT))
149 if (os::is_terminal(os::STDERR))
152 connect(proc_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
153 connect(proc_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
154 connect(proc_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
155 connect(proc_, SIGNAL(started()), this, SLOT(processStarted()));
156 connect(proc_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
161 void SystemcallPrivate::startProcess(const QString& cmd)
164 state = SystemcallPrivate::Starting;
170 void SystemcallPrivate::processEvents()
173 //static int count = 0; qDebug() << count++ << ": waitAndProcessEvents";
174 QCoreApplication::processEvents(QEventLoop::AllEvents);
179 void SystemcallPrivate::waitAndProcessEvents()
181 Sleep::millisec(100);
186 bool SystemcallPrivate::waitWhile(State waitwhile, bool proc_events, int timeout)
191 process_events = proc_events;
193 // Block GUI while waiting,
194 // relay on QProcess' wait functions
195 if (!process_events) {
196 if (waitwhile == Starting)
197 return proc_->waitForStarted(timeout);
198 if (waitwhile == Running)
199 return proc_->waitForFinished(timeout);
203 // process events while waiting, no timeout
205 while (state == waitwhile && state != Error) {
206 waitAndProcessEvents();
208 return state != Error;
211 // process events while waiting whith timeout
214 while (state == waitwhile && state != Error && timer.elapsed() < timeout) {
215 waitAndProcessEvents();
217 return (state != Error) && (timer.elapsed() < timeout);
221 SystemcallPrivate::~SystemcallPrivate()
226 outdata_[outindex_] = '\0';
232 errdata_[errindex_] = '\0';
242 void SystemcallPrivate::flush()
245 // If the output has been redirected, we write it all at once.
246 // Even if we are not running in a terminal, the output could go
247 // to some log file, for example ~/.xsession-errors on *nix.
248 if (!os::is_terminal(os::STDOUT) && outfile.empty())
249 cout << fromqstr(QString::fromLocal8Bit(
250 proc_->readAllStandardOutput().data()));
251 if (!os::is_terminal(os::STDERR))
252 cerr << fromqstr(QString::fromLocal8Bit(
253 proc_->readAllStandardError().data()));
258 void SystemcallPrivate::stdOut()
260 if (proc_ && showout_) {
262 proc_->setReadChannel(QProcess::StandardOutput);
263 while (proc_->getChar(&c)) {
264 outdata_[outindex_++] = c;
265 if (c == '\n' || outindex_ + 1 == bufsize_) {
266 outdata_[outindex_] = '\0';
276 void SystemcallPrivate::stdErr()
278 if (proc_ && showerr_) {
280 proc_->setReadChannel(QProcess::StandardError);
281 while (proc_->getChar(&c)) {
282 errdata_[errindex_++] = c;
283 if (c == '\n' || errindex_ + 1 == bufsize_) {
284 errdata_[errindex_] = '\0';
294 void SystemcallPrivate::processStarted()
297 // why do we get two started signals?
298 //disconnect(proc_, SIGNAL(started()), this, SLOT(processStarted()));
302 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
308 void SystemcallPrivate::processError(QProcess::ProcessError)
314 QString SystemcallPrivate::errorMessage() const
317 return "No QProcess available";
320 switch (proc_->error()) {
321 case QProcess::FailedToStart:
322 message = "The process failed to start. Either the invoked program is missing, "
323 "or you may have insufficient permissions to invoke the program.";
325 case QProcess::Crashed:
326 message = "The process crashed some time after starting successfully.";
328 case QProcess::Timedout:
329 message = "The process timed out. It might be restarted automatically.";
331 case QProcess::WriteError:
332 message = "An error occurred when attempting to write to the process-> For example, "
333 "the process may not be running, or it may have closed its input channel.";
335 case QProcess::ReadError:
336 message = "An error occurred when attempting to read from the process-> For example, "
337 "the process may not be running.";
339 case QProcess::UnknownError:
341 message = "An unknown error occured.";
348 QString SystemcallPrivate::exitStatusMessage() const
351 return "No QProcess available";
354 switch (proc_->exitStatus()) {
355 case QProcess::NormalExit:
356 message = "The process exited normally.";
358 case QProcess::CrashExit:
359 message = "The process crashed.";
362 message = "Unknown exit state.";
369 int SystemcallPrivate::exitCode()
374 return proc_->exitCode();
378 QProcess* SystemcallPrivate::releaseProcess()
380 QProcess* released = proc_;
386 void SystemcallPrivate::killProcess()
392 void SystemcallPrivate::killProcess(QProcess * p)
396 p->closeReadChannel(QProcess::StandardOutput);
397 p->closeReadChannel(QProcess::StandardError);
405 #include "moc_SystemcallPrivate.cpp"
408 } // namespace support