2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* START.C - start internal command.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 24-Jul-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Started.
|
|
|
|
*
|
|
|
|
* 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
|
|
|
|
* Remove all hardcode string to En.rc
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <precomp.h>
|
|
|
|
|
|
|
|
#ifdef INCLUDE_CMD_START
|
|
|
|
|
2008-08-09 21:51:34 +00:00
|
|
|
/* Find the end of an option, and turn it into a nul-terminated string
|
|
|
|
* in place. (It's moved back one character, to make room for the nul) */
|
|
|
|
static TCHAR *GetParameter(TCHAR **pPointer)
|
|
|
|
{
|
|
|
|
BOOL bInQuote = FALSE;
|
|
|
|
TCHAR *start = *pPointer;
|
|
|
|
TCHAR *p;
|
|
|
|
for (p = start; *p; p++)
|
|
|
|
{
|
|
|
|
if (!bInQuote && (*p == _T('/') || _istspace(*p)))
|
|
|
|
break;
|
|
|
|
bInQuote ^= (*p == _T('"'));
|
|
|
|
p[-1] = *p;
|
|
|
|
}
|
|
|
|
p[-1] = _T('\0');
|
|
|
|
*pPointer = p;
|
|
|
|
return start - 1;
|
|
|
|
}
|
|
|
|
|
2008-08-25 23:22:03 +00:00
|
|
|
INT cmd_start (LPTSTR Rest)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
TCHAR szFullName[CMDLINE_LENGTH];
|
2009-03-22 01:51:29 +00:00
|
|
|
TCHAR szUnquotedName[CMDLINE_LENGTH];
|
2006-02-16 23:23:37 +00:00
|
|
|
TCHAR *param = NULL;
|
2008-08-09 21:51:34 +00:00
|
|
|
TCHAR *dot;
|
2006-02-16 23:23:37 +00:00
|
|
|
INT size;
|
|
|
|
LPTSTR comspec;
|
|
|
|
BOOL bWait = FALSE;
|
|
|
|
BOOL bBat = FALSE;
|
|
|
|
BOOL bCreate = FALSE;
|
|
|
|
TCHAR szFullCmdLine [CMDLINE_LENGTH];
|
|
|
|
PROCESS_INFORMATION prci;
|
|
|
|
STARTUPINFO stui;
|
2006-08-25 23:12:50 +00:00
|
|
|
INT i = 0;
|
2009-03-23 23:52:50 +00:00
|
|
|
#ifdef UNICODE
|
|
|
|
DWORD dwCreationFlags = CREATE_NEW_CONSOLE | CREATE_UNICODE_ENVIRONMENT;
|
|
|
|
#else
|
|
|
|
DWORD dwCreationFlags = CREATE_NEW_CONSOLE;
|
|
|
|
#endif
|
|
|
|
DWORD dwAffinityMask = 0;
|
2008-08-09 21:51:34 +00:00
|
|
|
LPTSTR lpTitle = NULL;
|
|
|
|
LPTSTR lpDirectory = NULL;
|
2009-03-23 23:52:50 +00:00
|
|
|
LPTSTR lpEnvironment = NULL;
|
2008-08-09 21:51:34 +00:00
|
|
|
WORD wShowWindow = SW_SHOWNORMAL;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2008-08-09 21:51:34 +00:00
|
|
|
while (1)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-08-09 21:51:34 +00:00
|
|
|
if (_istspace(*Rest))
|
2006-08-25 23:12:50 +00:00
|
|
|
{
|
2008-08-09 21:51:34 +00:00
|
|
|
Rest++;
|
2006-08-25 23:12:50 +00:00
|
|
|
}
|
2008-08-09 21:51:34 +00:00
|
|
|
else if (*Rest == _T('"') && !lpTitle)
|
2006-08-25 23:12:50 +00:00
|
|
|
{
|
2008-08-09 21:51:34 +00:00
|
|
|
lpTitle = GetParameter(&Rest);
|
|
|
|
StripQuotes(lpTitle);
|
2006-08-25 23:12:50 +00:00
|
|
|
}
|
2008-08-09 21:51:34 +00:00
|
|
|
else if (*Rest == L'/')
|
2006-08-25 23:12:50 +00:00
|
|
|
{
|
2008-08-09 21:51:34 +00:00
|
|
|
LPTSTR option;
|
|
|
|
Rest++;
|
|
|
|
option = GetParameter(&Rest);
|
|
|
|
if (*option == _T('?'))
|
|
|
|
{
|
|
|
|
ConOutResPaging(TRUE,STRING_START_HELP1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (_totupper(*option) == _T('D'))
|
|
|
|
{
|
|
|
|
lpDirectory = option + 1;
|
2008-08-13 15:17:51 +00:00
|
|
|
if (!*lpDirectory)
|
|
|
|
{
|
|
|
|
while (_istspace(*Rest))
|
|
|
|
Rest++;
|
|
|
|
lpDirectory = GetParameter(&Rest);
|
|
|
|
}
|
2008-08-09 21:51:34 +00:00
|
|
|
StripQuotes(lpDirectory);
|
|
|
|
}
|
2009-03-23 23:52:50 +00:00
|
|
|
else if (_totupper(*option) == _T('I'))
|
|
|
|
{
|
|
|
|
/* rest of the option is apparently ignored */
|
|
|
|
lpEnvironment = lpOriginalEnvironment;
|
|
|
|
}
|
2008-08-09 21:51:34 +00:00
|
|
|
else if (!_tcsicmp(option, _T("MIN")))
|
|
|
|
{
|
|
|
|
wShowWindow = SW_MINIMIZE;
|
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("MAX")))
|
|
|
|
{
|
|
|
|
wShowWindow = SW_MAXIMIZE;
|
|
|
|
}
|
2009-03-23 23:52:50 +00:00
|
|
|
else if (!_tcsicmp(option, _T("AFFINITY")))
|
|
|
|
{
|
|
|
|
TCHAR *end;
|
|
|
|
while (_istspace(*Rest))
|
|
|
|
Rest++;
|
|
|
|
option = GetParameter(&Rest);
|
|
|
|
/* Affinity mask is given in hexadecimal */
|
|
|
|
dwAffinityMask = _tcstoul(option, &end, 16);
|
|
|
|
if (*end != _T('\0') || dwAffinityMask == 0 ||
|
|
|
|
dwAffinityMask == (DWORD)-1)
|
|
|
|
{
|
|
|
|
ConErrResPrintf(STRING_ERROR_INVALID_PARAM_FORMAT, option);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
dwCreationFlags |= CREATE_SUSPENDED;
|
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("B")))
|
|
|
|
{
|
|
|
|
dwCreationFlags &= ~CREATE_NEW_CONSOLE;
|
|
|
|
dwCreationFlags |= CREATE_NEW_PROCESS_GROUP;
|
|
|
|
}
|
2008-08-09 21:51:34 +00:00
|
|
|
else if (!_tcsicmp(option, _T("LOW")))
|
|
|
|
{
|
2009-03-23 23:52:50 +00:00
|
|
|
dwCreationFlags |= IDLE_PRIORITY_CLASS;
|
2008-08-09 21:51:34 +00:00
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("NORMAL")))
|
|
|
|
{
|
2009-03-23 23:52:50 +00:00
|
|
|
dwCreationFlags |= NORMAL_PRIORITY_CLASS;
|
2008-08-09 21:51:34 +00:00
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("HIGH")))
|
|
|
|
{
|
2009-03-23 23:52:50 +00:00
|
|
|
dwCreationFlags |= HIGH_PRIORITY_CLASS;
|
2008-08-09 21:51:34 +00:00
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("REALTIME")))
|
|
|
|
{
|
2009-03-23 23:52:50 +00:00
|
|
|
dwCreationFlags |= REALTIME_PRIORITY_CLASS;
|
2008-08-09 21:51:34 +00:00
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("ABOVENORMAL")))
|
|
|
|
{
|
2009-03-23 23:52:50 +00:00
|
|
|
dwCreationFlags |= ABOVE_NORMAL_PRIORITY_CLASS;
|
2008-08-09 21:51:34 +00:00
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("BELOWNORMAL")))
|
|
|
|
{
|
2009-03-23 23:52:50 +00:00
|
|
|
dwCreationFlags |= BELOW_NORMAL_PRIORITY_CLASS;
|
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("SEPARATE")))
|
|
|
|
{
|
|
|
|
dwCreationFlags |= CREATE_SEPARATE_WOW_VDM;
|
|
|
|
}
|
|
|
|
else if (!_tcsicmp(option, _T("SHARED")))
|
|
|
|
{
|
|
|
|
dwCreationFlags |= CREATE_SHARED_WOW_VDM;
|
2008-08-09 21:51:34 +00:00
|
|
|
}
|
2008-08-13 15:17:51 +00:00
|
|
|
else if (!_tcsicmp(option, _T("W")) ||
|
|
|
|
!_tcsicmp(option, _T("WAIT")))
|
2008-08-09 21:51:34 +00:00
|
|
|
{
|
|
|
|
bWait = TRUE;
|
|
|
|
}
|
2008-08-13 15:17:51 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ConErrResPrintf(STRING_TYPE_ERROR1, option);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-08-25 23:12:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-08-09 21:51:34 +00:00
|
|
|
/* It's not an option - must be the beginning of
|
|
|
|
* the actual command. Leave the loop. */
|
|
|
|
break;
|
2006-08-25 23:12:50 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
/* get comspec */
|
2007-08-01 10:17:13 +00:00
|
|
|
comspec = cmd_alloc ( MAX_PATH * sizeof(TCHAR));
|
2006-02-16 23:23:37 +00:00
|
|
|
if (comspec == NULL)
|
|
|
|
{
|
|
|
|
error_out_of_memory();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
SetLastError(0);
|
2007-07-28 17:48:23 +00:00
|
|
|
size = GetEnvironmentVariable (_T("COMSPEC"), comspec, MAX_PATH);
|
2006-02-16 23:23:37 +00:00
|
|
|
if(GetLastError() == ERROR_ENVVAR_NOT_FOUND)
|
|
|
|
{
|
2008-08-13 15:17:51 +00:00
|
|
|
_tcscpy(comspec, _T("cmd"));
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (size > MAX_PATH)
|
|
|
|
{
|
2007-08-01 10:17:13 +00:00
|
|
|
comspec = cmd_realloc(comspec,size * sizeof(TCHAR) );
|
2006-02-16 23:23:37 +00:00
|
|
|
if (comspec==NULL)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
size = GetEnvironmentVariable (_T("COMSPEC"), comspec, size);
|
2007-10-19 23:21:45 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nErrorLevel = 0;
|
|
|
|
|
2009-03-22 01:51:29 +00:00
|
|
|
if (!*Rest)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2009-03-22 01:51:29 +00:00
|
|
|
Rest = _T("cmd.exe");
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2009-03-22 01:51:29 +00:00
|
|
|
else
|
2006-02-16 23:23:37 +00:00
|
|
|
/* Parsing the command that gets called by start, and it's parameters */
|
|
|
|
{
|
|
|
|
BOOL bInside = FALSE;
|
|
|
|
|
|
|
|
/* find the end of the command and put the arguments in param */
|
2009-03-22 01:51:29 +00:00
|
|
|
for (i = 0; Rest[i]; i++)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2009-03-22 01:51:29 +00:00
|
|
|
if (Rest[i] == _T('\"'))
|
2006-02-16 23:23:37 +00:00
|
|
|
bInside = !bInside;
|
2009-03-22 01:51:29 +00:00
|
|
|
if (_istspace(Rest[i]) && !bInside)
|
2007-10-19 23:21:45 +00:00
|
|
|
{
|
2009-03-22 01:51:29 +00:00
|
|
|
param = &Rest[i+1];
|
|
|
|
Rest[i] = _T('\0');
|
2006-02-16 23:23:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2009-03-22 01:51:29 +00:00
|
|
|
_tcscpy(szUnquotedName, Rest);
|
|
|
|
StripQuotes(szUnquotedName);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
/* get the PATH environment variable and parse it */
|
|
|
|
/* search the PATH environment variable for the binary */
|
2009-03-22 01:51:29 +00:00
|
|
|
if (SearchForExecutable(szUnquotedName, szFullName))
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2009-03-22 01:51:29 +00:00
|
|
|
/* check if this is a .BAT or .CMD file */
|
|
|
|
dot = _tcsrchr(szFullName, _T('.'));
|
|
|
|
if (dot && (!_tcsicmp(dot, _T(".bat")) || !_tcsicmp(dot, _T(".cmd"))))
|
|
|
|
{
|
|
|
|
bBat = TRUE;
|
|
|
|
_stprintf(szFullCmdLine, _T("\"%s\" /K %s"), comspec, Rest);
|
|
|
|
TRACE ("[BATCH: %s %s]\n", debugstr_aw(szFullName), debugstr_aw(Rest));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE ("[EXEC: %s %s]\n", debugstr_aw(szFullName), debugstr_aw(Rest));
|
|
|
|
_tcscpy(szFullCmdLine, Rest);
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
/* build command line for CreateProcess() */
|
2009-03-22 01:51:29 +00:00
|
|
|
if (param != NULL)
|
|
|
|
{
|
|
|
|
_tcscat(szFullCmdLine, _T(" "));
|
|
|
|
_tcscat(szFullCmdLine, param);
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
/* fill startup info */
|
|
|
|
memset (&stui, 0, sizeof (STARTUPINFO));
|
|
|
|
stui.cb = sizeof (STARTUPINFO);
|
|
|
|
stui.dwFlags = STARTF_USESHOWWINDOW;
|
2008-08-09 21:51:34 +00:00
|
|
|
stui.lpTitle = lpTitle;
|
|
|
|
stui.wShowWindow = wShowWindow;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2009-03-23 23:52:50 +00:00
|
|
|
bCreate = CreateProcess(bBat ? comspec : szFullName,
|
|
|
|
szFullCmdLine, NULL, NULL, TRUE, dwCreationFlags,
|
|
|
|
lpEnvironment, lpDirectory, &stui, &prci);
|
2006-02-16 23:23:37 +00:00
|
|
|
if (bCreate)
|
2009-03-23 23:52:50 +00:00
|
|
|
{
|
|
|
|
if (dwAffinityMask)
|
|
|
|
{
|
|
|
|
SetProcessAffinityMask(prci.hProcess, dwAffinityMask);
|
|
|
|
ResumeThread(prci.hThread);
|
|
|
|
}
|
2009-03-22 01:51:29 +00:00
|
|
|
CloseHandle(prci.hThread);
|
2009-03-23 23:52:50 +00:00
|
|
|
}
|
2009-03-22 01:51:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The file name did not seem to be valid, but maybe it's actually a
|
|
|
|
* directory or URL, so we still want to pass it to ShellExecute. */
|
|
|
|
_tcscpy(szFullName, szUnquotedName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bCreate)
|
|
|
|
{
|
|
|
|
/* CreateProcess didn't work; try ShellExecute */
|
2009-03-23 23:52:50 +00:00
|
|
|
DWORD flags = SEE_MASK_NOCLOSEPROCESS;
|
|
|
|
if (!(dwCreationFlags & CREATE_NEW_CONSOLE))
|
|
|
|
flags |= SEE_MASK_NO_CONSOLE;
|
|
|
|
prci.hProcess = RunFile(flags, szFullName, param, lpDirectory, wShowWindow);
|
2009-03-22 01:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (prci.hProcess != NULL)
|
|
|
|
{
|
2006-02-16 23:23:37 +00:00
|
|
|
if (bWait)
|
|
|
|
{
|
|
|
|
DWORD dwExitCode;
|
|
|
|
WaitForSingleObject (prci.hProcess, INFINITE);
|
|
|
|
GetExitCodeProcess (prci.hProcess, &dwExitCode);
|
|
|
|
nErrorLevel = (INT)dwExitCode;
|
|
|
|
}
|
|
|
|
CloseHandle (prci.hProcess);
|
|
|
|
/* Get New code page if it has change */
|
|
|
|
InputCodePage= GetConsoleCP();
|
|
|
|
OutputCodePage = GetConsoleOutputCP();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ErrorMessage(GetLastError (),
|
|
|
|
_T("Error executing CreateProcess()!!\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 15:17:51 +00:00
|
|
|
cmd_free(comspec);
|
2006-02-16 23:23:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* EOF */
|