]> git.lyx.org Git - features.git/blob - src/support/Systemcall.cpp
Fix crash after cancellation code.
[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  * \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/gettext.h"
19 #include "support/lstrings.h"
20 #include "support/qstring_helpers.h"
21 #include "support/Systemcall.h"
22 #include "support/SystemcallPrivate.h"
23 #include "support/os.h"
24 #include "support/ProgressInterface.h"
25
26 #include "frontends/Application.h"
27
28 #include "LyX.h"
29 #include "LyXRC.h"
30
31 #include <cstdlib>
32 #include <iostream>
33
34 #include <QProcess>
35 #include <QTime>
36 #include <QThread>
37 #include <QCoreApplication>
38 #include <QDebug>
39
40 #define USE_QPROCESS
41
42
43 struct Sleep : QThread
44 {
45         static void millisec(unsigned long ms)
46         {
47                 QThread::usleep(ms * 1000);
48         }
49 };
50
51
52
53
54 using namespace std;
55
56 namespace lyx {
57 namespace support {
58
59
60 class ProgressDummy : public ProgressInterface
61 {
62 public:
63         ProgressDummy() {}
64
65         void processStarted(QString const &) {}
66         void processFinished(QString const &) {}
67         void appendMessage(QString const &) {}
68         void appendError(QString const &) {}
69         void clearMessages() {}
70         void lyxerrFlush() {}
71
72         void lyxerrConnect() {}
73         void lyxerrDisconnect() {}
74
75         void warning(QString const &, QString const &) {}
76         void toggleWarning(QString const &, QString const &, QString const &) {}
77         void error(QString const &, QString const &, QString const &) {}
78         void information(QString const &, QString const &) {}
79         int prompt(docstring const &, docstring const &, int default_but, int,
80                    docstring const &, docstring const &) { return default_but; }
81 };
82
83
84 static ProgressInterface * progress_instance = 0;
85
86 void ProgressInterface::setInstance(ProgressInterface* p)
87 {
88         progress_instance = p;
89 }
90
91
92 ProgressInterface * ProgressInterface::instance()
93 {
94         if (!progress_instance) {
95                 static ProgressDummy dummy;
96                 return &dummy;
97         }
98         return progress_instance;
99 }
100
101
102
103
104 // Reuse of instance
105 #ifndef USE_QPROCESS
106 int Systemcall::startscript(Starttype how, string const & what,
107                             string const & path, string const & lpath,
108                             bool /*process_events*/)
109 {
110         string command =
111                 to_filesystem8bit(from_utf8(latexEnvCmdPrefix(path, lpath)))
112                        + commandPrep(what);
113
114         if (how == DontWait) {
115                 switch (os::shell()) {
116                 case os::UNIX:
117                         command += " &";
118                         break;
119                 case os::CMD_EXE:
120                         command = "start /min " + command;
121                         break;
122                 }
123         } else if (os::shell() == os::CMD_EXE)
124                 command = subst(command, "cmd /d /c ", "");
125
126         return ::system(command.c_str());
127 }
128
129 #else
130
131 namespace {
132
133 /*
134  * This is a parser that (mostly) mimics the behavior of a posix shell as
135  * regards quoting, but its output is tailored for being processed by QProcess.
136  * Note that shell metacharacters are not parsed.
137  *
138  * The escape character is the backslash.
139  * A backslash that is not quoted preserves the literal value of the following
140  * character, with the exception of a double-quote '"'. If a double-quote
141  * follows a backslash, it will be replaced by three consecutive double-quotes
142  * (this is how the QProcess parser recognizes a '"' as a simple character
143  * instead of a quoting character). Thus, for example:
144  *     \\  ->  \
145  *     \a  ->  a
146  *     \"  ->  """
147  *
148  * Single-quotes.
149  * Characters enclosed in single-quotes ('') have their literal value preserved.
150  * A single-quote cannot occur within single-quotes. Indeed, a backslash cannot
151  * be used to escape a single-quote in a single-quoted string. In other words,
152  * anything enclosed in single-quotes is passed as is, but the single-quotes
153  * themselves are eliminated. Thus, for example:
154  *    '\'    ->  \
155  *    '\\'   ->  \\
156  *    '\a'   ->  \a
157  *    'a\"b' ->  a\"b
158  *
159  * Double-quotes.
160  * Characters enclosed in double-quotes ("") have their literal value preserved,
161  * with the exception of the backslash. The backslash retains its special
162  * meaning as an escape character only when followed by a double-quote.
163  * Contrarily to the behavior of a posix shell, the double-quotes themselves
164  * are *not* eliminated. Thus, for example:
165  *    "\\"   ->  "\\"
166  *    "\a"   ->  "\a"
167  *    "a\"b" ->  "a"""b"
168  */
169 string const parsecmd(string const & incmd, string & infile, string & outfile,
170                      string & errfile)
171 {
172         bool in_single_quote = false;
173         bool in_double_quote = false;
174         bool escaped = false;
175         string const python_call = "python -tt";
176         vector<string> outcmd(4);
177         size_t start = 0;
178
179         if (prefixIs(incmd, python_call)) {
180                 outcmd[0] = os::python();
181                 start = python_call.length();
182         }
183
184         for (size_t i = start, o = 0; i < incmd.length(); ++i) {
185                 char c = incmd[i];
186                 if (c == '\'') {
187                         if (in_double_quote || escaped) {
188                                 if (in_double_quote && escaped)
189                                         outcmd[o] += '\\';
190                                 outcmd[o] += c;
191                         } else
192                                 in_single_quote = !in_single_quote;
193                         escaped = false;
194                         continue;
195                 }
196                 if (in_single_quote) {
197                         outcmd[o] += c;
198                         continue;
199                 }
200                 if (c == '"') {
201                         if (escaped) {
202                                 // Don't triple double-quotes for redirection
203                                 // files as these won't be parsed by QProcess
204                                 outcmd[o] += string(o ? "\"" : "\"\"\"");
205                                 escaped = false;
206                         } else {
207                                 outcmd[o] += c;
208                                 in_double_quote = !in_double_quote;
209                         }
210                 } else if (c == '\\' && !escaped) {
211                         escaped = !escaped;
212                 } else if (c == '>' && !(in_double_quote || escaped)) {
213                         if (suffixIs(outcmd[o], " 2")) {
214                                 outcmd[o] = rtrim(outcmd[o], "2");
215                                 o = 2;
216                         } else {
217                                 if (suffixIs(outcmd[o], " 1"))
218                                         outcmd[o] = rtrim(outcmd[o], "1");
219                                 o = 1;
220                         }
221                 } else if (c == '<' && !(in_double_quote || escaped)) {
222                         o = 3;
223                 } else {
224                         if (escaped && in_double_quote)
225                                 outcmd[o] += '\\';
226                         outcmd[o] += c;
227                         escaped = false;
228                 }
229         }
230         infile  = trim(outcmd[3], " \"");
231         outfile = trim(outcmd[1], " \"");
232         errfile = trim(outcmd[2], " \"");
233         return trim(outcmd[0]);
234 }
235
236 } // namespace
237
238
239 int Systemcall::startscript(Starttype how, string const & what,
240                             string const & path, string const & lpath,
241                             bool process_events)
242 {
243         string const what_ss = commandPrep(what);
244         if (verbose)
245                 lyxerr << "\nRunning: " << what_ss << endl;
246         else
247                 LYXERR(Debug::INFO,"Running: " << what_ss);
248
249         string infile;
250         string outfile;
251         string errfile;
252         QString const cmd = QString::fromLocal8Bit(
253                         parsecmd(what_ss, infile, outfile, errfile).c_str());
254
255         SystemcallPrivate d(infile, outfile, errfile);
256         bool do_events = process_events || how == WaitLoop;
257
258 #ifdef Q_OS_WIN32
259         // QProcess::startDetached cannot provide environment variables. When the
260         // environment variables are set using the latexEnvCmdPrefix and the process
261         // is started with QProcess::startDetached, a console window is shown every
262         // time a viewer is started. To avoid this, we fall back on Windows to the
263         // original implementation that creates a QProcess object.
264         d.startProcess(cmd, path, lpath, false);
265         if (!d.waitWhile(SystemcallPrivate::Starting, do_events, -1)) {
266                 if (d.state == SystemcallPrivate::Error) {
267                         LYXERR0("Systemcall: '" << cmd << "' did not start!");
268                         LYXERR0("error " << d.errorMessage());
269                         return NOSTART;
270                 } else if (d.state == SystemcallPrivate::Killed) {
271                         LYXERR0("Killed: " << cmd);
272                         return KILLED;
273                 }
274         }
275         if (how == DontWait) {
276                 d.releaseProcess();
277                 return OK;
278         }
279 #else
280         d.startProcess(cmd, path, lpath, how == DontWait);
281         if (how == DontWait && d.state == SystemcallPrivate::Running)
282                 return OK;
283
284         if (d.state == SystemcallPrivate::Error
285                         || !d.waitWhile(SystemcallPrivate::Starting, do_events, -1)) {
286                 if (d.state == SystemcallPrivate::Error) {
287                         LYXERR0("Systemcall: '" << cmd << "' did not start!");
288                         LYXERR0("error " << d.errorMessage());
289                         return NOSTART;
290                 } else if (d.state == SystemcallPrivate::Killed) {
291                         LYXERR0("Killed: " << cmd);
292                         return KILLED;
293                 }
294         }
295 #endif
296
297         if (!d.waitWhile(SystemcallPrivate::Running, do_events,
298                          os::timeout_min() * 60 * 1000)) {
299                 if (d.state == SystemcallPrivate::Killed) {
300                         LYXERR0("Killed: " << cmd);
301                         return KILLED;
302                 }
303                 LYXERR0("Systemcall: '" << cmd << "' did not finish!");
304                 LYXERR0("error " << d.errorMessage());
305                 LYXERR0("status " << d.exitStatusMessage());
306                 return TIMEOUT;
307         }
308
309         int const exit_code = d.exitCode();
310         if (exit_code) {
311                 LYXERR0("Systemcall: '" << cmd << "' finished with exit code " << exit_code);
312         }
313
314         return exit_code;
315 }
316
317
318 SystemcallPrivate::SystemcallPrivate(std::string const & sf, std::string const & of,
319                                      std::string const & ef)
320         : state(Error), process_(new QProcess), out_index_(0), err_index_(0),
321           in_file_(sf), out_file_(of), err_file_(ef), process_events_(false)
322 {
323         if (!in_file_.empty())
324                 process_->setStandardInputFile(QString::fromLocal8Bit(in_file_.c_str()));
325         if (!out_file_.empty()) {
326                 if (out_file_[0] == '&') {
327                         if (subst(out_file_, " ", "") == "&2"
328                             && err_file_[0] != '&') {
329                                 out_file_ = err_file_;
330                                 process_->setProcessChannelMode(
331                                                 QProcess::MergedChannels);
332                         } else {
333                                 if (err_file_[0] == '&') {
334                                         // Leave alone things such as
335                                         // "1>&2 2>&1". Should not be harmful,
336                                         // but let's give anyway a warning.
337                                         LYXERR0("Unsupported stdout/stderr redirect.");
338                                         err_file_.erase();
339                                 } else {
340                                         LYXERR0("Ambiguous stdout redirect: "
341                                                 << out_file_);
342                                 }
343                                 out_file_ = os::nulldev();
344                         }
345                 }
346                 // Check whether we have to set the output file.
347                 if (out_file_ != os::nulldev()) {
348                         process_->setStandardOutputFile(QString::fromLocal8Bit(
349                                                         out_file_.c_str()));
350                 }
351         }
352         if (!err_file_.empty()) {
353                 if (err_file_[0] == '&') {
354                         if (subst(err_file_, " ", "") == "&1"
355                             && out_file_[0] != '&') {
356                                 process_->setProcessChannelMode(
357                                                 QProcess::MergedChannels);
358                         } else {
359                                 LYXERR0("Ambiguous stderr redirect: "
360                                         << err_file_);
361                         }
362                         // In MergedChannels mode stderr goes to stdout.
363                         err_file_ = os::nulldev();
364                 }
365                 // Check whether we have to set the error file.
366                 if (err_file_ != os::nulldev()) {
367                         process_->setStandardErrorFile(QString::fromLocal8Bit(
368                                                         err_file_.c_str()));
369                 }
370         }
371
372         connect(process_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
373         connect(process_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
374 #if QT_VERSION >= 0x050600
375         connect(process_, SIGNAL(errorOccurred(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
376 #else
377         connect(process_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
378 #endif
379         connect(process_, SIGNAL(started()), this, SLOT(processStarted()));
380         connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
381 }
382
383
384 void SystemcallPrivate::startProcess(QString const & cmd, string const & path,
385                                      string const & lpath, bool detached)
386 {
387         cmd_ = cmd;
388         if (detached) {
389                 state = SystemcallPrivate::Running;
390                 if (!QProcess::startDetached(toqstr(latexEnvCmdPrefix(path, lpath)) + cmd_)) {
391                         state = SystemcallPrivate::Error;
392                         return;
393                 }
394                 QProcess* released = releaseProcess();
395                 delete released;
396         } else if (process_) {
397                 state = SystemcallPrivate::Starting;
398                 process_->start(toqstr(latexEnvCmdPrefix(path, lpath)) + cmd_);
399         }
400 }
401
402
403 bool SystemcallPrivate::waitAndCheck()
404 {
405         Sleep::millisec(100);
406         if (theApp() && theApp()->cancel_export) {
407                 // is there a better place to reset this?
408                 process_->kill();
409                 state = Killed;
410                 theApp()->cancel_export = false;
411                 LYXERR0("Export Canceled!!");
412                 return false;
413         }
414         QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
415         return true;
416 }
417
418
419 namespace {
420
421 bool queryStopCommand(QString const & cmd)
422 {
423         docstring text = bformat(_(
424                 "The command\n%1$s\nhas not yet completed.\n\n"
425                 "Do you want to stop it?"), qstring_to_ucs4(cmd));
426         return ProgressInterface::instance()->prompt(_("Stop command?"), text,
427                         1, 1, _("&Stop it"), _("Let it &run")) == 0;
428 }
429
430 } // namespace
431
432
433 bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int timeout)
434 {
435         if (!process_)
436                 return false;
437
438         bool timedout = false;
439         process_events_ = process_events;
440
441         // Block GUI while waiting,
442         // relay on QProcess' wait functions
443         if (!process_events_) {
444                 if (waitwhile == Starting)
445                         return process_->waitForStarted(timeout);
446                 if (waitwhile == Running) {
447                         int bump = 2;
448                         while (!timedout) {
449                                 if (process_->waitForFinished(timeout))
450                                         return true;
451                                 bool stop = queryStopCommand(cmd_);
452                                 // The command may have finished in the meantime
453                                 if (process_->state() == QProcess::NotRunning)
454                                         return true;
455                                 if (stop) {
456                                         timedout = true;
457                                         process_->kill();
458                                 } else {
459                                         timeout *= bump;
460                                         bump = 3;
461                                 }
462                         }
463                 }
464                 return false;
465         }
466
467         // process events while waiting, no timeout
468         if (timeout == -1) {
469                 while (state == waitwhile && state != Error) {
470                         // check for cancellation of background process
471                         if (!waitAndCheck())
472                                 return false;
473                 }
474                 return state != Error;
475         }
476
477         // process events while waiting with timeout
478         QTime timer;
479         timer.start();
480         while (state == waitwhile && state != Error && !timedout) {
481                 // check for cancellation of background process
482                 if (!waitAndCheck())
483                         return false;
484
485                 if (timer.elapsed() > timeout) {
486                         bool stop = queryStopCommand(cmd_);
487                         // The command may have finished in the meantime
488                         if (process_->state() == QProcess::NotRunning)
489                                 break;
490                         if (stop) {
491                                 timedout = true;
492                                 process_->kill();
493                         } else
494                                 timeout *= 3;
495                 }
496         }
497         return (state != Error) && !timedout;
498 }
499
500
501 SystemcallPrivate::~SystemcallPrivate()
502 {
503         if (out_index_) {
504                 out_data_[out_index_] = '\0';
505                 out_index_ = 0;
506                 cout << out_data_;
507         }
508         cout.flush();
509         if (err_index_) {
510                 err_data_[err_index_] = '\0';
511                 err_index_ = 0;
512                 cerr << err_data_;
513         }
514         cerr.flush();
515
516         killProcess();
517 }
518
519
520 void SystemcallPrivate::stdOut()
521 {
522         if (process_) {
523                 char c;
524                 process_->setReadChannel(QProcess::StandardOutput);
525                 while (process_->getChar(&c)) {
526                         out_data_[out_index_++] = c;
527                         if (c == '\n' || out_index_ + 1 == buffer_size_) {
528                                 out_data_[out_index_] = '\0';
529                                 out_index_ = 0;
530                                 ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(out_data_));
531                                 cout << out_data_;
532                         }
533                 }
534         }
535 }
536
537
538 void SystemcallPrivate::stdErr()
539 {
540         if (process_) {
541                 char c;
542                 process_->setReadChannel(QProcess::StandardError);
543                 while (process_->getChar(&c)) {
544                         err_data_[err_index_++] = c;
545                         if (c == '\n' || err_index_ + 1 == buffer_size_) {
546                                 err_data_[err_index_] = '\0';
547                                 err_index_ = 0;
548                                 ProgressInterface::instance()->appendError(QString::fromLocal8Bit(err_data_));
549                                 cerr << err_data_;
550                         }
551                 }
552         }
553 }
554
555
556 void SystemcallPrivate::processStarted()
557 {
558         if (state != Running) {
559                 state = Running;
560                 ProgressInterface::instance()->processStarted(cmd_);
561         }
562 }
563
564
565 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
566 {
567         if (state != Finished) {
568                 state = Finished;
569                 ProgressInterface::instance()->processFinished(cmd_);
570         }
571 }
572
573
574 void SystemcallPrivate::processError(QProcess::ProcessError)
575 {
576         state = Error;
577         ProgressInterface::instance()->appendError(errorMessage());
578 }
579
580
581 QString SystemcallPrivate::errorMessage() const
582 {
583         if (!process_)
584                 return "No QProcess available";
585
586         QString message;
587         switch (process_->error()) {
588                 case QProcess::FailedToStart:
589                         message = "The process failed to start. Either the invoked program is missing, "
590                                       "or you may have insufficient permissions to invoke the program.";
591                         break;
592                 case QProcess::Crashed:
593                         message = "The process crashed some time after starting successfully.";
594                         break;
595                 case QProcess::Timedout:
596                         message = "The process timed out. It might be restarted automatically.";
597                         break;
598                 case QProcess::WriteError:
599                         message = "An error occurred when attempting to write to the process-> For example, "
600                                       "the process may not be running, or it may have closed its input channel.";
601                         break;
602                 case QProcess::ReadError:
603                         message = "An error occurred when attempting to read from the process-> For example, "
604                                       "the process may not be running.";
605                         break;
606                 case QProcess::UnknownError:
607                 default:
608                         message = "An unknown error occurred.";
609                         break;
610         }
611         return message;
612 }
613
614
615 QString SystemcallPrivate::exitStatusMessage() const
616 {
617         if (!process_)
618                 return "No QProcess available";
619
620         QString message;
621         switch (process_->exitStatus()) {
622                 case QProcess::NormalExit:
623                         message = "The process exited normally.";
624                         break;
625                 case QProcess::CrashExit:
626                         message = "The process crashed.";
627                         break;
628                 default:
629                         message = "Unknown exit state.";
630                         break;
631         }
632         return message;
633 }
634
635
636 int SystemcallPrivate::exitCode()
637 {
638         // From Qt's documentation, in regards to QProcess::exitCode(),
639         // "This value is not valid unless exitStatus() returns NormalExit"
640         if (!process_ || process_->exitStatus() != QProcess::NormalExit)
641                 return -1;
642
643         return process_->exitCode();
644 }
645
646
647 QProcess* SystemcallPrivate::releaseProcess()
648 {
649         QProcess* released = process_;
650         process_ = 0;
651         return released;
652 }
653
654
655 void SystemcallPrivate::killProcess()
656 {
657         killProcess(process_);
658 }
659
660
661 void SystemcallPrivate::killProcess(QProcess * p)
662 {
663         if (p) {
664                 p->disconnect();
665                 p->closeReadChannel(QProcess::StandardOutput);
666                 p->closeReadChannel(QProcess::StandardError);
667                 p->close();
668                 delete p;
669         }
670 }
671
672
673
674 #include "moc_SystemcallPrivate.cpp"
675 #endif
676
677 } // namespace support
678 } // namespace lyx