2006-02-16 23:23:37 +00:00
|
|
|
/*
|
|
|
|
* IF.C - if internal batch command.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* History:
|
|
|
|
*
|
|
|
|
* 16 Jul 1998 (Hans B Pufal)
|
|
|
|
* started.
|
|
|
|
*
|
|
|
|
* 16 Jul 1998 (John P Price)
|
|
|
|
* Seperated commands into individual files.
|
|
|
|
*
|
|
|
|
* 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
|
|
|
|
* added config.h include
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 07-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Added help text ("if /?") and cleaned up.
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 21-Jan-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Unicode and redirection ready!
|
|
|
|
*
|
2007-05-05 11:32:25 +00:00
|
|
|
* 01-Sep-1999 (Eric Kohl)
|
2006-02-16 23:23:37 +00:00
|
|
|
* Fixed help text.
|
|
|
|
*
|
|
|
|
* 17-Feb-2001 (ea)
|
|
|
|
* IF DEFINED variable command
|
|
|
|
*
|
|
|
|
* 28-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
|
|
|
|
* Remove all hardcode string to En.rc
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <precomp.h>
|
|
|
|
|
2008-12-22 22:34:51 +00:00
|
|
|
static INT GenericCmp(INT (*StringCmp)(LPCTSTR, LPCTSTR),
|
|
|
|
LPCTSTR Left, LPCTSTR Right)
|
|
|
|
{
|
|
|
|
TCHAR *end;
|
|
|
|
INT nLeft = _tcstol(Left, &end, 0);
|
|
|
|
if (*end == _T('\0'))
|
|
|
|
{
|
|
|
|
INT nRight = _tcstol(Right, &end, 0);
|
|
|
|
if (*end == _T('\0'))
|
|
|
|
{
|
|
|
|
/* both arguments are numeric */
|
|
|
|
return (nLeft < nRight) ? -1 : (nLeft > nRight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return StringCmp(Left, Right);
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-12-30 23:55:50 +00:00
|
|
|
INT cmd_if (LPTSTR param)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-08-25 23:22:03 +00:00
|
|
|
TRACE ("cmd_if: (\'%s\')\n", debugstr_aw(param));
|
2006-02-16 23:23:37 +00:00
|
|
|
|
|
|
|
if (!_tcsncmp (param, _T("/?"), 2))
|
|
|
|
{
|
|
|
|
ConOutResPaging(TRUE,STRING_IF_HELP1);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-12-30 23:55:50 +00:00
|
|
|
|
|
|
|
error_syntax(param);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-04-12 23:51:15 +00:00
|
|
|
INT ExecuteIf(PARSED_COMMAND *Cmd)
|
2008-12-30 23:55:50 +00:00
|
|
|
{
|
|
|
|
INT result = FALSE; /* when set cause 'then' clause to be executed */
|
|
|
|
LPTSTR param;
|
2009-02-14 01:13:17 +00:00
|
|
|
LPTSTR Left = NULL, Right;
|
|
|
|
|
|
|
|
if (Cmd->If.LeftArg)
|
|
|
|
{
|
|
|
|
Left = DoDelayedExpansion(Cmd->If.LeftArg);
|
|
|
|
if (!Left)
|
2009-04-12 23:51:15 +00:00
|
|
|
return 1;
|
2009-02-14 01:13:17 +00:00
|
|
|
}
|
|
|
|
Right = DoDelayedExpansion(Cmd->If.RightArg);
|
|
|
|
if (!Right)
|
|
|
|
{
|
|
|
|
cmd_free(Left);
|
2009-04-12 23:51:15 +00:00
|
|
|
return 1;
|
2009-02-14 01:13:17 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-12-22 22:34:51 +00:00
|
|
|
if (Cmd->If.Operator == IF_CMDEXTVERSION)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* IF CMDEXTVERSION n: check if Command Extensions version
|
|
|
|
* is greater or equal to n */
|
2009-02-14 01:13:17 +00:00
|
|
|
DWORD n = _tcstoul(Right, ¶m, 10);
|
2008-12-22 22:34:51 +00:00
|
|
|
if (*param != _T('\0'))
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2009-02-14 01:13:17 +00:00
|
|
|
error_syntax(Right);
|
|
|
|
cmd_free(Right);
|
2009-04-12 23:51:15 +00:00
|
|
|
return 1;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-12-22 22:34:51 +00:00
|
|
|
result = (2 >= n);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-12-22 22:34:51 +00:00
|
|
|
else if (Cmd->If.Operator == IF_DEFINED)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* IF DEFINED var: check if environment variable exists */
|
2009-02-14 01:13:17 +00:00
|
|
|
result = (GetEnvVarOrSpecial(Right) != NULL);
|
2008-12-22 22:34:51 +00:00
|
|
|
}
|
|
|
|
else if (Cmd->If.Operator == IF_ERRORLEVEL)
|
|
|
|
{
|
|
|
|
/* IF ERRORLEVEL n: check if last exit code is greater or equal to n */
|
2009-03-18 03:52:58 +00:00
|
|
|
INT n = _tcstol(Right, ¶m, 10);
|
2008-12-22 22:34:51 +00:00
|
|
|
if (*param != _T('\0'))
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2009-02-14 01:13:17 +00:00
|
|
|
error_syntax(Right);
|
|
|
|
cmd_free(Right);
|
2009-04-12 23:51:15 +00:00
|
|
|
return 1;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-12-22 22:34:51 +00:00
|
|
|
result = (nErrorLevel >= n);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-12-22 22:34:51 +00:00
|
|
|
else if (Cmd->If.Operator == IF_EXIST)
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* IF EXIST filename: check if file exists (wildcards allowed) */
|
2009-02-14 01:13:17 +00:00
|
|
|
StripQuotes(Right);
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2009-07-13 18:55:33 +00:00
|
|
|
if (_tcschr(Right, _T('*')) || _tcschr(Right, _T('?')))
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATA f;
|
|
|
|
HANDLE hFind = FindFirstFile(Right, &f);
|
|
|
|
if (hFind != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
result = TRUE;
|
|
|
|
FindClose(hFind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2008-12-22 22:34:51 +00:00
|
|
|
{
|
2009-07-13 18:55:33 +00:00
|
|
|
result = (GetFileAttributes(Right) != INVALID_FILE_ATTRIBUTES);
|
2008-12-22 22:34:51 +00:00
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* Do case-insensitive string comparisons if /I specified */
|
|
|
|
INT (*StringCmp)(LPCTSTR, LPCTSTR) =
|
|
|
|
(Cmd->If.Flags & IFFLAG_IGNORECASE) ? _tcsicmp : _tcscmp;
|
2006-02-16 23:23:37 +00:00
|
|
|
|
2008-12-22 22:34:51 +00:00
|
|
|
if (Cmd->If.Operator == IF_STRINGEQ)
|
2008-01-20 14:29:18 +00:00
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* IF str1 == str2 */
|
2009-02-14 01:13:17 +00:00
|
|
|
result = StringCmp(Left, Right) == 0;
|
2008-01-20 14:29:18 +00:00
|
|
|
}
|
2008-12-22 22:34:51 +00:00
|
|
|
else
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2009-02-14 01:13:17 +00:00
|
|
|
result = GenericCmp(StringCmp, Left, Right);
|
2008-12-22 22:34:51 +00:00
|
|
|
switch (Cmd->If.Operator)
|
|
|
|
{
|
|
|
|
case IF_EQU: result = (result == 0); break;
|
|
|
|
case IF_NEQ: result = (result != 0); break;
|
|
|
|
case IF_LSS: result = (result < 0); break;
|
|
|
|
case IF_LEQ: result = (result <= 0); break;
|
|
|
|
case IF_GTR: result = (result > 0); break;
|
|
|
|
case IF_GEQ: result = (result >= 0); break;
|
|
|
|
}
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-14 01:13:17 +00:00
|
|
|
cmd_free(Left);
|
|
|
|
cmd_free(Right);
|
|
|
|
|
2008-12-22 22:34:51 +00:00
|
|
|
if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0))
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* full condition was true, do the command */
|
|
|
|
return ExecuteCommand(Cmd->Subcommands);
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
2008-12-22 22:34:51 +00:00
|
|
|
else
|
2006-02-16 23:23:37 +00:00
|
|
|
{
|
2008-12-22 22:34:51 +00:00
|
|
|
/* full condition was false, do the "else" command if there is one */
|
|
|
|
if (Cmd->Subcommands->Next)
|
|
|
|
return ExecuteCommand(Cmd->Subcommands->Next);
|
2009-04-12 23:51:15 +00:00
|
|
|
return 0;
|
2006-02-16 23:23:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|