blob: 2d3580d3db35715bd9f6cd3fe8d4bb110882bed9 [file] [log] [blame]
Reid Spencerb016a372004-09-15 05:49:50 +00001//===- Win32/Program.cpp - Win32 Program Implementation ------- -*- C++ -*-===//
Reid Spencer52a7efa2004-08-29 19:20:41 +00002//
3// The LLVM Compiler Infrastructure
4//
Reid Spencerb016a372004-09-15 05:49:50 +00005// This file was developed by Jeff Cohen and is distributed under the
Reid Spencer52a7efa2004-08-29 19:20:41 +00006// 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 Spencerb016a372004-09-15 05:49:50 +000014#include "Win32.h"
Reid Spencerb016a372004-09-15 05:49:50 +000015#include <malloc.h>
16
Reid Spencer52a7efa2004-08-29 19:20:41 +000017//===----------------------------------------------------------------------===//
Reid Spencerb016a372004-09-15 05:49:50 +000018//=== WARNING: Implementation here must contain only Win32 specific code
19//=== and must not be UNIX code
Reid Spencer52a7efa2004-08-29 19:20:41 +000020//===----------------------------------------------------------------------===//
21
Reid Spencerb016a372004-09-15 05:49:50 +000022namespace llvm {
23using namespace sys;
24
25// This function just uses the PATH environment variable to find the program.
26Path
27Program::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 Spencer07adb282004-11-05 22:15:36 +000033 if (!temp.setFile(progName)) // invalid name
Reid Spencerb016a372004-09-15 05:49:50 +000034 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//
70int
71Program::ExecuteAndWait(const Path& path,
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000072 const char** args,
73 const char** envp,
74 const Path** redirects,
75 unsigned secondsToWait) {
Reid Spencerb016a372004-09-15 05:49:50 +000076 if (!path.executable())
Reid Spencer707a27c2004-12-11 17:37:01 +000077 throw path.toString() + " is not executable";
Reid Spencerb016a372004-09-15 05:49:50 +000078
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.
84 std::string progname(path.getLast());
85 unsigned len = progname.length() + 1;
86 if (progname.find(' ') != std::string::npos)
87 len += 2;
88
Jeff Cohena1b3d3d2004-12-20 03:24:56 +000089 for (unsigned i = 0; args[i]; i++) {
90 len += strlen(args[i]) + 1;
91 if (strchr(args[i], ' '))
Reid Spencerb016a372004-09-15 05:49:50 +000092 len += 2;
93 }
94
95 // Now build the command line.
96 char *command = reinterpret_cast<char *>(_alloca(len));
97 char *p = command;
98
99 bool needsQuoting = progname.find(' ') != std::string::npos;
100 if (needsQuoting)
101 *p++ = '"';
102 memcpy(p, progname.c_str(), progname.length());
103 p += progname.length();
104 if (needsQuoting)
105 *p++ = '"';
106 *p++ = ' ';
107
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000108 for (unsigned i = 0; args[i]; i++) {
109 const char *arg = args[i];
110 size_t len = strlen(arg);
111 needsQuoting = strchr(arg, ' ') != 0;
Reid Spencerb016a372004-09-15 05:49:50 +0000112 if (needsQuoting)
113 *p++ = '"';
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000114 memcpy(p, arg, len);
115 p += len;
Reid Spencerb016a372004-09-15 05:49:50 +0000116 if (needsQuoting)
117 *p++ = '"';
118 *p++ = ' ';
119 }
120
121 *p = 0;
122
123 // Create a child process.
124 STARTUPINFO si;
125 memset(&si, 0, sizeof(si));
126 si.cb = sizeof(si);
127
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000128 // TODO: do replacement of standard input/output/error handles.
129
Reid Spencerb016a372004-09-15 05:49:50 +0000130 PROCESS_INFORMATION pi;
131 memset(&pi, 0, sizeof(pi));
132
Reid Spencer707a27c2004-12-11 17:37:01 +0000133 if (!CreateProcess(path.c_str(), command, NULL, NULL, FALSE, 0,
Jeff Cohen626e38e2004-12-14 05:26:43 +0000134 envp, NULL, &si, &pi))
Reid Spencerb016a372004-09-15 05:49:50 +0000135 {
Reid Spencer707a27c2004-12-11 17:37:01 +0000136 ThrowError(std::string("Couldn't execute program '") +
137 path.toString() + "'");
Reid Spencerb016a372004-09-15 05:49:50 +0000138 }
139
140 // Wait for it to terminate.
Jeff Cohena1b3d3d2004-12-20 03:24:56 +0000141 DWORD millisecondsToWait = INFINITE;
142 if (secondsToWait > 0)
143 millisecondsToWait = secondsToWait * 1000;
144
145 if (WaitForSingleObject(pi.hProcess, millisecondsToWait) == WAIT_TIMEOUT) {
146 if (!TerminateProcess(pi.hProcess, 1)) {
147 ThrowError(std::string("Failed to terminate timed-out program '") +
148 path.toString() + "'");
149 }
150 WaitForSingleObject(pi.hProcess, INFINITE);
151 }
Reid Spencerb016a372004-09-15 05:49:50 +0000152
153 // Get its exit status.
154 DWORD status;
155 BOOL rc = GetExitCodeProcess(pi.hProcess, &status);
156
157 // Done with the handles; go close them.
158 CloseHandle(pi.hProcess);
159 CloseHandle(pi.hThread);
160
161 if (!rc)
Reid Spencer707a27c2004-12-11 17:37:01 +0000162 ThrowError(std::string("Failed getting status for program '") +
163 path.toString() + "'");
Reid Spencerb016a372004-09-15 05:49:50 +0000164
165 return status;
166}
167
168}
Reid Spencer52a7efa2004-08-29 19:20:41 +0000169// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab