]> git.lyx.org Git - lyx.git/blob - src/support/Systemcall.cpp
4273983bd2c458229a529e13424cb86f22bc8e4c
[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/lstrings.h"
19 #include "support/qstring_helpers.h"
20 #include "support/Systemcall.h"
21 #include "support/SystemcallPrivate.h"
22 #include "support/os.h"
23 #include "support/ProgressInterface.h"
24
25 #include "LyXRC.h"
26
27 #include <cstdlib>
28 #include <iostream>
29
30 #include <QProcess>
31 #include <QTime>
32 #include <QThread>
33 #include <QCoreApplication>
34 #include <QDebug>
35
36 #define USE_QPROCESS
37
38
39 struct Sleep : QThread
40 {
41         static void millisec(unsigned long ms) 
42         {
43                 QThread::usleep(ms * 1000);
44         }
45 };
46
47
48
49
50 using namespace std;
51
52 namespace lyx {
53 namespace support {
54
55
56 class ProgressDummy : public ProgressInterface 
57 {
58 public:
59         ProgressDummy() {}
60
61         void processStarted(QString const &) {}
62         void processFinished(QString const &) {}
63         void appendMessage(QString const &) {}
64         void appendError(QString const &) {}
65         void clearMessages() {}
66         void lyxerrFlush() {}
67
68         void lyxerrConnect() {}
69         void lyxerrDisconnect() {}
70
71         void warning(QString const &, QString const &) {}
72         void toggleWarning(QString const &, QString const &, QString const &) {}
73         void error(QString const &, QString const &) {}
74         void information(QString const &, QString const &) {}
75 };
76
77
78 static ProgressInterface* progress_instance = 0;
79
80 void ProgressInterface::setInstance(ProgressInterface* p)
81 {
82         progress_instance = p;
83 }
84
85
86 ProgressInterface* ProgressInterface::instance()
87 {
88         if (!progress_instance) {
89                 static ProgressDummy dummy;
90                 return &dummy;
91         }
92         return progress_instance;
93 }
94
95
96
97
98 // Reuse of instance
99 #ifndef USE_QPROCESS
100 int Systemcall::startscript(Starttype how, string const & what,
101                             std::string const & path, bool /*process_events*/)
102 {
103         string const python_call = "python -tt";
104         string command = to_filesystem8bit(from_utf8(latexEnvCmdPrefix(path)));
105
106         if (prefixIs(what, python_call))
107                 command += os::python() + what.substr(python_call.length());
108         else
109                 command += 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 = "python -tt";
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 = !escaped;
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                 } else {
221                         if (escaped && in_double_quote)
222                                 outcmd[o] += '\\';
223                         outcmd[o] += c;
224                         escaped = false;
225                 }
226         }
227         infile  = trim(outcmd[3], " \"");
228         outfile = trim(outcmd[1], " \"");
229         errfile = trim(outcmd[2], " \"");
230         return trim(outcmd[0]);
231 }
232
233 } // namespace anon
234
235
236
237 int Systemcall::startscript(Starttype how, string const & what,
238                             string const & path, bool process_events)
239 {
240         lyxerr << "\nRunning: " << what << endl;
241
242         string infile;
243         string outfile;
244         string errfile;
245         QString cmd = QString::fromLocal8Bit(
246                         parsecmd(what, infile, outfile, errfile).c_str());
247
248         SystemcallPrivate d(infile, outfile, errfile);
249
250
251         d.startProcess(cmd, path);
252         if (!d.waitWhile(SystemcallPrivate::Starting, process_events, -1)) {
253                 LYXERR0("Systemcall: '" << cmd << "' did not start!");
254                 LYXERR0("error " << d.errorMessage());
255                 return 10;
256         }
257
258         if (how == DontWait) {
259                 QProcess* released = d.releaseProcess();
260                 (void) released; // TODO who deletes it?
261                 return 0;
262         }
263
264         if (!d.waitWhile(SystemcallPrivate::Running, process_events,
265                          os::timeout_min() * 60 * 1000)) {
266                 LYXERR0("Systemcall: '" << cmd << "' did not finish!");
267                 LYXERR0("error " << d.errorMessage());
268                 LYXERR0("status " << d.exitStatusMessage());
269                 return 20;
270         }
271
272         int const exit_code = d.exitCode();
273         if (exit_code) {
274                 LYXERR0("Systemcall: '" << cmd << "' finished with exit code " << exit_code);
275         }
276
277         return exit_code;
278 }
279
280
281 SystemcallPrivate::SystemcallPrivate(std::string const & sf,
282                                      std::string const & of,
283                                      std::string const & ef) :
284                                 process_(new QProcess), 
285                                 out_index_(0),
286                                 err_index_(0),
287                                 in_file_(sf),
288                                 out_file_(of), 
289                                 err_file_(ef), 
290                                 process_events_(false)
291 {
292         if (!in_file_.empty())
293                 process_->setStandardInputFile(QString::fromLocal8Bit(in_file_.c_str()));
294         if (!out_file_.empty()) {
295                 if (out_file_[0] == '&') {
296                         if (subst(out_file_, " ", "") == "&2"
297                             && err_file_[0] != '&') {
298                                 out_file_ = err_file_;
299                                 process_->setProcessChannelMode(
300                                                 QProcess::MergedChannels);
301                         } else {
302                                 if (err_file_[0] == '&') {
303                                         // Leave alone things such as
304                                         // "1>&2 2>&1". Should not be harmful,
305                                         // but let's give anyway a warning.
306                                         LYXERR0("Unsupported stdout/stderr redirect.");
307                                         err_file_.erase();
308                                 } else {
309                                         LYXERR0("Ambiguous stdout redirect: "
310                                                 << out_file_);
311                                 }
312                                 out_file_ = os::nulldev();
313                         }
314                 }
315                 // Check whether we have to set the output file.
316                 if (out_file_ != os::nulldev()) {
317                         process_->setStandardOutputFile(QString::fromLocal8Bit(
318                                                         out_file_.c_str()));
319                 }
320         }
321         if (!err_file_.empty()) {
322                 if (err_file_[0] == '&') {
323                         if (subst(err_file_, " ", "") == "&1"
324                             && out_file_[0] != '&') {
325                                 process_->setProcessChannelMode(
326                                                 QProcess::MergedChannels);
327                         } else {
328                                 LYXERR0("Ambiguous stderr redirect: "
329                                         << err_file_);
330                         }
331                         // In MergedChannels mode stderr goes to stdout.
332                         err_file_ = os::nulldev();
333                 }
334                 // Check whether we have to set the error file.
335                 if (err_file_ != os::nulldev()) {
336                         process_->setStandardErrorFile(QString::fromLocal8Bit(
337                                                         err_file_.c_str()));
338                 }
339         }
340
341         connect(process_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
342         connect(process_, SIGNAL(readyReadStandardError()), SLOT(stdErr()));
343         connect(process_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
344         connect(process_, SIGNAL(started()), this, SLOT(processStarted()));
345         connect(process_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
346 }
347
348
349 void SystemcallPrivate::startProcess(QString const & cmd, string const & path)
350 {
351         cmd_ = cmd;
352         if (process_) {
353                 state = SystemcallPrivate::Starting;
354                 process_->start(toqstr(latexEnvCmdPrefix(path)) + cmd_);
355         }
356 }
357
358
359 void SystemcallPrivate::processEvents()
360 {
361         if(process_events_) {
362                 QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
363         }
364 }
365
366
367 void SystemcallPrivate::waitAndProcessEvents()
368 {
369         Sleep::millisec(100);
370         processEvents();
371 }
372
373
374 bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int timeout)
375 {
376         if (!process_)
377                 return false;
378
379         process_events_ = process_events;
380
381         // Block GUI while waiting,
382         // relay on QProcess' wait functions
383         if (!process_events_) {
384                 if (waitwhile == Starting)
385                         return process_->waitForStarted(timeout);
386                 if (waitwhile == Running)
387                         return process_->waitForFinished(timeout);
388                 return false;
389         }
390
391         // process events while waiting, no timeout
392         if (timeout == -1) {
393                 while (state == waitwhile && state != Error) {
394                         waitAndProcessEvents();
395                 }
396                 return state != Error;
397         } 
398
399         // process events while waiting whith timeout
400         QTime timer;
401         timer.start();
402         while (state == waitwhile && state != Error && timer.elapsed() < timeout) {
403                 waitAndProcessEvents();
404         }
405         return (state != Error) && (timer.elapsed() < timeout);
406 }
407
408
409 SystemcallPrivate::~SystemcallPrivate()
410 {
411         if (out_index_) {
412                 out_data_[out_index_] = '\0';
413                 out_index_ = 0;
414                 cout << out_data_;
415         }
416         cout.flush();
417         if (err_index_) {
418                 err_data_[err_index_] = '\0';
419                 err_index_ = 0;
420                 cerr << err_data_;
421         }
422         cerr.flush();
423
424         killProcess();
425 }
426
427
428 void SystemcallPrivate::stdOut()
429 {
430         if (process_) {
431                 char c;
432                 process_->setReadChannel(QProcess::StandardOutput);
433                 while (process_->getChar(&c)) {
434                         out_data_[out_index_++] = c;
435                         if (c == '\n' || out_index_ + 1 == buffer_size_) {
436                                 out_data_[out_index_] = '\0';
437                                 out_index_ = 0;
438                                 ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(out_data_));
439                                 cout << out_data_;
440                         }
441                 }
442         }
443 }
444
445
446 void SystemcallPrivate::stdErr()
447 {
448         if (process_) {
449                 char c;
450                 process_->setReadChannel(QProcess::StandardError);
451                 while (process_->getChar(&c)) {
452                         err_data_[err_index_++] = c;
453                         if (c == '\n' || err_index_ + 1 == buffer_size_) {
454                                 err_data_[err_index_] = '\0';
455                                 err_index_ = 0;
456                                 ProgressInterface::instance()->appendError(QString::fromLocal8Bit(err_data_));
457                                 cerr << err_data_;
458                         }
459                 }
460         }
461 }
462
463
464 void SystemcallPrivate::processStarted()
465 {
466         if (state != Running) {
467                 state = Running;
468                 ProgressInterface::instance()->processStarted(cmd_);
469         }
470 }
471
472
473 void SystemcallPrivate::processFinished(int, QProcess::ExitStatus)
474 {
475         if (state != Finished) {
476                 state = Finished;
477                 ProgressInterface::instance()->processFinished(cmd_);
478         }
479 }
480
481
482 void SystemcallPrivate::processError(QProcess::ProcessError)
483 {
484         state = Error;
485         ProgressInterface::instance()->appendError(errorMessage());
486 }
487
488
489 QString SystemcallPrivate::errorMessage() const 
490 {
491         if (!process_)
492                 return "No QProcess available";
493
494         QString message;
495         switch (process_->error()) {
496                 case QProcess::FailedToStart:
497                         message = "The process failed to start. Either the invoked program is missing, "
498                                       "or you may have insufficient permissions to invoke the program.";
499                         break;
500                 case QProcess::Crashed:
501                         message = "The process crashed some time after starting successfully.";
502                         break;
503                 case QProcess::Timedout:
504                         message = "The process timed out. It might be restarted automatically.";
505                         break;
506                 case QProcess::WriteError:
507                         message = "An error occurred when attempting to write to the process-> For example, "
508                                       "the process may not be running, or it may have closed its input channel.";
509                         break;
510                 case QProcess::ReadError:
511                         message = "An error occurred when attempting to read from the process-> For example, "
512                                       "the process may not be running.";
513                         break;
514                 case QProcess::UnknownError:
515                 default:
516                         message = "An unknown error occured.";
517                         break;
518         }
519         return message;
520 }
521
522
523 QString SystemcallPrivate::exitStatusMessage() const
524 {
525         if (!process_)
526                 return "No QProcess available";
527
528         QString message;
529         switch (process_->exitStatus()) {
530                 case QProcess::NormalExit:
531                         message = "The process exited normally.";
532                         break;
533                 case QProcess::CrashExit:
534                         message = "The process crashed.";
535                         break;
536                 default:
537                         message = "Unknown exit state.";
538                         break;
539         }
540         return message;
541 }
542
543
544 int SystemcallPrivate::exitCode()
545 {
546         if (!process_)
547                 return -1;
548
549         return process_->exitCode();
550 }
551
552
553 QProcess* SystemcallPrivate::releaseProcess()
554 {
555         QProcess* released = process_;
556         process_ = 0;
557         return released;
558 }
559
560
561 void SystemcallPrivate::killProcess()
562 {
563         killProcess(process_);
564 }
565
566
567 void SystemcallPrivate::killProcess(QProcess * p)
568 {
569         if (p) {
570                 p->disconnect();
571                 p->closeReadChannel(QProcess::StandardOutput);
572                 p->closeReadChannel(QProcess::StandardError);
573                 p->close();
574                 delete p;
575         }
576 }
577
578
579
580 #include "moc_SystemcallPrivate.cpp"
581 #endif
582
583 } // namespace support
584 } // namespace lyx