]> git.lyx.org Git - lyx.git/blobdiff - src/support/Systemcall.cpp
Create mechanism to handle packages for which we only know after parsing
[lyx.git] / src / support / Systemcall.cpp
index a42e7abef46be21081e2a2e28eec218e52a6c459..bc7790adb17cdd8f8dbd4f123836e6bb5610a09d 100644 (file)
@@ -14,6 +14,7 @@
 #include <config.h>
 
 #include "support/debug.h"
+#include "support/filetools.h"
 #include "support/lstrings.h"
 #include "support/qstring_helpers.h"
 #include "support/Systemcall.h"
@@ -21,6 +22,7 @@
 #include "support/os.h"
 #include "support/ProgressInterface.h"
 
+#include "LyXRC.h"
 
 #include <cstdlib>
 #include <iostream>
@@ -96,9 +98,15 @@ ProgressInterface* ProgressInterface::instance()
 // Reuse of instance
 #ifndef USE_QPROCESS
 int Systemcall::startscript(Starttype how, string const & what,
-                                                       bool /*process_events*/)
+                           std::string const & path, bool /*process_events*/)
 {
-       string command = what;
+       string const python_call = "python -tt";
+       string command = to_filesystem8bit(from_utf8(latexEnvCmdPrefix(path)));
+
+       if (prefixIs(what, python_call))
+               command += os::python() + what.substr(python_call.length());
+       else
+               command += what;
 
        if (how == DontWait) {
                switch (os::shell()) {
@@ -109,7 +117,8 @@ int Systemcall::startscript(Starttype how, string const & what,
                        command = "start /min " + command;
                        break;
                }
-       }
+       } else if (os::shell() == os::CMD_EXE)
+               command = subst(command, "cmd /d /c ", "");
 
        return ::system(command.c_str());
 }
@@ -158,9 +167,16 @@ string const parsecmd(string const & inputcmd, string & outfile)
        bool in_single_quote = false;
        bool in_double_quote = false;
        bool escaped = false;
+       string const python_call = "python -tt";
        string cmd;
+       int start = 0;
+
+       if (prefixIs(inputcmd, python_call)) {
+               cmd = os::python();
+               start = python_call.length();
+       }
 
