1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
35 #include "os_string.hpp"
42 * Trick from http://locklessinc.com/articles/pthreads_on_windows/
44 static CRITICAL_SECTION
46 (PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0
53 EnterCriticalSection(&criticalSection);
60 LeaveCriticalSection(&criticalSection);
68 size_t size = MAX_PATH;
69 char *buf = path.buf(size);
71 DWORD nWritten = GetModuleFileNameA(NULL, buf, size);
83 size_t size = MAX_PATH;
84 char *buf = path.buf(size);
86 DWORD ret = GetCurrentDirectoryA(size, buf);
96 String::exists(void) const
98 DWORD attrs = GetFileAttributesA(str());
99 return attrs != INVALID_FILE_ATTRIBUTES;
103 * Determine whether an argument should be quoted.
106 needsQuote(const char *arg)
114 if (c == ' ' || c == '\t' || c == '\"') {
131 quoteArg(std::string &s, const char *arg)
134 unsigned backslashes = 0;
142 } else if (c == '"') {
143 while (backslashes) {
160 int execute(char * const * args)
162 std::string commandLine;
164 const char *arg0 = *args;
167 while ((arg = *args++) != NULL) {
169 commandLine.push_back(sep);
172 if (needsQuote(arg)) {
173 quoteArg(commandLine, arg);
175 commandLine.append(arg);
181 STARTUPINFO startupInfo;
182 memset(&startupInfo, 0, sizeof(startupInfo));
183 startupInfo.cb = sizeof(startupInfo);
185 PROCESS_INFORMATION processInformation;
187 if (!CreateProcessA(NULL,
188 const_cast<char *>(commandLine.c_str()), // only modified by CreateProcessW
189 0, // process attributes
190 0, // thread attributes
191 FALSE, // inherit handles
192 0, // creation flags,
194 NULL, // current directory
198 log("error: failed to execute %s\n", arg0);
202 WaitForSingleObject(processInformation.hProcess, INFINITE);
205 GetExitCodeProcess(processInformation.hProcess, &exitCode);
207 CloseHandle(processInformation.hProcess);
208 CloseHandle(processInformation.hThread);
210 return (int)exitCode;
214 log(const char *format, ...)
219 va_start(ap, format);
221 vsnprintf(buf, sizeof buf, format, ap);
224 OutputDebugStringA(buf);
227 * Also write the message to stderr, when a debugger is not present (to
228 * avoid duplicate messages in command line debuggers).
230 #if _WIN32_WINNT > 0x0400
231 if (!IsDebuggerPresent()) {
242 static LARGE_INTEGER frequency;
243 LARGE_INTEGER counter;
244 if (!frequency.QuadPart)
245 QueryPerformanceFrequency(&frequency);
246 QueryPerformanceCounter(&counter);
247 return counter.QuadPart*1000000LL/frequency.QuadPart;
261 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
262 static void (*gCallback)(void) = NULL;
265 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
271 if (prevExceptionFilter) {
272 return prevExceptionFilter(pExceptionInfo);
274 return EXCEPTION_CONTINUE_SEARCH;
279 setExceptionCallback(void (*callback)(void))
284 gCallback = callback;
286 assert(!prevExceptionFilter);
289 * TODO: Unfortunately it seems that the CRT will reset the exception
290 * handler in certain circumnstances. See
291 * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
293 prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
298 resetExceptionCallback(void)