shell32: Merge menu implementations together.
[wine/multimedia.git] / programs / start / start.c
blobfe0ac1c72ae2ce54bd05b2571e8c907466c5cdb4
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 <windows.h>
24 #include <winuser.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <shlobj.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 void license(void)
129 fatal_string(STRING_LICENSE);
132 static WCHAR *build_args( int argc, WCHAR **argvW )
134 int i, wlen = 1;
135 WCHAR *ret, *p;
136 static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
137 static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
139 for (i = 0; i < argc; i++ )
141 wlen += strlenW(argvW[i]) + 1;
142 if (strchrW(argvW[i], ' '))
143 wlen += 2;
145 ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
146 ret[0] = 0;
148 for (i = 0, p = ret; i < argc; i++ )
150 if (strchrW(argvW[i], ' '))
151 p += sprintfW(p, FormatQuotesW, argvW[i]);
152 else
153 p += sprintfW(p, FormatW, argvW[i]);
155 return ret;
158 static WCHAR *get_parent_dir(WCHAR* path)
160 WCHAR *last_slash;
161 WCHAR *result;
162 int len;
164 last_slash = strrchrW( path, '\\' );
165 if (last_slash == NULL)
166 len = 1;
167 else
168 len = last_slash - path + 1;
170 result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
171 CopyMemory(result, path, (len-1)*sizeof(WCHAR));
172 result[len-1] = '\0';
174 return result;
177 int wmain (int argc, WCHAR *argv[])
179 SHELLEXECUTEINFOW sei;
180 WCHAR *args = NULL;
181 int i;
182 int unix_mode = 0;
183 int progid_open = 0;
184 WCHAR *dos_filename = NULL;
185 WCHAR *parent_directory = NULL;
186 DWORD binary_type;
188 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
189 static const WCHAR unixW[] = { 'u', 'n', 'i', 'x', 0 };
190 static const WCHAR progIDOpenW[] =
191 { 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
193 memset(&sei, 0, sizeof(sei));
194 sei.cbSize = sizeof(sei);
195 sei.lpVerb = openW;
196 sei.nShow = SW_SHOWNORMAL;
197 /* Dunno what these mean, but it looks like winMe's start uses them */
198 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
199 SEE_MASK_FLAG_NO_UI|
200 SEE_MASK_NO_CONSOLE;
202 /* Canonical Microsoft commandline flag processing:
203 * flags start with /, are case insensitive,
204 * and may be run together in same word.
206 for (i=1; i<argc; i++) {
207 int ci;
209 if (argv[i][0] != '/')
210 break;
212 /* Unix paths can start with / so we have to assume anything following /U is not a flag */
213 if (unix_mode || progid_open)
214 break;
216 /* Handle all options in this word */
217 for (ci=0; argv[i][ci]; ) {
218 /* Skip slash */
219 ci++;
220 switch(argv[i][ci]) {
221 case 'b':
222 case 'B':
223 break; /* FIXME: should stop new window from being created */
224 case 'i':
225 case 'I':
226 break; /* FIXME: should ignore any changes to current environment */
227 case 'l':
228 case 'L':
229 license();
230 break; /* notreached */
231 case 'm':
232 case 'M':
233 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
234 sei.nShow = SW_SHOWMAXIMIZED;
235 else
236 sei.nShow = SW_SHOWMINIMIZED;
237 break;
238 case 'r':
239 case 'R':
240 /* sei.nShow = SW_SHOWNORMAL; */
241 break;
242 case 'u':
243 case 'U':
244 if (strncmpiW(&argv[i][ci], unixW, 4) == 0)
245 unix_mode = 1;
246 else {
247 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
248 usage();
250 break;
251 case 'p':
252 case 'P':
253 if (strncmpiW(&argv[i][ci], progIDOpenW, 17) == 0)
254 progid_open = 1;
255 else {
256 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
257 usage();
259 break;
260 case 'w':
261 case 'W':
262 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
263 break;
264 case '?':
265 usage();
266 break;
267 default:
268 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
269 usage();
271 /* Skip to next slash */
272 while (argv[i][ci] && (argv[i][ci] != '/'))
273 ci++;
277 if (i == argc)
278 usage();
280 if (progid_open) {
281 sei.lpClass = argv[i++];
282 sei.fMask |= SEE_MASK_CLASSNAME;
285 sei.lpFile = argv[i++];
287 args = build_args( argc - i, &argv[i] );
288 sei.lpParameters = args;
290 if (unix_mode || progid_open) {
291 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
292 char* multibyte_unixpath;
293 int multibyte_unixpath_len;
295 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
297 if (!wine_get_dos_file_name_ptr)
298 fatal_string(STRING_UNIXFAIL);
300 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
301 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
303 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
305 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
307 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
309 if (!dos_filename)
310 fatal_string(STRING_UNIXFAIL);
312 sei.lpFile = dos_filename;
313 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
314 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
316 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
317 WCHAR *commandline;
318 STARTUPINFOW startup_info;
319 PROCESS_INFORMATION process_information;
320 static WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
322 /* 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 */
324 commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
325 sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
327 ZeroMemory(&startup_info, sizeof(startup_info));
328 startup_info.cb = sizeof(startup_info);
330 if (!CreateProcessW(
331 NULL, /* lpApplicationName */
332 commandline, /* lpCommandLine */
333 NULL, /* lpProcessAttributes */
334 NULL, /* lpThreadAttributes */
335 FALSE, /* bInheritHandles */
336 CREATE_NEW_CONSOLE, /* dwCreationFlags */
337 NULL, /* lpEnvironment */
338 sei.lpDirectory, /* lpCurrentDirectory */
339 &startup_info, /* lpStartupInfo */
340 &process_information /* lpProcessInformation */ ))
342 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
344 sei.hProcess = process_information.hProcess;
345 goto done;
349 if (!ShellExecuteExW(&sei))
350 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
352 done:
353 HeapFree( GetProcessHeap(), 0, args );
354 HeapFree( GetProcessHeap(), 0, dos_filename );
355 HeapFree( GetProcessHeap(), 0, parent_directory );
357 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
358 DWORD exitcode;
359 WaitForSingleObject(sei.hProcess, INFINITE);
360 GetExitCodeProcess(sei.hProcess, &exitcode);
361 ExitProcess(exitcode);
364 ExitProcess(0);