2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* CMD.C - command-line interface.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
*
|
|
|
|
* 17 Jun 1994 (Tim Norman)
|
|
|
|
* started.
|
|
|
|
*
|
|
|
|
* 08 Aug 1995 (Matt Rains)
|
|
|
|
* I have cleaned up the source code. changes now bring this source
|
|
|
|
* into guidelines for recommended programming practice.
|
|
|
|
*
|
|
|
|
* A added the the standard FreeDOS GNU licence test to the
|
|
|
|
* initialize() function.
|
|
|
|
*
|
|
|
|
* Started to replace puts() with printf(). this will help
|
|
|
|
* standardize output. please follow my lead.
|
|
|
|
*
|
|
|
|
* I have added some constants to help making changes easier.
|
|
|
|
*
|
|
|
|
* 15 Dec 1995 (Tim Norman)
|
|
|
|
* major rewrite of the code to make it more efficient and add
|
|
|
|
* redirection support (finally!)
|
|
|
|
*
|
|
|
|
* 06 Jan 1996 (Tim Norman)
|
|
|
|
* finished adding redirection support! Changed to use our own
|
|
|
|
* exec code (MUCH thanks to Svante Frey!!)
|
|
|
|
*
|
|
|
|
* 29 Jan 1996 (Tim Norman)
|
|
|
|
* added support for CHDIR, RMDIR, MKDIR, and ERASE, as per
|
|
|
|
* suggestion of Steffan Kaiser
|
|
|
|
*
|
|
|
|
* changed "file not found" error message to "bad command or
|
|
|
|
* filename" thanks to Dustin Norman for noticing that confusing
|
|
|
|
* message!
|
|
|
|
*
|
|
|
|
* changed the format to call internal commands (again) so that if
|
|
|
|
* they want to split their commands, they can do it themselves
|
|
|
|
* (none of the internal functions so far need that much power, anyway)
|
|
|
|
*
|
|
|
|
* 27 Aug 1996 (Tim Norman)
|
|
|
|
* added in support for Oliver Mueller's ALIAS command
|
|
|
|
*
|
|
|
|
* 14 Jun 1997 (Steffan Kaiser)
|
|
|
|
* added ctrl-break handling and error level
|
|
|
|
*
|
|
|
|
* 16 Jun 1998 (Rob Lake)
|
|
|
|
* Runs command.com if /P is specified in command line. Command.com
|
|
|
|
* also stays permanent. If /C is in the command line, starts the
|
|
|
|
* program next in the line.
|
|
|
|
*
|
|
|
|
* 21 Jun 1998 (Rob Lake)
|
|
|
|
* Fixed up /C so that arguments for the program
|
|
|
|
*
|
|
|
|
* 08-Jul-1998 (John P. Price)
|
|
|
|
* Now sets COMSPEC environment variable
|
|
|
|
* misc clean up and optimization
|
|
|
|
* added date and time commands
|
|
|
|
* changed to using spawnl instead of exec. exec does not copy the
|
|
|
|
* environment to the child process!
|
|
|
|
*
|
|
|
|
* 14 Jul 1998 (Hans B Pufal)
|
|
|
|
* Reorganised source to be more efficient and to more closely
|
|
|
|
* follow MS-DOS conventions. (eg %..% environment variable
|
|
|
|
* replacement works form command line as well as batch file.
|
|
|
|
*
|
|
|
|
* New organisation also properly support nested batch files.
|
|
|
|
*
|
|
|
|
* New command table structure is half way towards providing a
|
|
|
|
* system in which COMMAND will find out what internal commands
|
|
|
|
* are loaded
|
|
|
|
*
|
|
|
|
* 24 Jul 1998 (Hans B Pufal) [HBP_003]
|
|
|
|
* Fixed return value when called with /C option
|
|
|
|
*
|
|
|
|
* 27 Jul 1998 John P. Price
|
|
|
|
* added config.h include
|
|
|
|
*
|
|
|
|
* 28 Jul 1998 John P. Price
|
|
|
|
* added showcmds function to show commands and options available
|
|
|
|
*
|
|
|
|
* 07-Aug-1998 (John P Price <linux-guru@gcfl.net>)
|
|
|
|
* Fixed carrage return output to better match MSDOS with echo
|
|
|
|
* on or off. (marked with "JPP 19980708")
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 07-Dec-1998 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* First ReactOS release.
|
|
|
|
* Extended length of commandline buffers to 512.
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 13-Dec-1998 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Added COMSPEC environment variable.
|
|
|
|
* Added "/t" support (color) on cmd command line.
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 07-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Added help text ("cmd /?").
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 25-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Unicode and redirection safe!
|
|
|
|
* Fixed redirections and piping.
|
|
|
|
* Piping is based on temporary files, but basic support
|
|
|
|
* for anonymous pipes already exists.
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 27-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Replaced spawnl() by CreateProcess().
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 22-Oct-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Added break handler.
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 15-Dec-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Fixed current directory
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 28-Dec-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Restore window title after program/batch execution
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 03-Feb-2001 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Workaround because argc[0] is NULL under ReactOS
|
|
|
|
*
|
|
|
|
* 23-Feb-2001 (Carl Nettelblad <cnettel@hem.passagen.se>)
|
|
|
|
* %envvar% replacement conflicted with for.
|
|
|
|
*
|
|
|
|
* 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
|
|
|
|
* Make MakeSureDirectoryPathExistsEx unicode safe.
|
|
|
|
*
|
|
|
|
* 28-Mai-2004
|
|
|
|
* Removed MakeSureDirectoryPathExistsEx.
|
|
|
|
* Use the current directory if GetTempPath fails.
|
|
|
|
*
|
|
|
|
* 12-Jul-2004 (Jens Collin <jens.collin@lakhei.com>)
|
|
|
|
* Added ShellExecute call when all else fails to be able to "launch" any file.
|
|
|
|
*
|
|
|
|
* 02-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
|
|
|
|
* Remove all hardcode string to En.rc
|
|
|
|
*
|
|
|
|
* 06-May-2005 (Klemens Friedl <frik85@gmail.com>)
|
|
|
|
* Add 'help' command (list all commands plus description)
|
|
|
|
*
|
|
|
|
* 06-jul-2005 (Magnus Olsen <magnus@greatlord.com>)
|
|
|
|
* translate '%errorlevel%' to the internal value.
|
|
|
|
* Add proper memmory alloc ProcessInput, the error
|
|
|
|
* handling for memmory handling need to be improve
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <precomp.h>
|
|
|
|
|
|
|
|
#ifndef NT_SUCCESS
|
|
|
|
#define NT_SUCCESS(StatCode) ((NTSTATUS)(StatCode) >= 0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef NTSTATUS (WINAPI *NtQueryInformationProcessProc)(HANDLE, PROCESSINFOCLASS,
|
|
|
|
PVOID, ULONG, PULONG);
|
|
|
|
typedef NTSTATUS (WINAPI *NtReadVirtualMemoryProc)(HANDLE, PVOID, PVOID, ULONG, PULONG);
|
|
|
|
|
|
|
|
BOOL bExit = FALSE; /* indicates EXIT was typed */
|
|
|
|
BOOL bCanExit = TRUE; /* indicates if this shell is exitable */
|
|
|
|
BOOL bCtrlBreak = FALSE; /* Ctrl-Break or Ctrl-C hit */
|
2008-05-17 20:07:31 +00:00
|
|
|
BOOL bIgnoreEcho = FALSE; /* Set this to TRUE to prevent a newline, when executing a command */
|
2006-02-16 23:23:37 +00:00
|
|
|
INT nErrorLevel = 0; /* Errorlevel of last launched external program */
|
|
|
|
BOOL bChildProcessRunning = FALSE;
|
|
|
|
DWORD dwChildProcessId = 0;
|
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
HANDLE hIn;
|
|
|
|
HANDLE hOut;
|
|
|
|
HANDLE hConsole;
|
|
|
|
HANDLE CMD_ModuleHandle;
|
|
|
|
HMODULE NtDllModule;
|
|
|
|
|
|
|
|
static NtQueryInformationProcessProc NtQueryInformationProcessPtr = NULL;
|
|
|
|
static NtReadVirtualMemoryProc NtReadVirtualMemoryPtr = NULL;
|
|
|
|
|
|
|
|
#ifdef INCLUDE_CMD_COLOR
|
|
|
|
WORD wColor; /* current color */
|
|
|
|
WORD wDefColor; /* default color */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert
|
|
|
|
*
|
|
|
|
* insert commas into a number
|
|
|
|
*/
|
|
|
|
INT
|
|
|
|
ConvertULargeInteger (ULARGE_INTEGER num, LPTSTR des, INT len, BOOL bPutSeperator)
|
|
|
|
{
|
|
|
|
TCHAR temp[32];
|
2008-05-22 17:40:01 +00:00
|
|
|
UINT n, iTarget;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2007-02-04 18:19:34 +00:00
|
|
|
if (len <= 1)
|
|
|
|
return 0;
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
if (num.QuadPart == 0)
|
|
|
|
{
|
|
|
|
des[0] = _T('0');
|
|
|
|
des[1] = _T('\0');
|
2008-05-22 17:40:01 +00:00
|
|
|
return 1;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-05-22 17:40:01 +00:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
iTarget = nNumberGroups;
|
|
|
|
if (!nNumberGroups)
|
|
|
|
bPutSeperator = FALSE;
|
|
|
|
|
|
|
|
while (num.QuadPart > 0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-05-22 17:40:01 +00:00
|
|
|
if (iTarget == n && bPutSeperator)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-05-22 17:40:01 +00:00
|
|
|
iTarget += nNumberGroups + 1;
|
|
|
|
temp[31 - n++] = cThousandSeparator;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-05-22 17:40:01 +00:00
|
|
|
temp[31 - n++] = (TCHAR)(num.QuadPart % 10) + _T('0');
|
|
|
|
num.QuadPart /= 10;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-05-22 17:40:01 +00:00
|
|
|
if (n > len-1)
|
|
|
|
n = len-1;
|
|
|
|
|
|
|
|
memcpy(des, temp + 32 - n, n * sizeof(TCHAR));
|
|
|
|
des[n] = _T('\0');
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* is character a delimeter when used on first word?
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static BOOL IsDelimiter (TCHAR c)
|
|
|
|
{
|
|
|
|
return (c == _T('/') || c == _T('=') || c == _T('\0') || _istspace (c));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is a process a console process?
|
|
|
|
*/
|
|
|
|
static BOOL IsConsoleProcess(HANDLE Process)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PROCESS_BASIC_INFORMATION Info;
|
|
|
|
PEB ProcessPeb;
|
|
|
|
ULONG BytesRead;
|
|
|
|
|
|
|
|
if (NULL == NtQueryInformationProcessPtr || NULL == NtReadVirtualMemoryPtr)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = NtQueryInformationProcessPtr (
|
|
|
|
Process, ProcessBasicInformation,
|
|
|
|
&Info, sizeof(PROCESS_BASIC_INFORMATION), NULL);
|
|
|
|
if (! NT_SUCCESS(Status))
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("NtQueryInformationProcess failed with status %08x\n", Status);
|
2006-02-16 23:23:37 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
Status = NtReadVirtualMemoryPtr (
|
|
|
|
Process, Info.PebBaseAddress, &ProcessPeb,
|
|
|
|
sizeof(PEB), &BytesRead);
|
|
|
|
if (! NT_SUCCESS(Status) || sizeof(PEB) != BytesRead)
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("Couldn't read virt mem status %08x bytes read %lu\n", Status, BytesRead);
|
2006-02-16 23:23:37 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IMAGE_SUBSYSTEM_WINDOWS_CUI == ProcessPeb.ImageSubSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef _UNICODE
|
|
|
|
#define SHELLEXECUTETEXT "ShellExecuteW"
|
|
|
|
#else
|
|
|
|
#define SHELLEXECUTETEXT "ShellExecuteA"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef HINSTANCE (WINAPI *MYEX)(
|
|
|
|
HWND hwnd,
|
|
|
|
LPCTSTR lpOperation,
|
|
|
|
LPCTSTR lpFile,
|
|
|
|
LPCTSTR lpParameters,
|
|
|
|
LPCTSTR lpDirectory,
|
|
|
|
INT nShowCmd
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static BOOL RunFile(LPTSTR filename)
|
|
|
|
{
|
|
|
|
HMODULE hShell32;
|
|
|
|
MYEX hShExt;
|
|
|
|
HINSTANCE ret;
|
|
|
|
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("RunFile(%s)\n", debugstr_aw(filename));
|
2006-02-16 23:23:37 +00:00
|
|
|
hShell32 = LoadLibrary(_T("SHELL32.DLL"));
|
|
|
|
if (!hShell32)
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("RunFile: couldn't load SHELL32.DLL!\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
hShExt = (MYEX)(FARPROC)GetProcAddress(hShell32, SHELLEXECUTETEXT);
|
|
|
|
if (!hShExt)
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("RunFile: couldn't find ShellExecuteA/W in SHELL32.DLL!\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
FreeLibrary(hShell32);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-15 20:02:05 +00:00
|
|
|
TRACE ("RunFile: ShellExecuteA/W is at %x\n", hShExt);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
ret = (hShExt)(NULL, _T("open"), filename, NULL, NULL, SW_SHOWNORMAL);
|
|
|
|
|
2008-05-15 20:02:05 +00:00
|
|
|
TRACE ("RunFile: ShellExecuteA/W returned %d\n", (DWORD)ret);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
FreeLibrary(hShell32);
|
|
|
|
return (((DWORD)ret) > 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This command (in first) was not found in the command table
|
|
|
|
*
|
|
|
|
* Full - whole command line
|
|
|
|
* First - first word on command line
|
|
|
|
* Rest - rest of command line
|
|
|
|
*/
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
Execute (LPTSTR Full, LPTSTR First, LPTSTR Rest)
|
|
|
|
{
|
|
|
|
TCHAR *szFullName=NULL;
|
|
|
|
TCHAR *first = NULL;
|
|
|
|
TCHAR *rest = NULL;
|
|
|
|
TCHAR *full = NULL;
|
|
|
|
TCHAR *dot = NULL;
|
|
|
|
TCHAR szWindowTitle[MAX_PATH];
|
|
|
|
DWORD dwExitCode = 0;
|
|
|
|
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("Execute: \'%s\' \'%s\'\n", debugstr_aw(first), debugstr_aw(rest));
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
/* we need biger buffer that First, Rest, Full are already
|
2007-08-01 10:17:13 +00:00
|
|
|
need rewrite some code to use cmd_realloc when it need instead
|
2006-02-16 23:23:37 +00:00
|
|
|
of add 512bytes extra */
|
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
first = cmd_alloc ( (_tcslen(First) + 512) * sizeof(TCHAR));
|
2006-02-16 23:23:37 +00:00
|
|
|
if (first == NULL)
|
|
|
|
{
|
|
|
|
error_out_of_memory();
|
|
|
|
nErrorLevel = 1;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
rest = cmd_alloc ( (_tcslen(Rest) + 512) * sizeof(TCHAR));
|
2006-02-16 23:23:37 +00:00
|
|
|
if (rest == NULL)
|
|
|
|
{
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free (first);
|
2006-02-16 23:23:37 +00:00
|
|
|
error_out_of_memory();
|
|
|
|
nErrorLevel = 1;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
full = cmd_alloc ( (_tcslen(Full) + 512) * sizeof(TCHAR));
|
2006-02-16 23:23:37 +00:00
|
|
|
if (full == NULL)
|
|
|
|
{
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free (first);
|
|
|
|
cmd_free (rest);
|
2006-02-16 23:23:37 +00:00
|
|
|
error_out_of_memory();
|
|
|
|
nErrorLevel = 1;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
szFullName = cmd_alloc ( (_tcslen(Full) + 512) * sizeof(TCHAR));
|
2006-02-16 23:23:37 +00:00
|
|
|
if (full == NULL)
|
|
|
|
{
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free (first);
|
|
|
|
cmd_free (rest);
|
|
|
|
cmd_free (full);
|
2006-02-16 23:23:37 +00:00
|
|
|
error_out_of_memory();
|
|
|
|
nErrorLevel = 1;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Though it was already parsed once, we have a different set of rules
|
|
|
|
for parsing before we pass to CreateProccess */
|
|
|
|
if(!_tcschr(Full,_T('\"')))
|
|
|
|
{
|
|
|
|
_tcscpy(first,First);
|
|
|
|
_tcscpy(rest,Rest);
|
|
|
|
_tcscpy(full,Full);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UINT i = 0;
|
|
|
|
BOOL bInside = FALSE;
|
|
|
|
rest[0] = _T('\0');
|
|
|
|
full[0] = _T('\0');
|
|
|
|
first[0] = _T('\0');
|
|
|
|
_tcscpy(first,Full);
|
|
|
|
/* find the end of the command and start of the args */
|
|
|
|
for(i = 0; i < _tcslen(first); i++)
|
|
|
|
{
|
|
|
|
if(!_tcsncmp(&first[i], _T("\""), 1))
|
|
|
|
bInside = !bInside;
|
|
|
|
if(!_tcsncmp(&first[i], _T(" "), 1) && !bInside)
|
|
|
|
{
|
|
|
|
_tcscpy(rest,&first[i]);
|
|
|
|
first[i] = _T('\0');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
/* remove any slashes */
|
|
|
|
while(i < _tcslen(first))
|
|
|
|
{
|
|
|
|
if(first[i] == _T('\"'))
|
|
|
|
memmove(&first[i],&first[i + 1], _tcslen(&first[i]) * sizeof(TCHAR));
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
/* Drop quotes around it just in case there is a space */
|
|
|
|
_tcscpy(full,_T("\""));
|
|
|
|
_tcscat(full,first);
|
|
|
|
_tcscat(full,_T("\" "));
|
|
|
|
_tcscat(full,rest);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for a drive change */
|
|
|
|
if ((_istalpha (first[0])) && (!_tcscmp (first + 1, _T(":"))))
|
|
|
|
{
|
|
|
|
BOOL working = TRUE;
|
|
|
|
if (!SetCurrentDirectory(first))
|
|
|
|
/* Guess they changed disc or something, handle that gracefully and get to root */
|
|
|
|
{
|
|
|
|
TCHAR str[4];
|
|
|
|
str[0]=first[0];
|
|
|
|
str[1]=_T(':');
|
|
|
|
str[2]=_T('\\');
|
|
|
|
str[3]=0;
|
|
|
|
working = SetCurrentDirectory(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!working) ConErrResPuts (STRING_FREE_ERROR1);
|
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free (first);
|
|
|
|
cmd_free (rest);
|
|
|
|
cmd_free (full);
|
|
|
|
cmd_free (szFullName);
|
2006-02-16 23:23:37 +00:00
|
|
|
nErrorLevel = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the PATH environment variable and parse it */
|
|
|
|
/* search the PATH environment variable for the binary */
|
|
|
|
if (!SearchForExecutable (first, szFullName))
|
|
|
|
{
|
|
|
|
error_bad_command ();
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free (first);
|
|
|
|
cmd_free (rest);
|
|
|
|
cmd_free (full);
|
|
|
|
cmd_free (szFullName);
|
2006-02-16 23:23:37 +00:00
|
|
|
nErrorLevel = 1;
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GetConsoleTitle (szWindowTitle, MAX_PATH);
|
|
|
|
|
|
|
|
/* check if this is a .BAT or .CMD file */
|
|
|
|
dot = _tcsrchr (szFullName, _T('.'));
|
|
|
|
if (dot && (!_tcsicmp (dot, _T(".bat")) || !_tcsicmp (dot, _T(".cmd"))))
|
|
|
|
{
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("[BATCH: %s %s]\n", debugstr_aw(szFullName), debugstr_aw(rest));
|
2006-02-16 23:23:37 +00:00
|
|
|
Batch (szFullName, first, rest);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* exec the program */
|
|
|
|
PROCESS_INFORMATION prci;
|
|
|
|
STARTUPINFO stui;
|
|
|
|
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("[EXEC: %s %s]\n", debugstr_aw(full), debugstr_aw(rest));
|
2006-02-16 23:23:37 +00:00
|
|
|
/* build command line for CreateProcess() */
|
|
|
|
|
|
|
|
/* fill startup info */
|
|
|
|
memset (&stui, 0, sizeof (STARTUPINFO));
|
|
|
|
stui.cb = sizeof (STARTUPINFO);
|
|
|
|
stui.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
stui.wShowWindow = SW_SHOWDEFAULT;
|
|
|
|
|
|
|
|
// return console to standard mode
|
|
|
|
SetConsoleMode (GetStdHandle(STD_INPUT_HANDLE),
|
|
|
|
ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT );
|
|
|
|
|
|
|
|
if (CreateProcess (szFullName,
|
|
|
|
full,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
0, /* CREATE_NEW_PROCESS_GROUP */
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&stui,
|
|
|
|
&prci))
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
if (IsConsoleProcess(prci.hProcess))
|
|
|
|
{
|
|
|
|
/* FIXME: Protect this with critical section */
|
|
|
|
bChildProcessRunning = TRUE;
|
|
|
|
dwChildProcessId = prci.dwProcessId;
|
|
|
|
|
|
|
|
WaitForSingleObject (prci.hProcess, INFINITE);
|
|
|
|
|
|
|
|
/* FIXME: Protect this with critical section */
|
|
|
|
bChildProcessRunning = FALSE;
|
|
|
|
|
|
|
|
GetExitCodeProcess (prci.hProcess, &dwExitCode);
|
|
|
|
nErrorLevel = (INT)dwExitCode;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nErrorLevel = 0;
|
|
|
|
}
|
|
|
|
CloseHandle (prci.hThread);
|
|
|
|
CloseHandle (prci.hProcess);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("[ShellExecute: %s]\n", debugstr_aw(full));
|
2006-02-16 23:23:37 +00:00
|
|
|
// See if we can run this with ShellExecute() ie myfile.xls
|
|
|
|
if (!RunFile(full))
|
|
|
|
{
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("[ShellExecute failed!: %s]\n", debugstr_aw(full));
|
2006-02-16 23:23:37 +00:00
|
|
|
error_bad_command ();
|
|
|
|
nErrorLevel = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nErrorLevel = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// restore console mode
|
|
|
|
SetConsoleMode (
|
|
|
|
GetStdHandle( STD_INPUT_HANDLE ),
|
|
|
|
ENABLE_PROCESSED_INPUT );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get code page if it has been change */
|
|
|
|
InputCodePage= GetConsoleCP();
|
|
|
|
OutputCodePage = GetConsoleOutputCP();
|
|
|
|
SetConsoleTitle (szWindowTitle);
|
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free(first);
|
|
|
|
cmd_free(rest);
|
|
|
|
cmd_free(full);
|
|
|
|
cmd_free (szFullName);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* look through the internal commands and determine whether or not this
|
|
|
|
* command is one of them. If it is, call the command. If not, call
|
|
|
|
* execute to run it as an external program.
|
|
|
|
*
|
|
|
|
* line - the command line of the program to run
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-07-20 13:08:25 +00:00
|
|
|
VOID
|
2006-02-16 23:23:37 +00:00
|
|
|
DoCommand (LPTSTR line)
|
|
|
|
{
|
|
|
|
TCHAR *com = NULL; /* the first word in the command */
|
|
|
|
TCHAR *cp = NULL;
|
|
|
|
LPTSTR cstart;
|
|
|
|
LPTSTR rest; /* pointer to the rest of the command line */
|
|
|
|
INT cl;
|
|
|
|
LPCOMMAND cmdptr;
|
|
|
|
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("DoCommand: (\'%s\')\n", debugstr_aw(line));
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2007-08-01 10:17:13 +00:00
|
|
|
com = cmd_alloc( (_tcslen(line) +512)*sizeof(TCHAR) );
|
2006-02-16 23:23:37 +00:00
|
|
|
if (com == NULL)
|
|
|
|
{
|
|
|
|
error_out_of_memory();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = com;
|
|
|
|
/* Skip over initial white space */
|
|
|
|
while (_istspace (*line))
|
|
|
|
line++;
|
|
|
|
rest = line;
|
|
|
|
|
|
|
|
cstart = rest;
|
|
|
|
|
|
|
|
/* Anything to do ? */
|
|
|
|
if (*rest)
|
|
|
|
{
|
|
|
|
if (*rest == _T('"'))
|
|
|
|
{
|
|
|
|
/* treat quoted words specially */
|
|
|
|
|
|
|
|
rest++;
|
|
|
|
|
|
|
|
while(*rest != _T('\0') && *rest != _T('"'))
|
|
|
|
*cp++ = _totlower (*rest++);
|
|
|
|
if (*rest == _T('"'))
|
|
|
|
rest++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (!IsDelimiter (*rest))
|
|
|
|
*cp++ = _totlower (*rest++);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Terminate first word */
|
|
|
|
*cp = _T('\0');
|
|
|
|
|
|
|
|
/* Do not limit commands to MAX_PATH */
|
|
|
|
/*
|
|
|
|
if(_tcslen(com) > MAX_PATH)
|
|
|
|
{
|
|
|
|
error_bad_command();
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free(com);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Skip over whitespace to rest of line, exclude 'echo' command */
|
2007-10-19 23:21:45 +00:00
|
|
|
if (_tcsicmp (com, _T("echo")))
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
while (_istspace (*rest))
|
|
|
|
rest++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scan internal command table */
|
|
|
|
for (cmdptr = cmds;; cmdptr++)
|
|
|
|
{
|
|
|
|
/* If end of table execute ext cmd */
|
|
|
|
if (cmdptr->name == NULL)
|
|
|
|
{
|
|
|
|
Execute (line, com, rest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_tcscmp (com, cmdptr->name))
|
|
|
|
{
|
|
|
|
cmdptr->func (com, rest);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The following code handles the case of commands like CD which
|
|
|
|
* are recognised even when the command name and parameter are
|
|
|
|
* not space separated.
|
|
|
|
*
|
|
|
|
* e.g dir..
|
|
|
|
* cd\freda
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Get length of command name */
|
|
|
|
cl = _tcslen (cmdptr->name);
|
|
|
|
|
|
|
|
if ((cmdptr->flags & CMD_SPECIAL) &&
|
|
|
|
(!_tcsncmp (cmdptr->name, com, cl)) &&
|
|
|
|
(_tcschr (_T("\\.-"), *(com + cl))))
|
|
|
|
{
|
|
|
|
/* OK its one of the specials...*/
|
|
|
|
|
|
|
|
/* Terminate first word properly */
|
|
|
|
com[cl] = _T('\0');
|
|
|
|
|
|
|
|
/* Call with new rest */
|
|
|
|
cmdptr->func (com, cstart + cl);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free(com);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process the command line and execute the appropriate functions
|
|
|
|
* full input/output redirection and piping are supported
|
|
|
|
*/
|
|
|
|
|
|
|
|
VOID ParseCommandLine (LPTSTR cmd)
|
|
|
|
{
|
|
|
|
TCHAR cmdline[CMDLINE_LENGTH];
|
|
|
|
LPTSTR s;
|
|
|
|
#ifdef FEATURE_REDIRECTION
|
|
|
|
TCHAR in[CMDLINE_LENGTH] = _T("");
|
|
|
|
TCHAR out[CMDLINE_LENGTH] = _T("");
|
|
|
|
TCHAR err[CMDLINE_LENGTH] = _T("");
|
|
|
|
TCHAR szTempPath[MAX_PATH] = _T(".\\");
|
|
|
|
TCHAR szFileName[2][MAX_PATH] = {_T(""), _T("")};
|
|
|
|
HANDLE hFile[2] = {INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE};
|
|
|
|
LPTSTR t = NULL;
|
|
|
|
INT num = 0;
|
|
|
|
INT nRedirFlags = 0;
|
|
|
|
INT Length;
|
|
|
|
UINT Attributes;
|
|
|
|
BOOL bNewBatch = TRUE;
|
|
|
|
HANDLE hOldConIn;
|
|
|
|
HANDLE hOldConOut;
|
|
|
|
HANDLE hOldConErr;
|
|
|
|
#endif /* FEATURE_REDIRECTION */
|
|
|
|
|
|
|
|
_tcscpy (cmdline, cmd);
|
|
|
|
s = &cmdline[0];
|
|
|
|
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("ParseCommandLine: (\'%s\')\n", debugstr_aw(s));
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
#ifdef FEATURE_REDIRECTION
|
|
|
|
/* find the temp path to store temporary files */
|
|
|
|
Length = GetTempPath (MAX_PATH, szTempPath);
|
|
|
|
if (Length > 0 && Length < MAX_PATH)
|
|
|
|
{
|
|
|
|
Attributes = GetFileAttributes(szTempPath);
|
|
|
|
if (Attributes == 0xffffffff ||
|
|
|
|
!(Attributes & FILE_ATTRIBUTE_DIRECTORY))
|
|
|
|
{
|
|
|
|
Length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Length == 0 || Length >= MAX_PATH)
|
|
|
|
{
|
|
|
|
_tcscpy(szTempPath, _T(".\\"));
|
|
|
|
}
|
|
|
|
if (szTempPath[_tcslen (szTempPath) - 1] != _T('\\'))
|
|
|
|
_tcscat (szTempPath, _T("\\"));
|
|
|
|
|
|
|
|
/* get the redirections from the command line */
|
|
|
|
num = GetRedirection (s, in, out, err, &nRedirFlags);
|
|
|
|
|
|
|
|
/* more efficient, but do we really need to do this? */
|
|
|
|
for (t = in; _istspace (*t); t++)
|
|
|
|
;
|
|
|
|
_tcscpy (in, t);
|
|
|
|
|
|
|
|
for (t = out; _istspace (*t); t++)
|
|
|
|
;
|
|
|
|
_tcscpy (out, t);
|
|
|
|
|
|
|
|
for (t = err; _istspace (*t); t++)
|
|
|
|
;
|
|
|
|
_tcscpy (err, t);
|
|
|
|
|
|
|
|
if(bc && !_tcslen (in) && _tcslen (bc->In))
|
|
|
|
_tcscpy(in, bc->In);
|
|
|
|
if(bc && !out[0] && _tcslen(bc->Out))
|
|
|
|
{
|
|
|
|
nRedirFlags |= OUTPUT_APPEND;
|
|
|
|
_tcscpy(out, bc->Out);
|
|
|
|
}
|
|
|
|
if(bc && !_tcslen (err) && _tcslen (bc->Err))
|
|
|
|
{
|
|
|
|
nRedirFlags |= ERROR_APPEND;
|
|
|
|
_tcscpy(err, bc->Err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the initial conditions ... */
|
|
|
|
/* preserve STDIN, STDOUT and STDERR handles */
|
|
|
|
hOldConIn = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
hOldConOut = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
hOldConErr = GetStdHandle (STD_ERROR_HANDLE);
|
|
|
|
|
|
|
|
/* redirect STDIN */
|
|
|
|
if (in[0])
|
|
|
|
{
|
|
|
|
HANDLE hFile;
|
|
|
|
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
|
|
|
|
|
|
|
|
/* we need make sure the LastError msg is zero before calling CreateFile */
|
|
|
|
SetLastError(0);
|
|
|
|
|
|
|
|
/* Set up pipe for the standard input handler */
|
|
|
|
hFile = CreateFile (in, GENERIC_READ, FILE_SHARE_READ, &sa, OPEN_EXISTING,
|
|
|
|
FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR1, in);
|
2008-04-18 00:56:01 +00:00
|
|
|
return;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetStdHandle (STD_INPUT_HANDLE, hFile))
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR1, in);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("Input redirected from: %s\n", debugstr_aw(in));
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now do all but the last pipe command */
|
|
|
|
*szFileName[0] = _T('\0');
|
|
|
|
hFile[0] = INVALID_HANDLE_VALUE;
|
|
|
|
|
|
|
|
while (num-- > 1)
|
|
|
|
{
|
|
|
|
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
|
|
|
|
|
|
|
|
/* Create unique temporary file name */
|
|
|
|
GetTempFileName (szTempPath, _T("CMD"), 0, szFileName[1]);
|
|
|
|
|
|
|
|
/* we need make sure the LastError msg is zero before calling CreateFile */
|
|
|
|
SetLastError(0);
|
|
|
|
|
|
|
|
/* Set current stdout to temporary file */
|
|
|
|
hFile[1] = CreateFile (szFileName[1], GENERIC_WRITE, 0, &sa,
|
|
|
|
TRUNCATE_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL);
|
|
|
|
|
|
|
|
if (hFile[1] == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR2);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStdHandle (STD_OUTPUT_HANDLE, hFile[1]);
|
|
|
|
|
|
|
|
DoCommand (s);
|
|
|
|
|
|
|
|
/* close stdout file */
|
|
|
|
SetStdHandle (STD_OUTPUT_HANDLE, hOldConOut);
|
|
|
|
if ((hFile[1] != INVALID_HANDLE_VALUE) && (hFile[1] != hOldConOut))
|
|
|
|
{
|
|
|
|
CloseHandle (hFile[1]);
|
|
|
|
hFile[1] = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close old stdin file */
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
|
|
|
|
if ((hFile[0] != INVALID_HANDLE_VALUE) && (hFile[0] != hOldConIn))
|
|
|
|
{
|
|
|
|
/* delete old stdin file, if it is a real file */
|
|
|
|
CloseHandle (hFile[0]);
|
|
|
|
hFile[0] = INVALID_HANDLE_VALUE;
|
|
|
|
DeleteFile (szFileName[0]);
|
|
|
|
*szFileName[0] = _T('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy stdout file name to stdin file name */
|
|
|
|
_tcscpy (szFileName[0], szFileName[1]);
|
|
|
|
*szFileName[1] = _T('\0');
|
|
|
|
|
|
|
|
/* we need make sure the LastError msg is zero before calling CreateFile */
|
|
|
|
SetLastError(0);
|
|
|
|
|
|
|
|
/* open new stdin file */
|
|
|
|
hFile[0] = CreateFile (szFileName[0], GENERIC_READ, 0, &sa,
|
|
|
|
OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, NULL);
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, hFile[0]);
|
|
|
|
|
|
|
|
s = s + _tcslen (s) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now set up the end conditions... */
|
|
|
|
/* redirect STDOUT */
|
2008-04-18 00:56:01 +00:00
|
|
|
if (out[0])
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
/* Final output to here */
|
|
|
|
HANDLE hFile;
|
|
|
|
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
|
|
|
|
|
|
|
|
/* we need make sure the LastError msg is zero before calling CreateFile */
|
|
|
|
SetLastError(0);
|
|
|
|
|
|
|
|
hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, &sa,
|
|
|
|
(nRedirFlags & OUTPUT_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
INT size = _tcslen(out)-1;
|
|
|
|
|
|
|
|
if (out[size] != _T(':'))
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR3, out);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[size]=_T('\0');
|
|
|
|
hFile = CreateFile (out, GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, &sa,
|
|
|
|
(nRedirFlags & OUTPUT_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR3, out);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetStdHandle (STD_OUTPUT_HANDLE, hFile))
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR3, out);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nRedirFlags & OUTPUT_APPEND)
|
|
|
|
{
|
|
|
|
LONG lHighPos = 0;
|
|
|
|
|
|
|
|
if (GetFileType (hFile) == FILE_TYPE_DISK)
|
|
|
|
SetFilePointer (hFile, 0, &lHighPos, FILE_END);
|
|
|
|
}
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("Output redirected to: %s\n", debugstr_aw(out));
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else if (hOldConOut != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* Restore original stdout */
|
|
|
|
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
SetStdHandle (STD_OUTPUT_HANDLE, hOldConOut);
|
|
|
|
if (hOldConOut != hOut)
|
|
|
|
CloseHandle (hOut);
|
|
|
|
hOldConOut = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* redirect STDERR */
|
2008-04-18 00:56:01 +00:00
|
|
|
if (err[0])
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
/* Final output to here */
|
|
|
|
HANDLE hFile;
|
|
|
|
SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
|
|
|
|
|
|
|
|
if (!_tcscmp (err, out))
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
TRACE ("Stdout and stderr will use the same file!!\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
DuplicateHandle (GetCurrentProcess (),
|
|
|
|
GetStdHandle (STD_OUTPUT_HANDLE),
|
|
|
|
GetCurrentProcess (),
|
|
|
|
&hFile, 0, TRUE, DUPLICATE_SAME_ACCESS);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hFile = CreateFile (err,
|
|
|
|
GENERIC_WRITE,
|
|
|
|
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
|
|
|
|
&sa,
|
|
|
|
(nRedirFlags & ERROR_APPEND) ? OPEN_ALWAYS : CREATE_ALWAYS,
|
|
|
|
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH,
|
|
|
|
NULL);
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR3, err);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SetStdHandle (STD_ERROR_HANDLE, hFile))
|
|
|
|
{
|
2008-07-26 22:49:49 +00:00
|
|
|
ConErrResPrintf(STRING_CMD_ERROR3, err);
|
2006-02-16 23:23:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nRedirFlags & ERROR_APPEND)
|
|
|
|
{
|
|
|
|
LONG lHighPos = 0;
|
|
|
|
|
|
|
|
if (GetFileType (hFile) == FILE_TYPE_DISK)
|
|
|
|
SetFilePointer (hFile, 0, &lHighPos, FILE_END);
|
|
|
|
}
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("Error redirected to: %s\n", debugstr_aw(err));
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else if (hOldConErr != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* Restore original stderr */
|
|
|
|
HANDLE hErr = GetStdHandle (STD_ERROR_HANDLE);
|
|
|
|
SetStdHandle (STD_ERROR_HANDLE, hOldConErr);
|
|
|
|
if (hOldConErr != hErr)
|
|
|
|
CloseHandle (hErr);
|
|
|
|
hOldConErr = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bc)
|
|
|
|
bNewBatch = FALSE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* process final command */
|
|
|
|
DoCommand (s);
|
|
|
|
|
|
|
|
#ifdef FEATURE_REDIRECTION
|
|
|
|
if(bNewBatch && bc)
|
|
|
|
AddBatchRedirection(in, out, err);
|
|
|
|
/* close old stdin file */
|
|
|
|
#if 0 /* buggy implementation */
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
|
|
|
|
if ((hFile[0] != INVALID_HANDLE_VALUE) &&
|
|
|
|
(hFile[0] != hOldConIn))
|
|
|
|
{
|
|
|
|
/* delete old stdin file, if it is a real file */
|
|
|
|
CloseHandle (hFile[0]);
|
|
|
|
hFile[0] = INVALID_HANDLE_VALUE;
|
|
|
|
DeleteFile (szFileName[0]);
|
|
|
|
*szFileName[0] = _T('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore original STDIN */
|
|
|
|
if (hOldConIn != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
|
|
|
|
if (hOldConIn != hIn)
|
|
|
|
CloseHandle (hIn);
|
|
|
|
hOldConIn = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("Can't restore STDIN! Is invalid!!\n", out);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
#endif /* buggy implementation */
|
|
|
|
|
|
|
|
|
|
|
|
if (hOldConIn != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
SetStdHandle (STD_INPUT_HANDLE, hOldConIn);
|
|
|
|
if (hIn == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("Previous STDIN is invalid!!\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (GetFileType (hIn) == FILE_TYPE_DISK)
|
|
|
|
{
|
|
|
|
if (hFile[0] == hIn)
|
|
|
|
{
|
|
|
|
CloseHandle (hFile[0]);
|
|
|
|
hFile[0] = INVALID_HANDLE_VALUE;
|
|
|
|
DeleteFile (szFileName[0]);
|
|
|
|
*szFileName[0] = _T('\0');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-05-15 20:02:05 +00:00
|
|
|
WARN ("hFile[0] and hIn dont match!!!\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Restore original STDOUT */
|
|
|
|
if (hOldConOut != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
HANDLE hOut = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
SetStdHandle (STD_OUTPUT_HANDLE, hOldConOut);
|
|
|
|
if (hOldConOut != hOut)
|
|
|
|
CloseHandle (hOut);
|
|
|
|
hOldConOut = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore original STDERR */
|
|
|
|
if (hOldConErr != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
HANDLE hErr = GetStdHandle (STD_ERROR_HANDLE);
|
|
|
|
SetStdHandle (STD_ERROR_HANDLE, hOldConErr);
|
|
|
|
if (hOldConErr != hErr)
|
|
|
|
CloseHandle (hErr);
|
|
|
|
hOldConErr = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
#endif /* FEATURE_REDIRECTION */
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
2007-11-27 00:28:40 +00:00
|
|
|
GrowIfNecessary_dbg ( UINT needed, LPTSTR* ret, UINT* retlen, const char *file, int line )
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
if ( *ret && needed < *retlen )
|
|
|
|
return TRUE;
|
|
|
|
*retlen = needed;
|
|
|
|
if ( *ret )
|
2007-08-01 10:17:13 +00:00
|
|
|
cmd_free ( *ret );
|
2007-11-27 00:41:44 +00:00
|
|
|
#ifdef _DEBUG_MEM
|
2007-11-27 00:28:40 +00:00
|
|
|
*ret = (LPTSTR)cmd_alloc_dbg ( *retlen * sizeof(TCHAR), file, line );
|
2007-11-27 00:41:44 +00:00
|
|
|
#else
|
|
|
|
*ret = (LPTSTR)cmd_alloc ( *retlen * sizeof(TCHAR) );
|
|
|
|
#endif
|
2006-02-16 23:23:37 +00:00
|
|
|
if ( !*ret )
|
|
|
|
SetLastError ( ERROR_OUTOFMEMORY );
|
|
|
|
return *ret != NULL;
|
|
|
|
}
|
2007-11-27 00:28:40 +00:00
|
|
|
#define GrowIfNecessary(x, y, z) GrowIfNecessary_dbg(x, y, z, __FILE__, __LINE__)
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
LPCTSTR
|
|
|
|
GetEnvVarOrSpecial ( LPCTSTR varName )
|
|
|
|
{
|
|
|
|
static LPTSTR ret = NULL;
|
|
|
|
static UINT retlen = 0;
|
|
|
|
UINT size;
|
|
|
|
|
2008-08-12 23:46:15 +00:00
|
|
|
size = GetEnvironmentVariable ( varName, ret, retlen );
|
2006-02-16 23:23:37 +00:00
|
|
|
if ( size > retlen )
|
|
|
|
{
|
|
|
|
if ( !GrowIfNecessary ( size, &ret, &retlen ) )
|
|
|
|
return NULL;
|
2008-08-12 23:46:15 +00:00
|
|
|
size = GetEnvironmentVariable ( varName, ret, retlen );
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
if ( size )
|
2008-08-12 23:46:15 +00:00
|
|
|
return ret;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
/* env var doesn't exist, look for a "special" one */
|
|
|
|
/* %CD% */
|
2008-08-12 23:46:15 +00:00
|
|
|
if (_tcsicmp(varName,_T("cd")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
size = GetCurrentDirectory ( retlen, ret );
|
|
|
|
if ( size > retlen )
|
|
|
|
{
|
|
|
|
if ( !GrowIfNecessary ( size, &ret, &retlen ) )
|
|
|
|
return NULL;
|
|
|
|
size = GetCurrentDirectory ( retlen, ret );
|
|
|
|
}
|
|
|
|
if ( !size )
|
|
|
|
return NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* %TIME% */
|
2008-08-12 23:46:15 +00:00
|
|
|
else if (_tcsicmp(varName,_T("time")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
SYSTEMTIME t;
|
|
|
|
if ( !GrowIfNecessary ( MAX_PATH, &ret, &retlen ) )
|
|
|
|
return NULL;
|
|
|
|
GetSystemTime(&t);
|
|
|
|
_sntprintf ( ret, retlen, _T("%02d%c%02d%c%02d%c%02d"),
|
|
|
|
t.wHour, cTimeSeparator, t.wMinute, cTimeSeparator,
|
2007-02-23 20:05:39 +00:00
|
|
|
t.wSecond, cDecimalSeparator, t.wMilliseconds / 10);
|
2006-02-16 23:23:37 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* %DATE% */
|
2008-08-12 23:46:15 +00:00
|
|
|
else if (_tcsicmp(varName,_T("date")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
|
2007-02-28 13:17:34 +00:00
|
|
|
if ( !GrowIfNecessary ( GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, NULL, 0), &ret, &retlen ) )
|
2006-02-16 23:23:37 +00:00
|
|
|
return NULL;
|
2007-02-28 13:17:34 +00:00
|
|
|
|
|
|
|
size = GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, NULL, NULL, ret, retlen);
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
if ( !size )
|
|
|
|
return NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %RANDOM% */
|
2008-08-12 23:46:15 +00:00
|
|
|
else if (_tcsicmp(varName,_T("random")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
if ( !GrowIfNecessary ( MAX_PATH, &ret, &retlen ) )
|
|
|
|
return NULL;
|
|
|
|
/* Get random number */
|
|
|
|
_itot(rand(),ret,10);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %CMDCMDLINE% */
|
2008-08-12 23:46:15 +00:00
|
|
|
else if (_tcsicmp(varName,_T("cmdcmdline")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
return GetCommandLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %CMDEXTVERSION% */
|
2008-08-12 23:46:15 +00:00
|
|
|
else if (_tcsicmp(varName,_T("cmdextversion")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
if ( !GrowIfNecessary ( MAX_PATH, &ret, &retlen ) )
|
|
|
|
return NULL;
|
|
|
|
/* Set version number to 2 */
|
|
|
|
_itot(2,ret,10);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* %ERRORLEVEL% */
|
2008-08-12 23:46:15 +00:00
|
|
|
else if (_tcsicmp(varName,_T("errorlevel")) ==0)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
if ( !GrowIfNecessary ( MAX_PATH, &ret, &retlen ) )
|
|
|
|
return NULL;
|
|
|
|
_itot(nErrorLevel,ret,10);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-08-12 23:46:15 +00:00
|
|
|
return NULL;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
LPCTSTR
|
2008-08-12 23:46:15 +00:00
|
|
|
GetBatchVar ( LPCTSTR varName, UINT* varNameLen )
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
static LPTSTR ret = NULL;
|
|
|
|
static UINT retlen = 0;
|
|
|
|
|
|
|
|
if ( varNameLen )
|
2008-08-12 23:46:15 +00:00
|
|
|
*varNameLen = 1;
|
2008-02-01 21:25:21 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
switch ( *varName )
|
|
|
|
{
|
2008-01-31 19:01:23 +00:00
|
|
|
case _T('~'):
|
|
|
|
varName++;
|
|
|
|
if (_tcsncicmp(varName, _T("dp0"), 3) == 0)
|
|
|
|
{
|
|
|
|
if ( varNameLen )
|
2008-08-12 23:46:15 +00:00
|
|
|
*varNameLen = 4;
|
2008-02-01 14:15:41 +00:00
|
|
|
return bc->BatchFilePath;
|
2008-01-31 19:01:23 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
case _T('0'):
|
|
|
|
case _T('1'):
|
|
|
|
case _T('2'):
|
|
|
|
case _T('3'):
|
|
|
|
case _T('4'):
|
|
|
|
case _T('5'):
|
|
|
|
case _T('6'):
|
|
|
|
case _T('7'):
|
|
|
|
case _T('8'):
|
|
|
|
case _T('9'):
|
2008-08-12 23:46:15 +00:00
|
|
|
return FindArg(*varName - _T('0'));
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-06-29 02:48:52 +00:00
|
|
|
case _T('*'):
|
|
|
|
//
|
|
|
|
// Copy over the raw params(not including the batch file name
|
|
|
|
//
|
2008-08-12 23:46:15 +00:00
|
|
|
return bc->raw_params;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
case _T('%'):
|
2008-08-12 23:46:15 +00:00
|
|
|
return _T("%");
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
case _T('?'):
|
|
|
|
/* TODO FIXME 10 is only max size for 32-bit */
|
|
|
|
if ( !GrowIfNecessary ( 11, &ret, &retlen ) )
|
|
|
|
return NULL;
|
|
|
|
_sntprintf ( ret, retlen, _T("%u"), nErrorLevel);
|
|
|
|
ret[retlen-1] = 0;
|
|
|
|
if ( varNameLen )
|
|
|
|
*varNameLen = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
2008-08-12 23:46:15 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
SubstituteVars(TCHAR *Src, TCHAR *Dest, TCHAR Delim, BOOL bIsBatch)
|
|
|
|
{
|
|
|
|
#define APPEND(From, Length) { \
|
|
|
|
if (Dest + (Length) > DestEnd) \
|
|
|
|
goto too_long; \
|
|
|
|
memcpy(Dest, From, (Length) * sizeof(TCHAR)); \
|
|
|
|
Dest += Length; }
|
|
|
|
#define APPEND1(Char) { \
|
|
|
|
if (Dest >= DestEnd) \
|
|
|
|
goto too_long; \
|
|
|
|
*Dest++ = Char; }
|
|
|
|
|
|
|
|
TCHAR *DestEnd = Dest + CMDLINE_LENGTH - 1;
|
|
|
|
const TCHAR *Var;
|
|
|
|
int VarLength;
|
|
|
|
TCHAR *SubstStart;
|
|
|
|
TCHAR EndChr;
|
|
|
|
while (*Src)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-08-12 23:46:15 +00:00
|
|
|
if (*Src != Delim)
|
|
|
|
{
|
|
|
|
APPEND1(*Src++)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Src++;
|
|
|
|
if (bIsBatch && Delim == _T('%'))
|
|
|
|
{
|
|
|
|
UINT NameLen;
|
|
|
|
Var = GetBatchVar(Src, &NameLen);
|
|
|
|
if (Var != NULL)
|
|
|
|
{
|
|
|
|
VarLength = _tcslen(Var);
|
|
|
|
APPEND(Var, VarLength)
|
|
|
|
Src += NameLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the end of the variable name. A colon (:) will usually
|
|
|
|
* end the name and begin the optional modifier, but not if it
|
|
|
|
* is immediately followed by the delimiter (%VAR:%). */
|
|
|
|
SubstStart = Src;
|
|
|
|
while (*Src != Delim && !(*Src == _T(':') && Src[1] != Delim))
|
|
|
|
{
|
|
|
|
if (!*Src)
|
|
|
|
goto bad_subst;
|
|
|
|
Src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
EndChr = *Src;
|
|
|
|
*Src = _T('\0');
|
|
|
|
Var = GetEnvVarOrSpecial(SubstStart);
|
|
|
|
*Src++ = EndChr;
|
|
|
|
if (Var == NULL)
|
|
|
|
{
|
|
|
|
/* In a batch file, %NONEXISTENT% "expands" to an empty string */
|
|
|
|
if (bIsBatch)
|
|
|
|
continue;
|
|
|
|
goto bad_subst;
|
|
|
|
}
|
|
|
|
VarLength = _tcslen(Var);
|
|
|
|
|
|
|
|
if (EndChr == Delim)
|
|
|
|
{
|
|
|
|
/* %VAR% - use as-is */
|
|
|
|
APPEND(Var, VarLength)
|
|
|
|
}
|
|
|
|
else if (*Src == _T('~'))
|
|
|
|
{
|
|
|
|
/* %VAR:~[start][,length]% - substring
|
|
|
|
* Negative values are offsets from the end */
|
|
|
|
int Start = _tcstol(Src + 1, &Src, 0);
|
|
|
|
int End = VarLength;
|
|
|
|
if (Start < 0)
|
|
|
|
Start += VarLength;
|
|
|
|
Start = max(Start, 0);
|
|
|
|
Start = min(Start, VarLength);
|
|
|
|
if (*Src == _T(','))
|
|
|
|
{
|
|
|
|
End = _tcstol(Src + 1, &Src, 0);
|
|
|
|
End += (End < 0) ? VarLength : Start;
|
|
|
|
End = max(End, Start);
|
|
|
|
End = min(End, VarLength);
|
|
|
|
}
|
|
|
|
if (*Src++ != Delim)
|
|
|
|
goto bad_subst;
|
|
|
|
APPEND(&Var[Start], End - Start);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* %VAR:old=new% - replace all occurrences of old with new
|
|
|
|
* %VAR:*old=new% - replace first occurrence only,
|
|
|
|
* and remove everything before it */
|
|
|
|
TCHAR *Old, *New;
|
|
|
|
DWORD OldLength, NewLength;
|
|
|
|
BOOL Star = FALSE;
|
|
|
|
int LastMatch = 0, i = 0;
|
|
|
|
|
|
|
|
if (*Src == _T('*'))
|
|
|
|
{
|
|
|
|
Star = TRUE;
|
|
|
|
Src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the string to replace may contain the delimiter */
|
|
|
|
Src = _tcschr(Old = Src, _T('='));
|
|
|
|
if (Src == NULL)
|
|
|
|
goto bad_subst;
|
|
|
|
OldLength = Src++ - Old;
|
|
|
|
if (OldLength == 0)
|
|
|
|
goto bad_subst;
|
|
|
|
|
|
|
|
Src = _tcschr(New = Src, Delim);
|
|
|
|
if (Src == NULL)
|
|
|
|
goto bad_subst;
|
|
|
|
NewLength = Src++ - New;
|
|
|
|
|
|
|
|
while (i < VarLength)
|
|
|
|
{
|
|
|
|
if (_tcsnicmp(&Var[i], Old, OldLength) == 0)
|
|
|
|
{
|
|
|
|
if (!Star)
|
|
|
|
APPEND(&Var[LastMatch], i - LastMatch)
|
|
|
|
APPEND(New, NewLength)
|
|
|
|
i += OldLength;
|
|
|
|
LastMatch = i;
|
|
|
|
if (Star)
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
APPEND(&Var[LastMatch], VarLength - LastMatch)
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bad_subst:
|
|
|
|
Src = SubstStart;
|
|
|
|
if (!bIsBatch)
|
|
|
|
APPEND1(Delim)
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-08-12 23:46:15 +00:00
|
|
|
*Dest = _T('\0');
|
|
|
|
return TRUE;
|
|
|
|
too_long:
|
|
|
|
ConOutResPrintf(STRING_ALIAS_ERROR);
|
|
|
|
nErrorLevel = 9023;
|
|
|
|
return FALSE;
|
|
|
|
#undef APPEND
|
|
|
|
#undef APPEND1
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* do the prompt/input/process loop
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static INT
|
|
|
|
ProcessInput (BOOL bFlag)
|
|
|
|
{
|
|
|
|
TCHAR commandline[CMDLINE_LENGTH];
|
|
|
|
TCHAR readline[CMDLINE_LENGTH];
|
|
|
|
LPTSTR ip;
|
|
|
|
BOOL bEchoThisLine;
|
2008-01-31 19:01:23 +00:00
|
|
|
BOOL bIsBatch;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* if no batch input then... */
|
|
|
|
if (!(ip = ReadBatchLine (&bEchoThisLine)))
|
|
|
|
{
|
|
|
|
if (bFlag)
|
|
|
|
return nErrorLevel;
|
|
|
|
|
|
|
|
ReadCommand (readline, CMDLINE_LENGTH);
|
|
|
|
ip = readline;
|
|
|
|
bEchoThisLine = FALSE;
|
|
|
|
bIsBatch = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bIsBatch = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip leading blanks */
|
|
|
|
while ( _istspace(*ip) )
|
|
|
|
++ip;
|
|
|
|
|
2008-08-12 23:46:15 +00:00
|
|
|
if (!SubstituteVars(ip, commandline, _T('%'), bIsBatch))
|
|
|
|
continue;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
/* JPP 19980807 */
|
|
|
|
/* Echo batch file line */
|
|
|
|
if (bEchoThisLine)
|
|
|
|
{
|
|
|
|
PrintPrompt ();
|
|
|
|
ConOutPuts (commandline);
|
|
|
|
}
|
|
|
|
|
2008-08-12 23:46:15 +00:00
|
|
|
/* FIXME: !vars! should be substituted later, after parsing. */
|
|
|
|
if (!SubstituteVars(commandline, readline, _T('!'), bIsBatch))
|
|
|
|
continue;
|
|
|
|
_tcscpy(commandline, readline);
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
if (!CheckCtrlBreak(BREAK_INPUT) && *commandline)
|
|
|
|
{
|
|
|
|
ParseCommandLine (commandline);
|
|
|
|
if (bEcho && !bIgnoreEcho && (!bIsBatch || bEchoThisLine))
|
|
|
|
ConOutChar ('\n');
|
|
|
|
bIgnoreEcho = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!bCanExit || !bExit);
|
|
|
|
|
|
|
|
return nErrorLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* control-break handler.
|
|
|
|
*/
|
|
|
|
BOOL WINAPI BreakHandler (DWORD dwCtrlType)
|
|
|
|
{
|
|
|
|
|
|
|
|
DWORD dwWritten;
|
|
|
|
INPUT_RECORD rec;
|
|
|
|
static BOOL SelfGenerated = FALSE;
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
if ((dwCtrlType != CTRL_C_EVENT) &&
|
|
|
|
(dwCtrlType != CTRL_BREAK_EVENT))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
2007-10-19 23:21:45 +00:00
|
|
|
{
|
2006-02-16 23:23:37 +00:00
|
|
|
if(SelfGenerated)
|
|
|
|
{
|
|
|
|
SelfGenerated = FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
if (bChildProcessRunning == TRUE)
|
|
|
|
{
|
|
|
|
SelfGenerated = TRUE;
|
|
|
|
GenerateConsoleCtrlEvent (dwCtrlType, 0);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
rec.EventType = KEY_EVENT;
|
|
|
|
rec.Event.KeyEvent.bKeyDown = TRUE;
|
|
|
|
rec.Event.KeyEvent.wRepeatCount = 1;
|
|
|
|
rec.Event.KeyEvent.wVirtualKeyCode = _T('C');
|
|
|
|
rec.Event.KeyEvent.wVirtualScanCode = _T('C') - 35;
|
|
|
|
rec.Event.KeyEvent.uChar.AsciiChar = _T('C');
|
|
|
|
rec.Event.KeyEvent.uChar.UnicodeChar = _T('C');
|
2007-10-19 23:21:45 +00:00
|
|
|
rec.Event.KeyEvent.dwControlKeyState = RIGHT_CTRL_PRESSED;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
WriteConsoleInput(
|
|
|
|
hIn,
|
|
|
|
&rec,
|
|
|
|
1,
|
|
|
|
&dwWritten);
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
bCtrlBreak = TRUE;
|
|
|
|
/* FIXME: Handle batch files */
|
|
|
|
|
|
|
|
//ConOutPrintf(_T("^C"));
|
|
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID AddBreakHandler (VOID)
|
|
|
|
{
|
|
|
|
SetConsoleCtrlHandler ((PHANDLER_ROUTINE)BreakHandler, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
VOID RemoveBreakHandler (VOID)
|
|
|
|
{
|
|
|
|
SetConsoleCtrlHandler ((PHANDLER_ROUTINE)BreakHandler, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* show commands and options that are available.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#if 0
|
|
|
|
static VOID
|
|
|
|
ShowCommands (VOID)
|
|
|
|
{
|
|
|
|
/* print command list */
|
|
|
|
ConOutResPuts(STRING_CMD_HELP1);
|
|
|
|
PrintCommandList();
|
|
|
|
|
|
|
|
/* print feature list */
|
|
|
|
ConOutResPuts(STRING_CMD_HELP2);
|
|
|
|
|
|
|
|
#ifdef FEATURE_ALIASES
|
|
|
|
ConOutResPuts(STRING_CMD_HELP3);
|
|
|
|
#endif
|
|
|
|
#ifdef FEATURE_HISTORY
|
|
|
|
ConOutResPuts(STRING_CMD_HELP4);
|
|
|
|
#endif
|
|
|
|
#ifdef FEATURE_UNIX_FILENAME_COMPLETION
|
|
|
|
ConOutResPuts(STRING_CMD_HELP5);
|
|
|
|
#endif
|
|
|
|
#ifdef FEATURE_DIRECTORY_STACK
|
|
|
|
ConOutResPuts(STRING_CMD_HELP6);
|
|
|
|
#endif
|
|
|
|
#ifdef FEATURE_REDIRECTION
|
|
|
|
ConOutResPuts(STRING_CMD_HELP7);
|
|
|
|
#endif
|
|
|
|
ConOutChar(_T('\n'));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-23 13:27:00 +00:00
|
|
|
static VOID
|
|
|
|
ExecuteAutoRunFile (VOID)
|
|
|
|
{
|
|
|
|
TCHAR autorun[MAX_PATH];
|
|
|
|
DWORD len = MAX_PATH;
|
|
|
|
HKEY hkey;
|
|
|
|
|
|
|
|
if( RegOpenKeyEx(HKEY_LOCAL_MACHINE,
|
|
|
|
_T("SOFTWARE\\Microsoft\\Command Processor"),
|
|
|
|
0,
|
|
|
|
KEY_READ,
|
|
|
|
&hkey ) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
if(RegQueryValueEx(hkey,
|
|
|
|
_T("AutoRun"),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(LPBYTE)autorun,
|
|
|
|
&len) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
ParseCommandLine (autorun);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hkey);
|
|
|
|
}
|
|
|
|
|
2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* set up global initializations and process parameters
|
|
|
|
*
|
|
|
|
* argc - number of parameters to command.com
|
|
|
|
* argv - command-line parameters
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static VOID
|
2007-08-01 12:34:49 +00:00
|
|
|
Initialize (int argc, const TCHAR* argv[])
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
TCHAR commandline[CMDLINE_LENGTH];
|
|
|
|
TCHAR ModuleName[_MAX_PATH + 1];
|
|
|
|
INT i;
|
|
|
|
TCHAR lpBuffer[2];
|
|
|
|
|
|
|
|
//INT len;
|
|
|
|
//TCHAR *ptr, *cmdLine;
|
|
|
|
|
|
|
|
/* get version information */
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
|
|
|
|
GetVersionEx (&osvi);
|
|
|
|
|
|
|
|
/* Some people like to run ReactOS cmd.exe on Win98, it helps in the
|
|
|
|
* build process. So don't link implicitly against ntdll.dll, load it
|
|
|
|
* dynamically instead */
|
|
|
|
|
|
|
|
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
|
|
|
|
{
|
|
|
|
/* ntdll is always present on NT */
|
|
|
|
NtDllModule = GetModuleHandle(TEXT("ntdll.dll"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not all 9x versions have a ntdll.dll, try to load it */
|
|
|
|
NtDllModule = LoadLibrary(TEXT("ntdll.dll"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NtDllModule != NULL)
|
|
|
|
{
|
|
|
|
NtQueryInformationProcessPtr = (NtQueryInformationProcessProc)GetProcAddress(NtDllModule, "NtQueryInformationProcess");
|
|
|
|
NtReadVirtualMemoryPtr = (NtReadVirtualMemoryProc)GetProcAddress(NtDllModule, "NtReadVirtualMemory");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-15 20:02:05 +00:00
|
|
|
TRACE ("[command args:\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
for (i = 0; i < argc; i++)
|
|
|
|
{
|
2008-05-15 20:29:50 +00:00
|
|
|
TRACE ("%d. %s\n", i, debugstr_aw(argv[i]));
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-05-15 20:02:05 +00:00
|
|
|
TRACE ("]\n");
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
InitLocale ();
|
|
|
|
|
|
|
|
/* get default input and output console handles */
|
|
|
|
hOut = GetStdHandle (STD_OUTPUT_HANDLE);
|
|
|
|
hIn = GetStdHandle (STD_INPUT_HANDLE);
|
|
|
|
|
|
|
|
/* Set EnvironmentVariable PROMPT if it does not exists any env value.
|
|
|
|
for you can change the EnvirommentVariable for prompt before cmd start
|
|
|
|
this patch are not 100% right, if it does not exists a PROMPT value cmd should use
|
|
|
|
$P$G as defualt not set EnvirommentVariable PROMPT to $P$G if it does not exists */
|
|
|
|
if (GetEnvironmentVariable(_T("PROMPT"),lpBuffer, sizeof(lpBuffer) / sizeof(lpBuffer[0])) == 0)
|
|
|
|
SetEnvironmentVariable (_T("PROMPT"), _T("$P$G"));
|
|
|
|
|
|
|
|
|
|
|
|
if (argc >= 2 && !_tcsncmp (argv[1], _T("/?"), 2))
|
|
|
|
{
|
|
|
|
ConOutResPaging(TRUE,STRING_CMD_HELP8);
|
2007-08-01 12:34:49 +00:00
|
|
|
cmd_exit(0);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
SetConsoleMode (hIn, ENABLE_PROCESSED_INPUT);
|
|
|
|
|
|
|
|
#ifdef INCLUDE_CMD_CHDIR
|
|
|
|
InitLastPath ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (argc >= 2)
|
|
|
|
{
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
if (!_tcsicmp (argv[i], _T("/p")))
|
|
|
|
{
|
|
|
|
if (!IsExistingFile (_T("\\autoexec.bat")))
|
|
|
|
{
|
|
|
|
#ifdef INCLUDE_CMD_DATE
|
|
|
|
cmd_date (_T(""), _T(""));
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_CMD_TIME
|
|
|
|
cmd_time (_T(""), _T(""));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ParseCommandLine (_T("\\autoexec.bat"));
|
|
|
|
}
|
|
|
|
bCanExit = FALSE;
|
|
|
|
}
|
|
|
|
else if (!_tcsicmp (argv[i], _T("/c")))
|
|
|
|
{
|
|
|
|
/* This just runs a program and exits */
|
|
|
|
++i;
|
|
|
|
if (i < argc)
|
|
|
|
{
|
|
|
|
_tcscpy (commandline, argv[i]);
|
|
|
|
while (++i < argc)
|
|
|
|
{
|
|
|
|
_tcscat (commandline, _T(" "));
|
|
|
|
_tcscat (commandline, argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseCommandLine(commandline);
|
2007-08-01 12:34:49 +00:00
|
|
|
cmd_exit (ProcessInput (TRUE));
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-01 12:34:49 +00:00
|
|
|
cmd_exit (0);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!_tcsicmp (argv[i], _T("/k")))
|
|
|
|
{
|
|
|
|
/* This just runs a program and remains */
|
|
|
|
++i;
|
|
|
|
if (i < argc)
|
|
|
|
{
|
|
|
|
_tcscpy (commandline, _T("\""));
|
|
|
|
_tcscat (commandline, argv[i]);
|
|
|
|
_tcscat (commandline, _T("\""));
|
|
|
|
while (++i < argc)
|
|
|
|
{
|
|
|
|
_tcscat (commandline, _T(" "));
|
|
|
|
_tcscat (commandline, argv[i]);
|
|
|
|
}
|
|
|
|
ParseCommandLine(commandline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef INCLUDE_CMD_COLOR
|
|
|
|
else if (!_tcsnicmp (argv[i], _T("/t:"), 3))
|
|
|
|
{
|
|
|
|
/* process /t (color) argument */
|
|
|
|
wDefColor = (WORD)_tcstoul (&argv[i][3], NULL, 16);
|
|
|
|
wColor = wDefColor;
|
|
|
|
SetScreenColor (wColor, TRUE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2007-12-23 13:27:00 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Display a simple version string */
|
|
|
|
ConOutPrintf(_T("ReactOS Operating System [Version %s-%s]\n"),
|
|
|
|
_T(KERNEL_RELEASE_STR),
|
|
|
|
_T(KERNEL_VERSION_BUILD_STR));
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-03-30 10:01:37 +00:00
|
|
|
ConOutPuts (_T("(C) Copyright 1998-") _T(COPYRIGHT_YEAR) _T(" ReactOS Team.\n"));
|
2007-12-23 13:27:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ExecuteAutoRunFile ();
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
#ifdef FEATURE_DIR_STACK
|
|
|
|
/* initialize directory stack */
|
|
|
|
InitDirectoryStack ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef FEATURE_HISTORY
|
|
|
|
/*initialize history*/
|
|
|
|
InitHistory();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set COMSPEC environment variable */
|
|
|
|
if (0 != GetModuleFileName (NULL, ModuleName, _MAX_PATH + 1))
|
|
|
|
{
|
|
|
|
ModuleName[_MAX_PATH] = _T('\0');
|
|
|
|
SetEnvironmentVariable (_T("COMSPEC"), ModuleName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add ctrl break handler */
|
|
|
|
AddBreakHandler ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-01 12:34:49 +00:00
|
|
|
static VOID Cleanup (int argc, const TCHAR *argv[])
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
/* run cmdexit.bat */
|
|
|
|
if (IsExistingFile (_T("cmdexit.bat")))
|
|
|
|
{
|
|
|
|
ConErrResPuts(STRING_CMD_ERROR5);
|
|
|
|
|
|
|
|
ParseCommandLine (_T("cmdexit.bat"));
|
|
|
|
}
|
|
|
|
else if (IsExistingFile (_T("\\cmdexit.bat")))
|
|
|
|
{
|
|
|
|
ConErrResPuts (STRING_CMD_ERROR5);
|
|
|
|
ParseCommandLine (_T("\\cmdexit.bat"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FEATURE_DIECTORY_STACK
|
|
|
|
/* destroy directory stack */
|
|
|
|
DestroyDirectoryStack ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INCLUDE_CMD_CHDIR
|
|
|
|
FreeLastPath ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FEATURE_HISTORY
|
|
|
|
CleanHistory();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* remove ctrl break handler */
|
|
|
|
RemoveBreakHandler ();
|
|
|
|
SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE ),
|
|
|
|
ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_ECHO_INPUT );
|
|
|
|
|
|
|
|
if (NtDllModule != NULL)
|
|
|
|
{
|
|
|
|
FreeLibrary(NtDllModule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* main function
|
|
|
|
*/
|
2007-08-02 06:59:36 +00:00
|
|
|
int cmd_main (int argc, const TCHAR *argv[])
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
|
|
|
TCHAR startPath[MAX_PATH];
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO Info;
|
|
|
|
INT nExitCode;
|
|
|
|
|
|
|
|
GetCurrentDirectory(MAX_PATH,startPath);
|
|
|
|
_tchdir(startPath);
|
|
|
|
|
|
|
|
SetFileApisToOEM();
|
|
|
|
InputCodePage= 0;
|
|
|
|
OutputCodePage = 0;
|
|
|
|
|
|
|
|
hConsole = CreateFile(_T("CONOUT$"), GENERIC_READ|GENERIC_WRITE,
|
|
|
|
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
|
|
|
|
OPEN_EXISTING, 0, NULL);
|
|
|
|
if (GetConsoleScreenBufferInfo(hConsole, &Info) == FALSE)
|
|
|
|
{
|
|
|
|
ConErrFormatMessage(GetLastError());
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
wColor = Info.wAttributes;
|
|
|
|
wDefColor = wColor;
|
|
|
|
|
|
|
|
InputCodePage= GetConsoleCP();
|
|
|
|
OutputCodePage = GetConsoleOutputCP();
|
|
|
|
CMD_ModuleHandle = GetModuleHandle(NULL);
|
|
|
|
|
|
|
|
/* check switches on command-line */
|
|
|
|
Initialize(argc, argv);
|
|
|
|
|
|
|
|
/* call prompt routine */
|
|
|
|
nExitCode = ProcessInput(FALSE);
|
|
|
|
|
|
|
|
/* do the cleanup */
|
|
|
|
Cleanup(argc, argv);
|
|
|
|
|
2007-08-01 12:34:49 +00:00
|
|
|
cmd_exit(nExitCode);
|
2006-02-16 23:23:37 +00:00
|
|
|
return(nExitCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|