]> git.lyx.org Git - features.git/blob - src/support/Systemcall.cpp
Really flush output buffers.
[features.git] / src / support / Systemcall.cpp
1 /**
2  * \file Systemcall.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  *
8  * Interface cleaned up by
9  * \author Angus Leeming
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
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"
21
22 #include <cstdlib>
23 #include <iostream>
24
25 #include <QProcess>
26
27 #define USE_QPROCESS
28
29 using namespace std;
30
31 namespace lyx {
32 namespace support {
33
34 static void killProcess(QProcess * p)
35 {
36         p->disconnect();
37         p->closeReadChannel(QProcess::StandardOutput);
38         p->closeReadChannel(QProcess::StandardError);
39         p->close();
40         delete p;
41 }
42
43
44 // Reuse of instance
45 #ifndef USE_QPROCESS
46 int Systemcall::startscript(Starttype how, string const & what)
47 {
48         string command = what;
49
50         if (how == DontWait) {
51                 switch (os::shell()) {
52                 case os::UNIX:
53                         command += " &";
54                         break;
55                 case os::CMD_EXE:
56                         command = "start /min " + command;
57                         break;
58                 }
59         }
60
61         return ::system(command.c_str());
62 }
63
64 #else
65
66 namespace {
67
68 string const parsecmd(string const & cmd, string & outfile)
69 {
70         bool inquote = false;
71         bool escaped = false;
72
73         for (size_t i = 0; i < cmd.length(); ++i) {
74                 char c = cmd[i];
75                 if (c == '"' && !escaped)
76                         inquote = !inquote;
77                 else if (c == '\\' && !escaped)
78                         escaped = !escaped;
79                 else if (c == '>' && !(inquote || escaped)) {
80                         outfile = trim(cmd.substr(i + 1), " \"");
81                         return trim(cmd.substr(0, i));
82                 } else
83                         escaped = false;
84         }
85         outfile.erase();
86         return cmd;
87 }
88
89 } // namespace anon
90
91
92 int Systemcall::startscript(Starttype how, string const & what)
93 {
94         string outfile;
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))
103                 console.showout();
104         if (os::is_terminal(os::STDERR))
105                 console.showerr();
106
107         process->start(cmd);
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());
113                 return 10;
114         }
115         if (how == DontWait) {
116                 // TODO delete process later
117                 return 0;
118         }
119
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());
125                 return 20;
126         }
127         int const exit_code = process->exitCode();
128         if (exit_code) {
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());
134         }
135
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()));
145
146         killProcess(process);
147         return exit_code;
148 }
149
150
151 ConOut::ConOut(QProcess * proc) : proc_(proc), outindex_(0), errindex_(0),
152                                   showout_(false), showerr_(false)
153 {
154         connect(proc, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
155         connect(proc, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
156 }
157
158
159 ConOut::~ConOut()
160 {
161         if (outindex_) {
162                 outdata_[outindex_] = '\0';
163                 outindex_ = 0;
164                 cout << outdata_;
165         }
166         cout.flush();
167         if (errindex_) {
168                 errdata_[errindex_] = '\0';
169                 errindex_ = 0;
170                 cerr << errdata_;
171         }
172         cerr.flush();
173 }
174
175
176 void ConOut::stdOut()
177 {
178         if (showout_) {
179                 char c;
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';
185                                 outindex_ = 0;
186                                 cout << outdata_;
187                         }
188                 }
189         }
190 }
191
192
193 void ConOut::stdErr()
194 {
195         if (showerr_) {
196                 char c;
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';
202                                 errindex_ = 0;
203                                 cerr << errdata_;
204                         }
205                 }
206         }
207 }
208
209 #include "moc_Systemcall.cpp"
210 #endif
211
212 } // namespace support
213 } // namespace lyx