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 // Check whether we have to simply throw away the output.
103 if (outfile != os::nulldev())
104 process->setStandardOutputFile(toqstr(outfile));
105 } else if (os::is_terminal(os::STDOUT))
106 process->setStandardOutputFile(toqstr(os::stdoutdev()));
107 if (os::is_terminal(os::STDERR))
108 process->setStandardErrorFile(toqstr(os::stderrdev()));
111 if (!process->waitForStarted(3000)) {
112 LYXERR0("Qprocess " << cmd << " did not start!");
113 LYXERR0("error " << process->error());
114 LYXERR0("state " << process->state());
115 LYXERR0("status " << process->exitStatus());
121 if (!process->waitForFinished(180000)) {
122 LYXERR0("Qprocess " << cmd << " did not finished!");
123 LYXERR0("error " << process->error());
124 LYXERR0("state " << process->state());
125 LYXERR0("status " << process->exitStatus());
128 int const exit_code = process->exitCode();
130 LYXERR0("Qprocess " << cmd << " finished!");
131 LYXERR0("exitCode " << process->exitCode());
132 LYXERR0("error " << process->error());
133 LYXERR0("state " << process->state());
134 LYXERR0("status " << process->exitStatus());
137 // If the output has been redirected, we write it all at once.
138 // Even if we are not running in a terminal, the output could go
139 // to some log file, for example ~/.xsession-errors on *nix.
140 if (!os::is_terminal(os::STDOUT) && outfile.empty())
141 cout << fromqstr(QString::fromLocal8Bit(
142 process->readAllStandardOutput().data()));
143 if (!os::is_terminal(os::STDERR))
144 cerr << fromqstr(QString::fromLocal8Bit(
145 process->readAllStandardError().data()));
147 killProcess(process);
152 } // namespace support