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/SystemcallPrivate.h"
21 #include "support/os.h"
35 static void killProcess(QProcess * p)
38 p->closeReadChannel(QProcess::StandardOutput);
39 p->closeReadChannel(QProcess::StandardError);
47 int Systemcall::startscript(Starttype how, string const & what)
49 string command = what;
51 if (how == DontWait) {
52 switch (os::shell()) {
57 command = "start /min " + command;
62 return ::system(command.c_str());
69 string const parsecmd(string const & cmd, string & outfile)
74 for (size_t i = 0; i < cmd.length(); ++i) {
76 if (c == '"' && !escaped)
78 else if (c == '\\' && !escaped)
80 else if (c == '>' && !(inquote || escaped)) {
81 outfile = trim(cmd.substr(i + 1), " \"");
82 return trim(cmd.substr(0, i));
93 int Systemcall::startscript(Starttype how, string const & what)
96 QString cmd = toqstr(parsecmd(what, outfile));
97 QProcess * process = new QProcess;
98 ConOut console(process);
99 if (!outfile.empty()) {
100 // Check whether we have to simply throw away the output.
101 if (outfile != os::nulldev())
102 process->setStandardOutputFile(toqstr(outfile));
103 } else if (os::is_terminal(os::STDOUT))
105 if (os::is_terminal(os::STDERR))
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());
116 if (how == DontWait) {
117 // TODO delete process later
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 ConOut::ConOut(QProcess * proc) : proc_(proc), outindex_(0), errindex_(0),
153 showout_(false), showerr_(false)
155 connect(proc, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
156 connect(proc, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
163 outdata_[outindex_] = '\0';
169 errdata_[errindex_] = '\0';
177 void ConOut::stdOut()
181 proc_->setReadChannel(QProcess::StandardOutput);
182 while (proc_->getChar(&c)) {
183 outdata_[outindex_++] = c;
184 if (c == '\n' || outindex_ + 1 == bufsize_) {
185 outdata_[outindex_] = '\0';
194 void ConOut::stdErr()
198 proc_->setReadChannel(QProcess::StandardError);
199 while (proc_->getChar(&c)) {
200 errdata_[errindex_++] = c;
201 if (c == '\n' || errindex_ + 1 == bufsize_) {
202 errdata_[errindex_] = '\0';
210 #include "moc_SystemcallPrivate.cpp"
213 } // namespace support