qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / programs / start / start.c
blob9f2c3f74fafb0447a1888c72760cd3619efcdc02
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 BOOL unix_mode = FALSE;
185 BOOL progid_open = FALSE;
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 sei.lpDirectory = NULL;
221 creation_flags = CREATE_NEW_CONSOLE;
223 /* Canonical Microsoft commandline flag processing:
224 * flags start with / and are case insensitive.
226 for (i=1; i<argc; i++) {
227 if (argv[i][0] == '"') {
228 title = argv[i];
229 continue;
231 if (argv[i][0] != '/')
232 break;
234 /* Unix paths can start with / so we have to assume anything following /unix is not a flag */
235 if (unix_mode || progid_open)
236 break;
238 if (argv[i][0] == '/' && (argv[i][1] == 'd' || argv[i][1] == 'D')) {
239 if (argv[i][2])
240 /* The start directory was concatenated to the option */
241 sei.lpDirectory = argv[i]+2;
242 else if (i+1 == argc) {
243 WINE_ERR("you must specify a directory path for the /d option\n");
244 usage();
245 } else
246 sei.lpDirectory = argv[++i];
248 else if (is_option(argv[i], bW)) {
249 creation_flags &= ~CREATE_NEW_CONSOLE;
251 else if (argv[i][0] == '/' && (argv[i][1] == 'i' || argv[i][1] == 'I')) {
252 TRACE("/i is ignored\n"); /* FIXME */
254 else if (is_option(argv[i], minW)) {
255 sei.nShow = SW_SHOWMINIMIZED;
257 else if (is_option(argv[i], maxW)) {
258 sei.nShow = SW_SHOWMAXIMIZED;
260 else if (is_option(argv[i], lowW)) {
261 creation_flags |= IDLE_PRIORITY_CLASS;
263 else if (is_option(argv[i], normalW)) {
264 creation_flags |= NORMAL_PRIORITY_CLASS;
266 else if (is_option(argv[i], highW)) {
267 creation_flags |= HIGH_PRIORITY_CLASS;
269 else if (is_option(argv[i], realtimeW)) {
270 creation_flags |= REALTIME_PRIORITY_CLASS;
272 else if (is_option(argv[i], abovenormalW)) {
273 creation_flags |= ABOVE_NORMAL_PRIORITY_CLASS;
275 else if (is_option(argv[i], belownormalW)) {
276 creation_flags |= BELOW_NORMAL_PRIORITY_CLASS;
278 else if (is_option(argv[i], separateW)) {
279 TRACE("/separate is ignored\n"); /* FIXME */
281 else if (is_option(argv[i], sharedW)) {
282 TRACE("/shared is ignored\n"); /* FIXME */
284 else if (is_option(argv[i], nodeW)) {
285 if (i+1 == argc) {
286 WINE_ERR("you must specify a numa node for the /node option\n");
287 usage();
288 } else
290 TRACE("/node is ignored\n"); /* FIXME */
291 i++;
294 else if (is_option(argv[i], affinityW))
296 if (i+1 == argc) {
297 WINE_ERR("you must specify a numa node for the /node option\n");
298 usage();
299 } else
301 TRACE("/affinity is ignored\n"); /* FIXME */
302 i++;
305 else if (is_option(argv[i], wW) || is_option(argv[i], waitW)) {
306 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
308 else if (is_option(argv[i], helpW)) {
309 usage();
312 /* Wine extensions */
314 else if (is_option(argv[i], unixW)) {
315 unix_mode = TRUE;
317 else if (is_option(argv[i], progIDOpenW)) {
318 progid_open = TRUE;
319 } else
322 WINE_ERR("Unknown option '%s'\n", wine_dbgstr_w(argv[i]));
323 usage();
327 if (progid_open) {
328 if (i == argc)
329 usage();
330 sei.lpClass = argv[i++];
331 sei.fMask |= SEE_MASK_CLASSNAME;
334 if (i == argc) {
335 if (progid_open || unix_mode)
336 usage();
337 sei.lpFile = cmdW;
339 else
340 sei.lpFile = argv[i++];
342 args = build_args( argc - i, &argv[i] );
343 sei.lpParameters = args;
345 if (unix_mode || progid_open) {
346 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
347 char* multibyte_unixpath;
348 int multibyte_unixpath_len;
350 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
352 if (!wine_get_dos_file_name_ptr)
353 fatal_string(STRING_UNIXFAIL);
355 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
356 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
358 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
360 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
362 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
364 if (!dos_filename)
365 fatal_string(STRING_UNIXFAIL);
367 sei.lpFile = dos_filename;
368 if (!sei.lpDirectory)
369 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
370 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
372 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
373 WCHAR *commandline;
374 STARTUPINFOW startup_info;
375 PROCESS_INFORMATION process_information;
376 static const WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
378 /* 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 */
380 commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
381 sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
383 ZeroMemory(&startup_info, sizeof(startup_info));
384 startup_info.cb = sizeof(startup_info);
385 startup_info.lpTitle = title;
387 if (!CreateProcessW(
388 NULL, /* lpApplicationName */
389 commandline, /* lpCommandLine */
390 NULL, /* lpProcessAttributes */
391 NULL, /* lpThreadAttributes */
392 FALSE, /* bInheritHandles */
393 creation_flags, /* dwCreationFlags */
394 NULL, /* lpEnvironment */
395 sei.lpDirectory, /* lpCurrentDirectory */
396 &startup_info, /* lpStartupInfo */
397 &process_information /* lpProcessInformation */ ))
399 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
401 sei.hProcess = process_information.hProcess;
402 goto done;
406 if (!ShellExecuteExW(&sei))
407 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
409 done:
410 HeapFree( GetProcessHeap(), 0, args );
411 HeapFree( GetProcessHeap(), 0, dos_filename );
412 HeapFree( GetProcessHeap(), 0, parent_directory );
414 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
415 DWORD exitcode;
416 WaitForSingleObject(sei.hProcess, INFINITE);
417 GetExitCodeProcess(sei.hProcess, &exitcode);
418 ExitProcess(exitcode);
421 ExitProcess(0);