ieframe: Moved dialog resources to ieframe.
[wine/testsucceed.git] / programs / start / start.c
blob6e9b482f53a51d1654129089f3154d5462d69c4c
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 its file
50 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,
103 msg, sizeof(msg)/sizeof(WCHAR)))
104 WINE_ERR("LoadString failed, error %d\n", GetLastError());
106 fatal_error(msg, error_code, filename);
109 static void fatal_string(int which)
111 WCHAR msg[2048];
113 if (!LoadStringW(GetModuleHandleW(NULL), which,
114 msg, sizeof(msg)/sizeof(WCHAR)))
115 WINE_ERR("LoadString failed, error %d\n", GetLastError());
117 output(msg);
118 ExitProcess(1);
121 static void usage(void)
123 fatal_string(STRING_USAGE);
126 static void license(void)
128 fatal_string(STRING_LICENSE);
131 static WCHAR *build_args( int argc, WCHAR **argvW )
133 int i, wlen = 1;
134 WCHAR *ret, *p;
135 static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
136 static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
138 for (i = 0; i < argc; i++ )
140 wlen += strlenW(argvW[i]) + 1;
141 if (strchrW(argvW[i], ' '))
142 wlen += 2;
144 ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
145 ret[0] = 0;
147 for (i = 0, p = ret; i < argc; i++ )
149 if (strchrW(argvW[i], ' '))
150 p += sprintfW(p, FormatQuotesW, argvW[i]);
151 else
152 p += sprintfW(p, FormatW, argvW[i]);
154 return ret;
157 static WCHAR *get_parent_dir(WCHAR* path)
159 WCHAR *last_slash;
160 WCHAR *result;
161 int len;
163 last_slash = strrchrW( path, '\\' );
164 if (last_slash == NULL)
165 len = 1;
166 else
167 len = last_slash - path + 1;
169 result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
170 CopyMemory(result, path, (len-1)*sizeof(WCHAR));
171 result[len-1] = '\0';
173 return result;
176 int wmain (int argc, WCHAR *argv[])
178 SHELLEXECUTEINFOW sei;
179 WCHAR *args = NULL;
180 int i;
181 int unix_mode = 0;
182 int progid_open = 0;
183 WCHAR *dos_filename = NULL;
184 WCHAR *parent_directory = NULL;
185 DWORD binary_type;
187 static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
188 static const WCHAR unixW[] = { 'u', 'n', 'i', 'x', 0 };
189 static const WCHAR progIDOpenW[] =
190 { 'p', 'r', 'o', 'g', 'I', 'D', 'O', 'p', 'e', 'n', 0};
192 memset(&sei, 0, sizeof(sei));
193 sei.cbSize = sizeof(sei);
194 sei.lpVerb = openW;
195 sei.nShow = SW_SHOWNORMAL;
196 /* Dunno what these mean, but it looks like winMe's start uses them */
197 sei.fMask = SEE_MASK_FLAG_DDEWAIT|
198 SEE_MASK_FLAG_NO_UI|
199 SEE_MASK_NO_CONSOLE;
201 /* Canonical Microsoft commandline flag processing:
202 * flags start with /, are case insensitive,
203 * and may be run together in same word.
205 for (i=1; i<argc; i++) {
206 int ci;
208 if (argv[i][0] != '/')
209 break;
211 /* Unix paths can start with / so we have to assume anything following /U is not a flag */
212 if (unix_mode || progid_open)
213 break;
215 /* Handle all options in this word */
216 for (ci=0; argv[i][ci]; ) {
217 /* Skip slash */
218 ci++;
219 switch(argv[i][ci]) {
220 case 'b':
221 case 'B':
222 break; /* FIXME: should stop new window from being created */
223 case 'i':
224 case 'I':
225 break; /* FIXME: should ignore any changes to current environment */
226 case 'l':
227 case 'L':
228 license();
229 break; /* notreached */
230 case 'm':
231 case 'M':
232 if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
233 sei.nShow = SW_SHOWMAXIMIZED;
234 else
235 sei.nShow = SW_SHOWMINIMIZED;
236 break;
237 case 'r':
238 case 'R':
239 /* sei.nShow = SW_SHOWNORMAL; */
240 break;
241 case 'u':
242 case 'U':
243 if (strncmpiW(&argv[i][ci], unixW, 4) == 0)
244 unix_mode = 1;
245 else {
246 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
247 usage();
249 break;
250 case 'p':
251 case 'P':
252 if (strncmpiW(&argv[i][ci], progIDOpenW, 17) == 0)
253 progid_open = 1;
254 else {
255 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
256 usage();
258 break;
259 case 'w':
260 case 'W':
261 sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
262 break;
263 case '?':
264 usage();
265 break;
266 default:
267 WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
268 usage();
270 /* Skip to next slash */
271 while (argv[i][ci] && (argv[i][ci] != '/'))
272 ci++;
276 if (i == argc)
277 usage();
279 if (progid_open) {
280 sei.lpClass = argv[i++];
281 sei.fMask |= SEE_MASK_CLASSNAME;
284 sei.lpFile = argv[i++];
286 args = build_args( argc - i, &argv[i] );
287 sei.lpParameters = args;
289 if (unix_mode || progid_open) {
290 LPWSTR (*CDECL wine_get_dos_file_name_ptr)(LPCSTR);
291 char* multibyte_unixpath;
292 int multibyte_unixpath_len;
294 wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandleA("KERNEL32"), "wine_get_dos_file_name");
296 if (!wine_get_dos_file_name_ptr)
297 fatal_string(STRING_UNIXFAIL);
299 multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
300 multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
302 WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
304 dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
306 HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
308 if (!dos_filename)
309 fatal_string(STRING_UNIXFAIL);
311 sei.lpFile = dos_filename;
312 sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
313 sei.fMask &= ~SEE_MASK_FLAG_NO_UI;
315 if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
316 WCHAR *commandline;
317 STARTUPINFOW startup_info;
318 PROCESS_INFORMATION process_information;
319 static WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
321 /* 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 */
323 commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
324 sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
326 ZeroMemory(&startup_info, sizeof(startup_info));
327 startup_info.cb = sizeof(startup_info);
329 if (!CreateProcessW(
330 NULL, /* lpApplicationName */
331 commandline, /* lpCommandLine */
332 NULL, /* lpProcessAttributes */
333 NULL, /* lpThreadAttributes */
334 FALSE, /* bInheritHandles */
335 CREATE_NEW_CONSOLE, /* dwCreationFlags */
336 NULL, /* lpEnvironment */
337 sei.lpDirectory, /* lpCurrentDirectory */
338 &startup_info, /* lpStartupInfo */
339 &process_information /* lpProcessInformation */ ))
341 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
343 sei.hProcess = process_information.hProcess;
344 goto done;
348 if (!ShellExecuteExW(&sei))
349 fatal_string_error(STRING_EXECFAIL, GetLastError(), sei.lpFile);
351 done:
352 HeapFree( GetProcessHeap(), 0, args );
353 HeapFree( GetProcessHeap(), 0, dos_filename );
354 HeapFree( GetProcessHeap(), 0, parent_directory );
356 if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
357 DWORD exitcode;
358 WaitForSingleObject(sei.hProcess, INFINITE);
359 GetExitCodeProcess(sei.hProcess, &exitcode);
360 ExitProcess(exitcode);
363 ExitProcess(0);