diff src/sysdep.c @ 357:4711e16a8e49 r21-1-8

Import from CVS: tag r21-1-8
author cvs
date Mon, 13 Aug 2007 10:57:04 +0200
parents 7c94d56991e1
children 8e84bee8ddd0
line wrap: on
line diff
--- a/src/sysdep.c	Mon Aug 13 10:56:23 2007 +0200
+++ b/src/sysdep.c	Mon Aug 13 10:57:04 2007 +0200
@@ -233,8 +233,11 @@
 #endif /* NO_SUBPROCESSES */
 
 
-void
-wait_for_termination (int pid)
+#ifdef WINDOWSNT
+void wait_for_termination (HANDLE pHandle)
+#else
+void wait_for_termination (int pid)
+#endif
 {
   /* #### With the new improved SIGCHLD handling stuff, there is much
      less danger of race conditions and some of the comments below
@@ -344,6 +347,47 @@
 
      Since implementations may add their own error indicators on top,
      we ignore it by default.  */
+#elif defined (WINDOWSNT)
+  int ret = 0, status = 0;
+  if (pHandle == NULL)
+    {
+      stderr_out ("wait_for_termination: pHandle == NULL, GetLastError () = %d, (int)pHandle = %d\n", GetLastError (), (int)pHandle);
+      return;
+    }
+  do
+    {
+      QUIT;
+      ret = WaitForSingleObject(pHandle, 100);
+    }
+  while (ret == WAIT_TIMEOUT);
+  if (ret == WAIT_FAILED)
+    {
+      stderr_out ("wait_for_termination.WaitForSingleObject returns %d (WAIT_FAILED) GetLastError () %d for (int)pHandle %d\n", ret, GetLastError (), (int)pHandle);
+    }
+  if (ret == WAIT_ABANDONED)
+    {
+      stderr_out ("wait_for_termination.WaitForSingleObject returns %d (WAIT_ABANDONED) GetLastError () %d for (int)pHandle %d\n", ret, GetLastError (), (int)pHandle);
+    }
+  if (ret == WAIT_OBJECT_0)
+    {
+      ret = GetExitCodeProcess(pHandle, &status);
+      if (ret)
+	{
+	  synch_process_alive = 0;
+	  synch_process_retcode = status;
+	}
+      else
+	{
+	  /* GetExitCodeProcess() didn't return a valid exit status,
+	     nothing to do.  APA */
+	  stderr_out ("wait_for_termination.GetExitCodeProcess status %d GetLastError () %d for (int)pHandle %d\n", status, GetLastError (), (int)pHandle);
+	}
+    }
+  if (pHandle != NULL && !CloseHandle(pHandle)) 
+    {
+      stderr_out ("wait_for_termination.CloseHandle GetLastError () %d for (int)pHandle %d\n",
+		  GetLastError (), (int)pHandle);
+    }
 #elif defined (EMACS_BLOCK_SIGNAL) && !defined (BROKEN_WAIT_FOR_SIGNAL) && defined (SIGCHLD)
   while (1)
     {
@@ -375,7 +419,7 @@
 	   Try defining BROKEN_WAIT_FOR_SIGNAL. */
 	EMACS_WAIT_FOR_SIGNAL (SIGCHLD);
     }
-#else /* not HAVE_WAITPID and (not EMACS_BLOCK_SIGNAL or BROKEN_WAIT_FOR_SIGNAL) */
+#else /* not HAVE_WAITPID and not WINDOWSNT and (not EMACS_BLOCK_SIGNAL or BROKEN_WAIT_FOR_SIGNAL) */
   /* This approach is kind of cheesy but is guaranteed(?!) to work
      for all systems. */
   while (1)
@@ -577,7 +621,11 @@
 static void
 sys_subshell (void)
 {
+#ifdef WINDOWSNT
+  HANDLE pid;
+#else
   int pid;
+#endif
   struct save_signal saved_handlers[5];
   Lisp_Object dir;
   unsigned char *str = 0;
@@ -616,7 +664,7 @@
  xyzzy:
 
 #ifdef WINDOWSNT
-  pid = -1;
+  pid = NULL;
 #else /* not WINDOWSNT */
 
   pid = fork ();
@@ -650,7 +698,7 @@
 #ifdef WINDOWSNT
       /* Waits for process completion */
       pid = _spawnlp (_P_WAIT, sh, sh, NULL);
-      if (pid == -1)
+      if (pid == NULL)
         write (1, "Can't execute subshell", 22);
 
 #else   /* not WINDOWSNT */