]> code.delx.au - gnu-emacs/commitdiff
(fail): Exit with a negative return value.
authorGeoff Voelker <voelker@cs.washington.edu>
Fri, 17 Apr 1998 05:04:21 +0000 (05:04 +0000)
committerGeoff Voelker <voelker@cs.washington.edu>
Fri, 17 Apr 1998 05:04:21 +0000 (05:04 +0000)
(spawn): Return subprocess return code as an argument.
Explicitly copy environment block.
(main): Update to use return value argument with spawn.
Retry if spawn failed when a subshell was not tried.

nt/cmdproxy.c

index 18705af47f55c8928dc1909fce2eeaf444062c9d..64eae8d286a83bbd89ec07357f7b4551ca62c1e0 100644 (file)
@@ -90,7 +90,7 @@ fail (char * msg, ...)
   vfprintf (stderr, msg, args);
   va_end (args);
 
   vfprintf (stderr, msg, args);
   va_end (args);
 
-  exit (1);
+  exit (-1);
 }
 
 void
 }
 
 void
@@ -366,12 +366,18 @@ console_event_handler (DWORD event)
   return TRUE;
 }
 
   return TRUE;
 }
 
+/* Change from normal usage; return value indicates whether spawn
+   succeeded or failed - program return code is returned separately.  */
 int
 int
-spawn (char * progname, char * cmdline)
+spawn (char * progname, char * cmdline, int * retcode)
 {
 {
-  DWORD rc = 0xff;
+  BOOL success = FALSE;
   SECURITY_ATTRIBUTES sec_attrs;
   STARTUPINFO start;
   SECURITY_ATTRIBUTES sec_attrs;
   STARTUPINFO start;
+  /* In theory, passing NULL for the environment block to CreateProcess
+     is the same as passing the value of GetEnvironmentStrings, but
+     doing this explicitly seems to cure problems running DOS programs
+     in some cases.  */
   char * envblock = GetEnvironmentStrings ();
 
   sec_attrs.nLength = sizeof (sec_attrs);
   char * envblock = GetEnvironmentStrings ();
 
   sec_attrs.nLength = sizeof (sec_attrs);
@@ -384,9 +390,11 @@ spawn (char * progname, char * cmdline)
   if (CreateProcess (progname, cmdline, &sec_attrs, NULL, TRUE,
                     0, envblock, NULL, &start, &child))
   {
   if (CreateProcess (progname, cmdline, &sec_attrs, NULL, TRUE,
                     0, envblock, NULL, &start, &child))
   {
+    success = TRUE;
     /* wait for completion and pass on return code */
     WaitForSingleObject (child.hProcess, INFINITE);
     /* wait for completion and pass on return code */
     WaitForSingleObject (child.hProcess, INFINITE);
-    GetExitCodeProcess (child.hProcess, &rc);
+    if (retcode)
+      GetExitCodeProcess (child.hProcess, (DWORD *)retcode);
     CloseHandle (child.hThread);
     CloseHandle (child.hProcess);
     child.hProcess = NULL;
     CloseHandle (child.hThread);
     CloseHandle (child.hProcess);
     child.hProcess = NULL;
@@ -394,7 +402,7 @@ spawn (char * progname, char * cmdline)
 
   FreeEnvironmentStrings (envblock);
 
 
   FreeEnvironmentStrings (envblock);
 
-  return (int) rc;
+  return success;
 }
 
 /* Return size of current environment block.  */
 }
 
 /* Return size of current environment block.  */
@@ -454,7 +462,9 @@ main (int argc, char ** argv)
       /* We are being used as a helper to run a DOS app; just pass
         command line to DOS app without change.  */
       /* TODO: fill in progname.  */
       /* We are being used as a helper to run a DOS app; just pass
         command line to DOS app without change.  */
       /* TODO: fill in progname.  */
-      return spawn (NULL, GetCommandLine ());
+      if (spawn (NULL, GetCommandLine (), &rc))
+       return rc;
+      fail ("Could not run %s\n", GetCommandLine ());
     }
 
   /* Process command line.  If running interactively (-c or /c not
     }
 
   /* Process command line.  If running interactively (-c or /c not
@@ -568,6 +578,7 @@ main (int argc, char ** argv)
        }
     }
 
        }
     }
 
+pass_to_shell:
   if (need_shell)
     {
       char * p;
   if (need_shell)
     {
       char * p;
@@ -649,7 +660,16 @@ main (int argc, char ** argv)
   if (!cmdline)
     cmdline = progname;
 
   if (!cmdline)
     cmdline = progname;
 
-  rc = spawn (progname, cmdline);
+  if (spawn (progname, cmdline, &rc))
+    return rc;
 
 
-  return rc;
+  if (!need_shell)
+    {
+      need_shell = TRUE;
+      goto pass_to_shell;
+    }
+
+  fail ("Could not run %s\n", progname);
+
+  return 0;
 }
 }