]> git.lyx.org Git - lyx.git/blobdiff - src/support/forkedcall.C
hopefully fix tex2lyx linking.
[lyx.git] / src / support / forkedcall.C
index 5fb660e76132684c9edb14d34b587decc22a6c8c..e1d5ef50cfd04655cc54f83479cc4438c06fda5c 100644 (file)
 
 #include <boost/bind.hpp>
 
+#include <vector>
 #include <cerrno>
-#include <csignal>
-#include <cstdlib>
-#include <sys/types.h>
-#include <sys/wait.h>
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
 
-#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;
@@ -152,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;
 }
@@ -172,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());
@@ -195,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) {
@@ -223,6 +257,7 @@ int ForkedProcess::waitForChild()
                        wait = false;
                }
        }
+#endif
        return retval_;
 }
 
@@ -282,11 +317,23 @@ int Forkedcall::generateChild()
                        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;
                }
        }
@@ -303,15 +350,20 @@ int Forkedcall::generateChild()
        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__
+       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
@@ -322,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) {