]> git.lyx.org Git - lyx.git/blobdiff - src/support/forkedcall.C
hopefully fix tex2lyx linking.
[lyx.git] / src / support / forkedcall.C
index 7fa823cf4c3261c966fa86b76ba32e4a102651a5..e1d5ef50cfd04655cc54f83479cc4438c06fda5c 100644 (file)
 
 #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 <vector>
 #include <cerrno>
-#include <csignal>
-#include <cstdlib>
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <unistd.h>
 
-#include <vector>
+#ifdef _WIN32
+# define SIGHUP 1
+# define SIGKILL 9
+# include <process.h>
+# include <windows.h>
+
+#else
+# include <csignal>
+# include <cstdlib>
+# ifdef HAVE_UNISTD_H
+#  include <unistd.h>
+# endif
+# include <sys/wait.h>
+#endif
 
 using std::endl;
 using std::string;
@@ -148,13 +159,15 @@ bool ForkedProcess::running() const
        if (!pid())
                return false;
 
+#if !defined (_WIN32)
        // 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);
+#endif
 
        // 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;
 }
@@ -168,7 +181,9 @@ void ForkedProcess::kill(int tol)
                return;
        }
 
-       int const tolerance = std::max(0, tol);
+       // The weird (std::max)(a,b) signature prevents expansion
+       // of an evil MSVC macro.
+       int const tolerance = (std::max)(0, tol);
        if (tolerance == 0) {
                // Kill it dead NOW!
                Murder::killItDead(0, pid());
@@ -191,6 +206,29 @@ int ForkedProcess::waitForChild()
 {
        // We'll pretend that the child returns 1 on all error conditions.
        retval_ = 1;
+
+#if defined (_WIN32)
+       HANDLE const hProcess = HANDLE(pid_);
+
+       DWORD const wait_status = ::WaitForSingleObject(hProcess, INFINITE);
+
+       switch (wait_status) {
+       case WAIT_OBJECT_0: {
+               DWORD exit_code = 0;
+               if (!GetExitCodeProcess(hProcess, &exit_code)) {
+                       lyxerr << "GetExitCodeProcess failed waiting for child\n"
+                              << getChildErrorMessage() << std::endl;
+               } else
+                       retval_ = exit_code;
+               break;
+       }
+       case WAIT_FAILED:
+               lyxerr << "WaitForSingleObject failed waiting for child\n"
+                      << getChildErrorMessage() << std::endl;
+               break;
+       }
+
+#else
        int status;
        bool wait = true;
        while (wait) {
@@ -219,6 +257,7 @@ int ForkedProcess::waitForChild()
                        wait = false;
                }
        }
+#endif
        return retval_;
 }
 
@@ -253,49 +292,78 @@ int Forkedcall::generateChild()
                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();
+       // 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);
-       // 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;
+       // 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 == '"') {
+#if defined (_WIN32)
+                               // How perverse!
+                               // spawnvp *requires* the quotes or it will
+                               // split the arg at the internal whitespace!
+                               // Make shure the quote is a DOS-style one.
+                               *it = '"';
+#else
+                               *it = '\0';
+#endif
+                               inside_quote = c;
                        }
+               } else if (c == inside_quote) {
+#if defined (_WIN32)
+                       *it = '"';
+#else
+                       *it = '\0';
+#endif
+                       inside_quote = 0;
                }
        }
 
-       ait = argv.begin();
-       for (; ait != aend; ++ait)
-               std::cout << *ait << std::endl;
+       // 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.push_back(0);
 
-#ifndef __EMX__
+       // Debug output.
+       if (lyxerr.debugging(Debug::FILES)) {
+               vector<char *>::iterator ait = argv.begin();
+               vector<char *>::iterator const aend = argv.end();
+               lyxerr << "<command>\n\t" << line
+                      << "\n\tInterpretted as:\n\n";
+               for (; ait != aend; ++ait)
+                       if (*ait)
+                               lyxerr << '\t'<< *ait << '\n';
+               lyxerr << "</command>" << std::endl;
+       }
+
+#ifdef _WIN32
+       pid_t const cpid = spawnvp(_P_NOWAIT, argv[0], &*argv.begin());
+#else // POSIX
        pid_t const cpid = ::fork();
        if (cpid == 0) {
                // Child
@@ -306,9 +374,6 @@ int Forkedcall::generateChild()
                       << strerror(errno) << endl;
                _exit(1);
        }
-#else
-       pid_t const cpid = spawnvp(P_SESSION|P_DEFAULT|P_MINIMIZE|P_BACKGROUND,
-                                  argv[0], &*argv.begin());
 #endif
 
        if (cpid < 0) {