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/lstrings.h"
18 #include "support/qstring_helpers.h"
19 #include "support/Systemcall.h"
20 #include "support/os.h"
34 static void killProcess(QProcess * p)
36 p->closeReadChannel(QProcess::StandardOutput);
37 p->closeReadChannel(QProcess::StandardError);
45 int Systemcall::startscript(Starttype how, string const & what)
47 string command = what;
49 if (how == DontWait) {
50 switch (os::shell()) {
55 command = "start /min " + command;
60 return ::system(command.c_str());
67 string const parsecmd(string const & cmd, string & outfile)
72 for (size_t i = 0; i < cmd.length(); ++i) {
74 if (c == '"' && !escaped)
76 else if (c == '\\' && !escaped)
78 else if (c == '>' && !(inquote || escaped)) {
79 outfile = trim(cmd.substr(i + 1), " \"");
80 return trim(cmd.substr(0, i));
92 int Systemcall::startscript(Starttype how, string const & what)
95 QString cmd = toqstr(parsecmd(what, outfile));
96 QProcess * process = new QProcess;
98 // Qt won't start the process if we redirect stdout/stderr in
99 // this way and they are not connected to a terminal (maybe
100 // because we were launched from some desktop GUI).
101 if (!outfile.empty())
102 process->setStandardOutputFile(toqstr(outfile));
103 else if (os::is_terminal(os::STDOUT))
104 process->setStandardOutputFile(toqstr(os::stdoutdev()));
105 if (os::is_terminal(os::STDERR))
106 process->setStandardErrorFile(toqstr(os::stderrdev()));
109 if (!process->waitForStarted(3000)) {
110 LYXERR0("Qprocess " << cmd << " did not start!");
111 LYXERR0("error " << process->error());
112 LYXERR0("state " << process->state());
113 LYXERR0("status " << process->exitStatus());
119 if (!process->waitForFinished(180000)) {
120 LYXERR0("Qprocess " << cmd << " did not finished!");
121 LYXERR0("error " << process->error());
122 LYXERR0("state " << process->state());
123 LYXERR0("status " << process->exitStatus());
126 int const exit_code = process->exitCode();
128 LYXERR0("Qprocess " << cmd << " finished!");
129 LYXERR0("exitCode " << process->exitCode());
130 LYXERR0("error " << process->error());
131 LYXERR0("state " << process->state());
132 LYXERR0("status " << process->exitStatus());
135 // If the output has been redirected, we write it all at once.
136 // Even if we are not running in a terminal, the output could go
137 // to some log file, for example ~/.xsession-errors on *nix.
138 if (!os::is_terminal(os::STDOUT) && outfile.empty())
139 cout << fromqstr(QString::fromLocal8Bit(
140 process->readAllStandardOutput().data()));
141 if (!os::is_terminal(os::STDERR))
142 cerr << fromqstr(QString::fromLocal8Bit(
143 process->readAllStandardError().data()));
145 killProcess(process);
150 } // namespace support