X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Fsupport%2FSystemcall.cpp;h=3cb420bb518ebd1ccd106206e81c2dae48ed815e;hb=68957712dd4228a76b14611108fb61d926071e1f;hp=499994b34c57ef4f11c023b9c511b24699f1087e;hpb=a0bdf5e6667ca2998a20861d943916254ae8af0e;p=lyx.git diff --git a/src/support/Systemcall.cpp b/src/support/Systemcall.cpp index 499994b34c..3cb420bb51 100644 --- a/src/support/Systemcall.cpp +++ b/src/support/Systemcall.cpp @@ -28,7 +28,7 @@ #include #include #include - +#include #define USE_QPROCESS @@ -48,22 +48,12 @@ using namespace std; namespace lyx { namespace support { -static void killProcess(QProcess * p) -{ - p->disconnect(); - p->closeReadChannel(QProcess::StandardOutput); - p->closeReadChannel(QProcess::StandardError); - p->close(); - delete p; -} - - - // Reuse of instance #ifndef USE_QPROCESS -int Systemcall::startscript(Starttype how, string const & what) +int Systemcall::startscript(Starttype how, string const & what, + bool /*process_events*/) { string command = what; @@ -110,95 +100,99 @@ string const parsecmd(string const & cmd, string & outfile) -int Systemcall::startscript(Starttype how, string const & what) +int Systemcall::startscript(Starttype how, string const & what, bool process_events) { string outfile; QString cmd = toqstr(parsecmd(what, outfile)); - QProcess * process = new QProcess; - SystemcallPrivate d(process); - if (!outfile.empty()) { - // Check whether we have to simply throw away the output. - if (outfile != os::nulldev()) - process->setStandardOutputFile(toqstr(outfile)); - } else if (os::is_terminal(os::STDOUT)) - d.showout(); - if (os::is_terminal(os::STDERR)) - d.showerr(); - + SystemcallPrivate d(outfile); + - bool processEvents = false; d.startProcess(cmd); - if (!d.waitWhile(SystemcallPrivate::Starting, processEvents, 3000)) { - LYXERR0("QProcess " << cmd << " did not start!"); + if (!d.waitWhile(SystemcallPrivate::Starting, process_events, -1)) { + LYXERR0("Systemcall: '" << cmd << " did not start!"); LYXERR0("error " << d.errorMessage()); return 10; } if (how == DontWait) { - // TODO delete process later + QProcess* released = d.releaseProcess(); + (void) released; // TODO who deletes it? return 0; } - if (!d.waitWhile(SystemcallPrivate::Running, processEvents, 180000)) { - LYXERR0("QProcess " << cmd << " did not finished!"); + if (!d.waitWhile(SystemcallPrivate::Running, process_events, 180000)) { + LYXERR0("Systemcall: '" << cmd << "' did not finished!"); LYXERR0("error " << d.errorMessage()); LYXERR0("status " << d.exitStatusMessage()); return 20; } - int const exit_code = process->exitCode(); + int const exit_code = d.exitCode(); if (exit_code) { - LYXERR0("QProcess " << cmd << " finished!"); - LYXERR0("error " << exit_code << ": " << d.errorMessage()); + LYXERR0("Systemcall: '" << cmd << "' finished with exit code " << exit_code); } - // 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. - if (!os::is_terminal(os::STDOUT) && outfile.empty()) - cout << fromqstr(QString::fromLocal8Bit( - process->readAllStandardOutput().data())); - if (!os::is_terminal(os::STDERR)) - cerr << fromqstr(QString::fromLocal8Bit( - process->readAllStandardError().data())); - - killProcess(process); - return exit_code; } -SystemcallPrivate::SystemcallPrivate(QProcess * proc) : proc_(proc), outindex_(0), - errindex_(0), showout_(false), showerr_(false) +SystemcallPrivate::SystemcallPrivate(const std::string& of) : + proc_(new QProcess), outindex_(0), errindex_(0), + outfile(of), showout_(false), showerr_(false), process_events(false) { - connect(proc, SIGNAL(readyReadStandardOutput()), SLOT(stdOut())); - connect(proc, SIGNAL(readyReadStandardError()), SLOT(stdErr())); - connect(proc, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError))); - connect(proc, SIGNAL(started()), this, SLOT(processStarted())); - connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus))); + if (!outfile.empty()) { + // Check whether we have to simply throw away the output. + if (outfile != os::nulldev()) + proc_->setStandardOutputFile(toqstr(outfile)); + } else if (os::is_terminal(os::STDOUT)) + showout(); + if (os::is_terminal(os::STDERR)) + showerr(); + + connect(proc_, SIGNAL(readyReadStandardOutput()), SLOT(stdOut())); + connect(proc_, SIGNAL(readyReadStandardError()), SLOT(stdErr())); + connect(proc_, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError))); + connect(proc_, SIGNAL(started()), this, SLOT(processStarted())); + connect(proc_, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus))); } void SystemcallPrivate::startProcess(const QString& cmd) { - state = SystemcallPrivate::Starting; - proc_->start(cmd); + if (proc_) { + state = SystemcallPrivate::Starting; + proc_->start(cmd); + } +} + + +void SystemcallPrivate::processEvents() +{ + if(process_events) { + //static int count = 0; qDebug() << count++ << ": waitAndProcessEvents"; + QCoreApplication::processEvents(QEventLoop::AllEvents); + } } void SystemcallPrivate::waitAndProcessEvents() { Sleep::millisec(100); - QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents); + processEvents(); } -bool SystemcallPrivate::waitWhile(State waitwhile, bool processEvents, int timeout) +bool SystemcallPrivate::waitWhile(State waitwhile, bool proc_events, int timeout) { + if (!proc_) + return false; + + process_events = proc_events; + // Block GUI while waiting, // relay on QProcess' wait functions - if (!processEvents) { + if (!process_events) { if (waitwhile == Starting) return proc_->waitForStarted(timeout); if (waitwhile == Running) @@ -226,6 +220,8 @@ bool SystemcallPrivate::waitWhile(State waitwhile, bool processEvents, int timeo SystemcallPrivate::~SystemcallPrivate() { + flush(); + if (outindex_) { outdata_[outindex_] = '\0'; outindex_ = 0; @@ -238,12 +234,30 @@ SystemcallPrivate::~SystemcallPrivate() cerr << errdata_; } cerr.flush(); + + killProcess(); +} + + +void SystemcallPrivate::flush() +{ + if (proc_) { + // 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. + if (!os::is_terminal(os::STDOUT) && outfile.empty()) + cout << fromqstr(QString::fromLocal8Bit( + proc_->readAllStandardOutput().data())); + if (!os::is_terminal(os::STDERR)) + cerr << fromqstr(QString::fromLocal8Bit( + proc_->readAllStandardError().data())); + } } void SystemcallPrivate::stdOut() { - if (showout_) { + if (proc_ && showout_) { char c; proc_->setReadChannel(QProcess::StandardOutput); while (proc_->getChar(&c)) { @@ -255,12 +269,13 @@ void SystemcallPrivate::stdOut() } } } + processEvents(); } void SystemcallPrivate::stdErr() { - if (showerr_) { + if (proc_ && showerr_) { char c; proc_->setReadChannel(QProcess::StandardError); while (proc_->getChar(&c)) { @@ -272,10 +287,25 @@ void SystemcallPrivate::stdErr() } } } + processEvents(); } -void SystemcallPrivate::processError(QProcess::ProcessError err) +void SystemcallPrivate::processStarted() +{ + state = Running; + // why do we get two started signals? + //disconnect(proc_, SIGNAL(started()), this, SLOT(processStarted())); +} + + +void SystemcallPrivate::processFinished(int, QProcess::ExitStatus) +{ + state = Finished; +} + + +void SystemcallPrivate::processError(QProcess::ProcessError) { state = Error; } @@ -283,6 +313,9 @@ void SystemcallPrivate::processError(QProcess::ProcessError err) QString SystemcallPrivate::errorMessage() const { + if (!proc_) + return "No QProcess available"; + QString message; switch (proc_->error()) { case QProcess::FailedToStart: @@ -312,22 +345,11 @@ QString SystemcallPrivate::errorMessage() const } -void SystemcallPrivate::processStarted() -{ - state = Running; - // why do we get two started signals? - //disconnect(proc_, SIGNAL(started()), this, SLOT(processStarted())); -} - - -void SystemcallPrivate::processFinished(int, QProcess::ExitStatus status) -{ - state = Finished; -} - - QString SystemcallPrivate::exitStatusMessage() const { + if (!proc_) + return "No QProcess available"; + QString message; switch (proc_->exitStatus()) { case QProcess::NormalExit: @@ -344,6 +366,42 @@ QString SystemcallPrivate::exitStatusMessage() const } +int SystemcallPrivate::exitCode() +{ + if (!proc_) + return -1; + + return proc_->exitCode(); +} + + +QProcess* SystemcallPrivate::releaseProcess() +{ + QProcess* released = proc_; + proc_ = 0; + return released; +} + + +void SystemcallPrivate::killProcess() +{ + killProcess(proc_); +} + + +void SystemcallPrivate::killProcess(QProcess * p) +{ + if (p) { + p->disconnect(); + p->closeReadChannel(QProcess::StandardOutput); + p->closeReadChannel(QProcess::StandardError); + p->close(); + delete p; + } +} + + + #include "moc_SystemcallPrivate.cpp" #endif