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