wined3d: Move texture2d_load_drawable() to texture.c.
[wine.git] / programs / start / start.c
blob18fd5f517f66aabff2dc648a68a488493e20ad07
1 /*
2 * Start a program using ShellExecuteEx, optionally wait for it to finish
3 * Compatible with Microsoft's "c:\windows\command\start.exe"
5 * Copyright 2003 Dan Kegel
6 * Copyright 2007 Lyutin Anatoly (Etersoft)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <windows.h>
26 #include <shlobj.h>
27 #include <shellapi.h>
29 #include <wine/debug.h>
31 #include "resources.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(start);
35 /**
36 Output given message to stdout without formatting.
38 static void output(const WCHAR *message)
40 DWORD count;
41 DWORD res;
42 int wlen = lstrlenW(message);
44 if (!wlen) return;
46 res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), message, wlen, &count, NULL);
48 /* If writing to console fails, assume it's file
49 * i/o so convert to OEM codepage and output
51 if (!res)
53 DWORD len;
54 char *mesA;
55 /* Convert to OEM, then output */
56 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL );
57 mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char));
58 if (!mesA) return;
59 WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL );
60 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE);
61 HeapFree(GetProcessHeap(), 0, mesA);
65 /**
66 Output given message from string table,
67 followed by ": ",
68 followed by description of given GetLastError() value to stdout,
69 followed by a trailing newline,
70 then terminate.
73 static void fatal_error(const WCHAR *msg, DWORD error_code, const WCHAR *filename)
75 DWORD_PTR args[1];
76 LPVOID lpMsgBuf;
77 int status;
78 static const WCHAR colonsW[] = { ':', ' ', 0 };
79 static const WCHAR newlineW[] = { '\n', 0 };
81 output(msg);
82 output(colonsW);
83 args[0] = (DWORD_PTR)filename;
84 status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
85 NULL, error_code, 0, (LPWSTR)&lpMsgBuf, 0, (__ms_va_list *)args );
86 if (!status)
88 WINE_ERR("FormatMessage failed\n");
89 } else
91 output(lpMsgBuf);
92 LocalFree((HLOCAL) lpMsgBuf);
93 output(newlineW);
95 ExitProcess(1);
98 static void fatal_string_error(int which, DWORD error_code, const WCHAR *filename)
100 WCHAR msg[2048];
102 if (!LoadStringW(GetModuleHandleW(NULL), which, msg, ARRAY_SIZE(msg)))
103 WINE_ERR("LoadString failed, error %d\n", GetLastError());
105 fatal_error(msg, error_code, filename);
108 static void fatal_string(int which)
110 WCHAR msg[2048];
112 if (!LoadStringW(GetModuleHandleW(NULL), which, msg, ARRAY_SIZE(msg)))
113 WINE_ERR("LoadString failed, error %d\n", GetLastError());
115 output(msg);
116 ExitProcess(1);
119 static void usage(void)
121 fatal_string(STRING_USAGE);
124 static WCHAR *build_args( int argc, WCHAR **argvW )
126 int i, wlen = 1;
127 WCHAR *ret, *p;
128 static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
129 static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
131 for (i = 0; i < argc; i++ )
133 wlen += lstrlenW(argvW[i]) + 1;
134 if (wcschr(argvW[i], ' '))
135 wlen += 2;
137 ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
138 ret[0] = 0;
140 for (i = 0, p = ret; i < argc; i++ )
142 if (wcschr(argvW[i], ' '))
143 p += swprintf(p, wlen - (p - ret), FormatQuotesW, argvW[i]);
144 else
145 p += swprintf(p, wlen - (p - ret), FormatW, argvW[i]);
147 return ret;
150 static WCHAR *get_parent_dir(WCHAR* path)
152 WCHAR *last_slash;
153 WCHAR *result;
154 int len;
156 last_slash = wcsrchr( path, '\\' );
157 if (last_slash == NULL)
158 len = 1;
159 else
160 len = last_slash - path + 1;
162 result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
163 CopyMemory(result, path, (len-1)*sizeof(WCHAR));
164 result[len-1] = '\0';
166 return result;
169 static BOOL is_option(const WCHAR* arg, const WCHAR* opt)
171 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
172 arg, -1, opt, -1) == CSTR_EQUAL;
175 int wmain (int argc, WCHAR *argv[])
177 SHELLEXECUTEINFOW sei;
178 DWORD creation_flags;
179 WCHAR *args = NULL;
180 int i;
181 BOOL unix_mode = FALSE;
182 BOOL progid_open = FALSE;
183 WCHAR *title = NULL;
184 WCHAR *dos_filename = NULL;
185 WCHAR *parent_directory = NULL;
186 DWORD binary_type;
188 static const WCHAR bW[] = { '/', 'b', 0 };
189 static const WCHAR minW[] = { '/', 'm', 'i', 'n', 0 };
190 static const WCHAR maxW[] = { '/', 'm', 'a', 'x', 0 };
191 static const WCHAR lowW[] = { '/', 'l', 'o', 'w', 0 };
192 static const WCHAR normalW[] = { '/', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
193 static const WCHAR highW[] = { '/', 'h', 'i', 'g', 'h', 0 };
194 static const WCHAR realtimeW[] = { '/', 'r', 'e', 'a', 'l', 't', 'i', 'm', 'e', 0 };
195 static const WCHAR abovenormalW[] = { '/', 'a', 'b', 'o', 'v', 'e', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
196 static const WCHAR belownormalW[] = { '/', 'b', 'e', 'l', 'o', 'w', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
197 static const WCHAR separateW[] = { '/', 's', 'e', 'p', 'a', 'r', 'a', 't', 'e', 0 };
198 static const WCHAR sharedW[] = { '/', 's', 'h', 'a', 'r', 'e', 'd', 0 };
199 static const WCHAR nodeW[] = { '/', 'n', 'o', 'd', 'e', 0 };
200 static const WCHAR affinityW[] = { '/', 'a', 'f', 'f', 'i', 'n', 'i', 't', 'y', 0 };
201 static const WCHAR wW[] = { '/', 'w', 0 };
202 static const WCHAR waitW[] = { '/', 'w', 'a', 'i', 't', 0 };
203 static const WCHAR helpW[] = { '/', '?', 0 };
204 static const WCHAR unixW[] = { '/', 'u', 'n', 'i', 'x', 0 };
205 static const WCHAR progIDOpenW[] =
206 { '/', 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
207 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
208 static const WCHAR cmdW[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', 0 };
210 memset(&sei, 0, sizeof(sei));
211 sei.cbSize = sizeof(sei);
212 sei.lpVerb = openW;
213 sei.nShow = SW_SHOWNORMAL;
214 /* Dunno what these mean, but it looks like winMe's start uses them */
215 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
216 SEE_MASK_FLAG_NO_UI;
217 sei.lpDirectory = NULL;
218 creation_flags = CREATE_NEW_CONSOLE;
220 /* Canonical Microsoft commandline flag processing:
221 * flags start with / and are case insensitive.
223 for (i=1; i<argc; i++) {
224 /* parse first quoted argument as console title */
225 if (!title && argv[i][0] == '"') {
226 title = argv[i];
227 continue;
229 if (argv[i][0] != '/')
230 break;
232 /* Unix paths can start with / so we have to assume anything following /unix is not a flag */
233 if (unix_mode || progid_open)
234 break;
236 if (argv[i][0] == '/' && (argv[i][1] == 'd' || argv[i][1] == 'D')) {
237 if (argv[i][2])
238 /* The start directory was concatenated to the option */
239 sei.lpDirectory = argv[i]+2;
240 else if (i+1 == argc) {
241 WINE_ERR("you must specify a directory path for the /d option\n");
242 usage();
243 } else
244 sei.lpDirectory = argv[++i];
246 else if (is_option(argv[i], bW)) {
247 creation_flags &= ~CREATE_NEW_CONSOLE;
249 else if (argv[i][0] == '/' && (argv[i][1] == 'i' || argv[i][1] == 'I')) {
250 TRACE("/i is ignored\n"); /* FIXME */
252 else if (is_option(argv[i], minW)) {
253 sei.nShow = SW_SHOWMINIMIZED;
255 else if (is_option(argv[i], maxW)) {
256 sei.nShow = SW_SHOWMAXIMIZED;
258 else if (is_option(argv[i], lowW)) {
259 creation_flags |= IDLE_PRIORITY_CLASS;
261 else if (is_option(argv[i], normalW)) {
262 creation_flags |= NORMAL_PRIORITY_CLASS;
264 else if (is_option(argv[i], highW)) {
265 creation_flags |= HIGH_PRIORITY_CLASS;
267 else if (is_option(argv[i], realtimeW)) {
268 creation_flags |= REALTIME_PRIORITY_CLASS;
270 else if (is_option(argv[i], abovenormalW)) {
271 creation_flags |= ABOVE_NORMAL_PRIORITY_CLASS;
273 else if (is_option(argv[i], belownormalW)) {
274 creation_flags |= BELOW_NORMAL_PRIORITY_CLASS;
276 else if (is_option(argv[i], separateW)) {
277 TRACE("/separate is ignored\n"); /* FIXME */
279 else if (is_option(argv[i], sharedW)) {
280 TRACE("/shared is ignored\n"); /* FIXME */
282 else if (is_option(argv[i], nodeW)) {
283 if (i+1 == argc) {
284 WINE_ERR("you must specify a numa node for the /node option\n");
285 usage();
286 } else
288 TRACE("/node is ignored\n"); /* FIXME */
289 i++;
292 else if (is_option(argv[i], affinityW))
294 if (i+1 == argc) {
295 WINE_ERR("you must specify a numa node for the /node option\n");
296 usage();
297 } else
299 TRACE("/affinity is ignored\n"); /* FIXME */
300 i++;
303 else if (is_option(argv[i], wW) || is_option(argv[i], waitW)) {
304 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
306 else if (is_option(argv[i], helpW)) {
307 usage();
310 /* Wine extensions */
312 else if (is_option(argv[i], unixW)) {
313 unix_mode = TRUE;
315 else if (is_option(argv[i], progIDOpenW)) {
316 progid_open = TRUE;
317 } else
320 WINE_ERR("Unknown option '%s'\n", wine_dbgstr_w(argv[i]));
321 usage();
325 if (progid_open) {
326 if (i == argc)
327 usage();
328 sei.lpClass = argv[i++];
329 sei.fMask |= SEE_MASK_CLASSNAME;
332 if (i == argc) {
333 if (progid_open || unix_mode)
334 usage();
335 sei.lpFile = cmdW;
337 else
338 sei.lpFile = argv[i++];
340 args = build_args( argc - i, &argv[i] );
341 sei.lpParameters = args;
343 if (unix_mode || progid_open) {
344 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
345 char* multibyte_unixpath;
346 int multibyte_unixpath_len;
348 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
350 if (!wine_get_dos_file_name_ptr)
351 fatal_string(STRING_UNIXFAIL);
353 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
354 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
356 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
358 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
360 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
362 if (!dos_filename)
363 fatal_string(STRING_UNIXFAIL);
365 sei.lpFile = dos_filename;
366 if (!sei.lpDirectory)
367 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
368 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
370 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
371 WCHAR *commandline;
372 STARTUPINFOW startup_info;
373 PROCESS_INFORMATION process_information;
374 static const WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
376 /* explorer on windows always quotes the filename when running a binary on windows (see bug 5224) so we have to use CreateProcessW in this case */
378 commandline = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(sei.lpFile)+3+lstrlenW(sei.lpParameters))*sizeof(WCHAR));
379 swprintf(commandline, lstrlenW(sei.lpFile) + 3 + lstrlenW(sei.lpParameters),
380 commandlineformat, sei.lpFile, sei.lpParameters);
382 ZeroMemory(&startup_info, sizeof(startup_info));
383 startup_info.cb = sizeof(startup_info);
384 startup_info.lpTitle = title;
386 if (!CreateProcessW(
387 NULL, /* lpApplicationName */
388 commandline, /* lpCommandLine */
389 NULL, /* lpProcessAttributes */
390 NULL, /* lpThreadAttributes */
391 FALSE, /* bInheritHandles */
392 creation_flags, /* dwCreationFlags */
393 NULL, /* lpEnvironment */
394 sei.lpDirectory, /* lpCurrentDirectory */
395 &startup_info, /* lpStartupInfo */
396 &process_information /* lpProcessInformation */ ))
398 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
400 sei.hProcess = process_information.hProcess;
401 goto done;
405 if (!ShellExecuteExW(&sei))
407 static const WCHAR pathextW[] = {'P','A','T','H','E','X','T',0};
408 const WCHAR *filename = sei.lpFile;
409 DWORD size, filename_len;
410 WCHAR *name, *env;
412 size = GetEnvironmentVariableW(pathextW, NULL, 0);
413 if (size)
415 WCHAR *start, *ptr;
417 env = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
418 if (!env)
419 fatal_string_error(STRING_EXECFAIL, ERROR_OUTOFMEMORY, sei.lpFile);
420 GetEnvironmentVariableW(pathextW, env, size);
422 filename_len = lstrlenW(filename);
423 name = HeapAlloc(GetProcessHeap(), 0, (filename_len + size) * sizeof(WCHAR));
424 if (!name)
425 fatal_string_error(STRING_EXECFAIL, ERROR_OUTOFMEMORY, sei.lpFile);
427 sei.lpFile = name;
428 start = env;
429 while ((ptr = wcschr(start, ';')))
431 if (start == ptr)
433 start = ptr + 1;
434 continue;
437 lstrcpyW(name, filename);
438 memcpy(&name[filename_len], start, (ptr - start) * sizeof(WCHAR));
439 name[filename_len + (ptr - start)] = 0;
441 if (ShellExecuteExW(&sei))
443 HeapFree(GetProcessHeap(), 0, name);
444 HeapFree(GetProcessHeap(), 0, env);
445 goto done;
448 start = ptr + 1;
453 fatal_string_error(STRING_EXECFAIL, GetLastError(), filename);
456 done:
457 HeapFree( GetProcessHeap(), 0, args );
458 HeapFree( GetProcessHeap(), 0, dos_filename );
459 HeapFree( GetProcessHeap(), 0, parent_directory );
461 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
462 DWORD exitcode;
463 WaitForSingleObject(sei.hProcess, INFINITE);
464 GetExitCodeProcess(sei.hProcess, &exitcode);
465 ExitProcess(exitcode);
468 ExitProcess(0);