[llvm-commits] CVS: llvm/lib/System/Win32/Program.inc

Jeff Cohen jeffc at jolt-lang.org
Sat Feb 19 18:43:15 PST 2005



Changes in directory llvm/lib/System/Win32:

Program.inc updated: 1.10 -> 1.11
---
Log message:

Implement standard I/O redirection in ExecuteAndWait().

---
Diffs of the changes:  (+73 -7)

 Program.inc |   80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 73 insertions(+), 7 deletions(-)


Index: llvm/lib/System/Win32/Program.inc
diff -u llvm/lib/System/Win32/Program.inc:1.10 llvm/lib/System/Win32/Program.inc:1.11
--- llvm/lib/System/Win32/Program.inc:1.10	Tue Feb 15 22:43:45 2005
+++ llvm/lib/System/Win32/Program.inc	Sat Feb 19 20:43:04 2005
@@ -13,6 +13,7 @@
 
 #include "Win32.h"
 #include <malloc.h>
+#include <io.h>
 
 //===----------------------------------------------------------------------===//
 //=== WARNING: Implementation here must contain only Win32 specific code 
@@ -66,7 +67,34 @@
   }
 }
 
-//
+static HANDLE RedirectIO(const Path *path, int fd) {
+  HANDLE h;
+  if (path == 0) {
+    DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
+                    GetCurrentProcess(), &h,
+                    0, TRUE, DUPLICATE_SAME_ACCESS);
+    return h;
+  }
+
+  const char *fname = path->toString().c_str();
+  if (*fname == 0)
+    fname = "NUL";
+
+  SECURITY_ATTRIBUTES sa;
+  sa.nLength = sizeof(sa);
+  sa.lpSecurityDescriptor = 0;
+  sa.bInheritHandle = TRUE;
+
+  h = CreateFile(fname, fd ? GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ,
+                 &sa, fd ? OPEN_EXISTING : CREATE_ALWAYS,
+                 FILE_ATTRIBUTE_NORMAL, NULL);
+  if (h == INVALID_HANDLE_VALUE) {
+    ThrowError(std::string(fname) + ": Can't open file for " +
+        (fd ? "input: " : "output: "));
+  }
+  return h;
+}
+
 int 
 Program::ExecuteAndWait(const Path& path, 
                         const char** args,
@@ -111,15 +139,50 @@
   STARTUPINFO si;
   memset(&si, 0, sizeof(si));
   si.cb = sizeof(si);
-
-  // TODO: do replacement of standard input/output/error handles.
+  si.hStdInput = INVALID_HANDLE_VALUE;
+  si.hStdOutput = INVALID_HANDLE_VALUE;
+  si.hStdError = INVALID_HANDLE_VALUE;
+
+  if (redirects) {
+    si.dwFlags = STARTF_USESTDHANDLES;
+    
+    try {
+      si.hStdInput = RedirectIO(redirects[0], 0);
+      si.hStdOutput = RedirectIO(redirects[1], 1);
+      if (redirects[1] && redirects[2] && *(redirects[1]) != *(redirects[2])) {
+        si.hStdError = RedirectIO(redirects[2], 2);
+      } else {
+        DuplicateHandle(GetCurrentProcess(), si.hStdOutput,
+                        GetCurrentProcess(), &si.hStdError,
+                        0, TRUE, DUPLICATE_SAME_ACCESS);
+      }
+    } catch (...) {
+      CloseHandle(si.hStdInput);
+      CloseHandle(si.hStdOutput);
+      CloseHandle(si.hStdError);
+      throw;
+    }
+  }
 
   PROCESS_INFORMATION pi;
   memset(&pi, 0, sizeof(pi));
 
-  if (!CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
-                     envp, NULL, &si, &pi))
+  fflush(stdout);
+  fflush(stderr);
+  BOOL rc = CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
+                          envp, NULL, &si, &pi);
+  DWORD err = GetLastError();
+
+  // Regardless of whether the process got created or not, we are done with
+  // the handles we created for it to inherit.
+  CloseHandle(si.hStdInput);
+  CloseHandle(si.hStdOutput);
+  CloseHandle(si.hStdError);
+
+  // Now throw an error if the process didn't get created.
+  if (!rc)
   {
+    SetLastError(err);
     ThrowError(std::string("Couldn't execute program '") + 
                path.toString() + "'");
   }
@@ -139,15 +202,18 @@
   
   // Get its exit status.
   DWORD status;
-  BOOL rc = GetExitCodeProcess(pi.hProcess, &status);
+  rc = GetExitCodeProcess(pi.hProcess, &status);
+  err = GetLastError();
 
   // Done with the handles; go close them.
   CloseHandle(pi.hProcess);
   CloseHandle(pi.hThread);
 
-  if (!rc)
+  if (!rc) {
+    SetLastError(err);
     ThrowError(std::string("Failed getting status for program '") + 
                path.toString() + "'");
+  }
 
   return status;
 }






More information about the llvm-commits mailing list