/** @file
Main file for If and else shell level 1 function.
Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "UefiShellLevel1CommandsLib.h"
#include <Library/PrintLib.h>
typedef enum {
} END_TAG_TYPE;
typedef enum {
/**
Extract the next fragment, if there is one.
@param[in, out] Statement The current remaining statement.
@param[in] Fragment The current fragment.
@retval FALSE There is not another fragment.
@retval TRUE There is another fragment.
**/
)
{
if (gUnicodeCollation->StriColl(
Tester) == 0) {
//
// increment the string pointer to the end of what we found and then chop off spaces...
//
while (*Statement[0] == L' ') {
(*Statement)++;
}
return (TRUE);
}
return (FALSE);
}
/**
Determine if String represents a valid profile.
@param[in] String The pointer to the string to test.
@retval TRUE String is a valid profile.
@retval FALSE String is not a valid profile.
**/
)
{
if ((TempLocation != NULL) && (*(TempLocation-1) == L';') && (*(TempLocation+StrLen(String)) == L';')) {
return (TRUE);
}
return (FALSE);
}
/**
Do a comparison between 2 things.
@param[in] Compare1 The first item to compare.
@param[in] Compare2 The second item to compare.
@param[in] BinOp The type of comparison to perform.
@param[in] CaseInsensitive TRUE to do non-case comparison, FALSE otherwise.
@param[in] ForceStringCompare TRUE to force string comparison, FALSE otherwise.
@return The result of the comparison.
**/
)
{
//
// "Compare1 BinOp Compare2"
//
switch (BinOp) {
case OperatorGreaterThan:
if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {
//
// string compare
//
if ((CaseInsensitive && StringNoCaseCompare(&Compare1, &Compare2) > 0) || (StringCompare(&Compare1, &Compare2) > 0)) {
return (TRUE);
}
} else {
//
// numeric compare
//
if (Compare1[0] == L'-') {
} else {
}
if (Compare2[0] == L'-') {
} else {
}
if (BinOp == OperatorGreaterThan) {
return (TRUE);
}
} else {
return (TRUE);
}
}
}
return (FALSE);
break;
case OperatorUnsignedLessThan:
case OperatorLessThan:
if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {
//
// string compare
//
if ((CaseInsensitive && StringNoCaseCompare(&Compare1, &Compare2) < 0) || (StringCompare(&Compare1, &Compare2) < 0)) {
return (TRUE);
}
} else {
//
// numeric compare
//
if (Compare1[0] == L'-') {
} else {
}
if (Compare2[0] == L'-') {
} else {
}
if (BinOp == OperatorLessThan) {
return (TRUE);
}
} else {
return (TRUE);
}
}
}
return (FALSE);
break;
case OperatorEqual:
if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {
//
// string compare
//
if ((CaseInsensitive && StringNoCaseCompare(&Compare1, &Compare2) == 0) || (StringCompare(&Compare1, &Compare2) == 0)) {
return (TRUE);
}
} else {
//
// numeric compare
//
if (Compare1[0] == L'-') {
} else {
}
if (Compare2[0] == L'-') {
} else {
}
return (TRUE);
}
}
return (FALSE);
break;
case OperatorNotEqual:
if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {
//
// string compare
//
if ((CaseInsensitive && StringNoCaseCompare(&Compare1, &Compare2) != 0) || (StringCompare(&Compare1, &Compare2) != 0)) {
return (TRUE);
}
} else {
//
// numeric compare
//
if (Compare1[0] == L'-') {
} else {
}
if (Compare2[0] == L'-') {
} else {
}
return (TRUE);
}
}
return (FALSE);
break;
case OperatorGreatorOrEqual:
if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {
//
// string compare
//
if ((CaseInsensitive && StringNoCaseCompare(&Compare1, &Compare2) >= 0) || (StringCompare(&Compare1, &Compare2) >= 0)) {
return (TRUE);
}
} else {
//
// numeric compare
//
if (Compare1[0] == L'-') {
} else {
}
if (Compare2[0] == L'-') {
} else {
}
if (BinOp == OperatorGreatorOrEqual) {
return (TRUE);
}
} else {
return (TRUE);
}
}
}
return (FALSE);
break;
case OperatorLessOrEqual:
if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {
//
// string compare
//
if ((CaseInsensitive && StringNoCaseCompare(&Compare1, &Compare2) <= 0) || (StringCompare(&Compare1, &Compare2) <= 0)) {
return (TRUE);
}
} else {
//
// numeric compare
//
if (Compare1[0] == L'-') {
} else {
}
if (Compare2[0] == L'-') {
} else {
}
if (BinOp == OperatorLessOrEqual) {
return (TRUE);
}
} else {
return (TRUE);
}
}
}
return (FALSE);
break;
default:
return (FALSE);
}
}
/**
Process an if statement and determine if its is valid or not.
@param[in, out] PassingState Opon entry, the current state. Upon exit,
the new state.
@param[in] StartParameterNumber The number of the first parameter of
this statement.
@param[in] EndParameterNumber The number of the final parameter of
this statement.
@param[in] OperatorToUse The type of termination operator.
@param[in] CaseInsensitive TRUE for case insensitive, FALSE otherwise.
@param[in] ForceStringCompare TRUE for all string based, FALSE otherwise.
@retval EFI_INVALID_PARAMETER A parameter was invalid.
@retval EFI_SUCCESS The operation was successful.
**/
)
{
BinOp = OperatorMax;
NotPresent = TRUE;
} else {
NotPresent = FALSE;
}
//
// now check for 'boolfunc' operators
//
if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {
} else {
}
} else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exists") || IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exist")) {
if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {
//
// is what remains a file in CWD???
//
OperationResult = (BOOLEAN)(ShellFileExists(gEfiShellParametersProtocol->Argv[++StartParameterNumber])==EFI_SUCCESS);
} else {
}
if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {
//
// is what remains a file in the CWD or path???
//
} else {
}
if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {
//
// Chop off that ')'
//
} else {
}
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[StartParameterNumber]);
} else {
//
// must be 'item binop item' style
//
BinOp = OperatorMax;
//
// get the first item
//
} else {
}
} else {
}
UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>2));
} else {
}
} else {
}
UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>1));
} else {
}
} else {
}
} else {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_STARTING), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[StartParameterNumber+2]);
} else {
//
// must be a raw string
//
}
}
//
// get the operator
//
} else {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_INVALID_BINOP), gShellLevel1HiiHandle, StatementWalker);
}
//
// get the second item
//
} else {
}
} else {
}
//
// can this be collapsed into the above?
//
UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>2));
} else {
}
} else {
}
UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>1));
} else {
}
} else {
}
} else {
//
// must be a raw string
//
}
}
}
//
// done processing do result...
//
if (NotPresent) {
}
switch(OperatorToUse) {
case EndTagOr:
break;
case EndTagAnd:
break;
case EndTagMax:
break;
default:
}
}
return (Status);
}
/**
Break up the next part of the if statement (until the next 'and', 'or', or 'then').
@param[in] ParameterNumber The current parameter number.
@param[out] EndParameter Upon successful return, will point to the
parameter to start the next iteration with.
@param[out] EndTag Upon successful return, will point to the
type that was found at the end of this statement.
@retval TRUE A valid statement was found.
@retval FALSE A valid statement was not found.
**/
)
{
; ParameterNumber++
) {
if (gUnicodeCollation->StriColl(
L"or") == 0) {
break;
} else if (gUnicodeCollation->StriColl(
L"and") == 0) {
break;
} else if (gUnicodeCollation->StriColl(
L"then") == 0) {
*EndTag = EndTagThen;
break;
}
}
return (FALSE);
}
return (TRUE);
}
/**
Move the script file pointer to a different place in the script file.
@param[in] ScriptFile The script file from GetCurrnetScriptFile().
@retval TRUE The move target was found and the move was successful.
@retval FALSE Something went wrong.
**/
)
{
TargetCount = 1;
if (ScriptFile == NULL) {
return FALSE;
}
for (CommandNode = (SCRIPT_COMMAND_LIST *)GetNextNode(&ScriptFile->CommandList, &ScriptFile->CurrentCommand->Link), Found = FALSE
){
//
// get just the first part of the command line...
//
CommandName = NULL;
if (CommandName == NULL) {
continue;
}
while (CommandWalker[0] == L' ') {
}
if (TempLocation != NULL) {
}
//
// did we find a nested item ?
//
if (gUnicodeCollation->StriColl(
L"If") == 0) {
TargetCount++;
(CHAR16*)L"else") == 0) {
//
// else can only decrement the last part... not an nested if
// hence the TargetCount compare added
//
TargetCount--;
} else if (gUnicodeCollation->StriColl(
(CHAR16*)L"endif") == 0) {
TargetCount--;
}
if (TargetCount == 0) {
ScriptFile->CurrentCommand = (SCRIPT_COMMAND_LIST *)GetNextNode(&ScriptFile->CommandList, &CommandNode->Link);
}
//
// Free the memory for this loop...
//
}
return (Found);
}
/**
Deal with the result of the if operation.
@param[in] Result The result of the if.
@retval EFI_SUCCESS The operation was successful.
@retval EFI_NOT_FOUND The ending tag could not be found.
**/
)
{
return (EFI_SUCCESS);
}
return (EFI_NOT_FOUND);
}
/**
Function for 'if' command.
@param[in] ImageHandle Handle to the Image (NULL if Internal).
@param[in] SystemTable Pointer to the System Table (NULL if Internal).
**/
)
{
Status = CommandInit();
if (!gEfiShellProtocol->BatchIsActive()) {
return (SHELL_UNSUPPORTED);
}
return (SHELL_INVALID_PARAMETER);
}
//
// Make sure that an End exists.
//
-1,
-1,
NULL,
L"EnfIf",
L"If",
return (SHELL_DEVICE_ERROR);
}
//
// initialize the shell lib (we must be in non-auto-init...)
//
Status = ShellInitialize();
CurrentParameter = 1;
EndParameter = 0;
if (gUnicodeCollation->StriColl(
L"/i") == 0 ||
L"/i") == 0 ||
L"/i") == 0)) {
} else {
}
if (gUnicodeCollation->StriColl(
L"/s") == 0 ||
L"/s") == 0 ||
L"/s") == 0)) {
ForceString = TRUE;
} else {
ForceString = FALSE;
}
; CurrentParameter++) {
if (gUnicodeCollation->StriColl(
L"then") == 0) {
//
// we are at the then
//
} else {
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
}
}
} else {
//
// build up the next statement for analysis
//
-1,
-1,
NULL,
L"Then",
L"If",
} else {
//
// Analyze the statement
//
Status = ProcessStatement(&CurrentValue, CurrentParameter, EndParameter, PreviousEnding, CaseInsensitive, ForceString);
// ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_STARTING), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
} else {
//
// Optomize to get out of the loop early...
//
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);
}
break;
}
}
}
if (ShellStatus == SHELL_SUCCESS){
//
// Skip over the or or and parameter.
//
}
}
}
}
return (ShellStatus);
}
/**
Function for 'else' command.
@param[in] ImageHandle Handle to the Image (NULL if Internal).
@param[in] SystemTable Pointer to the System Table (NULL if Internal).
**/
)
{
return (SHELL_INVALID_PARAMETER);
}
if (!gEfiShellProtocol->BatchIsActive()) {
return (SHELL_UNSUPPORTED);
}
-1,
-1,
NULL,
L"If",
L"Else",
return (SHELL_DEVICE_ERROR);
}
-1,
-1,
NULL,
L"If",
L"Else",
return (SHELL_DEVICE_ERROR);
}
-1,
-1,
NULL,
L"EndIf",
"Else",
return (SHELL_DEVICE_ERROR);
}
return (SHELL_SUCCESS);
}
/**
Function for 'endif' command.
@param[in] ImageHandle Handle to the Image (NULL if Internal).
@param[in] SystemTable Pointer to the System Table (NULL if Internal).
**/
)
{
return (SHELL_INVALID_PARAMETER);
}
if (!gEfiShellProtocol->BatchIsActive()) {
return (SHELL_UNSUPPORTED);
}
-1,
-1,
NULL,
L"If",
L"EndIf",
return (SHELL_DEVICE_ERROR);
}
return (SHELL_SUCCESS);
}