]> git.lyx.org Git - lyx.git/blob - src/support/Systemcall.cpp
bc7790adb17cdd8f8dbd4f123836e6bb5610a09d
[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  * \author Angus Leeming
8  * \author Enrico Forestieri
9  * \author Peter Kuemmel
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/filetools.h"
18 #include "support/lstrings.h"
19 #include "support/qstring_helpers.h"
20 #include "support/Systemcall.h"
21 #include "support/SystemcallPrivate.h"
22 #include "support/os.h"
23 #include "support/ProgressInterface.h"
24
25 #include "LyXRC.h"
26
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <QProcess>
31 #include <QTime>
32 #include <QThread>
33 #include <QCoreApplication>
34 #include <QDebug>
35
36 #define USE_QPROCESS
37
38
39 struct Sleep : QThread
40 {
41         static void millisec(unsigned long ms) 
42         {
43                 QThread::usleep(ms * 1000);
44         }
45 };
46
47
48
49
50 using namespace std;
51
52 namespace lyx {
53 namespace support {
54
55
56 class ProgressDummy : public ProgressInterface 
57 {
58 public:
59         ProgressDummy() {}
60
61         void processStarted(QString const &) {}
62         void processFinished(QString const &) {}
63         void appendMessage(QString const &) {}
64         void appendError(QString const &) {}
65         void clearMessages() {}
66         void lyxerrFlush() {}
67
68         void lyxerrConnect() {}
69         void lyxerrDisconnect() {}
70
71         void warning(QString const &, QString const &) {}
72         void toggleWarning(QString const &, QString const &, QString const &) {}
73         void error(QString const &, QString const &) {}
74         void information(QString const &, QString const &) {}
75 };
76
77
78 static ProgressInterface* progress_instance = 0;
79
80 void ProgressInterface::setInstance(ProgressInterface* p)
81 {
82         progress_instance = p;
83 }
84
85
86 ProgressInterface* ProgressInterface::instance()
87 {
88         if (!progress_instance) {
89                 static ProgressDummy dummy;
90                 return &dummy;
91         }
92         return progress_instance;
93 }
94
95
96
97
98 // Reuse of instance
99 #ifndef USE_QPROCESS
100 int Systemcall::startscript(Starttype how, string const & what,
101                             std::string const & path, bool /*process_events*/)
102 {
103         string const python_call = "python -tt";
104         string command = to_filesystem8bit(from_utf8(latexEnvCmdPrefix(path)));
105
106         if (prefixIs(what, python_call))
107                 command += os::python() + what.substr(python_call.length());
108         else
109                 command += what;
110
111         if (how == DontWait) {
112                 switch (os::shell()) {
113                 case os::UNIX:
114                         command += " &";
115                         break;
116                 case os::CMD_EXE:
117                         command = "start /min " + command;
118                         break;
119                 }
120         } else if (os::shell() == os::CMD_EXE)
121                 command = subst(command, "cmd /d /c ", "");
122
123         return ::system(command.c_str());
124 }
125
126 #else
127
128 namespace {
129
130 /*
131  * This is a parser that (mostly) mimics the behavior of a posix shell but
132  * its output is tailored for being processed by QProcess.
133  *
134  * The escape character is the backslash.
135  * A backslash that is not quoted preserves the literal value of the following
136  * character, with the exception of a double-quote '"'. If a double-quote
137  * follows a backslash, it will be replaced by three consecutive double-quotes
138  * (this is how the QProcess parser recognizes a '"' as a simple character
139  * instead of a quoting character). Thus, for example:
140  *     \\  ->  \
141  *     \a  ->  a
142  *     \"  ->  """
143  *
144  * Single-quotes.
145  * Characters enclosed in single-quotes ('') have their literal value preserved.
146  * A single-quote cannot occur within single-quotes. Indeed, a backslash cannot
147  * be used to escape a single-quote in a single-quoted string. In other words,
148  * anything enclosed in single-quotes is passed as is, but the single-quotes
149  * themselves are eliminated. Thus, for example:
150  *    '\'    ->  \
151  *    '\\'   ->  \\
152  *    '\a'   ->  \a
153  *    'a\"b' ->  a\"b
154  *
155  * Double-quotes.
156  * Characters enclosed in double-quotes ("") have their literal value preserved,
157  * with the exception of the backslash. The backslash retains its special
158  * meaning as an escape character only when followed by a double-quote.
159  * Contrarily to the behavior of a posix shell, the double-quotes themselves
160  * are *not* eliminated. Thus, for example:
161  *    "\\"   ->  "\\"
162  *    "\a"   ->  "\a"
163  *    "a\"b" ->  "a"""b"
164  */
165 string const parsecmd(string const & inputcmd, string & outfile)
166 {
167         bool in_single_quote = false;
168         bool in_double_quote = false;
169         bool escaped = false;
170         string const python_call = "python -tt";
171         string cmd;
172         int start = 0;
173
174         if (prefixIs(inputcmd, python_call)) {
175                 cmd = os::python();
176                 start = python_call.length();
177         }
178
179         for (size_t i = start; i < inputcmd.length(); ++i) {
180                 char c = inputcmd[i];
181                 if (c == '\'') {
182                         if (in_double_quote || escaped) {
183                                 if (in_double_quote && escaped)
184                                         cmd += '\\';
185                                 cmd += c;
186                         } else
187                                 in_single_quote = !in_single_quote;
188                         escaped = false;
189                         continue;
190                 }
191                 if (in_single_quote) {
192                         cmd += c;
193                         continue;
194                 }
195                 if (c == '"') {
196                         if (escaped) {
197                                 cmd += "\"\"\"";
198                                 escaped = false;
199                         } else {
200                                 cmd += c;
201                                 in_double_quote = !in_double_quote;
202                         }
203                 } else if (c == '\\' && !escaped) {
204                         escaped = !escaped;
205                 } else if (c == '>' && !(in_double_quote || escaped)) {
206                         outfile = trim(inputcmd.substr(i + 1), " \"");
207                         return trim(cmd);
208                 } else {
209                         if (escaped && in_double_quote)
210                                 cmd += '\\';
211                         cmd += c;
212                         escaped = false;
213                 }
214         }
215         outfile.erase();
216         return cmd;
217 }
218
219 } // namespace anon
220
221
222
223 int Systemcall::startscript(Starttype how, string const & what,
224                             string const & path, bool process_events)
225 {
226         string outfile;
227         QString cmd = QString::fromLocal8Bit(parsecmd(what, outfile).c_str());
228
229         SystemcallPrivate d(outfile);
230
231
232         d.startProcess(cmd, path);
233         if (!d.waitWhile(SystemcallPrivate::Starting, process_events, -1)) {
234                 LYXERR0("Systemcall: '" << cmd << "' did not start!");
235                 LYXERR0("error " << d.errorMessage());
236                 return 10;
237         }
238
239         if (how == DontWait) {
240                 QProcess* released = d.releaseProcess();
241                 (void) released; // TODO who deletes it?
242                 return 0;
243         }
244
245         if (!d.waitWhile(SystemcallPrivate::Running, process_events,
246                          os::timeout_min() * 60 * 1000)) {
247                 LYXERR0("Systemcall: '" << cmd << "' did not finish!");
248                 LYXERR0("error " << d.errorMessage());
249                 LYXERR0("status " << d.exitStatusMessage());
250                 return 20;
251         }
252
253         int const exit_code = d.exitCode();
254         if (exit_code) {
255                 LYXERR0("Systemcall: '" << cmd << "' finished with exit code " << exit_code);
256         }
257
258         return exit_code;
259 }
260
261
262 SystemcallPrivate::SystemcallPrivate(const std::string& of) :
263                                 process_(new QProcess), 
264                                 out_index_(0),
265                                 err_index_(0),
266                                 out_file_(of), 
267                                 process_events_(false)
268 {
269         if (!out_file_.empty()) {
270                 // Check whether we have to simply throw away the output.
271                 if (out_file_ != os::nulldev())
272                         process_->setStandardOutputFile(QString::fromLocal8Bit(out_file_.c_str()));
273         }
274
275         connect(process_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
276         connect(process_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
277         connect(process_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
278         connect(process_, SIGNAL(started()), this, SLOT(processStarted()));
279         connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
280 }
281
282
283 void SystemcallPrivate::startProcess(QString const & cmd, string const & path)
284 {
285         cmd_ = cmd;
286         if (process_) {
287                 state = SystemcallPrivate::Starting;
288                 process_->start(toqstr(latexEnvCmdPrefix(path)) + cmd_);
289         }
290 }
291
292
293 void SystemcallPrivate::processEvents()
294 {
295         if(process_events_) {
296                 QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
297         }
298 }
299
300
301 void SystemcallPrivate::waitAndProcessEvents()
302 {
303         Sleep::millisec(100);
304         processEvents();
305 }
306
307
308 bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int timeout)
309 {
310         if (!process_)
311                 return false;
312
313         process_events_ = process_events;
314
315         // Block GUI while waiting,
316         // relay on QProcess' wait functions
317         if (!process_events_) {
318                 if (waitwhile == Starting)
319                         return process_->waitForStarted(timeout);
320                 if (waitwhile == Running)
321                         return process_->waitForFinished(timeout);
322                 return false;
323         }
324
325         // process events while waiting, no timeout
326         if (timeout == -1) {
327                 while (state == waitwhile && state != Error) {
328                         waitAndProcessEvents();
329                 }
330                 return state != Error;
331         } 
332
333         // process events while waiting whith timeout
334         QTime timer;
335         timer.start();
336         while (state == waitwhile && state != Error && timer.elapsed() < timeout) {
337                 waitAndProcessEvents();
338         }
339         return (state != Error) && (timer.elapsed() < timeout);
340 }
341
342
343 SystemcallPrivate::~SystemcallPrivate()
344 {
345         if (out_index_) {
346                 out_data_[out_index_] = '\0';
347                 out_index_ = 0;
348                 cout << out_data_;
349         }
350         cout.flush();
351         if (err_index_) {
352                 err_data_[err_index_] = '\0';
353                 err_index_ = 0;
354                 cerr << err_data_;
355         }
356         cerr.flush();
357
358         killProcess();
359 }
360
361
362 void SystemcallPrivate::stdOut()
363 {
364         if (process_) {
365                 char c;
366                 process_->setReadChannel(QProcess::StandardOutput);
367                 while (process_->getChar(&c)) {
368                         out_data_[out_index_++] = c;
369                         if (c == '\n' || out_index_ + 1 == buffer_size_) {
370                                 out_data_[out_index_] = '\0';
371                                 out_index_ = 0;
372                                 ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(out_data_));
373                                 cout << out_data_;
374                         }
375                 }
376         }
377 }
378
379
380 void SystemcallPrivate::stdErr()
381 {
382         if (process_) {
383                 char c;
384                 process_->setReadChannel(QProcess::StandardError);
385                 while (process_->getChar(&c)) {
386                         err_data_[err_index_++] = c;
387                         if (c == '\n' || err_index_ + 1 == buffer_size_) {
388                                 err_data_[err_index_] = '\0';
389                                 err_index_ = 0;
390                                 ProgressInterface::instance()->appendError(QString::fromLocal8Bit(err_data_));
391                                 cerr << err_data_;
392                         }
393                 }
394         }
395 }
396
397
398 void SystemcallPrivate::processStarted()
399 {
400         if (state != Running) {
401                 state = Running;
402                 ProgressInterface::instance()->processStarted(cmd_);
403         }
404 }
405
406
407 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
408 {
409         if (state != Finished) {
410                 state = Finished;
411                 ProgressInterface::instance()->processFinished(cmd_);
412         }
413 }
414
415
416 void SystemcallPrivate::processError(QProcess::ProcessError)
417 {
418         state = Error;
419         ProgressInterface::instance()->appendError(errorMessage());
420 }
421
422
423 QString SystemcallPrivate::errorMessage() const 
424 {
425         if (!process_)
426                 return "No QProcess available";
427
428         QString message;
429         switch (process_->error()) {
430                 case QProcess::FailedToStart:
431                         message = "The process failed to start. Either the invoked program is missing, "
432                                       "or you may have insufficient permissions to invoke the program.";
433                         break;
434                 case QProcess::Crashed:
435                         message = "The process crashed some time after starting successfully.";
436                         break;
437                 case QProcess::Timedout:
438                         message = "The process timed out. It might be restarted automatically.";
439                         break;
440                 case QProcess::WriteError:
441                         message = "An error occurred when attempting to write to the process-> For example, "
442                                       "the process may not be running, or it may have closed its input channel.";
443                         break;
444                 case QProcess::ReadError:
445                         message = "An error occurred when attempting to read from the process-> For example, "
446                                       "the process may not be running.";
447                         break;
448                 case QProcess::UnknownError:
449                 default:
450                         message = "An unknown error occured.";
451                         break;
452         }
453         return message;
454 }
455
456
457 QString SystemcallPrivate::exitStatusMessage() const
458 {
459         if (!process_)
460                 return "No QProcess available";
461
462         QString message;
463         switch (process_->exitStatus()) {
464                 case QProcess::NormalExit:
465                         message = "The process exited normally.";
466                         break;
467                 case QProcess::CrashExit:
468                         message = "The process crashed.";
469                         break;
470                 default:
471                         message = "Unknown exit state.";
472                         break;
473         }
474         return message;
475 }
476
477
478 int SystemcallPrivate::exitCode()
479 {
480         if (!process_)
481                 return -1;
482
483         return process_->exitCode();
484 }
485
486
487 QProcess* SystemcallPrivate::releaseProcess()
488 {
489         QProcess* released = process_;
490         process_ = 0;
491         return released;
492 }
493
494
495 void SystemcallPrivate::killProcess()
496 {
497         killProcess(process_);
498 }
499
500
501 void SystemcallPrivate::killProcess(QProcess * p)
502 {
503         if (p) {
504                 p->disconnect();
505                 p->closeReadChannel(QProcess::StandardOutput);
506                 p->closeReadChannel(QProcess::StandardError);
507                 p->close();
508                 delete p;
509         }
510 }
511
512
513
514 #include "moc_SystemcallPrivate.cpp"
515 #endif
516
517 } // namespace support
518 } // namespace lyx