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