-       for (size_t i = 0; i < inputcmd.length(); ++i) {
+       for (size_t i = start; i < inputcmd.length(); ++i) {
                char c = inputcmd[i];
                if (c == '\'') {
                        if (in_double_quote || escaped) {
@@ -204,15 +220,16 @@ string const parsecmd(string const & inputcmd, string & outfile)
 
 
 
-int Systemcall::startscript(Starttype how, string const & what, bool process_events)
+int Systemcall::startscript(Starttype how, string const & what,
+                           string const & path, bool process_events)
 {
        string outfile;
-       QString cmd = toqstr(parsecmd(what, outfile));
+       QString cmd = QString::fromLocal8Bit(parsecmd(what, outfile).c_str());
 
        SystemcallPrivate d(outfile);
 
 
-       d.startProcess(cmd);
+       d.startProcess(cmd, path);
        if (!d.waitWhile(SystemcallPrivate::Starting, process_events, -1)) {
                LYXERR0("Systemcall: '" << cmd << "' did not start!");
                LYXERR0("error " << d.errorMessage());
@@ -225,8 +242,9 @@ int Systemcall::startscript(Starttype how, string const & what, bool process_eve
                return 0;
        }
 
-       if (!d.waitWhile(SystemcallPrivate::Running, process_events, 180000)) {
-               LYXERR0("Systemcall: '" << cmd << "' did not finished!");
+       if (!d.waitWhile(SystemcallPrivate::Running, process_events,
+                        os::timeout_min() * 60 * 1000)) {
+               LYXERR0("Systemcall: '" << cmd << "' did not finish!");
                LYXERR0("error " << d.errorMessage());
                LYXERR0("status " << d.exitStatusMessage());
                return 20;
@@ -243,17 +261,15 @@ int Systemcall::startscript(Starttype how, string const & what, bool process_eve
 
 SystemcallPrivate::SystemcallPrivate(const std::string& of) :
                                 process_(new QProcess), 
-                                outIndex_(0),
-                                errIndex_(0),
-                                outFile_(of), 
-                                terminalOutExists_(os::is_terminal(os::STDOUT)),
-                                terminalErrExists_(os::is_terminal(os::STDERR)),
-                                processEvents_(false)
+                                out_index_(0),
+                                err_index_(0),
+                                out_file_(of), 
+                                process_events_(false)
 {
-       if (!outFile_.empty()) {
+       if (!out_file_.empty()) {
                // Check whether we have to simply throw away the output.
-               if (outFile_ != os::nulldev())
-                       process_->setStandardOutputFile(toqstr(outFile_));
+               if (out_file_ != os::nulldev())
+                       process_->setStandardOutputFile(QString::fromLocal8Bit(out_file_.c_str()));
        }
 
        connect(process_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut()));
@@ -264,20 +280,19 @@ SystemcallPrivate::SystemcallPrivate(const std::string& of) :
 }
 
 
-
-void SystemcallPrivate::startProcess(const QString& cmd)
+void SystemcallPrivate::startProcess(QString const & cmd, string const & path)
 {
        cmd_ = cmd;
        if (process_) {
                state = SystemcallPrivate::Starting;
-               process_->start(cmd_);
+               process_->start(toqstr(latexEnvCmdPrefix(path)) + cmd_);
        }
 }
 
 
 void SystemcallPrivate::processEvents()
 {
-       if(processEvents_) {
+       if(process_events_) {
                QCoreApplication::processEvents(/*QEventLoop::ExcludeUserInputEvents*/);
        }
 }
@@ -295,11 +310,11 @@ bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int time
        if (!process_)
                return false;
 
-       process_events = process_events;
+       process_events_ = process_events;
 
        // Block GUI while waiting,
        // relay on QProcess' wait functions
-       if (!process_events) {
+       if (!process_events_) {
                if (waitwhile == Starting)
                        return process_->waitForStarted(timeout);
                if (waitwhile == Running)
@@ -327,20 +342,16 @@ bool SystemcallPrivate::waitWhile(State waitwhile, bool process_events, int time
 
 SystemcallPrivate::~SystemcallPrivate()
 {
-       flush();
-
-       if (outIndex_) {
-               outData_[outIndex_] = '\0';
-               outIndex_ = 0;
-               if (terminalOutExists_)
-                       cout << outData_;
+       if (out_index_) {
+               out_data_[out_index_] = '\0';
+               out_index_ = 0;
+               cout << out_data_;
        }
        cout.flush();
-       if (errIndex_) {
-               errData_[errIndex_] = '\0';
-               errIndex_ = 0;
-               if (terminalErrExists_)
-                       cerr << errData_;
+       if (err_index_) {
+               err_data_[err_index_] = '\0';
+               err_index_ = 0;
+               cerr << err_data_;
        }
        cerr.flush();
 
@@ -348,39 +359,18 @@ SystemcallPrivate::~SystemcallPrivate()
 }
 
 
-void SystemcallPrivate::flush()
-{
-       if (process_) {
-               // If the output has been redirected, we write it all at once.
-               // Even if we are not running in a terminal, the output could go
-               // to some log file, for example ~/.xsession-errors on *nix.
-               
-               QString data = QString::fromLocal8Bit(process_->readAllStandardOutput().data());
-               ProgressInterface::instance()->appendMessage(data);
-               if (!terminalOutExists_ && outFile_.empty())
-                       cout << fromqstr(data);
-               
-               data = QString::fromLocal8Bit(process_->readAllStandardError().data());
-               ProgressInterface::instance()->appendError(data);
-               if (!terminalErrExists_)
-                       cerr << fromqstr(data);
-       }
-}
-
-
 void SystemcallPrivate::stdOut()
 {
        if (process_) {
                char c;
                process_->setReadChannel(QProcess::StandardOutput);
                while (process_->getChar(&c)) {
-                       outData_[outIndex_++] = c;
-                       if (c == '\n' || outIndex_ + 1 == bufferSize_) {
-                               outData_[outIndex_] = '\0';
-                               outIndex_ = 0;
-                               ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(outData_));
-                               if (terminalOutExists_)
-                                       cout << outData_;
+                       out_data_[out_index_++] = c;
+                       if (c == '\n' || out_index_ + 1 == buffer_size_) {
+                               out_data_[out_index_] = '\0';
+                               out_index_ = 0;
+                               ProgressInterface::instance()->appendMessage(QString::fromLocal8Bit(out_data_));
+                               cout << out_data_;
                        }
                }
        }
@@ -393,13 +383,12 @@ void SystemcallPrivate::stdErr()
                char c;
                process_->setReadChannel(QProcess::StandardError);
                while (process_->getChar(&c)) {
-                       errData_[errIndex_++] = c;
-                       if (c == '\n' || errIndex_ + 1 == bufferSize_) {
-                               errData_[errIndex_] = '\0';
-                               errIndex_ = 0;
-                               ProgressInterface::instance()->appendError(QString::fromLocal8Bit(errData_));
-                               if (terminalErrExists_)
-                                       cerr << errData_;
+                       err_data_[err_index_++] = c;
+                       if (c == '\n' || err_index_ + 1 == buffer_size_) {
+                               err_data_[err_index_] = '\0';
+                               err_index_ = 0;
+                               ProgressInterface::instance()->appendError(QString::fromLocal8Bit(err_data_));
+                               cerr << err_data_;
                        }
                }
        }