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)
37 p->closeReadChannel(QProcess::StandardOutput);
38 p->closeReadChannel(QProcess::StandardError);
46 int Systemcall::startscript(Starttype how, string const & what)
48 string command = what;
50 if (how == DontWait) {
51 switch (os::shell()) {
56 command = "start /min " + command;
61 return ::system(command.c_str());
68 string const parsecmd(string const & cmd, string & outfile)
73 for (size_t i = 0; i < cmd.length(); ++i) {
75 if (c == '"' && !escaped)
77 else if (c == '\\' && !escaped)
79 else if (c == '>' && !(inquote || escaped)) {
80 outfile = trim(cmd.substr(i + 1), " \"");
81 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;
97 ConOut console(process);
98 if (!outfile.empty()) {
99 // Check whether we have to simply throw away the output.
100 if (outfile != os::nulldev())
101 process->setStandardOutputFile(toqstr(outfile));
102 } else if (os::is_terminal(os::STDOUT))
104 if (os::is_terminal(os::STDERR))
108 if (!process->waitForStarted(3000)) {
109 LYXERR0("Qprocess " << cmd << " did not start!");
110 LYXERR0("error " << process->error());
111 LYXERR0("state " << process->state());
112 LYXERR0("status " << process->exitStatus());
115 if (how == DontWait) {
116 // TODO delete process later
120 if (!process->waitForFinished(180000)) {
121 LYXERR0("Qprocess " << cmd << " did not finished!");
122 LYXERR0("error " << process->error());
123 LYXERR0("state " << process->state());
124 LYXERR0("status " << process->exitStatus());
127 int const exit_code = process->exitCode();
129 LYXERR0("Qprocess " << cmd << " finished!");
130 LYXERR0("exitCode " << process->exitCode());
131 LYXERR0("error " << process->error());
132 LYXERR0("state " << process->state());
133 LYXERR0("status " << process->exitStatus());
136 // If the output has been redirected, we write it all at once.
137 // Even if we are not running in a terminal, the output could go
138 // to some log file, for example ~/.xsession-errors on *nix.
139 if (!os::is_terminal(os::STDOUT) && outfile.empty())
140 cout << fromqstr(QString::fromLocal8Bit(
141 process->readAllStandardOutput().data()));
142 if (!os::is_terminal(os::STDERR))
143 cerr << fromqstr(QString::fromLocal8Bit(
144 process->readAllStandardError().data()));
146 killProcess(process);
151 ConOut::ConOut(QProcess * proc) : proc_(proc), outindex_(0), errindex_(0),
152 showout_(false), showerr_(false)
154 connect(proc, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
155 connect(proc, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
162 outdata_[outindex_] = '\0';
168 errdata_[errindex_] = '\0';
176 void ConOut::stdOut()
180 proc_->setReadChannel(QProcess::StandardOutput);
181 while (proc_->getChar(&c)) {
182 outdata_[outindex_++] = c;
183 if (c == '\n' || outindex_ + 1 == bufsize_) {
184 outdata_[outindex_] = '\0';
193 void ConOut::stdErr()
197 proc_->setReadChannel(QProcess::StandardError);
198 while (proc_->getChar(&c)) {
199 errdata_[errindex_++] = c;
200 if (c == '\n' || errindex_ + 1 == bufsize_) {
201 errdata_[errindex_] = '\0';
209 #include "moc_Systemcall.cpp"
212 } // namespace support