d3dcompiler: Don't allow semantics on void functions.
[wine/multimedia.git] / programs / start / start.c
blobf3d934eeed25c53e195aa9c33bdcb78bfe153e67
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/unicode.h>
30 #include <wine/debug.h>
32 #include "resources.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(start);
36 /**
37 Output given message to stdout without formatting.
39 static void output(const WCHAR *message)
41 DWORD count;
42 DWORD res;
43 int wlen = strlenW(message);
45 if (!wlen) return;
47 res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), message, wlen, &count, NULL);
49 /* If writing to console fails, assume it's file
50 * i/o so convert to OEM codepage and output
52 if (!res)
54 DWORD len;
55 char *mesA;
56 /* Convert to OEM, then output */
57 len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL );
58 mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char));
59 if (!mesA) return;
60 WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL );
61 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE);
62 HeapFree(GetProcessHeap(), 0, mesA);
66 /**
67 Output given message from string table,
68 followed by ": ",
69 followed by description of given GetLastError() value to stdout,
70 followed by a trailing newline,
71 then terminate.
74 static void fatal_error(const WCHAR *msg, DWORD error_code, const WCHAR *filename)
76 DWORD_PTR args[1];
77 LPVOID lpMsgBuf;
78 int status;
79 static const WCHAR colonsW[] = { ':', ' ', 0 };
80 static const WCHAR newlineW[] = { '\n', 0 };
82 output(msg);
83 output(colonsW);
84 args[0] = (DWORD_PTR)filename;
85 status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
86 NULL, error_code, 0, (LPWSTR)&lpMsgBuf, 0, (__ms_va_list *)args );
87 if (!status)
89 WINE_ERR("FormatMessage failed\n");
90 } else
92 output(lpMsgBuf);
93 LocalFree((HLOCAL) lpMsgBuf);
94 output(newlineW);
96 ExitProcess(1);
99 static void fatal_string_error(int which, DWORD error_code, const WCHAR *filename)
101 WCHAR msg[2048];
103 if (!LoadStringW(GetModuleHandleW(NULL), which,
104 msg, sizeof(msg)/sizeof(WCHAR)))
105 WINE_ERR("LoadString failed, error %d\n", GetLastError());
107 fatal_error(msg, error_code, filename);
110 static void fatal_string(int which)
112 WCHAR msg[2048];
114 if (!LoadStringW(GetModuleHandleW(NULL), which,
115 msg, sizeof(msg)/sizeof(WCHAR)))
116 WINE_ERR("LoadString failed, error %d\n", GetLastError());
118 output(msg);
119 ExitProcess(1);
122 static void usage(void)
124 fatal_string(STRING_USAGE);
127 static WCHAR *build_args( int argc, WCHAR **argvW )
129 int i, wlen = 1;
130 WCHAR *ret, *p;
131 static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
132 static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
134 for (i = 0; i < argc; i++ )
136 wlen += strlenW(argvW[i]) + 1;
137 if (strchrW(argvW[i], ' '))
138 wlen += 2;
140 ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
141 ret[0] = 0;
143 for (i = 0, p = ret; i < argc; i++ )
145 if (strchrW(argvW[i], ' '))
146 p += sprintfW(p, FormatQuotesW, argvW[i]);
147 else
148 p += sprintfW(p, FormatW, argvW[i]);
150 return ret;
153 static WCHAR *get_parent_dir(WCHAR* path)
155 WCHAR *last_slash;
156 WCHAR *result;
157 int len;
159 last_slash = strrchrW( path, '\\' );
160 if (last_slash == NULL)
161 len = 1;
162 else
163 len = last_slash - path + 1;
165 result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
166 CopyMemory(result, path, (len-1)*sizeof(WCHAR));
167 result[len-1] = '\0';
169 return result;
172 static BOOL is_option(const WCHAR* arg, const WCHAR* opt)
174 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
175 arg, -1, opt, -1) == CSTR_EQUAL;
178 int wmain (int argc, WCHAR *argv[])
180 SHELLEXECUTEINFOW sei;
181 DWORD creation_flags;
182 WCHAR *args = NULL;
183 int i;
184 int unix_mode = 0;
185 int progid_open = 0;
186 WCHAR *title = NULL;
187 WCHAR *dos_filename = NULL;
188 WCHAR *parent_directory = NULL;
189 DWORD binary_type;
191 static const WCHAR bW[] = { '/', 'b', 0 };
192 static const WCHAR minW[] = { '/', 'm', 'i', 'n', 0 };
193 static const WCHAR maxW[] = { '/', 'm', 'a', 'x', 0 };
194 static const WCHAR lowW[] = { '/', 'l', 'o', 'w', 0 };
195 static const WCHAR normalW[] = { '/', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
196 static const WCHAR highW[] = { '/', 'h', 'i', 'g', 'h', 0 };
197 static const WCHAR realtimeW[] = { '/', 'r', 'e', 'a', 'l', 't', 'i', 'm', 'e', 0 };
198 static const WCHAR abovenormalW[] = { '/', 'a', 'b', 'o', 'v', 'e', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
199 static const WCHAR belownormalW[] = { '/', 'b', 'e', 'l', 'o', 'w', 'n', 'o', 'r', 'm', 'a', 'l', 0 };
200 static const WCHAR separateW[] = { '/', 's', 'e', 'p', 'a', 'r', 'a', 't', 'e', 0 };
201 static const WCHAR sharedW[] = { '/', 's', 'h', 'a', 'r', 'e', 'd', 0 };
202 static const WCHAR nodeW[] = { '/', 'n', 'o', 'd', 'e', 0 };
203 static const WCHAR affinityW[] = { '/', 'a', 'f', 'f', 'i', 'n', 'i', 't', 'y', 0 };
204 static const WCHAR wW[] = { '/', 'w', 0 };
205 static const WCHAR waitW[] = { '/', 'w', 'a', 'i', 't', 0 };
206 static const WCHAR helpW[] = { '/', '?', 0 };
207 static const WCHAR unixW[] = { '/', 'u', 'n', 'i', 'x', 0 };
208 static const WCHAR progIDOpenW[] =
209 { '/', 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
210 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
211 static const WCHAR cmdW[] = { 'c', 'm', 'd', '.', 'e', 'x', 'e', 0 };
213 memset(&sei, 0, sizeof(sei));
214 sei.cbSize = sizeof(sei);
215 sei.lpVerb = openW;
216 sei.nShow = SW_SHOWNORMAL;
217 /* Dunno what these mean, but it looks like winMe's start uses them */
218 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
219 SEE_MASK_FLAG_NO_UI|
220 SEE_MASK_NO_CONSOLE;
221 sei.lpDirectory = NULL;
222 creation_flags = CREATE_NEW_CONSOLE;
224 /* Canonical Microsoft commandline flag processing:
225 * flags start with / and are case insensitive.
227 for (i=1; i<argc; i++) {
228 if (argv[i][0] == '"') {
229 title = argv[i];
230 continue;
232 if (argv[i][0] != '/')
233 break;
235 /* Unix paths can start with / so we have to assume anything following /unix is not a flag */
236 if (unix_mode || progid_open)
237 break;
239 if (argv[i][0] == '/' && (argv[i][1] == 'd' || argv[i][1] == 'D')) {
240 if (argv[i][2])
241 /* The start directory was concatenated to the option */
242 sei.lpDirectory = argv[i]+2;
243 else if (i+1 == argc) {
244 WINE_ERR("you must specify a directory path for the /d option\n");
245 usage();
246 } else
247 sei.lpDirectory = argv[++i];
249 else if (is_option(argv[i], bW)) {
250 creation_flags &= !CREATE_NEW_CONSOLE;
252 else if (argv[i][0] == '/' && (argv[i][1] == 'i' || argv[i][1] == 'I')) {
253 TRACE("/i is ignored\n"); /* FIXME */
255 else if (is_option(argv[i], minW)) {
256 sei.nShow = SW_SHOWMINIMIZED;
258 else if (is_option(argv[i], maxW)) {
259 sei.nShow = SW_SHOWMAXIMIZED;
261 else if (is_option(argv[i], lowW)) {
262 creation_flags |= IDLE_PRIORITY_CLASS;
264 else if (is_option(argv[i], normalW)) {
265 creation_flags |= NORMAL_PRIORITY_CLASS;
267 else if (is_option(argv[i], highW)) {
268 creation_flags |= HIGH_PRIORITY_CLASS;
270 else if (is_option(argv[i], realtimeW)) {
271 creation_flags |= REALTIME_PRIORITY_CLASS;
273 else if (is_option(argv[i], abovenormalW)) {
274 creation_flags |= ABOVE_NORMAL_PRIORITY_CLASS;
276 else if (is_option(argv[i], belownormalW)) {
277 creation_flags |= BELOW_NORMAL_PRIORITY_CLASS;
279 else if (is_option(argv[i], separateW)) {
280 TRACE("/separate is ignored\n"); /* FIXME */
282 else if (is_option(argv[i], sharedW)) {
283 TRACE("/shared is ignored\n"); /* FIXME */
285 else if (is_option(argv[i], nodeW)) {
286 if (i+1 == argc) {
287 WINE_ERR("you must specify a numa node for the /node option\n");
288 usage();
289 } else
291 TRACE("/node is ignored\n"); /* FIXME */
292 i++;
295 else if (is_option(argv[i], affinityW))
297 if (i+1 == argc) {
298 WINE_ERR("you must specify a numa node for the /node option\n");
299 usage();
300 } else
302 TRACE("/affinity is ignored\n"); /* FIXME */
303 i++;
306 else if (is_option(argv[i], wW) || is_option(argv[i], waitW)) {
307 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
309 else if (is_option(argv[i], helpW)) {
310 usage();
313 /* Wine extensions */
315 else if (is_option(argv[i], unixW)) {
316 unix_mode = 1;
318 else if (is_option(argv[i], progIDOpenW)) {
319 progid_open = 1;
320 } else
323 WINE_ERR("Unknown option '%s'\n", wine_dbgstr_w(argv[i]));
324 usage();
328 if (progid_open) {
329 if (i == argc)
330 usage();
331 sei.lpClass = argv[i++];
332 sei.fMask |= SEE_MASK_CLASSNAME;
335 if (i == argc) {
336 if (progid_open || unix_mode)
337 usage();
338 sei.lpFile = cmdW;
340 else
341 sei.lpFile = argv[i++];
343 args = build_args( argc - i, &argv[i] );
344 sei.lpParameters = args;
346 if (unix_mode || progid_open) {
347 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
348 char* multibyte_unixpath;
349 int multibyte_unixpath_len;
351 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
353 if (!wine_get_dos_file_name_ptr)
354 fatal_string(STRING_UNIXFAIL);
356 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
357 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
359 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
361 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
363 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
365 if (!dos_filename)
366 fatal_string(STRING_UNIXFAIL);
368 sei.lpFile = dos_filename;
369 if (!sei.lpDirectory)
370 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
371 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
373 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
374 WCHAR *commandline;
375 STARTUPINFOW startup_info;
376 PROCESS_INFORMATION process_information;
377 static WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
379 /* 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 */
381 commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
382 sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
384 ZeroMemory(&startup_info, sizeof(startup_info));
385 startup_info.cb = sizeof(startup_info);
386 startup_info.lpTitle = title;
388 if (!CreateProcessW(
389 NULL, /* lpApplicationName */
390 commandline, /* lpCommandLine */
391 NULL, /* lpProcessAttributes */
392 NULL, /* lpThreadAttributes */
393 FALSE, /* bInheritHandles */
394 creation_flags, /* dwCreationFlags */
395 NULL, /* lpEnvironment */
396 sei.lpDirectory, /* lpCurrentDirectory */
397 &startup_info, /* lpStartupInfo */
398 &process_information /* lpProcessInformation */ ))
400 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
402 sei.hProcess = process_information.hProcess;
403 goto done;
407 if (!ShellExecuteExW(&sei))
408 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
410 done:
411 HeapFree( GetProcessHeap(), 0, args );
412 HeapFree( GetProcessHeap(), 0, dos_filename );
413 HeapFree( GetProcessHeap(), 0, parent_directory );
415 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
416 DWORD exitcode;
417 WaitForSingleObject(sei.hProcess, INFINITE);
418 GetExitCodeProcess(sei.hProcess, &exitcode);
419 ExitProcess(exitcode);
422 ExitProcess(0);