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