]> git.lyx.org Git - lyx.git/blob - src/support/Systemcall.cpp
Typo.
[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
109         if (prefixIs(what, python_call))
110                 command += os::python() + what.substr(python_call.length());
111         else
112                 command += 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 anon
237
238
239
240 int Systemcall::startscript(Starttype how, string const & what,
241                             string const & path, bool process_events)
242 {
243         lyxerr << "\nRunning: " << what << endl;
244
245         string infile;
246         string outfile;
247         string errfile;
248         QString cmd = QString::fromLocal8Bit(
249                         parsecmd(what, infile, outfile, errfile).c_str());
250
251         SystemcallPrivate d(infile, outfile, errfile);
252
253
254         d.startProcess(cmd, path);
255         if (!d.waitWhile(SystemcallPrivate::Starting, process_events, -1)) {
256                 LYXERR0("Systemcall: '" << cmd << "' did not start!");
257                 LYXERR0("error " << d.errorMessage());
258                 return 10;
259         }
260
261         if (how == DontWait) {
262                 QProcess* released = d.releaseProcess();
263                 (void) released; // TODO who deletes it?
264                 return 0;
265         }
266
267         if (!d.waitWhile(SystemcallPrivate::Running, process_events,
268                          os::timeout_min() * 60 * 1000)) {
269                 LYXERR0("Systemcall: '" << cmd << "' did not finish!");
270                 LYXERR0("error " << d.errorMessage());
271                 LYXERR0("status " << d.exitStatusMessage());
272                 return 20;
273         }
274
275         int const exit_code = d.exitCode();
276         if (exit_code) {
277                 LYXERR0("Systemcall: '" << cmd << "' finished with exit code " << exit_code);
278         }
279
280         return exit_code;
281 }
282
283
284 SystemcallPrivate::SystemcallPrivate(std::string const & sf,
285                                      std::string const & of,
286                                      std::string const & ef) :
287                                 process_(new QProcess), 
288                                 out_index_(0),
289                                 err_index_(0),
290                                 in_file_(sf),
291                                 out_file_(of), 
292                                 err_file_(ef), 
293                                 process_events_(false)
294 {
295         if (!in_file_.empty())
296                 process_->setStandardInputFile(QString::fromLocal8Bit(in_file_.c_str()));
297         if (!out_file_.empty()) {
298                 if (out_file_[0] == '&') {
299                         if (subst(out_file_, " ", "") == "&2"
300                             && err_file_[0] != '&') {
301                                 out_file_ = err_file_;
302                                 process_->setProcessChannelMode(
303                                                 QProcess::MergedChannels);
304                         } else {
305                                 if (err_file_[0] == '&') {
306                                         // Leave alone things such as
307                                         // "1>&2 2>&1". Should not be harmful,
308                                         // but let's give anyway a warning.
309                                         LYXERR0("Unsupported stdout/stderr redirect.");
310                                         err_file_.erase();
311                                 } else {
312                                         LYXERR0("Ambiguous stdout redirect: "
313                                                 << out_file_);
314                                 }
315                                 out_file_ = os::nulldev();
316                         }
317                 }
318                 // Check whether we have to set the output file.
319                 if (out_file_ != os::nulldev()) {
320                         process_->setStandardOutputFile(QString::fromLocal8Bit(
321                                                         out_file_.c_str()));
322                 }
323         }
324         if (!err_file_.empty()) {
325                 if (err_file_[0] == '&') {
326                         if (subst(err_file_, " ", "") == "&1"
327                             && out_file_[0] != '&') {
328                                 process_->setProcessChannelMode(
329                                                 QProcess::MergedChannels);
330                         } else {
331                                 LYXERR0("Ambiguous stderr redirect: "
332                                         << err_file_);
333                         }
334                         // In MergedChannels mode stderr goes to stdout.
335                         err_file_ = os::nulldev();
336                 }
337                 // Check whether we have to set the error file.
338                 if (err_file_ != os::nulldev()) {
339                         process_->setStandardErrorFile(QString::fromLocal8Bit(
340                                                         err_file_.c_str()));
341                 }
342         }
343
344         connect(process_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
345         connect(process_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
346         connect(process_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
347         connect(process_, SIGNAL(started()), this, SLOT(processStarted()));
348         connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
349 }
350
351
352 void SystemcallPrivate::startProcess(QString const & cmd, string const & path)
353 {
354         cmd_ = cmd;
355         if (process_) {
356                 state = SystemcallPrivate::Starting;
357                 process_->start(toqstr(latexEnvCmdPrefix(path)) + cmd_);
358         }
359 }
360
361
362 void SystemcallPrivate::processEvents()
363 {
364         if (process_events_) {
365                 QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
366         }
367 }
368
369
370 void SystemcallPrivate::waitAndProcessEvents()
371 {
372         Sleep::millisec(100);
373         processEvents();
374 }
375
376
377 namespace {
378
379 bool queryStopCommand(QString const & cmd)
380 {
381         docstring text = bformat(_(
382                 "The command\n%1$s\nhas not yet completed.\n\n"
383                 "Do you want to stop it?"), qstring_to_ucs4(cmd));
384         return ProgressInterface::instance()->prompt(_("Stop command?"), text,
385                         1, 1, _("&Stop it"), _("Let it &run")) == 0;
386 }
387
388 }
389
390
391 bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int timeout)
392 {
393         if (!process_)
394                 return false;
395
396         bool timedout = false;
397         process_events_ = process_events;
398
399         // Block GUI while waiting,
400         // relay on QProcess' wait functions
401         if (!process_events_) {
402                 if (waitwhile == Starting)
403                         return process_->waitForStarted(timeout);
404                 if (waitwhile == Running) {
405                         int bump = 2;
406                         while (!timedout) {
407                                 if (process_->waitForFinished(timeout))
408                                         return true;
409                                 bool stop = queryStopCommand(cmd_);
410                                 // The command may have finished in the meantime
411                                 if (process_->state() == QProcess::NotRunning)
412                                         return true;
413                                 if (stop) {
414                                         timedout = true;
415                                         process_->kill();
416                                 } else {
417                                         timeout *= bump;
418                                         bump = 3;
419                                 }
420                         }
421                 }
422                 return false;
423         }
424
425         // process events while waiting, no timeout
426         if (timeout == -1) {
427                 while (state == waitwhile && state != Error) {
428                         waitAndProcessEvents();
429                 }
430                 return state != Error;
431         } 
432
433         // process events while waiting whith timeout
434         QTime timer;
435         timer.start();
436         while (state == waitwhile && state != Error && !timedout) {
437                 waitAndProcessEvents();
438                 if (timer.elapsed() > timeout) {
439                         bool stop = queryStopCommand(cmd_);
440                         // The command may have finished in the meantime
441                         if (process_->state() == QProcess::NotRunning)
442                                 break;
443                         if (stop) {
444                                 timedout = true;
445                                 process_->kill();
446                         } else
447                                 timeout *= 3;
448                 }
449         }
450         return (state != Error) && !timedout;
451 }
452
453
454 SystemcallPrivate::~SystemcallPrivate()
455 {
456         if (out_index_) {
457                 out_data_[out_index_] = '\0';
458                 out_index_ = 0;
459                 cout << out_data_;
460         }
461         cout.flush();
462         if (err_index_) {
463                 err_data_[err_index_] = '\0';
464                 err_index_ = 0;
465                 cerr << err_data_;
466         }
467         cerr.flush();
468
469         killProcess();
470 }
471
472
473 void SystemcallPrivate::stdOut()
474 {
475         if (process_) {
476                 char c;
477                 process_->setReadChannel(QProcess::StandardOutput);
478                 while (process_->getChar(&c)) {
479                         out_data_[out_index_++] = c;
480                         if (c == '\n' || out_index_ + 1 == buffer_size_) {
481                                 out_data_[out_index_] = '\0';
482                                 out_index_ = 0;
483                                 ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(out_data_));
484                                 cout << out_data_;
485                         }
486                 }
487         }
488 }
489
490
491 void SystemcallPrivate::stdErr()
492 {
493         if (process_) {
494                 char c;
495                 process_->setReadChannel(QProcess::StandardError);
496                 while (process_->getChar(&c)) {
497                         err_data_[err_index_++] = c;
498                         if (c == '\n' || err_index_ + 1 == buffer_size_) {
499                                 err_data_[err_index_] = '\0';
500                                 err_index_ = 0;
501                                 ProgressInterface::instance()->appendError(QString::fromLocal8Bit(err_data_));
502                                 cerr << err_data_;
503                         }
504                 }
505         }
506 }
507
508
509 void SystemcallPrivate::processStarted()
510 {
511         if (state != Running) {
512                 state = Running;
513                 ProgressInterface::instance()->processStarted(cmd_);
514         }
515 }
516
517
518 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
519 {
520         if (state != Finished) {
521                 state = Finished;
522                 ProgressInterface::instance()->processFinished(cmd_);
523         }
524 }
525
526
527 void SystemcallPrivate::processError(QProcess::ProcessError)
528 {
529         state = Error;
530         ProgressInterface::instance()->appendError(errorMessage());
531 }
532
533
534 QString SystemcallPrivate::errorMessage() const 
535 {
536         if (!process_)
537                 return "No QProcess available";
538
539         QString message;
540         switch (process_->error()) {
541                 case QProcess::FailedToStart:
542                         message = "The process failed to start. Either the invoked program is missing, "
543                                       "or you may have insufficient permissions to invoke the program.";
544                         break;
545                 case QProcess::Crashed:
546                         message = "The process crashed some time after starting successfully.";
547                         break;
548                 case QProcess::Timedout:
549                         message = "The process timed out. It might be restarted automatically.";
550                         break;
551                 case QProcess::WriteError:
552                         message = "An error occurred when attempting to write to the process-> For example, "
553                                       "the process may not be running, or it may have closed its input channel.";
554                         break;
555                 case QProcess::ReadError:
556                         message = "An error occurred when attempting to read from the process-> For example, "
557                                       "the process may not be running.";
558                         break;
559                 case QProcess::UnknownError:
560                 default:
561                         message = "An unknown error occured.";
562                         break;
563         }
564         return message;
565 }
566
567
568 QString SystemcallPrivate::exitStatusMessage() const
569 {
570         if (!process_)
571                 return "No QProcess available";
572
573         QString message;
574         switch (process_->exitStatus()) {
575                 case QProcess::NormalExit:
576                         message = "The process exited normally.";
577                         break;
578                 case QProcess::CrashExit:
579                         message = "The process crashed.";
580                         break;
581                 default:
582                         message = "Unknown exit state.";
583                         break;
584         }
585         return message;
586 }
587
588
589 int SystemcallPrivate::exitCode()
590 {
591         if (!process_)
592                 return -1;
593
594         return process_->exitCode();
595 }
596
597
598 QProcess* SystemcallPrivate::releaseProcess()
599 {
600         QProcess* released = process_;
601         process_ = 0;
602         return released;
603 }
604
605
606 void SystemcallPrivate::killProcess()
607 {
608         killProcess(process_);
609 }
610
611
612 void SystemcallPrivate::killProcess(QProcess * p)
613 {
614         if (p) {
615                 p->disconnect();
616                 p->closeReadChannel(QProcess::StandardOutput);
617                 p->closeReadChannel(QProcess::StandardError);
618                 p->close();
619                 delete p;
620         }
621 }
622
623
624
625 #include "moc_SystemcallPrivate.cpp"
626 #endif
627
628 } // namespace support
629 } // namespace lyx