2003-01-21 21:14:36 +01:00
|
|
|
/*
|
|
|
|
* Start a program using ShellExecuteEx, optionally wait for it to finish
|
|
|
|
* Compatible with Microsoft's "c:\windows\command\start.exe"
|
|
|
|
*
|
|
|
|
* Copyright 2003 Dan Kegel
|
2007-12-11 21:14:13 +01:00
|
|
|
* Copyright 2007 Lyutin Anatoly (Etersoft)
|
2003-01-21 21:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this program; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-01-21 21:14:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winuser.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
#include <wine/unicode.h>
|
|
|
|
#include <wine/debug.h>
|
|
|
|
|
2003-01-21 21:14:36 +01:00
|
|
|
#include "resources.h"
|
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(start);
|
|
|
|
|
2003-01-21 21:14:36 +01:00
|
|
|
/**
|
|
|
|
Output given message to stdout without formatting.
|
|
|
|
*/
|
2007-12-11 21:14:13 +01:00
|
|
|
static void output(const WCHAR *message)
|
2003-01-21 21:14:36 +01:00
|
|
|
{
|
|
|
|
DWORD count;
|
2007-12-11 21:14:13 +01:00
|
|
|
DWORD res;
|
|
|
|
int wlen = strlenW(message);
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
if (!wlen) return;
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
res = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), message, wlen, &count, NULL);
|
|
|
|
|
|
|
|
/* If writing to console fails, assume its file
|
|
|
|
i/o so convert to OEM codepage and output */
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
DWORD len;
|
|
|
|
char *mesA;
|
|
|
|
/* Convert to OEM, then output */
|
|
|
|
len = WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, NULL, 0, NULL, NULL );
|
|
|
|
mesA = HeapAlloc(GetProcessHeap(), 0, len*sizeof(char));
|
|
|
|
if (!mesA) return;
|
|
|
|
WideCharToMultiByte( GetConsoleOutputCP(), 0, message, wlen, mesA, len, NULL, NULL );
|
|
|
|
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), mesA, len, &count, FALSE);
|
|
|
|
HeapFree(GetProcessHeap(), 0, mesA);
|
2003-01-21 21:14:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Output given message from string table,
|
|
|
|
followed by ": ",
|
|
|
|
followed by description of given GetLastError() value to stdout,
|
|
|
|
followed by a trailing newline,
|
|
|
|
then terminate.
|
|
|
|
*/
|
2007-12-11 21:14:13 +01:00
|
|
|
|
|
|
|
static void fatal_error(const WCHAR *msg, DWORD error_code)
|
|
|
|
{
|
|
|
|
LPVOID lpMsgBuf;
|
|
|
|
int status;
|
|
|
|
static const WCHAR colonsW[] = { ':', ' ', 0 };
|
|
|
|
static const WCHAR newlineW[] = { '\n', 0 };
|
|
|
|
|
|
|
|
output(msg);
|
|
|
|
output(colonsW);
|
|
|
|
status = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, error_code, 0, (LPWSTR) & lpMsgBuf, 0, NULL);
|
|
|
|
if (!status)
|
|
|
|
{
|
|
|
|
WINE_ERR("FormatMessage failed\n");
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
output(lpMsgBuf);
|
|
|
|
LocalFree((HLOCAL) lpMsgBuf);
|
|
|
|
output(newlineW);
|
|
|
|
}
|
|
|
|
ExitProcess(1);
|
|
|
|
}
|
|
|
|
|
2003-01-21 21:14:36 +01:00
|
|
|
static void fatal_string_error(int which, DWORD error_code)
|
|
|
|
{
|
2007-12-11 21:14:13 +01:00
|
|
|
WCHAR msg[2048];
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
if (!LoadStringW(GetModuleHandle(NULL), which,
|
|
|
|
msg, sizeof(msg)/sizeof(WCHAR)))
|
|
|
|
WINE_ERR("LoadString failed, error %d\n", GetLastError());
|
2003-01-21 21:14:36 +01:00
|
|
|
|
|
|
|
fatal_error(msg, error_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fatal_string(int which)
|
|
|
|
{
|
2007-12-11 21:14:13 +01:00
|
|
|
WCHAR msg[2048];
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
if (!LoadStringW(GetModuleHandle(NULL), which,
|
|
|
|
msg, sizeof(msg)/sizeof(WCHAR)))
|
|
|
|
WINE_ERR("LoadString failed, error %d\n", GetLastError());
|
2003-01-21 21:14:36 +01:00
|
|
|
|
|
|
|
output(msg);
|
|
|
|
ExitProcess(1);
|
|
|
|
}
|
|
|
|
|
2005-06-27 11:57:28 +02:00
|
|
|
static void usage(void)
|
2003-01-21 21:14:36 +01:00
|
|
|
{
|
|
|
|
fatal_string(STRING_USAGE);
|
|
|
|
}
|
|
|
|
|
2005-06-27 11:57:28 +02:00
|
|
|
static void license(void)
|
2003-01-21 21:14:36 +01:00
|
|
|
{
|
|
|
|
fatal_string(STRING_LICENSE);
|
|
|
|
}
|
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
static WCHAR *build_args( int argc, WCHAR **argvW )
|
2005-11-10 12:06:15 +01:00
|
|
|
{
|
2007-12-11 21:14:13 +01:00
|
|
|
int i, wlen = 1;
|
|
|
|
WCHAR *ret, *p;
|
|
|
|
static const WCHAR FormatQuotesW[] = { ' ', '\"', '%', 's', '\"', 0 };
|
|
|
|
static const WCHAR FormatW[] = { ' ', '%', 's', 0 };
|
2005-11-10 12:06:15 +01:00
|
|
|
|
|
|
|
for (i = 0; i < argc; i++ )
|
2007-07-10 23:47:02 +02:00
|
|
|
{
|
2007-12-11 21:14:13 +01:00
|
|
|
wlen += strlenW(argvW[i]) + 1;
|
|
|
|
if (strchrW(argvW[i], ' '))
|
|
|
|
wlen += 2;
|
2007-07-10 23:47:02 +02:00
|
|
|
}
|
2007-12-11 21:14:13 +01:00
|
|
|
ret = HeapAlloc( GetProcessHeap(), 0, wlen*sizeof(WCHAR) );
|
2006-02-01 13:50:52 +01:00
|
|
|
ret[0] = 0;
|
2005-11-10 12:06:15 +01:00
|
|
|
|
|
|
|
for (i = 0, p = ret; i < argc; i++ )
|
2007-07-10 23:47:02 +02:00
|
|
|
{
|
2007-12-11 21:14:13 +01:00
|
|
|
if (strchrW(argvW[i], ' '))
|
|
|
|
p += sprintfW(p, FormatQuotesW, argvW[i]);
|
2007-07-10 23:47:02 +02:00
|
|
|
else
|
2007-12-11 21:14:13 +01:00
|
|
|
p += sprintfW(p, FormatW, argvW[i]);
|
2007-07-10 23:47:02 +02:00
|
|
|
}
|
2005-11-10 12:06:15 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-04-13 02:30:42 +02:00
|
|
|
static WCHAR *get_parent_dir(WCHAR* path)
|
|
|
|
{
|
|
|
|
WCHAR *last_slash;
|
|
|
|
WCHAR *result;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
last_slash = strrchrW( path, '\\' );
|
|
|
|
if (last_slash == NULL)
|
|
|
|
len = 1;
|
|
|
|
else
|
|
|
|
len = last_slash - path + 1;
|
|
|
|
|
|
|
|
result = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
|
|
|
|
CopyMemory(result, path, (len-1)*sizeof(WCHAR));
|
|
|
|
result[len-1] = '\0';
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
int wmain (int argc, WCHAR *argv[])
|
2003-01-21 21:14:36 +01:00
|
|
|
{
|
2007-12-11 21:14:13 +01:00
|
|
|
SHELLEXECUTEINFOW sei;
|
|
|
|
WCHAR *args = NULL;
|
2005-11-10 12:06:15 +01:00
|
|
|
int i;
|
2008-04-13 02:30:42 +02:00
|
|
|
int unix_mode = 0;
|
|
|
|
WCHAR *dos_filename = NULL;
|
|
|
|
WCHAR *parent_directory = NULL;
|
|
|
|
DWORD binary_type;
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2007-12-11 21:14:13 +01:00
|
|
|
static const WCHAR openW[] = { 'o', 'p', 'e', 'n', 0 };
|
2008-04-13 02:30:42 +02:00
|
|
|
static const WCHAR unixW[] = { 'u', 'n', 'i', 'x', 0 };
|
2007-12-11 21:14:13 +01:00
|
|
|
|
2003-01-21 21:14:36 +01:00
|
|
|
memset(&sei, 0, sizeof(sei));
|
|
|
|
sei.cbSize = sizeof(sei);
|
2007-12-11 21:14:13 +01:00
|
|
|
sei.lpVerb = openW;
|
2003-01-21 21:14:36 +01:00
|
|
|
sei.nShow = SW_SHOWNORMAL;
|
|
|
|
/* Dunno what these mean, but it looks like winMe's start uses them */
|
2007-07-16 17:19:06 +02:00
|
|
|
sei.fMask = SEE_MASK_FLAG_DDEWAIT|
|
|
|
|
SEE_MASK_FLAG_NO_UI|
|
|
|
|
SEE_MASK_NO_CONSOLE;
|
2003-01-21 21:14:36 +01:00
|
|
|
|
|
|
|
/* Canonical Microsoft commandline flag processing:
|
|
|
|
* flags start with /, are case insensitive,
|
|
|
|
* and may be run together in same word.
|
|
|
|
*/
|
2005-11-10 12:06:15 +01:00
|
|
|
for (i=1; i<argc; i++) {
|
2003-01-21 21:14:36 +01:00
|
|
|
int ci;
|
|
|
|
|
2005-11-10 12:06:15 +01:00
|
|
|
if (argv[i][0] != '/')
|
2003-01-21 21:14:36 +01:00
|
|
|
break;
|
|
|
|
|
2008-04-13 02:30:42 +02:00
|
|
|
/* Unix paths can start with / so we have to assume anything following /U is not a flag */
|
|
|
|
if (unix_mode)
|
|
|
|
break;
|
|
|
|
|
2003-01-21 21:14:36 +01:00
|
|
|
/* Handle all options in this word */
|
2005-11-10 12:06:15 +01:00
|
|
|
for (ci=0; argv[i][ci]; ) {
|
2003-01-21 21:14:36 +01:00
|
|
|
/* Skip slash */
|
|
|
|
ci++;
|
2005-11-10 12:06:15 +01:00
|
|
|
switch(argv[i][ci]) {
|
2003-01-21 21:14:36 +01:00
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
license();
|
|
|
|
break; /* notreached */
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2005-11-10 12:06:15 +01:00
|
|
|
if (argv[i][ci+1] == 'a' || argv[i][ci+1] == 'A')
|
2003-01-21 21:14:36 +01:00
|
|
|
sei.nShow = SW_SHOWMAXIMIZED;
|
|
|
|
else
|
|
|
|
sei.nShow = SW_SHOWMINIMIZED;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
/* sei.nShow = SW_SHOWNORMAL; */
|
|
|
|
break;
|
2008-04-13 02:30:42 +02:00
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
if (strncmpiW(&argv[i][ci], unixW, 4) == 0)
|
|
|
|
unix_mode = 1;
|
|
|
|
else {
|
|
|
|
WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
break;
|
2003-01-21 21:14:36 +01:00
|
|
|
case 'w':
|
|
|
|
case 'W':
|
|
|
|
sei.fMask |= SEE_MASK_NOCLOSEPROCESS;
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-11 21:14:13 +01:00
|
|
|
WINE_ERR("Option '%s' not recognized\n", wine_dbgstr_w( argv[i]+ci-1));
|
2003-01-21 21:14:36 +01:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
/* Skip to next slash */
|
2005-11-10 12:06:15 +01:00
|
|
|
while (argv[i][ci] && (argv[i][ci] != '/'))
|
2003-01-21 21:14:36 +01:00
|
|
|
ci++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-10 12:06:15 +01:00
|
|
|
if (i == argc)
|
2003-01-21 21:14:36 +01:00
|
|
|
usage();
|
|
|
|
|
2005-11-10 12:06:15 +01:00
|
|
|
sei.lpFile = argv[i++];
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2005-11-10 12:06:15 +01:00
|
|
|
args = build_args( argc - i, &argv[i] );
|
|
|
|
sei.lpParameters = args;
|
2003-01-21 21:14:36 +01:00
|
|
|
|
2008-04-13 02:30:42 +02:00
|
|
|
if (unix_mode) {
|
|
|
|
LPWSTR (*wine_get_dos_file_name_ptr)(LPCSTR);
|
|
|
|
char* multibyte_unixpath;
|
|
|
|
int multibyte_unixpath_len;
|
|
|
|
|
|
|
|
wine_get_dos_file_name_ptr = (void*)GetProcAddress(GetModuleHandle("KERNEL32"), "wine_get_dos_file_name");
|
|
|
|
|
|
|
|
if (!wine_get_dos_file_name_ptr)
|
|
|
|
fatal_string(STRING_UNIXFAIL);
|
|
|
|
|
|
|
|
multibyte_unixpath_len = WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, NULL, 0, NULL, NULL);
|
|
|
|
multibyte_unixpath = HeapAlloc(GetProcessHeap(), 0, multibyte_unixpath_len);
|
|
|
|
|
|
|
|
WideCharToMultiByte(CP_UNIXCP, 0, sei.lpFile, -1, multibyte_unixpath, multibyte_unixpath_len, NULL, NULL);
|
|
|
|
|
|
|
|
dos_filename = wine_get_dos_file_name_ptr(multibyte_unixpath);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, multibyte_unixpath);
|
|
|
|
|
|
|
|
if (!dos_filename)
|
|
|
|
fatal_string(STRING_UNIXFAIL);
|
|
|
|
|
|
|
|
sei.lpFile = dos_filename;
|
|
|
|
sei.lpDirectory = parent_directory = get_parent_dir(dos_filename);
|
|
|
|
|
|
|
|
if (GetBinaryTypeW(sei.lpFile, &binary_type)) {
|
|
|
|
WCHAR *commandline;
|
|
|
|
STARTUPINFOW startup_info;
|
|
|
|
PROCESS_INFORMATION process_information;
|
|
|
|
static WCHAR commandlineformat[] = {'"','%','s','"','%','s',0};
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
commandline = HeapAlloc(GetProcessHeap(), 0, (strlenW(sei.lpFile)+3+strlenW(sei.lpParameters))*sizeof(WCHAR));
|
|
|
|
sprintfW(commandline, commandlineformat, sei.lpFile, sei.lpParameters);
|
|
|
|
|
|
|
|
ZeroMemory(&startup_info, sizeof(startup_info));
|
|
|
|
startup_info.cb = sizeof(startup_info);
|
|
|
|
|
|
|
|
if (!CreateProcessW(
|
|
|
|
NULL, /* lpApplicationName */
|
|
|
|
commandline, /* lpCommandLine */
|
|
|
|
NULL, /* lpProcessAttributes */
|
|
|
|
NULL, /* lpThreadAttributes */
|
|
|
|
FALSE, /* bInheritHandles */
|
|
|
|
CREATE_NEW_CONSOLE, /* dwCreationFlags */
|
|
|
|
NULL, /* lpEnvironment */
|
|
|
|
sei.lpDirectory, /* lpCurrentDirectory */
|
|
|
|
&startup_info, /* lpStartupInfo */
|
|
|
|
&process_information /* lpProcessInformation */ ))
|
|
|
|
{
|
|
|
|
fatal_string_error(STRING_EXECFAIL, GetLastError());
|
|
|
|
}
|
|
|
|
sei.hProcess = process_information.hProcess;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!ShellExecuteExW(&sei))
|
2003-01-21 21:14:36 +01:00
|
|
|
fatal_string_error(STRING_EXECFAIL, GetLastError());
|
|
|
|
|
2005-11-10 12:06:15 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, args );
|
2008-04-13 02:30:42 +02:00
|
|
|
HeapFree( GetProcessHeap(), 0, dos_filename );
|
|
|
|
HeapFree( GetProcessHeap(), 0, parent_directory );
|
2005-11-10 12:06:15 +01:00
|
|
|
|
2003-01-21 21:14:36 +01:00
|
|
|
if (sei.fMask & SEE_MASK_NOCLOSEPROCESS) {
|
|
|
|
DWORD exitcode;
|
2007-12-11 21:14:13 +01:00
|
|
|
WaitForSingleObject(sei.hProcess, INFINITE);
|
|
|
|
GetExitCodeProcess(sei.hProcess, &exitcode);
|
2003-01-21 21:14:36 +01:00
|
|
|
ExitProcess(exitcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitProcess(0);
|
|
|
|
}
|