3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Angus Leeming
8 * \author Enrico Forestieri
9 * \author Peter Kuemmel
11 * Full author contact details are available in file CREDITS.
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"
32 #include <QElapsedTimer>
34 #include <QCoreApplication>
40 struct Sleep : QThread
42 static void millisec(unsigned long ms)
44 QThread::usleep(ms * 1000);
57 class ProgressDummy : public ProgressInterface
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 {}
69 void lyxerrConnect() override {}
70 void lyxerrDisconnect() override {}
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; }
81 static ProgressInterface * progress_instance = nullptr;
83 void ProgressInterface::setInstance(ProgressInterface* p)
85 progress_instance = p;
89 ProgressInterface * ProgressInterface::instance()
91 if (!progress_instance) {
92 static ProgressDummy dummy;
95 return progress_instance;
103 int Systemcall::startscript(Starttype how, string const & what,
104 string const & path, string const & lpath,
105 bool /*process_events*/)
108 to_filesystem8bit(from_utf8(latexEnvCmdPrefix(path, lpath)))
111 if (how == DontWait) {
112 switch (os::shell()) {
117 command = "start /min " + command;
120 } else if (os::shell() == os::CMD_EXE)
121 command = subst(command, "cmd /d /c ", "");
123 return ::system(command.c_str());
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.
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:
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:
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:
166 string const parsecmd(string const & incmd, string & infile, string & outfile,
169 bool in_single_quote = false;
170 bool in_double_quote = false;
171 bool escaped = false;
172 string const python_call = "python -tt";
173 vector<string> outcmd(4);
176 if (prefixIs(incmd, python_call)) {
177 outcmd[0] = os::python();
178 start = python_call.length();
181 for (size_t i = start, o = 0; i < incmd.length(); ++i) {
184 if (in_double_quote || escaped) {
185 if (in_double_quote && escaped)
189 in_single_quote = !in_single_quote;
193 if (in_single_quote) {
199 // Don't triple double-quotes for redirection
200 // files as these won't be parsed by QProcess
201 outcmd[o] += string(o ? "\"" : "\"\"\"");
205 in_double_quote = !in_double_quote;
207 } else if (c == '\\' && !escaped) {
209 } else if (c == '>' && !(in_double_quote || escaped)) {
210 if (suffixIs(outcmd[o], " 2")) {
211 outcmd[o] = rtrim(outcmd[o], "2");
214 if (suffixIs(outcmd[o], " 1"))
215 outcmd[o] = rtrim(outcmd[o], "1");
218 } else if (c == '<' && !(in_double_quote || escaped)) {
221 if (escaped && in_double_quote)
227 infile = trim(outcmd[3], " \"");
228 outfile = trim(outcmd[1], " \"");
229 errfile = trim(outcmd[2], " \"");
230 return trim(outcmd[0]);
236 void Systemcall::killscript()
238 SystemcallPrivate::kill_script = true;
242 int Systemcall::startscript(Starttype how, string const & what,
243 string const & path, string const & lpath,
246 string const what_ss = commandPrep(what);
248 lyxerr << "\nRunning: " << what_ss << endl;
250 LYXERR(Debug::INFO,"Running: " << what_ss);
255 QString const cmd = QString::fromLocal8Bit(
256 parsecmd(what_ss, infile, outfile, errfile).c_str());
258 SystemcallPrivate d(infile, outfile, errfile);
259 bool do_events = process_events || how == WaitLoop;
261 d.startProcess(cmd, path, lpath, how == DontWait);
262 if (how == DontWait && d.state == SystemcallPrivate::Running)
265 if (d.state == SystemcallPrivate::Error
266 || !d.waitWhile(SystemcallPrivate::Starting, do_events, -1)) {
267 if (d.state == SystemcallPrivate::Error) {
268 LYXERR0("Systemcall: '" << cmd << "' did not start!");
269 LYXERR0("error " << d.errorMessage());
271 } else if (d.state == SystemcallPrivate::Killed) {
272 LYXERR0("Killed: " << cmd);
277 if (!d.waitWhile(SystemcallPrivate::Running, do_events,
278 os::timeout_min() * 60 * 1000)) {
279 if (d.state == SystemcallPrivate::Killed) {
280 LYXERR0("Killed: " << cmd);
283 LYXERR0("Systemcall: '" << cmd << "' did not finish!");
284 LYXERR0("error " << d.errorMessage());
285 LYXERR0("status " << d.exitStatusMessage());
289 int const exit_code = d.exitCode();
291 LYXERR0("Systemcall: '" << cmd << "' finished with exit code " << exit_code);
298 bool SystemcallPrivate::kill_script = false;
301 SystemcallPrivate::SystemcallPrivate(std::string const & sf, std::string const & of,
302 std::string const & ef)
303 : state(Error), process_(new QProcess), out_index_(0), err_index_(0),
304 in_file_(sf), out_file_(of), err_file_(ef), process_events_(false)
306 if (!in_file_.empty())
307 process_->setStandardInputFile(QString::fromLocal8Bit(in_file_.c_str()));
308 if (!out_file_.empty()) {
309 if (out_file_[0] == '&') {
310 if (subst(out_file_, " ", "") == "&2"
311 && err_file_[0] != '&') {
312 out_file_ = err_file_;
313 process_->setProcessChannelMode(
314 QProcess::MergedChannels);
316 if (err_file_[0] == '&') {
317 // Leave alone things such as
318 // "1>&2 2>&1". Should not be harmful,
319 // but let's give anyway a warning.
320 LYXERR0("Unsupported stdout/stderr redirect.");
323 LYXERR0("Ambiguous stdout redirect: "
326 out_file_ = os::nulldev();
329 // Check whether we have to set the output file.
330 if (out_file_ != os::nulldev()) {
331 process_->setStandardOutputFile(QString::fromLocal8Bit(
335 if (!err_file_.empty()) {
336 if (err_file_[0] == '&') {
337 if (subst(err_file_, " ", "") == "&1"
338 && out_file_[0] != '&') {
339 process_->setProcessChannelMode(
340 QProcess::MergedChannels);
342 LYXERR0("Ambiguous stderr redirect: "
345 // In MergedChannels mode stderr goes to stdout.
346 err_file_ = os::nulldev();
348 // Check whether we have to set the error file.
349 if (err_file_ != os::nulldev()) {
350 process_->setStandardErrorFile(QString::fromLocal8Bit(
355 connect(process_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
356 connect(process_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
357 #if QT_VERSION >= 0x050600
358 connect(process_, SIGNAL(errorOccurred(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
360 connect(process_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
362 connect(process_, SIGNAL(started()), this, SLOT(processStarted()));
363 connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
367 void SystemcallPrivate::startProcess(QString const & cmd, string const & path,
368 string const & lpath, bool detached)
371 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
372 // FIXME pass command and arguments separated in the first place
373 /* The versions of startDetached() and start() that accept a
374 * QStringList object exist since Qt4, but it is only in Qt 5.15
375 * that splitCommand() was introduced and the plain versions of
376 * start/startDetached() have been deprecated.
377 * The cleanest solution would be to have parsecmd() produce a
378 * QStringList for arguments, instead of transforming the string
379 * into something that the QProcess splitter accepts.
381 QStringList arguments = QProcess::splitCommand(toqstr(latexEnvCmdPrefix(path, lpath)) + cmd_);
382 QString command = (arguments.empty()) ? QString() : arguments.first();
383 if (arguments.size() == 1)
385 else if (!arguments.empty())
386 arguments.removeFirst();
389 state = SystemcallPrivate::Running;
391 // Avoid opening a console window when a viewer is started
392 if (in_file_.empty())
393 process_->setStandardInputFile(QProcess::nullDevice());
394 if (out_file_.empty())
395 process_->setStandardOutputFile(QProcess::nullDevice());
396 if (err_file_.empty())
397 process_->setStandardErrorFile(QProcess::nullDevice());
399 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
400 if (!QProcess::startDetached(command, arguments)) {
402 if (!QProcess::startDetached(toqstr(latexEnvCmdPrefix(path, lpath)) + cmd_)) {
404 state = SystemcallPrivate::Error;
407 QProcess* released = releaseProcess();
409 } else if (process_) {
410 state = SystemcallPrivate::Starting;
411 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
412 process_->start(command, arguments);
414 process_->start(toqstr(latexEnvCmdPrefix(path, lpath)) + cmd_);
420 bool SystemcallPrivate::waitAndCheck()
422 Sleep::millisec(100);
424 // is there a better place to reset this?
428 LYXERR0("Export Canceled!!");
431 QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
438 bool queryStopCommand(QString const & cmd)
440 docstring text = bformat(_(
441 "The command\n%1$s\nhas not yet completed.\n\n"
442 "Do you want to stop it?"), qstring_to_ucs4(cmd));
443 return ProgressInterface::instance()->prompt(_("Stop command?"), text,
444 1, 1, _("&Stop it"), _("Let it &run")) == 0;
450 bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int timeout)
455 bool timedout = false;
456 process_events_ = process_events;
458 // Block GUI while waiting,
459 // relay on QProcess' wait functions
460 if (!process_events_) {
461 if (waitwhile == Starting)
462 return process_->waitForStarted(timeout);
463 if (waitwhile == Running) {
466 if (process_->waitForFinished(timeout))
468 bool const stop = queryStopCommand(cmd_);
469 // The command may have finished in the meantime
470 if (process_->state() == QProcess::NotRunning)
484 // process events while waiting, no timeout
486 while (state == waitwhile && state != Error) {
487 // check for cancellation of background process
491 return state != Error;
494 // process events while waiting with timeout
497 while (state == waitwhile && state != Error && !timedout) {
498 // check for cancellation of background process
502 if (timer.elapsed() > timeout) {
503 bool const stop = queryStopCommand(cmd_);
504 // The command may have finished in the meantime
505 if (process_->state() == QProcess::NotRunning)
514 return (state != Error) && !timedout;
518 SystemcallPrivate::~SystemcallPrivate()
521 out_data_[out_index_] = '\0';
527 err_data_[err_index_] = '\0';
537 void SystemcallPrivate::stdOut()
541 process_->setReadChannel(QProcess::StandardOutput);
542 while (process_->getChar(&c)) {
543 out_data_[out_index_++] = c;
544 if (c == '\n' || out_index_ + 1 == buffer_size_) {
545 out_data_[out_index_] = '\0';
547 ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(out_data_));
555 void SystemcallPrivate::stdErr()
559 process_->setReadChannel(QProcess::StandardError);
560 while (process_->getChar(&c)) {
561 err_data_[err_index_++] = c;
562 if (c == '\n' || err_index_ + 1 == buffer_size_) {
563 err_data_[err_index_] = '\0';
565 ProgressInterface::instance()->appendError(QString::fromLocal8Bit(err_data_));
573 void SystemcallPrivate::processStarted()
575 if (state != Running) {
577 ProgressInterface::instance()->processStarted(cmd_);
582 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
584 if (state != Finished) {
586 ProgressInterface::instance()->processFinished(cmd_);
591 void SystemcallPrivate::processError(QProcess::ProcessError)
594 ProgressInterface::instance()->appendError(errorMessage());
598 QString SystemcallPrivate::errorMessage() const
601 return "No QProcess available";
604 switch (process_->error()) {
605 case QProcess::FailedToStart:
606 message = "The process failed to start. Either the invoked program is missing, "
607 "or you may have insufficient permissions to invoke the program.";
609 case QProcess::Crashed:
610 message = "The process crashed some time after starting successfully.";
612 case QProcess::Timedout:
613 message = "The process timed out. It might be restarted automatically.";
615 case QProcess::WriteError:
616 message = "An error occurred when attempting to write to the process-> For example, "
617 "the process may not be running, or it may have closed its input channel.";
619 case QProcess::ReadError:
620 message = "An error occurred when attempting to read from the process-> For example, "
621 "the process may not be running.";
623 case QProcess::UnknownError:
625 message = "An unknown error occurred.";
632 QString SystemcallPrivate::exitStatusMessage() const
635 return "No QProcess available";
638 switch (process_->exitStatus()) {
639 case QProcess::NormalExit:
640 message = "The process exited normally.";
642 case QProcess::CrashExit:
643 message = "The process crashed.";
646 message = "Unknown exit state.";
653 int SystemcallPrivate::exitCode()
655 // From Qt's documentation, in regards to QProcess::exitCode(),
656 // "This value is not valid unless exitStatus() returns NormalExit"
657 if (!process_ || process_->exitStatus() != QProcess::NormalExit)
660 return process_->exitCode();
664 QProcess* SystemcallPrivate::releaseProcess()
666 QProcess* released = process_;
672 void SystemcallPrivate::killProcess()
674 killProcess(process_);
678 void SystemcallPrivate::killProcess(QProcess * p)
682 p->closeReadChannel(QProcess::StandardOutput);
683 p->closeReadChannel(QProcess::StandardError);
691 #include "moc_SystemcallPrivate.cpp"
694 } // namespace support