Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 1 | //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===// |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 5 | // This file was developed by Jeff Cohen and is distributed under the |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides the Win32 specific implementation of the Program class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 14 | #include "Win32.h" |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 15 | #include <malloc.h> |
| 16 | |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 18 | //=== WARNING: Implementation here must contain only Win32 specific code |
| 19 | //=== and must not be UNIX code |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | using namespace sys; |
| 24 | |
| 25 | // This function just uses the PATH environment variable to find the program. |
| 26 | Path |
| 27 | Program::FindProgramByName(const std::string& progName) { |
| 28 | |
| 29 | // Check some degenerate cases |
| 30 | if (progName.length() == 0) // no program |
| 31 | return Path(); |
| 32 | Path temp; |
Reid Spencer | 07adb28 | 2004-11-05 22:15:36 +0000 | [diff] [blame] | 33 | if (!temp.setFile(progName)) // invalid name |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 34 | return Path(); |
| 35 | if (temp.executable()) // already executable as is |
| 36 | return temp; |
| 37 | |
| 38 | // At this point, the file name is valid and its not executable. |
| 39 | // Let Windows search for it. |
| 40 | char buffer[MAX_PATH]; |
| 41 | char *dummy = NULL; |
| 42 | DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH, |
| 43 | buffer, &dummy); |
| 44 | |
| 45 | // See if it wasn't found. |
| 46 | if (len == 0) |
| 47 | return Path(); |
| 48 | |
| 49 | // See if we got the entire path. |
| 50 | if (len < MAX_PATH) |
| 51 | return Path(buffer); |
| 52 | |
| 53 | // Buffer was too small; grow and retry. |
| 54 | while (true) { |
| 55 | char *b = reinterpret_cast<char *>(_alloca(len+1)); |
| 56 | DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy); |
| 57 | |
| 58 | // It is unlikely the search failed, but it's always possible some file |
| 59 | // was added or removed since the last search, so be paranoid... |
| 60 | if (len2 == 0) |
| 61 | return Path(); |
| 62 | else if (len2 <= len) |
| 63 | return Path(b); |
| 64 | |
| 65 | len = len2; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // |
| 70 | int |
| 71 | Program::ExecuteAndWait(const Path& path, |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 72 | const char** args, |
| 73 | const char** envp, |
| 74 | const Path** redirects, |
| 75 | unsigned secondsToWait) { |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 76 | if (!path.executable()) |
Reid Spencer | 707a27c | 2004-12-11 17:37:01 +0000 | [diff] [blame] | 77 | throw path.toString() + " is not executable"; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 78 | |
| 79 | // Windows wants a command line, not an array of args, to pass to the new |
| 80 | // process. We have to concatenate them all, while quoting the args that |
| 81 | // have embedded spaces. |
| 82 | |
| 83 | // First, determine the length of the command line. |
Jeff Cohen | e5f7e65 | 2005-02-16 04:43:45 +0000 | [diff] [blame^] | 84 | unsigned len = 0; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 85 | for (unsigned i = 0; args[i]; i++) { |
| 86 | len += strlen(args[i]) + 1; |
| 87 | if (strchr(args[i], ' ')) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 88 | len += 2; |
| 89 | } |
| 90 | |
| 91 | // Now build the command line. |
| 92 | char *command = reinterpret_cast<char *>(_alloca(len)); |
| 93 | char *p = command; |
| 94 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 95 | for (unsigned i = 0; args[i]; i++) { |
| 96 | const char *arg = args[i]; |
| 97 | size_t len = strlen(arg); |
Jeff Cohen | e5f7e65 | 2005-02-16 04:43:45 +0000 | [diff] [blame^] | 98 | bool needsQuoting = strchr(arg, ' ') != 0; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 99 | if (needsQuoting) |
| 100 | *p++ = '"'; |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 101 | memcpy(p, arg, len); |
| 102 | p += len; |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 103 | if (needsQuoting) |
| 104 | *p++ = '"'; |
| 105 | *p++ = ' '; |
| 106 | } |
| 107 | |
| 108 | *p = 0; |
| 109 | |
| 110 | // Create a child process. |
| 111 | STARTUPINFO si; |
| 112 | memset(&si, 0, sizeof(si)); |
| 113 | si.cb = sizeof(si); |
| 114 | |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 115 | // TODO: do replacement of standard input/output/error handles. |
| 116 | |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 117 | PROCESS_INFORMATION pi; |
| 118 | memset(&pi, 0, sizeof(pi)); |
| 119 | |
Reid Spencer | 707a27c | 2004-12-11 17:37:01 +0000 | [diff] [blame] | 120 | if (!CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0, |
Jeff Cohen | 626e38e | 2004-12-14 05:26:43 +0000 | [diff] [blame] | 121 | envp, NULL, &si, &pi)) |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 122 | { |
Reid Spencer | 707a27c | 2004-12-11 17:37:01 +0000 | [diff] [blame] | 123 | ThrowError(std::string("Couldn't execute program '") + |
| 124 | path.toString() + "'"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // Wait for it to terminate. |
Jeff Cohen | a1b3d3d | 2004-12-20 03:24:56 +0000 | [diff] [blame] | 128 | DWORD millisecondsToWait = INFINITE; |
| 129 | if (secondsToWait > 0) |
| 130 | millisecondsToWait = secondsToWait * 1000; |
| 131 | |
| 132 | if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) { |
| 133 | if (!TerminateProcess(pi.hProcess, 1)) { |
| 134 | ThrowError(std::string("Failed to terminate timed-out program '") + |
| 135 | path.toString() + "'"); |
| 136 | } |
| 137 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 138 | } |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 139 | |
| 140 | // Get its exit status. |
| 141 | DWORD status; |
| 142 | BOOL rc = GetExitCodeProcess(pi.hProcess, &status); |
| 143 | |
| 144 | // Done with the handles; go close them. |
| 145 | CloseHandle(pi.hProcess); |
| 146 | CloseHandle(pi.hThread); |
| 147 | |
| 148 | if (!rc) |
Reid Spencer | 707a27c | 2004-12-11 17:37:01 +0000 | [diff] [blame] | 149 | ThrowError(std::string("Failed getting status for program '") + |
| 150 | path.toString() + "'"); |
Reid Spencer | b016a37 | 2004-09-15 05:49:50 +0000 | [diff] [blame] | 151 | |
| 152 | return status; |
| 153 | } |
| 154 | |
| 155 | } |
Reid Spencer | 52a7efa | 2004-08-29 19:20:41 +0000 | [diff] [blame] | 156 | // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab |