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