[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"
/* 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 */
/* Buffer for reading Batch file lines */
TCHAR textline[BATCH_BUFFSIZE];
@ -79,7 +77,6 @@ TCHAR textline[BATCH_BUFFSIZE];
* If no batch file is current, returns NULL
*
*/
LPTSTR FindArg(TCHAR Char, BOOL *IsParam0)
{
LPTSTR pp;
@ -109,10 +106,9 @@ LPTSTR FindArg(TCHAR Char, BOOL *IsParam0)
* NULL character signalling the end of the parameters.
*
*/
LPTSTR BatchParams(LPTSTR s1, LPTSTR s2)
static 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 */
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)
cmd_free(bc->mem);
@ -187,7 +183,7 @@ VOID ExitBatch(VOID)
{
ClearBatch();
TRACE ("ExitBatch\n");
TRACE("ExitBatch\n");
UndoRedirection(bc->RedirList, NULL);
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->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 (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.
*
*/
INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
{
BATCH_CONTEXT new;
LPFOR_CONTEXT saved_fc;
INT i;
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);
if (bc && bc->mem)
{
TCHAR fpname[MAX_PATH];
GetFullPathName(fullname, sizeof(fpname) / sizeof(TCHAR), fpname, NULL);
if (_tcsicmp(bc->BatchFilePath,fpname)==0)
same_fn=TRUE;
GetFullPathName(fullname, ARRAYSIZE(fpname), fpname, NULL);
if (_tcsicmp(bc->BatchFilePath, fpname) == 0)
bSameFn = TRUE;
}
TRACE ("Batch: (\'%s\', \'%s\', \'%s\') same_fn = %d\n",
debugstr_aw(fullname), debugstr_aw(firstword), debugstr_aw(param), same_fn);
TRACE("Batch(\'%s\', \'%s\', \'%s\') bSameFn = %d\n",
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,
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 */
new.prev = bc;
/* copy some fields in the new structure if it is the same file */
if (same_fn)
if (bSameFn)
{
new.mem = bc->mem;
new.memsize = bc->memsize;
@ -312,23 +306,22 @@ INT Batch(LPTSTR fullname, LPTSTR firstword, LPTSTR param, PARSED_COMMAND *Cmd)
bc->setlocal = setlocal;
}
GetFullPathName(fullname, sizeof(bc->BatchFilePath) / sizeof(TCHAR), bc->BatchFilePath, NULL);
/* if a new batch file, load it into memory and close the file */
if (!same_fn)
GetFullPathName(fullname, ARRAYSIZE(bc->BatchFilePath), bc->BatchFilePath, NULL);
/* If a new batch file, load it into memory and close the file */
if (!bSameFn)
{
BatchFile2Mem(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 */
for (i = 0; i < 10; i++)
bc->shiftlevel[i] = i;
bc->params = BatchParams (firstword, param);
//
// Allocate enough memory to hold the params and copy them over without modifications
//
/* Parse the parameters and make a raw copy of them without modifications */
bc->params = BatchParams(firstword, param);
bc->raw_params = cmd_dup(param);
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 */
ret = nErrorLevel;
TRACE ("Batch: returns TRUE\n");
TRACE("Batch: returns TRUE\n");
fc = saved_fc;
return ret;
@ -443,25 +436,25 @@ BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength)
*/
LPTSTR ReadBatchLine(VOID)
{
TRACE ("ReadBatchLine ()\n");
TRACE("ReadBatchLine()\n");
/* User halt */
if (CheckCtrlBreak (BREAK_BATCHFILE))
if (CheckCtrlBreak(BREAK_BATCHFILE))
{
while (bc)
ExitBatch();
return NULL;
}
if (!BatchGetString (textline, sizeof (textline) / sizeof (textline[0]) - 1))
if (!BatchGetString(textline, ARRAYSIZE(textline) - 1))
{
TRACE ("ReadBatchLine(): Reached EOF!\n");
/* End of file.... */
TRACE("ReadBatchLine(): Reached EOF!\n");
/* End of file */
ExitBatch();
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'))
_tcscat(textline, _T("\n"));

View file

@ -4,9 +4,9 @@
#pragma once
typedef struct tagBATCHCONTEXT
typedef struct _BATCH_CONTEXT
{
struct tagBATCHCONTEXT *prev;
struct _BATCH_CONTEXT *prev;
char *mem; /* batchfile content in memory */
DWORD memsize; /* size of batchfile */
DWORD mempos; /* current position to read from */
@ -19,24 +19,23 @@ typedef struct tagBATCHCONTEXT
REDIRECTION *RedirList;
PARSED_COMMAND *current;
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;
UINT varcount;
LPTSTR *values;
} FOR_CONTEXT, *LPFOR_CONTEXT;
} FOR_CONTEXT, *PFOR_CONTEXT;
/*
* The stack of current batch contexts.
* NULL when no batch is active
* NULL when no batch is active.
*/
extern LPBATCH_CONTEXT bc;
extern LPFOR_CONTEXT fc;
extern PBATCH_CONTEXT bc;
extern PFOR_CONTEXT fc;
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
* command is one of them. If it is, call the command. If not, call
* 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.
*
* first - first word on command line
@ -553,7 +553,7 @@ DoCommand(LPTSTR first, LPTSTR rest, PARSED_COMMAND *Cmd)
{
TCHAR *com;
TCHAR *cp;
LPTSTR param; /* pointer to command's parameters */
LPTSTR param; /* Pointer to command's parameters */
INT cl;
LPCOMMAND cmdptr;
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));
/* full command line */
/* Full command line */
com = cmd_alloc((_tcslen(first) + _tcslen(rest) + 2) * sizeof(TCHAR));
if (com == NULL)
{
@ -795,12 +795,14 @@ ExecuteCommand(
cmd_free(First);
}
break;
case C_QUIET:
case C_BLOCK:
case C_MULTI:
for (Sub = Cmd->Subcommands; Sub; Sub = Sub->Next)
Ret = ExecuteCommand(Sub);
break;
case C_OR:
Sub = Cmd->Subcommands;
Ret = ExecuteCommand(Sub);
@ -810,18 +812,22 @@ ExecuteCommand(
Ret = ExecuteCommand(Sub->Next);
}
break;
case C_AND:
Sub = Cmd->Subcommands;
Ret = ExecuteCommand(Sub);
if (Ret == 0)
Ret = ExecuteCommand(Sub->Next);
break;
case C_PIPE:
Ret = ExecutePipeline(Cmd);
break;
case C_IF:
Ret = ExecuteIf(Cmd);
break;
case C_FOR:
Ret = ExecuteFor(Cmd);
break;
@ -878,48 +884,44 @@ GetEnvVarOrSpecial(LPCTSTR varName)
/* env var doesn't exist, look for a "special" one */
/* %CD% */
if (_tcsicmp(varName,_T("cd")) ==0)
if (_tcsicmp(varName, _T("CD")) == 0)
{
GetCurrentDirectory(MAX_PATH, ret);
GetCurrentDirectory(ARRAYSIZE(ret), ret);
return ret;
}
/* %TIME% */
else if (_tcsicmp(varName,_T("time")) ==0)
{
return GetTimeString();
}
/* %DATE% */
else if (_tcsicmp(varName,_T("date")) ==0)
else if (_tcsicmp(varName, _T("DATE")) == 0)
{
return GetDateString();
}
/* %TIME% */
else if (_tcsicmp(varName, _T("TIME")) == 0)
{
return GetTimeString();
}
/* %RANDOM% */
else if (_tcsicmp(varName,_T("random")) ==0)
else if (_tcsicmp(varName, _T("RANDOM")) == 0)
{
/* Get random number */
_itot(rand(),ret,10);
_itot(rand(), ret, 10);
return ret;
}
/* %CMDCMDLINE% */
else if (_tcsicmp(varName,_T("cmdcmdline")) ==0)
else if (_tcsicmp(varName, _T("CMDCMDLINE")) == 0)
{
return GetCommandLine();
}
/* %CMDEXTVERSION% */
else if (_tcsicmp(varName,_T("cmdextversion")) ==0)
else if (_tcsicmp(varName, _T("CMDEXTVERSION")) == 0)
{
/* Set version number to 2 */
_itot(2,ret,10);
_itot(2, ret, 10);
return ret;
}
/* %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;
}
@ -1018,7 +1020,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
PathVar = GetEnvVar(PathVarName);
FormatEnd[-1] = _T(':');
if (!PathVar ||
!SearchPath(PathVar, Result, NULL, MAX_PATH, FullPath, NULL))
!SearchPath(PathVar, Result, NULL, ARRAYSIZE(FullPath), FullPath, NULL))
{
return _T("");
}
@ -1038,7 +1040,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
else
{
/* 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("");
}
@ -1059,7 +1061,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
if (Next)
*Next++ = _T('\0');
/* 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("");
_tcscpy(Out, In);
hFind = FindFirstFile(FixedPath, &w32fd);
@ -1074,7 +1076,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
}
FindClose(hFind);
if (Out + _tcslen(FixedComponent) + 1 >= &FixedPath[MAX_PATH])
if (Out + _tcslen(FixedComponent) + 1 >= &FixedPath[ARRAYSIZE(FixedPath)])
return _T("");
_tcscpy(Out, FixedComponent);
}
@ -1107,7 +1109,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
{ _T('t'), FILE_ATTRIBUTE_TEMPORARY },
{ _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
? Attrib->Character
@ -1176,7 +1178,7 @@ GetEnhancedVar(TCHAR **pFormat, LPTSTR (*GetVar)(TCHAR, BOOL *))
return Result;
}
LPCTSTR
static LPCTSTR
GetBatchVar(TCHAR *varName, UINT *varNameLen)
{
LPCTSTR ret;
@ -1197,6 +1199,7 @@ GetBatchVar(TCHAR *varName, UINT *varNameLen)
}
*varNameLen = varNameEnd - varName;
return ret;
case _T('0'):
case _T('1'):
case _T('2'):
@ -1210,9 +1213,7 @@ GetBatchVar(TCHAR *varName, UINT *varNameLen)
return FindArg(*varName, &dummy);
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;
case _T('%'):
@ -1375,9 +1376,11 @@ too_long:
}
/* 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;
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
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;
INPUT_RECORD rec;
DWORD dwWritten;
INPUT_RECORD rec;
if ((dwCtrlType != CTRL_C_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
static VOID
@ -1799,18 +1801,21 @@ ExecuteAutoRunFile(HKEY hKeyRoot)
/* Get the command that comes after a /C or /K switch */
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))
ptr++;
++ptr;
/* Remove leading quote, find final quote */
if (*ptr == _T('"') &&
(LastQuote = _tcsrchr(++ptr, _T('"'))) != NULL)
{
TCHAR *Space;
const TCHAR* Space;
/* Under certain circumstances, all quotes are preserved.
* CMD /? documents these conditions as follows:
* 1. No /S switch
@ -1822,7 +1827,7 @@ GetCmdLineCommand(TCHAR *commandline, TCHAR *ptr, BOOL AlwaysStrip)
* 5. Enclosed string is an executable filename
*/
*LastQuote = _T('\0');
for (Space = ptr + 1; Space < LastQuote; Space++)
for (Space = ptr + 1; Space < LastQuote; ++Space)
{
if (_istspace(*Space)) /* Rule 4 */
{
@ -1858,15 +1863,14 @@ static VOID
Initialize(VOID)
{
HMODULE NtDllModule;
TCHAR commandline[CMDLINE_LENGTH];
TCHAR ModuleName[_MAX_PATH + 1];
// INT nExitCode;
HANDLE hIn, hOut;
TCHAR *ptr, *cmdLine, option = 0;
LPTSTR ptr, cmdLine;
TCHAR option = 0;
BOOL AlwaysStrip = FALSE;
BOOL AutoRun = TRUE;
TCHAR ModuleName[MAX_PATH + 1];
TCHAR commandline[CMDLINE_LENGTH];
/* Get version information */
InitOSVersion();
@ -1904,7 +1908,7 @@ Initialize(VOID)
/* Set COMSPEC environment variable */
if (GetModuleFileName(NULL, ModuleName, ARRAYSIZE(ModuleName)) != 0)
{
ModuleName[_MAX_PATH] = _T('\0');
ModuleName[MAX_PATH] = _T('\0');
SetEnvironmentVariable (_T("COMSPEC"), ModuleName);
}
@ -1921,32 +1925,32 @@ Initialize(VOID)
cmdLine = GetCommandLine();
TRACE ("[command args: %s]\n", debugstr_aw(cmdLine));
for (ptr = cmdLine; *ptr; ptr++)
for (ptr = cmdLine; *ptr; ++ptr)
{
if (*ptr == _T('/'))
{
option = _totupper(ptr[1]);
if (option == _T('?'))
{
ConOutResPaging(TRUE,STRING_CMD_HELP8);
ConOutResPaging(TRUE, STRING_CMD_HELP8);
nErrorLevel = 1;
bExit = TRUE;
return;
}
else if (option == _T('P'))
{
if (!IsExistingFile (_T("\\autoexec.bat")))
if (!IsExistingFile(_T("\\autoexec.bat")))
{
#ifdef INCLUDE_CMD_DATE
cmd_date (_T(""));
cmd_date(_T(""));
#endif
#ifdef INCLUDE_CMD_TIME
cmd_time (_T(""));
cmd_time(_T(""));
#endif
}
else
{
ParseCommandLine (_T("\\autoexec.bat"));
ParseCommandLine(_T("\\autoexec.bat"));
}
bCanExit = FALSE;
}
@ -2126,7 +2130,6 @@ int _tmain(int argc, const TCHAR *argv[])
/* Do the cleanup */
Cleanup();
cmd_free(lpOriginalEnvironment);
cmd_exit(nErrorLevel);

View file

@ -328,10 +328,11 @@ typedef struct _PARSED_COMMAND
TCHAR Variable;
LPTSTR Params;
LPTSTR List;
struct tagFORCONTEXT *Context;
struct _FOR_CONTEXT *Context;
} For;
};
} PARSED_COMMAND;
PARSED_COMMAND *ParseCommand(LPTSTR Line);
VOID EchoCommand(PARSED_COMMAND *Cmd);
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 */
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;
}
@ -50,7 +50,7 @@ INT cmd_for (LPTSTR param)
/* The stack of current FOR contexts.
* NULL when no FOR command is active */
LPFOR_CONTEXT fc = NULL;
PFOR_CONTEXT fc = NULL;
/* Get the next element of the FOR's list */
static BOOL GetNextElement(TCHAR **pStart, TCHAR **pEnd)
@ -116,6 +116,7 @@ static LPTSTR ReadFileContents(FILE *InputFile, TCHAR *Buffer)
return Contents;
}
/* FOR /F: Parse the contents of each file */
static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
{
LPTSTR Delims = _T(" \t");
@ -248,6 +249,7 @@ static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
goto single_element;
}
/* Loop over each file */
End = List;
while (GetNextElement(&Start, &End))
{
@ -325,7 +327,7 @@ static INT ForF(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
*CurVar++ = In;
/* Find end of token */
In += _tcscspn(In, Delims);
/* Nul-terminate it and advance to next token */
/* NULL-terminate it and advance to next token */
if (*In)
{
*In++ = _T('\0');
@ -352,9 +354,9 @@ static INT ForLoop(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
INT params[3] = { 0, 0, 0 };
INT i;
INT Ret = 0;
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);
i = params[START];
@ -365,6 +367,7 @@ static INT ForLoop(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer)
Ret = RunInstance(Cmd);
i += params[STEP];
}
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. */
static INT ForDir(PARSED_COMMAND *Cmd, LPTSTR List, TCHAR *Buffer, TCHAR *BufPos)
{
TCHAR *Start, *End = List;
INT Ret = 0;
TCHAR *Start, *End = List;
while (!Exiting(Cmd) && GetNextElement(&Start, &End))
{
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;
}
/* 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)
{
INT Ret = 0;
HANDLE hFind;
WIN32_FIND_DATA w32fd;
INT Ret = 0;
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));
} while (!Exiting(Cmd) && FindNextFile(hFind, &w32fd));
FindClose(hFind);
return Ret;
}
INT
ExecuteFor(PARSED_COMMAND *Cmd)
{
INT Ret;
LPTSTR List;
PFOR_CONTEXT lpNew;
TCHAR Buffer[CMDLINE_LENGTH]; /* Buffer to hold the variable value */
LPTSTR BufferPtr = Buffer;
LPFOR_CONTEXT lpNew;
INT Ret;
LPTSTR List = DoDelayedExpansion(Cmd->For.List);
List = DoDelayedExpansion(Cmd->For.List);
if (!List)
return 1;

View file

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

View file

@ -49,13 +49,13 @@ static INT GenericCmp(INT (*StringCmp)(LPCTSTR, LPCTSTR),
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))
{
ConOutResPaging(TRUE,STRING_IF_HELP1);
ConOutResPaging(TRUE, STRING_IF_HELP1);
return 0;
}
@ -181,12 +181,12 @@ INT ExecuteIf(PARSED_COMMAND *Cmd)
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);
}
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)
return ExecuteCommand(Cmd->Subcommands->Next);
return 0;