1 //===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Jeff Cohen and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provides the Win32 specific implementation of the Program class.
12 //===----------------------------------------------------------------------===//
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only Win32 specific code
19 //=== and must not be UNIX code
20 //===----------------------------------------------------------------------===//
25 // This function just uses the PATH environment variable to find the program.
27 Program::FindProgramByName(const std::string& progName) {
29 // Check some degenerate cases
30 if (progName.length() == 0) // no program
33 if (!temp.setFile(progName)) // invalid name
35 if (temp.executable()) // already executable as is
38 // At this point, the file name is valid and its not executable.
39 // Let Windows search for it.
40 char buffer[MAX_PATH];
42 DWORD len = SearchPath(NULL, progName.c_str(), ".exe", MAX_PATH,
45 // See if it wasn't found.
49 // See if we got the entire path.
53 // Buffer was too small; grow and retry.
55 char *b = reinterpret_cast<char *>(_alloca(len+1));
56 DWORD len2 = SearchPath(NULL, progName.c_str(), ".exe", len+1, b, &dummy);
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...
71 Program::ExecuteAndWait(const Path& path,
74 const Path** redirects,
75 unsigned secondsToWait) {
76 if (!path.executable())
77 throw path.toString() + " is not executable";
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.
83 // First, determine the length of the command line.
85 for (unsigned i = 0; args[i]; i++) {
86 len += strlen(args[i]) + 1;
87 if (strchr(args[i], ' '))
91 // Now build the command line.
92 char *command = reinterpret_cast<char *>(_alloca(len));
95 for (unsigned i = 0; args[i]; i++) {
96 const char *arg = args[i];
97 size_t len = strlen(arg);
98 bool needsQuoting = strchr(arg, ' ') != 0;
110 // Create a child process.
112 memset(&si, 0, sizeof(si));
115 // TODO: do replacement of standard input/output/error handles.
117 PROCESS_INFORMATION pi;
118 memset(&pi, 0, sizeof(pi));
120 if (!CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
121 envp, NULL, &si, &pi))
123 ThrowError(std::string("Couldn't execute program '") +
124 path.toString() + "'");
127 // Wait for it to terminate.
128 DWORD millisecondsToWait = INFINITE;
129 if (secondsToWait > 0)
130 millisecondsToWait = secondsToWait * 1000;
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() + "'");
137 WaitForSingleObject(pi.hProcess, INFINITE);
140 // Get its exit status.
142 BOOL rc = GetExitCodeProcess(pi.hProcess, &status);
144 // Done with the handles; go close them.
145 CloseHandle(pi.hProcess);
146 CloseHandle(pi.hThread);
149 ThrowError(std::string("Failed getting status for program '") +
150 path.toString() + "'");
156 // vim: sw=2 smartindent smarttab tw=80 autoindent expandtab