]> git.lyx.org Git - lyx.git/blobdiff - src/support/forkedcall.C
another safety belt
[lyx.git] / src / support / forkedcall.C
index 25947e3898d6149093b222881c981136502c05cb..a3e01df1a5ff104507d5943fa11512bd18e6f38d 100644 (file)
@@ -1,12 +1,14 @@
 /**
  *  \file forkedcall.C
- *  Copyright 2002 the LyX Team
- *  Read the file COPYING
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
  *
  * \author Asger Alstrup
  *
  * Interface cleaned up by
- * \author Angus Leeming <a.leeming@ic.ac.uk>
+ * \author Angus Leeming
+ *
+ * Full author contact details are available in file CREDITS
  *
  * An instance of Class Forkedcall represents a single child process.
  *
@@ -35,6 +37,8 @@
 #include "debug.h"
 #include "frontends/Timeout.h"
 
+#include <boost/bind.hpp>
+
 #include <cerrno>
 #include <sys/types.h>
 #include <sys/wait.h>
@@ -49,65 +53,9 @@ using std::strerror;
 #endif
 
 
-Forkedcall::Forkedcall()
-       : pid_(0), retval_(0)
-{}
-
-
-int Forkedcall::startscript(Starttype wait, string const & what)
-{
-       if (wait == Wait) {
-               command_ = what;
-               retval_  = 0;
-
-               pid_ = generateChild();
-               if (pid_ <= 0) { // child or fork failed.
-                       retval_ = 1;
-               } else {
-                       retval_ = waitForChild();
-               }
-
-               return retval_;
-       }
-
-       // DontWait
-       retval_ = startscript(what, SignalTypePtr());
-       return retval_;
-}
-
-
-int Forkedcall::startscript(string const & what, SignalTypePtr signal)
-{
-       command_ = what;
-       signal_  = signal;
-       retval_  = 0;
-
-       pid_ = generateChild();
-       if (pid_ <= 0) { // child or fork failed.
-               retval_ = 1;
-               return retval_;
-       }
-
-       // Non-blocking execution.
-       // Integrate into the Controller
-       ForkedcallsController & contr = ForkedcallsController::get();
-       contr.addCall(*this);
-
-       return retval_;
-}
-
-
-void Forkedcall::emitSignal()
-{
-       if (signal_.get()) {
-               signal_->emit(command_, pid_, retval_);
-       }
-}
-
-
 namespace {
 
-class Murder : public SigC::Object {
+class Murder : public boost::signals::trackable {
 public:
        //
        static void killItDead(int secs, pid_t pid)
@@ -135,7 +83,7 @@ private:
                : timeout_(0), pid_(pid)
        {
                timeout_ = new Timeout(1000*secs, Timeout::ONETIME);
-               timeout_->timeout.connect(SigC::slot(this, &Murder::kill));
+               timeout_->timeout.connect(boost::bind(&Murder::kill, this));
                timeout_->start();
        }
 
@@ -153,9 +101,55 @@ private:
 } // namespace anon
 
 
-void Forkedcall::kill(int tol)
+ForkedProcess::ForkedProcess()
+       : pid_(0), retval_(0)
+{}
+
+
+void ForkedProcess::emitSignal()
+{
+       if (signal_.get()) {
+               signal_->operator()(pid_, retval_);
+       }
+}
+
+
+// Wait for child process to finish.
+int ForkedProcess::runBlocking() 
+{
+       retval_  = 0;
+       pid_ = generateChild();
+       if (pid_ <= 0) { // child or fork failed.
+               retval_ = 1;
+               return retval_;
+       }
+
+       retval_ = waitForChild();
+       return retval_;
+}
+
+
+// Do not wait for child process to finish.
+int ForkedProcess::runNonBlocking()
+{
+       retval_ = 0;
+       pid_ = generateChild();
+       if (pid_ <= 0) { // child or fork failed.
+               retval_ = 1;
+               return retval_;
+       }
+
+       // Non-blocking execution.
+       // Integrate into the Controller
+       ForkedcallsController & contr = ForkedcallsController::get();
+       contr.addCall(*this);
+
+       return retval_;
+}
+
+void ForkedProcess::kill(int tol)
 {
-       lyxerr << "Forkedcall::kill(" << tol << ")" << std::endl;
+       lyxerr << "ForkedProcess::kill(" << tol << ")" << std::endl;
        if (pid() == 0) {
                lyxerr << "Can't kill non-existent process!" << endl;
                return;
@@ -180,7 +174,8 @@ void Forkedcall::kill(int tol)
 
 
 // Wait for child process to finish. Returns returncode from child.
-int Forkedcall::waitForChild() {
+int ForkedProcess::waitForChild()
+{
        // We'll pretend that the child returns 1 on all error conditions.
        retval_ = 1;
        int status;
@@ -215,55 +210,77 @@ int Forkedcall::waitForChild() {
 }
 
 
+int Forkedcall::startscript(Starttype wait, string const & what)
+{
+       if (wait != Wait) {
+               retval_ = startscript(what, SignalTypePtr());
+               return retval_;
+       }
+
+       command_ = what;
+       signal_.reset();
+       return runBlocking();
+}
+
+
+int Forkedcall::startscript(string const & what, SignalTypePtr signal)
+{
+       command_ = what;
+       signal_  = signal;
+
+       return runNonBlocking();
+}
+
+
 // generate child in background
-pid_t Forkedcall::generateChild()
+int Forkedcall::generateChild()
 {
-       const int MAX_ARGV = 255;
-       char *syscmd = 0;
+       // Split command_ up into a char * array
+       int const MAX_ARGV = 255;
        char *argv[MAX_ARGV];
 
-       string childcommand(command_); // copy
-       bool more = true;
-       string rest = split(command_, childcommand, ' ');
-
-       int  index = 0;
-       while (more) {
-               childcommand = frontStrip(childcommand);
-               if (syscmd == 0) {
-                       syscmd = new char[childcommand.length() + 1];
-                       childcommand.copy(syscmd, childcommand.length());
-                       syscmd[childcommand.length()] = '\0';
-               }
-               if (!childcommand.empty()) {
-                       char * tmp = new char[childcommand.length() + 1];
-                       childcommand.copy(tmp, childcommand.length());
-                       tmp[childcommand.length()] = '\0';
-                       argv[index++] = tmp;
-               }
+       string line = command_;
+       int index = 0;
+       for (; index < MAX_ARGV-1; ++index) {
+               string word;
+               line = split(line, word, ' ');
+               if (word.empty())
+                       break;
+
+               char * tmp = new char[word.length() + 1];
+               word.copy(tmp, word.length());
+               tmp[word.length()] = '\0';
 
-               // reinit
-               more = !rest.empty();
-               if (more)
-                       rest = split(rest, childcommand, ' ');
+               argv[index] = tmp;
        }
        argv[index] = 0;
 
 #ifndef __EMX__
-       pid_t cpid = ::fork();
-       if (cpid == 0) { // child
-               execvp(syscmd, argv);
-               // If something goes wrong, we end up here:
-               lyxerr << "execvp failed: "
+       pid_t const cpid = ::fork();
+       if (cpid == 0) {
+               // Child
+               execvp(argv[0], argv);
+
+               // If something goes wrong, we end up here
+               lyxerr << "execvp of \"" << command_ << "\" failed: "
                       << strerror(errno) << endl;
+               _exit(1);
        }
 #else
-       pid_t cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
-                            syscmd, argv);
+       pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
+                                  argv[0], argv);
 #endif
 
-       if (cpid < 0) { // error
-               lyxerr << "Could not fork: "
-                      << strerror(errno) << endl;
+       if (cpid < 0) {
+               // Error.
+               lyxerr << "Could not fork: " << strerror(errno) << endl;
+       }
+
+       // Clean-up.
+       for (int i = 0; i < MAX_ARGV; ++i) {
+               if (argv[i] == 0)
+                       break;
+               delete [] argv[i];
        }
 
        return cpid;