[CMD] Further code style and formatting fixes.

This commit is contained in:
Hermès Bélusca-Maïto 2020-07-26 20:30:03 +02:00
parent c93f511241
commit ca912d7b36
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
7 changed files with 164 additions and 168 deletions

View file

@ -61,14 +61,12 @@
#include "precomp.h" #include "precomp.h"
/* The stack of current batch contexts. /* The stack of current batch contexts.
* NULL when no batch is active * NULL when no batch is active.
*/ */
LPBATCH_CONTEXT bc = NULL; PBATCH_CONTEXT bc = NULL;
BOOL bEcho = TRUE; /* The echo flag */ BOOL bEcho = TRUE; /* The echo flag */
/* Buffer for reading Batch file lines */ /* Buffer for reading Batch file lines */
TCHAR textline[BATCH_BUFFSIZE]; TCHAR textline[BATCH_BUFFSIZE];
@ -79,7 +77,6 @@ TCHAR textline[BATCH_BUFFSIZE];
* If no batch file is current, returns NULL * If no batch file is current, returns NULL
* *
*/ */
LPTSTR FindArg(TCHAR Char, BOOL *IsParam0) LPTSTR FindArg(TCHAR Char, BOOL *IsParam0)
{ {
LPTSTR pp; LPTSTR pp;
@ -109,10 +106,9 @@ LPTSTR FindArg(TCHAR Char, BOOL *IsParam0)
* NULL character signalling the end of the parameters. * NULL character signalling the end of the parameters.
* *
*/ */
static LPTSTR BatchParams(LPTSTR s1, LPTSTR s2)
LPTSTR BatchParams(LPTSTR s1, LPTSTR s2)
{ {
LPTSTR dp = (LPTSTR)cmd_alloc ((_tcslen(s1) + _tcslen(s2) + 3) * sizeof (TCHAR)); LPTSTR dp = (LPTSTR)cmd_alloc((_tcslen(s1) + _tcslen(s2) + 3) * sizeof (TCHAR));
/* JPP 20-Jul-1998 added error checking */ /* JPP 20-Jul-1998 added error checking */
if (dp == NULL) if (dp == NULL)
@ -157,11 +153,11 @@ LPTSTR BatchParams(LPTSTR s1, LPTSTR s2)
} }
/* /*
* free the allocated memory of a batch file * Free the allocated memory of a batch file.
*/ */
VOID ClearBatch(VOID) static VOID ClearBatch(VOID)
{ {
TRACE ("ClearBatch mem = %08x free = %d\n", bc->mem, bc->memfree); TRACE("ClearBatch mem = %08x ; free = %d\n", bc->mem, bc->memfree);
if (bc->mem && bc->memfree) if (bc->mem && bc->memfree)
cmd_free(bc->mem); cmd_free(bc->mem);
@ -187,7 +183,7 @@ VOID ExitBatch(VOID)
{ {
ClearBatch(); ClearBatch();
TRACE ("ExitBatch\n"); TRACE("ExitBatch\n");
UndoRedirection(bc->RedirList, NULL); UndoRedirection(bc->RedirList, NULL);
FreeRedirection(bc->RedirList); FreeRedirection(bc->RedirList);
@ -202,15 +198,14 @@ VOID ExitBatch(VOID)
} }
/* /*
* Load batch file into memory * Load batch file into memory.
*
*/ */
void BatchFile2Mem(HANDLE hBatchFile) static void BatchFile2Mem(HANDLE hBatchFile)
{ {
TRACE ("BatchFile2Mem ()\n"); TRACE("BatchFile2Mem()\n");
bc->memsize = GetFileSize(hBatchFile, NULL); bc->memsize = GetFileSize(hBatchFile, NULL);
bc->mem = (char *)cmd_alloc(bc->memsize+1); /* 1 extra for '\0' */ bc->mem = (char *)cmd_alloc(bc->memsize+1); /* 1 extra for '\0' */
/* if memory is available, read it in and close the file */ /* if memory is available, read it in and close the file */
if (bc->mem != NULL) if (bc->mem != NULL)
@ -230,32 +225,31 @@ void BatchFile2Mem(HANDLE hBatchFile)
} }
/* /*
* Start batch file execution * Start batch file execution.
* *
* The firstword parameter is the full filename of the batch file. * The firstword parameter is the full filename of the batch file.
*
*/ */
INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd) INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
{ {
BATCH_CONTEXT new;
LPFOR_CONTEXT saved_fc;
INT i;
INT ret = 0; INT ret = 0;
BOOL same_fn = FALSE; INT i;
HANDLE hFile = NULL;
BOOL bSameFn = FALSE;
BATCH_CONTEXT new;
PFOR_CONTEXT saved_fc;
HANDLE hFile = 0;
SetLastError(0); SetLastError(0);
if (bc && bc->mem) if (bc && bc->mem)
{ {
TCHAR fpname[MAX_PATH]; TCHAR fpname[MAX_PATH];
GetFullPathName(fullname, sizeof(fpname) / sizeof(TCHAR), fpname, NULL); GetFullPathName(fullname, ARRAYSIZE(fpname), fpname, NULL);
if (_tcsicmp(bc->BatchFilePath,fpname)==0) if (_tcsicmp(bc->BatchFilePath, fpname) == 0)
same_fn=TRUE; bSameFn = TRUE;
} }
TRACE ("Batch: (\'%s\', \'%s\', \'%s\') same_fn = %d\n", TRACE("Batch(\'%s\', \'%s\', \'%s\') bSameFn = %d\n",
debugstr_aw(fullname), debugstr_aw(firstword), debugstr_aw(param), same_fn); debugstr_aw(fullname), debugstr_aw(firstword), debugstr_aw(param), bSameFn);
if (!same_fn) if (!bSameFn)
{ {
hFile = CreateFile(fullname, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, hFile = CreateFile(fullname, GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL |
@ -300,7 +294,7 @@ INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
* return until this context has been exited */ * return until this context has been exited */
new.prev = bc; new.prev = bc;
/* copy some fields in the new structure if it is the same file */ /* copy some fields in the new structure if it is the same file */
if (same_fn) if (bSameFn)
{ {
new.mem = bc->mem; new.mem = bc->mem;
new.memsize = bc->memsize; new.memsize = bc->memsize;
@ -312,23 +306,22 @@ INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
bc->setlocal = setlocal; bc->setlocal = setlocal;
} }
GetFullPathName(fullname, sizeof(bc->BatchFilePath) / sizeof(TCHAR), bc->BatchFilePath, NULL); GetFullPathName(fullname, ARRAYSIZE(bc->BatchFilePath), bc->BatchFilePath, NULL);
/* if a new batch file, load it into memory and close the file */
if (!same_fn) /* If a new batch file, load it into memory and close the file */
if (!bSameFn)
{ {
BatchFile2Mem(hFile); BatchFile2Mem(hFile);
CloseHandle(hFile); CloseHandle(hFile);
} }
bc->mempos = 0; /* goto begin of batch file */ bc->mempos = 0; /* Go to the beginning of the batch file */
bc->bEcho = bEcho; /* Preserve echo across batch calls */ bc->bEcho = bEcho; /* Preserve echo across batch calls */
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++)
bc->shiftlevel[i] = i; bc->shiftlevel[i] = i;
bc->params = BatchParams (firstword, param); /* Parse the parameters and make a raw copy of them without modifications */
// bc->params = BatchParams(firstword, param);
// Allocate enough memory to hold the params and copy them over without modifications
//
bc->raw_params = cmd_dup(param); bc->raw_params = cmd_dup(param);
if (bc->raw_params == NULL) if (bc->raw_params == NULL)
{ {
@ -362,7 +355,7 @@ INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
/* Always return the current errorlevel */ /* Always return the current errorlevel */
ret = nErrorLevel; ret = nErrorLevel;
TRACE ("Batch: returns TRUE\n"); TRACE("Batch: returns TRUE\n");
fc = saved_fc; fc = saved_fc;
return ret; return ret;
@ -443,25 +436,25 @@ BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength)
*/ */
LPTSTR ReadBatchLine(VOID) LPTSTR ReadBatchLine(VOID)
{ {
TRACE ("ReadBatchLine ()\n"); TRACE("ReadBatchLine()\n");
/* User halt */ /* User halt */
if (CheckCtrlBreak (BREAK_BATCHFILE)) if (CheckCtrlBreak(BREAK_BATCHFILE))
{ {
while (bc) while (bc)
ExitBatch(); ExitBatch();
return NULL; return NULL;
} }
if (!BatchGetString (textline, sizeof (textline) / sizeof (textline[0]) - 1)) if (!BatchGetString(textline, ARRAYSIZE(textline) - 1))
{ {
TRACE ("ReadBatchLine(): Reached EOF!\n"); TRACE("ReadBatchLine(): Reached EOF!\n");
/* End of file.... */ /* End of file */
ExitBatch(); ExitBatch();
return NULL; return NULL;
} }
TRACE ("ReadBatchLine(): textline: \'%s\'\n", debugstr_aw(textline)); TRACE("ReadBatchLine(): textline: \'%s\'\n", debugstr_aw(textline));
if (textline[_tcslen(textline) - 1] != _T('\n')) if (textline[_tcslen(textline) - 1] != _T('\n'))
_tcscat(textline, _T("\n")); _tcscat(textline, _T("\n"));

View file

@ -4,9 +4,9 @@
#pragma once #pragma once
typedef struct tagBATCHCONTEXT typedef struct _BATCH_CONTEXT
{ {
struct tagBATCHCONTEXT *prev; struct _BATCH_CONTEXT *prev;
char *mem; /* batchfile content in memory */ char *mem; /* batchfile content in memory */
DWORD memsize; /* size of batchfile */ DWORD memsize; /* size of batchfile */
DWORD mempos; /* current position to read from */ DWORD mempos; /* current position to read from */
@ -19,24 +19,23 @@ typedef struct tagBATCHCONTEXT
REDIRECTION *RedirList; REDIRECTION *RedirList;
PARSED_COMMAND *current; PARSED_COMMAND *current;
struct _SETLOCAL *setlocal; struct _SETLOCAL *setlocal;
} BATCH_CONTEXT, *LPBATCH_CONTEXT; } BATCH_CONTEXT, *PBATCH_CONTEXT;
typedef struct tagFORCONTEXT typedef struct _FOR_CONTEXT
{ {
struct tagFORCONTEXT *prev; struct _FOR_CONTEXT *prev;
TCHAR firstvar; TCHAR firstvar;
UINT varcount; UINT varcount;
LPTSTR *values; LPTSTR *values;
} FOR_CONTEXT, *LPFOR_CONTEXT; } FOR_CONTEXT, *PFOR_CONTEXT;
/* /*
* The stack of current batch contexts. * The stack of current batch contexts.
* NULL when no batch is active * NULL when no batch is active.
*/ */
extern LPBATCH_CONTEXT bc; extern PBATCH_CONTEXT bc;
extern PFOR_CONTEXT fc;
extern LPFOR_CONTEXT fc;
extern BOOL bEcho; /* The echo flag */ extern BOOL bEcho; /* The echo flag */

View file

@ -541,8 +541,8 @@ Execute(LPTSTR Full, LPTSTR First, LPTSTR Rest, PARSED_COMMAND *Cmd)
/* /*
* look through the internal commands and determine whether or not this * 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 * command is one of them. If it is, call the command. If not, call
* execute to run it as an external program. * execute to run it as an external program.
* *
* first - first word on command line * first - first word on command line
@ -553,7 +553,7 @@ DoCommand(LPTSTR first, LPTSTR rest, PARSED_COMMAND *Cmd)
{ {
TCHAR *com; TCHAR *com;
TCHAR *cp; TCHAR *cp;
LPTSTR param; /* pointer to command's parameters */ LPTSTR param; /* Pointer to command's parameters */
INT cl; INT cl;
LPCOMMAND cmdptr; LPCOMMAND cmdptr;
BOOL nointernal = FALSE; BOOL nointernal = FALSE;
@ -561,7 +561,7 @@ DoCommand(LPTSTR first, LPTSTR rest, PARSED_COMMAND *Cmd)
TRACE ("DoCommand: (\'%s\' \'%s\')\n", debugstr_aw(first), debugstr_aw(rest)); TRACE ("DoCommand: (\'%s\' \'%s\')\n", debugstr_aw(first), debugstr_aw(rest));
/* full command line */ /* Full command line */
com = cmd_alloc((_tcslen(first) + _tcslen(rest) + 2) * sizeof(TCHAR)); com = cmd_alloc((_tcslen(first) + _tcslen(rest) + 2) * sizeof(TCHAR));
if (com == NULL) if (com == NULL)
{ {
@ -795,12 +795,14 @@ ExecuteCommand(
cmd_free(First); cmd_free(First);
} }
break; break;
case C_QUIET: case C_QUIET:
case C_BLOCK: case C_BLOCK:
case C_MULTI: case C_MULTI:
for (Sub = Cmd->Subcommands; Sub; Sub = Sub->Next) for (Sub = Cmd->Subcommands; Sub; Sub = Sub->Next)
Ret = ExecuteCommand(Sub); Ret = ExecuteCommand(Sub);
break; break;
case C_OR: case C_OR:
Sub = Cmd->Subcommands; Sub = Cmd->Subcommands;
Ret = ExecuteCommand(Sub); Ret = ExecuteCommand(Sub);
@ -810,18 +812,22 @@ ExecuteCommand(
Ret = ExecuteCommand(Sub->Next); Ret = ExecuteCommand(Sub->Next);
} }
break; break;
case C_AND: case C_AND:
Sub = Cmd->Subcommands; Sub = Cmd->Subcommands;
Ret = ExecuteCommand(Sub); Ret = ExecuteCommand(Sub);
if (Ret == 0) if (Ret == 0)
Ret = ExecuteCommand(Sub->Next); Ret = ExecuteCommand(Sub->Next);
break; break;
case C_PIPE: case C_PIPE:
Ret = ExecutePipeline(Cmd); Ret = ExecutePipeline(Cmd);
break; break;
case C_IF: case C_IF:
Ret = ExecuteIf(Cmd); Ret = ExecuteIf(Cmd);
break; break;
case C_FOR: case C_FOR:
Ret = ExecuteFor(Cmd); Ret = ExecuteFor(Cmd);
break; break;
@ -878,48 +884,44 @@ GetEnvVarOrSpecial(LPCTSTR varName)
/* env var doesn't exist, look for a "special" one */ /* env var doesn't exist, look for a "special" one */
/* %CD% */ /* %CD% */
if (_tcsicmp(varName,_T("cd")) ==0) if (_tcsicmp(varName, _T("CD")) == 0)
{ {
GetCurrentDirectory(MAX_PATH, ret); GetCurrentDirectory(ARRAYSIZE(ret), ret);
return ret; return ret;
} }
/* %TIME% */
else if (_tcsicmp(varName,_T("time")) ==0)
{
return GetTimeString();
}
/* %DATE% */ /* %DATE% */
else if (_tcsicmp(varName,_T("date")) ==0) else if (_tcsicmp(varName, _T("DATE")) == 0)
{ {
return GetDateString(); return GetDateString();
} }
/* %TIME% */
else if (_tcsicmp(varName, _T("TIME")) == 0)
{
return GetTimeString();
}
/* %RANDOM% */ /* %RANDOM% */
else if (_tcsicmp(varName,_T("random")) ==0) else if (_tcsicmp(varName, _T("RANDOM")) == 0)
{ {
/* Get random number */ /* Get random number */
_itot(rand(),ret,10); _itot(rand(), ret, 10);
return ret; return ret;
} }
/* %CMDCMDLINE% */ /* %CMDCMDLINE% */
else if (_tcsicmp(varName,_T("cmdcmdline")) ==0) else if (_tcsicmp(varName, _T("CMDCMDLINE")) == 0)
{ {
return GetCommandLine(); return GetCommandLine();
} }
/* %CMDEXTVERSION% */ /* %CMDEXTVERSION% */
else if (_tcsicmp(varName,_T("cmdextversion")) ==0) else if (_tcsicmp(varName, _T("CMDEXTVERSION")) == 0)
{ {
/* Set version number to 2 */ /* Set version number to 2 */
_itot(2,ret,10); _itot(2, ret, 10);
return ret; return ret;
} }
/* %ERRORLEVEL% */ /* %ERRORLEVEL% */
else if (_tcsicmp(varName,_T("errorlevel")) ==0) else if (_tcsicmp(varName, _T("ERRORLEVEL")) == 0)
{ {
_itot(nErrorLevel,ret,10); _itot(nErrorLevel, ret, 10);
return ret; return ret;
} }
@ -1018,7 +1020,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
PathVar = GetEnvVar(PathVarName); PathVar = GetEnvVar(PathVarName);
FormatEnd[-1] = _T(':'); FormatEnd[-1] = _T(':');
if (!PathVar || if (!PathVar ||
!SearchPath(PathVar, Result, NULL, MAX_PATH, FullPath, NULL)) !SearchPath(PathVar, Result, NULL, ARRAYSIZE(FullPath), FullPath, NULL))
{ {
return _T(""); return _T("");
} }
@ -1038,7 +1040,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
else else
{ {
/* Convert the variable, now without quotes, to a full path */ /* Convert the variable, now without quotes, to a full path */
if (!GetFullPathName(Result, MAX_PATH, FullPath, NULL)) if (!GetFullPathName(Result, ARRAYSIZE(FullPath), FullPath, NULL))
return _T(""); return _T("");
} }
@ -1059,7 +1061,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
if (Next) if (Next)
*Next++ = _T('\0'); *Next++ = _T('\0');
/* Use FindFirstFile to get the correct name */ /* Use FindFirstFile to get the correct name */
if (Out + _tcslen(In) + 1 >= &FixedPath[MAX_PATH]) if (Out + _tcslen(In) + 1 >= &FixedPath[ARRAYSIZE(FixedPath)])
return _T(""); return _T("");
_tcscpy(Out, In); _tcscpy(Out, In);
hFind = FindFirstFile(FixedPath, &w32fd); hFind = FindFirstFile(FixedPath, &w32fd);
@ -1074,7 +1076,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
} }
FindClose(hFind); FindClose(hFind);
if (Out + _tcslen(FixedComponent) + 1 >= &FixedPath[MAX_PATH]) if (Out + _tcslen(FixedComponent) + 1 >= &FixedPath[ARRAYSIZE(FixedPath)])
return _T(""); return _T("");
_tcscpy(Out, FixedComponent); _tcscpy(Out, FixedComponent);
} }
@ -1107,7 +1109,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
{ _T('t'), FILE_ATTRIBUTE_TEMPORARY }, { _T('t'), FILE_ATTRIBUTE_TEMPORARY },
{ _T('l'), FILE_ATTRIBUTE_REPARSE_POINT }, { _T('l'), FILE_ATTRIBUTE_REPARSE_POINT },
}; };
for (Attrib = Table; Attrib != &Table[9]; Attrib++) for (Attrib = Table; Attrib != &Table[ARRAYSIZE(Table)]; Attrib++)
{ {
*Out++ = w32fd.dwFileAttributes & Attrib->Value *Out++ = w32fd.dwFileAttributes & Attrib->Value
? Attrib->Character ? Attrib->Character
@ -1176,7 +1178,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
return Result; return Result;
} }
LPCTSTR static LPCTSTR
GetBatchVar(TCHAR *varName, UINT *varNameLen) GetBatchVar(TCHAR *varName, UINT *varNameLen)
{ {
LPCTSTR ret; LPCTSTR ret;
@ -1197,6 +1199,7 @@ GetBatchVar(TCHAR *varName, UINT *varNameLen)
} }
*varNameLen = varNameEnd - varName; *varNameLen = varNameEnd - varName;
return ret; return ret;
case _T('0'): case _T('0'):
case _T('1'): case _T('1'):
case _T('2'): case _T('2'):
@ -1210,9 +1213,7 @@ GetBatchVar(TCHAR *varName, UINT *varNameLen)
return FindArg(*varName, &dummy); return FindArg(*varName, &dummy);
case _T('*'): case _T('*'):
// /* Copy over the raw params (not including the batch file name) */
// Copy over the raw params(not including the batch file name
//
return bc->raw_params; return bc->raw_params;
case _T('%'): case _T('%'):
@ -1375,9 +1376,11 @@ too_long:
} }
/* Search the list of FOR contexts for a variable */ /* Search the list of FOR contexts for a variable */
static LPTSTR FindForVar(TCHAR Var, BOOL *IsParam0) static LPTSTR
FindForVar(TCHAR Var, BOOL *IsParam0)
{ {
FOR_CONTEXT *Ctx; PFOR_CONTEXT Ctx;
*IsParam0 = FALSE; *IsParam0 = FALSE;
for (Ctx = fc; Ctx != NULL; Ctx = Ctx->prev) for (Ctx = fc; Ctx != NULL; Ctx = Ctx->prev)
{ {
@ -1446,10 +1449,8 @@ DoDelayedExpansion(LPTSTR Line)
/* /*
* do the prompt/input/process loop * Do the prompt/input/process loop.
*
*/ */
BOOL BOOL
ReadLine(TCHAR *commandline, BOOL bMore) ReadLine(TCHAR *commandline, BOOL bMore)
{ {
@ -1522,12 +1523,14 @@ ProcessInput(VOID)
/* /*
* control-break handler. * Control-break handler.
*/ */
BOOL WINAPI BreakHandler(DWORD dwCtrlType) static BOOL
WINAPI
BreakHandler(IN DWORD dwCtrlType)
{ {
DWORD dwWritten; DWORD dwWritten;
INPUT_RECORD rec; INPUT_RECORD rec;
if ((dwCtrlType != CTRL_C_EVENT) && if ((dwCtrlType != CTRL_C_EVENT) &&
(dwCtrlType != CTRL_BREAK_EVENT)) (dwCtrlType != CTRL_BREAK_EVENT))
@ -1582,8 +1585,7 @@ VOID RemoveBreakHandler(VOID)
/* /*
* show commands and options that are available. * Show commands and options that are available.
*
*/ */
#if 0 #if 0
static VOID static VOID
@ -1799,18 +1801,21 @@ ExecuteAutoRunFile(HKEY hKeyRoot)
/* Get the command that comes after a /C or /K switch */ /* Get the command that comes after a /C or /K switch */
static VOID static VOID
GetCmdLineCommand(TCHAR *commandline, TCHAR *ptr, BOOL AlwaysStrip) GetCmdLineCommand(
OUT LPTSTR commandline,
IN LPCTSTR ptr,
IN BOOL AlwaysStrip)
{ {
TCHAR *LastQuote; TCHAR* LastQuote;
while (_istspace(*ptr)) while (_istspace(*ptr))
ptr++; ++ptr;
/* Remove leading quote, find final quote */ /* Remove leading quote, find final quote */
if (*ptr == _T('"') && if (*ptr == _T('"') &&
(LastQuote = _tcsrchr(++ptr, _T('"'))) != NULL) (LastQuote = _tcsrchr(++ptr, _T('"'))) != NULL)
{ {
TCHAR *Space; const TCHAR* Space;
/* Under certain circumstances, all quotes are preserved. /* Under certain circumstances, all quotes are preserved.
* CMD /? documents these conditions as follows: * CMD /? documents these conditions as follows:
* 1. No /S switch * 1. No /S switch
@ -1822,7 +1827,7 @@ GetCmdLineCommand(TCHAR *commandline, TCHAR *ptr, BOOL AlwaysStrip)
* 5. Enclosed string is an executable filename * 5. Enclosed string is an executable filename
*/ */
*LastQuote = _T('\0'); *LastQuote = _T('\0');
for (Space = ptr + 1; Space < LastQuote; Space++) for (Space = ptr + 1; Space < LastQuote; ++Space)
{ {
if (_istspace(*Space)) /* Rule 4 */ if (_istspace(*Space)) /* Rule 4 */
{ {
@ -1858,15 +1863,14 @@ static VOID
Initialize(VOID) Initialize(VOID)
{ {
HMODULE NtDllModule; HMODULE NtDllModule;
TCHAR commandline[CMDLINE_LENGTH];
TCHAR ModuleName[_MAX_PATH + 1];
// INT nExitCode; // INT nExitCode;
HANDLE hIn, hOut; HANDLE hIn, hOut;
LPTSTR ptr, cmdLine;
TCHAR *ptr, *cmdLine, option = 0; TCHAR option = 0;
BOOL AlwaysStrip = FALSE; BOOL AlwaysStrip = FALSE;
BOOL AutoRun = TRUE; BOOL AutoRun = TRUE;
TCHAR ModuleName[MAX_PATH + 1];
TCHAR commandline[CMDLINE_LENGTH];
/* Get version information */ /* Get version information */
InitOSVersion(); InitOSVersion();
@ -1904,7 +1908,7 @@ Initialize(VOID)
/* Set COMSPEC environment variable */ /* Set COMSPEC environment variable */
if (GetModuleFileName(NULL, ModuleName, ARRAYSIZE(ModuleName)) != 0) if (GetModuleFileName(NULL, ModuleName, ARRAYSIZE(ModuleName)) != 0)
{ {
ModuleName[_MAX_PATH] = _T('\0'); ModuleName[MAX_PATH] = _T('\0');
SetEnvironmentVariable (_T("COMSPEC"), ModuleName); SetEnvironmentVariable (_T("COMSPEC"), ModuleName);
} }
@ -1921,32 +1925,32 @@ Initialize(VOID)
cmdLine = GetCommandLine(); cmdLine = GetCommandLine();
TRACE ("[command args: %s]\n", debugstr_aw(cmdLine)); TRACE ("[command args: %s]\n", debugstr_aw(cmdLine));
for (ptr = cmdLine; *ptr; ptr++) for (ptr = cmdLine; *ptr; ++ptr)
{ {
if (*ptr == _T('/')) if (*ptr == _T('/'))
{ {
option = _totupper(ptr[1]); option = _totupper(ptr[1]);
if (option == _T('?')) if (option == _T('?'))
{ {
ConOutResPaging(TRUE,STRING_CMD_HELP8); ConOutResPaging(TRUE, STRING_CMD_HELP8);
nErrorLevel = 1; nErrorLevel = 1;
bExit = TRUE; bExit = TRUE;
return; return;
} }
else if (option == _T('P')) else if (option == _T('P'))
{ {
if (!IsExistingFile (_T("\\autoexec.bat"))) if (!IsExistingFile(_T("\\autoexec.bat")))
{ {
#ifdef INCLUDE_CMD_DATE #ifdef INCLUDE_CMD_DATE
cmd_date (_T("")); cmd_date(_T(""));
#endif #endif
#ifdef INCLUDE_CMD_TIME #ifdef INCLUDE_CMD_TIME
cmd_time (_T("")); cmd_time(_T(""));
#endif #endif
} }
else else
{ {
ParseCommandLine (_T("\\autoexec.bat")); ParseCommandLine(_T("\\autoexec.bat"));
} }
bCanExit = FALSE; bCanExit = FALSE;
} }
@ -2126,7 +2130,6 @@ int _tmain(int argc, const TCHAR *argv[])
/* Do the cleanup */ /* Do the cleanup */
Cleanup(); Cleanup();
cmd_free(lpOriginalEnvironment); cmd_free(lpOriginalEnvironment);
cmd_exit(nErrorLevel); cmd_exit(nErrorLevel);

View file

@ -328,10 +328,11 @@ typedef struct _PARSED_COMMAND
TCHAR Variable; TCHAR Variable;
LPTSTR Params; LPTSTR Params;
LPTSTR List; LPTSTR List;
struct tagFORCONTEXT *Context; struct _FOR_CONTEXT *Context;
} For; } For;
}; };
} PARSED_COMMAND; } PARSED_COMMAND;
PARSED_COMMAND *ParseCommand(LPTSTR Line); PARSED_COMMAND *ParseCommand(LPTSTR Line);
VOID EchoCommand(PARSED_COMMAND *Cmd); VOID EchoCommand(PARSED_COMMAND *Cmd);
TCHAR *Unparse(PARSED_COMMAND *Cmd, TCHAR *Out, TCHAR *OutEnd); TCHAR *Unparse(PARSED_COMMAND *Cmd, TCHAR *Out, TCHAR *OutEnd);

View file

@ -34,13 +34,13 @@
/* FOR is a special command, so this function is only used for showing help now */ /* FOR is a special command, so this function is only used for showing help now */
INT cmd_for (LPTSTR param) INT cmd_for(LPTSTR param)
{ {
TRACE ("cmd_for (\'%s\')\n", debugstr_aw(param)); TRACE("cmd_for(\'%s\')\n", debugstr_aw(param));
if (!_tcsncmp (param, _T("/?"), 2)) if (!_tcsncmp(param, _T("/?"), 2))
{ {
ConOutResPaging(TRUE,STRING_FOR_HELP1); ConOutResPaging(TRUE, STRING_FOR_HELP1);
return 0; return 0;
} }
@ -50,7 +50,7 @@ INT cmd_for (LPTSTR param)
/* The stack of current FOR contexts. /* The stack of current FOR contexts.
* NULL when no FOR command is active */ * NULL when no FOR command is active */
LPFOR_CONTEXT fc = NULL; PFOR_CONTEXT fc = NULL;
/* Get the next element of the FOR's list */ /* Get the next element of the FOR's list */
static BOOL GetNextElement(TCHAR **pStart, TCHAR **pEnd) static BOOL GetNextElement(TCHAR **pStart, TCHAR **pEnd)
@ -116,6 +116,7 @@ static LPTSTR ReadFileContents(FILE *InputFile, TCHAR *Buffer)
return Contents; return Contents;
} }
/* FOR /F: Parse the contents of each file */
static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer) static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
{ {
LPTSTR Delims = _T(" \t"); LPTSTR Delims = _T(" \t");
@ -248,6 +249,7 @@ static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
goto single_element; goto single_element;
} }
/* Loop over each file */
End = List; End = List;
while (GetNextElement(&Start, &End)) while (GetNextElement(&Start, &End))
{ {
@ -325,7 +327,7 @@ static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
*CurVar++ = In; *CurVar++ = In;
/* Find end of token */ /* Find end of token */
In += _tcscspn(In, Delims); In += _tcscspn(In, Delims);
/* Nul-terminate it and advance to next token */ /* NULL-terminate it and advance to next token */
if (*In) if (*In)
{ {
*In++ = _T('\0'); *In++ = _T('\0');
@ -352,9 +354,9 @@ static INT ForLoop(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
INT params[3] = { 0, 0, 0 }; INT params[3] = { 0, 0, 0 };
INT i; INT i;
INT Ret = 0; INT Ret = 0;
TCHAR *Start, *End = List; TCHAR *Start, *End = List;
for (i = 0; i < 3 && GetNextElement(&Start, &End); i++)
for (i = 0; i < 3 && GetNextElement(&Start, &End); ++i)
params[i] = _tcstol(Start, NULL, 0); params[i] = _tcstol(Start, NULL, 0);
i = params[START]; i = params[START];
@ -365,6 +367,7 @@ static INT ForLoop(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
Ret = RunInstance(Cmd); Ret = RunInstance(Cmd);
i += params[STEP]; i += params[STEP];
} }
return Ret; return Ret;
} }
@ -373,8 +376,9 @@ static INT ForLoop(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
* it will be empty, but in FOR /R it will be the directory name. */ * it will be empty, but in FOR /R it will be the directory name. */
static INT ForDir(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *BufPos) static INT ForDir(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *BufPos)
{ {
TCHAR *Start, *End = List;
INT Ret = 0; INT Ret = 0;
TCHAR *Start, *End = List;
while (!Exiting(Cmd) && GetNextElement(&Start, &End)) while (!Exiting(Cmd) && GetNextElement(&Start, &End))
{ {
if (BufPos + (End - Start) > &Buffer[CMDLINE_LENGTH]) if (BufPos + (End - Start) > &Buffer[CMDLINE_LENGTH])
@ -417,12 +421,12 @@ static INT ForDir(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *BufPos
return Ret; return Ret;
} }
/* FOR /R: Process a FOR in each directory of a tree, recursively. */ /* FOR /R: Process a FOR in each directory of a tree, recursively */
static INT ForRecursive(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *BufPos) static INT ForRecursive(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *BufPos)
{ {
INT Ret = 0;
HANDLE hFind; HANDLE hFind;
WIN32_FIND_DATA w32fd; WIN32_FIND_DATA w32fd;
INT Ret = 0;
if (BufPos[-1] != _T('\\')) if (BufPos[-1] != _T('\\'))
{ {
@ -446,18 +450,20 @@ static INT ForRecursive(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *
Ret = ForRecursive(Cmd, List, Buffer, _stpcpy(BufPos, w32fd.cFileName)); Ret = ForRecursive(Cmd, List, Buffer, _stpcpy(BufPos, w32fd.cFileName));
} while (!Exiting(Cmd) && FindNextFile(hFind, &w32fd)); } while (!Exiting(Cmd) && FindNextFile(hFind, &w32fd));
FindClose(hFind); FindClose(hFind);
return Ret; return Ret;
} }
INT INT
ExecuteFor(PARSED_COMMAND *Cmd) ExecuteFor(PARSED_COMMAND *Cmd)
{ {
INT Ret;
LPTSTR List;
PFOR_CONTEXT lpNew;
TCHAR Buffer[CMDLINE_LENGTH]; /* Buffer to hold the variable value */ TCHAR Buffer[CMDLINE_LENGTH]; /* Buffer to hold the variable value */
LPTSTR BufferPtr = Buffer; LPTSTR BufferPtr = Buffer;
LPFOR_CONTEXT lpNew;
INT Ret;
LPTSTR List = DoDelayedExpansion(Cmd->For.List);
List = DoDelayedExpansion(Cmd->For.List);
if (!List) if (!List)
return 1; return 1;

View file

@ -28,32 +28,28 @@
#include "precomp.h" #include "precomp.h"
/* /*
* Perform GOTO command. * Perform GOTO command.
* *
* Only valid if batch file current. * Only valid when a batch context is active.
*
*/ */
INT cmd_goto(LPTSTR param)
INT cmd_goto (LPTSTR param)
{ {
LPTSTR tmp, tmp2; LPTSTR tmp, tmp2;
TRACE ("cmd_goto (\'%s\')\n", debugstr_aw(param)); TRACE("cmd_goto(\'%s\')\n", debugstr_aw(param));
if (!_tcsncmp (param, _T("/?"), 2)) if (!_tcsncmp(param, _T("/?"), 2))
{ {
ConOutResPaging(TRUE,STRING_GOTO_HELP1); ConOutResPaging(TRUE, STRING_GOTO_HELP1);
return 0; return 0;
} }
/* if not in batch -- error!! */ /* If not in batch, fail */
if (bc == NULL) if (bc == NULL)
{
return 1; return 1;
}
/* Fail if no label has been provided */
if (*param == _T('\0')) if (*param == _T('\0'))
{ {
ConErrResPrintf(STRING_GOTO_ERROR1); ConErrResPrintf(STRING_GOTO_ERROR1);
@ -61,11 +57,11 @@ INT cmd_goto (LPTSTR param)
return 1; return 1;
} }
/* terminate label at first space char */ /* Terminate label at first space char */
tmp = param+1; tmp = param + 1;
while (!_istcntrl (*tmp) && !_istspace (*tmp) && (*tmp != _T(':'))) while (!_istcntrl(*tmp) && !_istspace(*tmp) && (*tmp != _T(':')))
tmp++; ++tmp;
*(tmp) = _T('\0'); *tmp = _T('\0');
/* jump to end of the file */ /* jump to end of the file */
if ( _tcsicmp( param, _T(":eof"))==0) if ( _tcsicmp( param, _T(":eof"))==0)
@ -77,38 +73,36 @@ INT cmd_goto (LPTSTR param)
/* jump to begin of the file */ /* jump to begin of the file */
bc->mempos=0; bc->mempos=0;
while (BatchGetString (textline, sizeof(textline) / sizeof(textline[0]))) while (BatchGetString(textline, ARRAYSIZE(textline)))
{ {
int pos; INT pos;
INT_PTR size; INT_PTR size;
/* Strip out any trailing spaces or control chars */ /* Strip out any trailing spaces or control chars */
tmp = textline + _tcslen (textline) - 1; tmp = textline + _tcslen(textline) - 1;
while (tmp > textline && (_istcntrl(*tmp) || _istspace(*tmp) || (*tmp == _T(':'))))
while (tmp > textline && (_istcntrl (*tmp) || _istspace (*tmp) || (*tmp == _T(':')))) --tmp;
tmp--;
*(tmp + 1) = _T('\0'); *(tmp + 1) = _T('\0');
/* Then leading spaces... */ /* Then leading spaces... */
tmp = textline; tmp = textline;
while (_istspace (*tmp)) while (_istspace(*tmp))
tmp++; ++tmp;
/* All space after leading space terminate the string */ /* All space after leading space terminate the string */
size = _tcslen(tmp) -1; size = _tcslen(tmp) - 1;
pos=0; pos = 0;
while (tmp+pos < tmp+size) while (tmp + pos < tmp + size)
{ {
if (_istspace(tmp[pos])) if (_istspace(tmp[pos]))
tmp[pos]=_T('\0'); tmp[pos]=_T('\0');
pos++; ++pos;
} }
tmp2 = param; tmp2 = param;
/* use whole label name */ /* Use whole label name */
if ((*tmp == _T(':')) && ((_tcsicmp (++tmp, param) == 0) || (_tcsicmp (tmp, ++tmp2) == 0))) if ((*tmp == _T(':')) && ((_tcsicmp(++tmp, param) == 0) || (_tcsicmp(tmp, ++tmp2) == 0)))
return 0; return 0;
} }
ConErrResPrintf(STRING_GOTO_ERROR2, param); ConErrResPrintf(STRING_GOTO_ERROR2, param);

View file

@ -49,13 +49,13 @@ static INT GenericCmp(INT (*StringCmp)(LPCTSTR, LPCTSTR),
return StringCmp(Left, Right); return StringCmp(Left, Right);
} }
INT cmd_if (LPTSTR param) INT cmd_if(LPTSTR param)
{ {
TRACE ("cmd_if: (\'%s\')\n", debugstr_aw(param)); TRACE("cmd_if(\'%s\')\n", debugstr_aw(param));
if (!_tcsncmp (param, _T("/?"), 2)) if (!_tcsncmp (param, _T("/?"), 2))
{ {
ConOutResPaging(TRUE,STRING_IF_HELP1); ConOutResPaging(TRUE, STRING_IF_HELP1);
return 0; return 0;
} }
@ -181,12 +181,12 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0)) if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0))
{ {
/* full condition was true, do the command */ /* Full condition was true, do the command */
return ExecuteCommand(Cmd->Subcommands); return ExecuteCommand(Cmd->Subcommands);
} }
else else
{ {
/* full condition was false, do the "else" command if there is one */ /* Full condition was false, do the "else" command if there is one */
if (Cmd->Subcommands->Next) if (Cmd->Subcommands->Next)
return ExecuteCommand(Cmd->Subcommands->Next); return ExecuteCommand(Cmd->Subcommands->Next);
return 0; return 0;