]> git.lyx.org Git - lyx.git/blobdiff - src/support/forkedcall.C
make "make distcheck" work
[lyx.git] / src / support / forkedcall.C
index c3e6938098a6b9a6332a4488d38e491deae38dce..9f416959e7f8c405ba656113f2a026fe9fe4059b 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>
 
-#include "forkedcall.h"
-#include "forkedcontr.h"
-#include "lstrings.h"
-#include "lyxlib.h"
-#include "filetools.h"
-#include "os.h"
+#include "support/forkedcall.h"
+#include "support/forkedcontr.h"
+#include "support/lstrings.h"
+#include "support/lyxlib.h"
+#include "support/filetools.h"
+#include "support/os.h"
+
 #include "debug.h"
+
 #include "frontends/Timeout.h"
 
 #include <boost/bind.hpp>
 
 #include <cerrno>
-#include <sys/types.h>
-#include <sys/wait.h>
 #include <csignal>
 #include <cstdlib>
-#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <vector>
 
 using std::endl;
+using std::string;
+using std::vector;
 
 #ifndef CXX_GLOBAL_CSTD
 using std::strerror;
 #endif
 
+namespace lyx {
+namespace support {
+
 
 namespace {
 
@@ -59,7 +70,7 @@ public:
                if (secs > 0) {
                        new Murder(secs, pid);
                } else if (pid != 0) {
-                       lyx::kill(pid, SIGKILL);
+                       lyx::support::kill(pid, SIGKILL);
                }
        }
 
@@ -67,7 +78,7 @@ public:
        void kill()
        {
                if (pid_ != 0) {
-                       lyx::kill(pid_, SIGKILL);
+                       lyx::support::kill(pid_, SIGKILL);
                }
                lyxerr << "Killed " << pid_ << std::endl;
                delete this;
@@ -110,8 +121,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();
@@ -120,25 +131,17 @@ int ForkedProcess::runBlocking()
                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_;
+       switch (type) {
+       case Wait:
+               retval_ = waitForChild();
+               break;
+       case DontWait: {
+               // Integrate into the Controller
+               ForkedcallsController & contr = ForkedcallsController::get();
+               contr.addCall(*this);
+               break;
+       }
        }
-
-       // Non-blocking execution.
-       // Integrate into the Controller
-       ForkedcallsController & contr = ForkedcallsController::get();
-       contr.addCall(*this);
 
        return retval_;
 }
@@ -155,7 +158,7 @@ bool ForkedProcess::running() const
        waitpid(pid(), &waitstatus, WNOHANG);
 
        // Racy of course, but it will do.
-       if (::kill(pid(), 0) && errno == ESRCH)
+       if (lyx::support::kill(pid(), 0) && errno == ESRCH)
                return false;
        return true;
 }
@@ -175,7 +178,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);
@@ -233,7 +236,7 @@ int Forkedcall::startscript(Starttype wait, string const & what)
 
        command_ = what;
        signal_.reset();
-       return runBlocking();
+       return run(Wait);
 }
 
 
@@ -242,38 +245,77 @@ int Forkedcall::startscript(string const & what, SignalTypePtr signal)
        command_ = what;
        signal_  = signal;
 
-       return runNonBlocking();
+       return run(DontWait);
 }
 
 
 // generate child in background
 int Forkedcall::generateChild()
 {
-       // Split command_ up into a char * array
-       int const MAX_ARGV = 255;
-       char *argv[MAX_ARGV];
-
-       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';
-
-               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. The array contains pointers
+       // to each word.
+       // Don't forget the terminating `\0' character.
+       char const * const c_str = line.c_str();
+       vector<char> vec(c_str, c_str + line.size() + 1);
+
+       // Splitting the command up into an array of words means replacing
+       // the whitespace between words with '\0'. Life is complicated
+       // however, because words protected by quotes can contain whitespace.
+       //
+       // The strategy we adopt is:
+       // 1. If we're not inside quotes, then replace white space with '\0'.
+       // 2. If we are inside quotes, then don't replace the white space
+       //    but do remove the quotes themselves. We do this naively by
+       //    replacing the quote with '\0' which is fine if quotes
+       //    delimit the entire word.
+       char inside_quote = 0;
+       vector<char>::iterator it = vec.begin();
+       vector<char>::iterator const end = vec.end();
+       for (; it != end; ++it) {
+               char const c = *it;
+               if (!inside_quote) {
+                       if (c == ' ')
+                               *it = '\0';
+                       else if (c == '\'' || c == '"') {
+                               *it = '\0';
+                               inside_quote = c;
+                       }
+               } else if (c == inside_quote) {
+                       *it = '\0';
+                       inside_quote = 0;
+               }
+       }
+
+       // Build an array of pointers to each word.
+       it = vec.begin();
+       vector<char *> argv;
+       char prev = '\0';
+       for (; it != end; ++it) {
+               if (*it != '\0' && prev == '\0')
+                       argv.push_back(&*it);
+               prev = *it;
        }
-       argv[index] = 0;
+       argv.push_back(0);
+
+       // Debug output.
+       vector<char *>::iterator ait = argv.begin();
+       vector<char *>::iterator const aend = argv.end();
+       lyxerr << "<command>\n";
+       for (; ait != aend; ++ait)
+               if (*ait)
+                       lyxerr << '\t'<< *ait << '\n';
+       lyxerr << "</command>" << std::endl;
 
 #ifndef __EMX__
        pid_t const cpid = ::fork();
        if (cpid == 0) {
                // Child
-               execvp(argv[0], argv);
+               execvp(argv[0], &*argv.begin());
 
                // If something goes wrong, we end up here
                lyxerr << "execvp of \"" << command_ << "\" failed: "
@@ -282,7 +324,7 @@ int Forkedcall::generateChild()
        }
 #else
        pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
-                                  argv[0], argv);
+                                  argv[0], &*argv.begin());
 #endif
 
        if (cpid < 0) {
@@ -290,12 +332,8 @@ int Forkedcall::generateChild()
                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;
 }
+
+} // namespace support
+} // namespace lyx