Ejemplo n.º 1
0
static BOOL
seta_identval ( LPCTSTR ident, INT* result )
{
	LPCTSTR identVal = GetEnvVarOrSpecial ( ident );
	if ( !identVal )
	{
		/* TODO FIXME - what to do upon failure? */
		*result = 0;
		return FALSE;
	}
	*result = _tcstol ( identVal, NULL, 0 );
	return TRUE;
}
Ejemplo n.º 2
0
INT ExecuteIf(PARSED_COMMAND *Cmd)
{
    INT result = FALSE; /* when set cause 'then' clause to be executed */
    LPTSTR param;
    LPTSTR Left = NULL, Right;

    if (Cmd->If.LeftArg)
    {
        Left = DoDelayedExpansion(Cmd->If.LeftArg);
        if (!Left)
            return 1;
    }
    Right = DoDelayedExpansion(Cmd->If.RightArg);
    if (!Right)
    {
        cmd_free(Left);
        return 1;
    }

    if (Cmd->If.Operator == IF_CMDEXTVERSION)
    {
        /* IF CMDEXTVERSION n: check if Command Extensions version
         * is greater or equal to n */
        DWORD n = _tcstoul(Right, &param, 10);
        if (*param != _T('\0'))
        {
            error_syntax(Right);
            cmd_free(Right);
            return 1;
        }
        result = (2 >= n);
    }
    else if (Cmd->If.Operator == IF_DEFINED)
    {
        /* IF DEFINED var: check if environment variable exists */
        result = (GetEnvVarOrSpecial(Right) != NULL);
    }
    else if (Cmd->If.Operator == IF_ERRORLEVEL)
    {
        /* IF ERRORLEVEL n: check if last exit code is greater or equal to n */
        INT n = _tcstol(Right, &param, 10);
        if (*param != _T('\0'))
        {
            error_syntax(Right);
            cmd_free(Right);
            return 1;
        }
        result = (nErrorLevel >= n);
    }
    else if (Cmd->If.Operator == IF_EXIST)
    {
        BOOL IsDir;
        INT Size;
        WIN32_FIND_DATA f;
        HANDLE hFind;

        /* IF EXIST filename: check if file exists (wildcards allowed) */
        StripQuotes(Right);

        Size = _tcslen(Right);
        IsDir = (Right[Size - 1] == '\\');
        if (IsDir)
            Right[Size - 1] = 0;


        hFind = FindFirstFile(Right, &f);
        if (hFind != INVALID_HANDLE_VALUE)
        {
            if (IsDir)
            {
                result = ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
            }
            else
            {
                result = TRUE;
            }
            FindClose(hFind);
        }

        if (IsDir)
            Right[Size - 1] = '\\';
    }
    else
    {
        /* Do case-insensitive string comparisons if /I specified */
        INT (*StringCmp)(LPCTSTR, LPCTSTR) =
            (Cmd->If.Flags & IFFLAG_IGNORECASE) ? _tcsicmp : _tcscmp;

        if (Cmd->If.Operator == IF_STRINGEQ)
        {
            /* IF str1 == str2 */
            result = StringCmp(Left, Right) == 0;
        }
        else
        {
            result = GenericCmp(StringCmp, Left, Right);
            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;
            }
        }
    }

    cmd_free(Left);
    cmd_free(Right);

    if (result ^ ((Cmd->If.Flags & IFFLAG_NEGATE) != 0))
    {
        /* full condition was true, do the command */
        return ExecuteCommand(Cmd->Subcommands);
    }
    else
    {
        /* full condition was false, do the "else" command if there is one */
        if (Cmd->Subcommands->Next)
            return ExecuteCommand(Cmd->Subcommands->Next);
        return 0;
    }
}