3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
8 * Interface cleaned up by
9 * \author Angus Leeming
11 * Full author contact details are available in file CREDITS.
16 #include "support/debug.h"
17 #include "support/qstring_helpers.h"
18 #include "support/Systemcall.h"
19 #include "support/os.h"
33 static void killProcess(QProcess * p)
35 p->closeReadChannel(QProcess::StandardOutput);
36 p->closeReadChannel(QProcess::StandardError);
43 int Systemcall::startscript(Starttype how, string const & what)
46 string command = what;
48 if (how == DontWait) {
49 switch (os::shell()) {
54 command = "start /min " + command;
59 return ::system(command.c_str());
61 QString cmd = QString::fromLocal8Bit(what.c_str());
62 QProcess * process = new QProcess;
64 // Qt won't start the process if we redirect stdout/stderr in
65 // this way and they are not connected to a terminal (maybe
66 // because we were launched from some desktop GUI).
67 if (os::is_terminal(os::STDOUT))
68 process->setStandardOutputFile(toqstr(os::stdoutdev()));
69 if (os::is_terminal(os::STDERR))
70 process->setStandardErrorFile(toqstr(os::stderrdev()));
73 if (!process->waitForStarted(3000)) {
74 LYXERR0("Qprocess " << cmd << " did not start!");
75 LYXERR0("error " << process->error());
76 LYXERR0("state " << process->state());
77 LYXERR0("status " << process->exitStatus());
83 if (!process->waitForFinished(180000)) {
84 LYXERR0("Qprocess " << cmd << " did not finished!");
85 LYXERR0("error " << process->error());
86 LYXERR0("state " << process->state());
87 LYXERR0("status " << process->exitStatus());
90 int const exit_code = process->exitCode();
92 LYXERR0("Qprocess " << cmd << " finished!");
93 LYXERR0("exitCode " << process->exitCode());
94 LYXERR0("error " << process->error());
95 LYXERR0("state " << process->state());
96 LYXERR0("status " << process->exitStatus());
99 // If the output has been redirected, we write it all at once.
100 // Even if we are not running in a terminal, the output could go
101 // to some log file, for example ~/.xsession-errors on *nix.
102 if (!os::is_terminal(os::STDOUT))
103 cout << fromqstr(QString::fromLocal8Bit(
104 process->readAllStandardOutput().data()));
105 if (!os::is_terminal(os::STDERR))
106 cerr << fromqstr(QString::fromLocal8Bit(
107 process->readAllStandardError().data()));
109 killProcess(process);
114 } // namespace support