]> git.lyx.org Git - lyx.git/blobdiff - src/support/forkedcall.C
* lyxfunctional.h: delete compare_memfun and helper classes
[lyx.git] / src / support / forkedcall.C
index 6eadbc4c08f18e79b2ac244a328724566aa42b57..7fa823cf4c3261c966fa86b76ba32e4a102651a5 100644 (file)
@@ -1,5 +1,5 @@
 /**
- *  \file forkedcall.C
+ * \file forkedcall.C
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
@@ -8,7 +8,7 @@
  * Interface cleaned up by
  * \author Angus Leeming
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  *
  * An instance of Class Forkedcall represents a single child process.
  *
 
 #include <config.h>
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 #include "forkedcall.h"
 #include "forkedcontr.h"
 #include "lstrings.h"
 #include <boost/bind.hpp>
 
 #include <cerrno>
-#include <sys/types.h>
-#include <sys/wait.h>
 #include <csignal>
 #include <cstdlib>
+#include <sys/types.h>
+#include <sys/wait.h>
 #include <unistd.h>
 
+#include <vector>
+
 using std::endl;
+using std::string;
+using std::vector;
 
 #ifndef CXX_GLOBAL_CSTD
 using std::strerror;
 #endif
 
+namespace lyx {
+namespace support {
+
 
 namespace {
 
@@ -63,7 +66,7 @@ public:
                if (secs > 0) {
                        new Murder(secs, pid);
                } else if (pid != 0) {
-                       lyx::kill(pid, SIGKILL);
+                       lyx::support::kill(pid, SIGKILL);
                }
        }
 
@@ -71,7 +74,7 @@ public:
        void kill()
        {
                if (pid_ != 0) {
-                       lyx::kill(pid_, SIGKILL);
+                       lyx::support::kill(pid_, SIGKILL);
                }
                lyxerr << "Killed " << pid_ << std::endl;
                delete this;
@@ -114,8 +117,8 @@ void ForkedProcess::emitSignal()
 }
 
 
-// Wait for child process to finish.
-int ForkedProcess::runBlocking() 
+// Spawn the child process
+int ForkedProcess::run(Starttype type)
 {
        retval_  = 0;
        pid_ = generateChild();
@@ -124,32 +127,42 @@ int ForkedProcess::runBlocking()
                return retval_;
        }
 
-       retval_ = waitForChild();
+       switch (type) {
+       case Wait:
+               retval_ = waitForChild();
+               break;
+       case DontWait: {
+               // Integrate into the Controller
+               ForkedcallsController & contr = ForkedcallsController::get();
+               contr.addCall(*this);
+               break;
+       }
+       }
+
        return retval_;
 }
 
 
-// Do not wait for child process to finish.
-int ForkedProcess::runNonBlocking()
+bool ForkedProcess::running() const
 {
-       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_;
+       if (!pid())
+               return false;
+
+       // Un-UNIX like, but we don't have much use for
+       // knowing if a zombie exists, so just reap it first.
+       int waitstatus;
+       waitpid(pid(), &waitstatus, WNOHANG);
+
+       // Racy of course, but it will do.
+       if (::kill(pid(), 0) && errno == ESRCH)
+               return false;
+       return true;
 }
 
+
 void ForkedProcess::kill(int tol)
 {
-       lyxerr << "ForkedProcess::kill(" << tol << ")" << std::endl;
+       lyxerr << "ForkedProcess::kill(" << tol << ')' << endl;
        if (pid() == 0) {
                lyxerr << "Can't kill non-existent process!" << endl;
                return;
@@ -161,7 +174,7 @@ void ForkedProcess::kill(int tol)
                Murder::killItDead(0, pid());
 
        } else {
-               int ret = lyx::kill(pid(), SIGHUP);
+               int ret = lyx::support::kill(pid(), SIGHUP);
 
                // The process is already dead if wait_for_death is false
                bool const wait_for_death = (ret == 0 && errno != ESRCH);
@@ -219,7 +232,7 @@ int Forkedcall::startscript(Starttype wait, string const & what)
 
        command_ = what;
        signal_.reset();
-       return runBlocking();
+       return run(Wait);
 }
 
 
@@ -228,60 +241,74 @@ int Forkedcall::startscript(string const & what, SignalTypePtr signal)
        command_ = what;
        signal_  = signal;
 
-       return runNonBlocking();
+       return run(DontWait);
 }
 
 
 // generate child in background
 int Forkedcall::generateChild()
 {
-       const int MAX_ARGV = 255;
-       char *syscmd = 0;
-       char *argv[MAX_ARGV];
-
-       string childcommand(command_); // copy
-       bool more = true;
-       string rest = split(command_, childcommand, ' ');
-
-       int  index = 0;
-       while (more) {
-               childcommand = ltrim(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 = trim(command_);
+       if (line.empty())
+               return 1;
+
+       // Split the input command up into an array of words stored
+       // in a contiguous block of memory.
+       char const * const c_str = line.c_str();
+       // Don't forget the terminating `\0' character.
+       vector<char> vec(c_str, c_str + line.size() + 1);
+       // Turn the string into an array of words, each terminated with '\0'.
+       std::replace(vec.begin(), vec.end(), ' ', '\0');
+
+       // Build an array of pointers to each word.
+       vector<char>::iterator vit = vec.begin();
+       vector<char>::iterator vend = vec.end();
+       vector<char *> argv;
+       char prev = '\0';
+       for (; vit != vend; ++vit) {
+               if (*vit != '\0' && prev == '\0')
+                       argv.push_back(&*vit);
+               prev = *vit;
+       }
+       // Strip quotes. Does so naively, assuming that the word begins
+       // and ends in quotes.
+       vector<char *>::iterator ait = argv.begin();
+       vector<char *>::iterator const aend = argv.end();
+       for (; ait != aend; ++ait) {
+               char * word = *ait;
+               std::size_t const len = strlen(word);
+               if (len >= 2) {
+                       char & first = word[0];
+                       char & last = word[len-1];
+
+                       if (first == last &&
+                           (first == '\'' || first == '"')) {
+                               first = '\0';
+                               last = '\0';
+                               *ait += 1;
+                       }
                }
-
-               // reinit
-               more = !rest.empty();
-               if (more)
-                       rest = split(rest, childcommand, ' ');
        }
-       argv[index] = 0;
+
+       ait = argv.begin();
+       for (; ait != aend; ++ait)
+               std::cout << *ait << std::endl;
+       argv.push_back(0);
 
 #ifndef __EMX__
-       pid_t cpid = ::fork();
+       pid_t const cpid = ::fork();
        if (cpid == 0) {
                // Child
-               execvp(syscmd, argv);
+               execvp(argv[0], &*argv.begin());
+
                // If something goes wrong, we end up here
-               string args;
-               int i = 0;
-               while (argv[i] != 0)
-                       args += string(" ") + argv[i++];
-               lyxerr << "execvp of \"" << syscmd << args << "\" failed: "
+               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.begin());
 #endif
 
        if (cpid < 0) {
@@ -289,13 +316,8 @@ int Forkedcall::generateChild()
                lyxerr << "Could not fork: " << strerror(errno) << endl;
        }
 
-       // Clean-up.
-       delete [] syscmd;
-       for (int i = 0; i < MAX_ARGV; ++i) {
-               if (argv[i] == 0)
-                       break;
-               delete [] argv[i];
-       }
-
        return cpid;
 }
+
+} // namespace support
+} // namespace lyx