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);
201 WaitForSingleObject(processInformation.hProcess, INFINITE);
204 GetExitCodeProcess(processInformation.hProcess, &exitCode);
206 CloseHandle(processInformation.hProcess);
207 CloseHandle(processInformation.hThread);
209 return (int)exitCode;
213 log(const char *format, ...)
218 va_start(ap, format);
220 vsnprintf(buf, sizeof buf, format, ap);
223 OutputDebugStringA(buf);
226 * Also write the message to stderr, when a debugger is not present (to
227 * avoid duplicate messages in command line debuggers).
229 #if _WIN32_WINNT > 0x0400
230 if (!IsDebuggerPresent()) {
241 static LARGE_INTEGER frequency;
242 LARGE_INTEGER counter;
243 if (!frequency.QuadPart)
244 QueryPerformanceFrequency(&frequency);
245 QueryPerformanceCounter(&counter);
246 return counter.QuadPart*1000000LL/frequency.QuadPart;
260 static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
261 static void (*gCallback)(void) = NULL;
264 unhandledExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
270 if (prevExceptionFilter) {
271 return prevExceptionFilter(pExceptionInfo);
273 return EXCEPTION_CONTINUE_SEARCH;
278 setExceptionCallback(void (*callback)(void))
283 gCallback = callback;
285 assert(!prevExceptionFilter);
288 * TODO: Unfortunately it seems that the CRT will reset the exception
289 * handler in certain circumnstances. See
290 * http://www.codeproject.com/KB/winsdk/crash_hook.aspx
292 prevExceptionFilter = SetUnhandledExceptionFilter(unhandledExceptionFilter);
297 resetExceptionCallback(void)