X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=ShellPkg%2FLibrary%2FUefiShellLevel1CommandsLib%2FIf.c;h=35c5ca6835817f942e6044d72c7641c0eb278558;hp=f3fc6b06a83d7d300aab8d31b4de31252fe6f82a;hb=2d70c90d2e8bf4405c5c16c0c7ea5b2446d1517a;hpb=7effcbff2953172b63d9416f8785be7a5bc5c015 diff --git a/ShellPkg/Library/UefiShellLevel1CommandsLib/If.c b/ShellPkg/Library/UefiShellLevel1CommandsLib/If.c index f3fc6b06a8..35c5ca6835 100644 --- a/ShellPkg/Library/UefiShellLevel1CommandsLib/If.c +++ b/ShellPkg/Library/UefiShellLevel1CommandsLib/If.c @@ -1,7 +1,8 @@ /** @file Main file for If and else shell level 1 function. - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.
+ Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
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 @@ -15,35 +16,43 @@ #include "UefiShellLevel1CommandsLib.h" #include - - - typedef enum { - END_TAG_OR, - END_TAG_AND, - END_TAG_THEN, - END_TAG_MAX + EndTagOr, + EndTagAnd, + EndTagThen, + EndTagMax } END_TAG_TYPE; typedef enum { - OPERATOR_GT, - OPERATOR_LT, - OPERATOR_EQ, - OPERATOR_NE, - OPERATOR_GE, - OPERATOR_LE, - OPERATOR_UGT, - OPERATOR_ULT, - OPERATOR_UGE, - OPERATOR_ULE, - OPERATOR_MAX + OperatorGreaterThan, + OperatorLessThan, + OperatorEqual, + OperatorNotEqual, + OperatorGreatorOrEqual, + OperatorLessOrEqual, + OperatorUnisgnedGreaterThan, + OperatorUnsignedLessThan, + OperatorUnsignedGreaterOrEqual, + OperatorUnsignedLessOrEqual, + OperatorMax } BIN_OPERATOR_TYPE; -BOOLEAN -EFIAPI +/** + Extract the next fragment, if there is one. + + @param[in, out] Statement The current remaining statement. + @param[in] Fragment The current fragment. + @param[out] Match TRUE when there is another Fragment in Statement, + FALSE otherwise. + + @retval EFI_SUCCESS The match operation is performed successfully. + @retval EFI_OUT_OF_RESOURCES Out of resources. +**/ +EFI_STATUS IsNextFragment ( - IN CONST CHAR16 **Statement, - IN CONST CHAR16 *Fragment + IN OUT CONST CHAR16 **Statement, + IN CONST CHAR16 *Fragment, + OUT BOOLEAN *Match ) { CHAR16 *Tester; @@ -51,7 +60,9 @@ IsNextFragment ( Tester = NULL; Tester = StrnCatGrow(&Tester, NULL, *Statement, StrLen(Fragment)); - ASSERT(Tester != NULL); + if (Tester == NULL) { + return EFI_OUT_OF_RESOURCES; + } Tester[StrLen(Fragment)] = CHAR_NULL; if (gUnicodeCollation->StriColl( gUnicodeCollation, @@ -64,15 +75,23 @@ IsNextFragment ( while (*Statement[0] == L' ') { (*Statement)++; } - FreePool(Tester); - return (TRUE); + *Match = TRUE; + } else { + *Match = FALSE; } FreePool(Tester); - return (FALSE); + return EFI_SUCCESS; } +/** + 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. +**/ BOOLEAN -EFIAPI IsValidProfile ( IN CONST CHAR16 *String ) @@ -81,6 +100,7 @@ IsValidProfile ( CONST CHAR16 *TempLocation; ProfilesString = ShellGetEnvironmentVariable(L"profiles"); + ASSERT(ProfilesString != NULL); TempLocation = StrStr(ProfilesString, String); if ((TempLocation != NULL) && (*(TempLocation-1) == L';') && (*(TempLocation+StrLen(String)) == L';')) { return (TRUE); @@ -88,8 +108,18 @@ IsValidProfile ( 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. +**/ BOOLEAN -EFIAPI TestOperation ( IN CONST CHAR16 *Compare1, IN CONST CHAR16 *Compare2, @@ -105,8 +135,8 @@ TestOperation ( // "Compare1 BinOp Compare2" // switch (BinOp) { - case OPERATOR_UGT: - case OPERATOR_GT: + case OperatorUnisgnedGreaterThan: + case OperatorGreaterThan: if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) { // // string compare @@ -119,16 +149,16 @@ TestOperation ( // numeric compare // if (Compare1[0] == L'-') { - Cmp1 = 0 - (INTN)StrDecimalToUintn(Compare1+1); + Cmp1 = 0 - (INTN)ShellStrToUintn(Compare1+1); } else { - Cmp1 = (INTN)StrDecimalToUintn(Compare1); + Cmp1 = (INTN)ShellStrToUintn(Compare1); } if (Compare2[0] == L'-') { - Cmp2 = 0 - (INTN)StrDecimalToUintn(Compare2+1); + Cmp2 = 0 - (INTN)ShellStrToUintn(Compare2+1); } else { - Cmp2 = (INTN)StrDecimalToUintn(Compare2); + Cmp2 = (INTN)ShellStrToUintn(Compare2); } - if (BinOp == OPERATOR_GT) { + if (BinOp == OperatorGreaterThan) { if (Cmp1 > Cmp2) { return (TRUE); } @@ -139,9 +169,8 @@ TestOperation ( } } return (FALSE); - break; - case OPERATOR_ULT: - case OPERATOR_LT: + case OperatorUnsignedLessThan: + case OperatorLessThan: if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) { // // string compare @@ -154,16 +183,16 @@ TestOperation ( // numeric compare // if (Compare1[0] == L'-') { - Cmp1 = 0 - (INTN)StrDecimalToUintn(Compare1+1); + Cmp1 = 0 - (INTN)ShellStrToUintn(Compare1+1); } else { - Cmp1 = (INTN)StrDecimalToUintn(Compare1); + Cmp1 = (INTN)ShellStrToUintn(Compare1); } if (Compare2[0] == L'-') { - Cmp2 = 0 - (INTN)StrDecimalToUintn(Compare2+1); + Cmp2 = 0 - (INTN)ShellStrToUintn(Compare2+1); } else { - Cmp2 = (INTN)StrDecimalToUintn(Compare2); + Cmp2 = (INTN)ShellStrToUintn(Compare2); } - if (BinOp == OPERATOR_LT) { + if (BinOp == OperatorLessThan) { if (Cmp1 < Cmp2) { return (TRUE); } @@ -175,8 +204,7 @@ TestOperation ( } return (FALSE); - break; - case OPERATOR_EQ: + case OperatorEqual: if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) { // // string compare @@ -203,8 +231,7 @@ TestOperation ( } } return (FALSE); - break; - case OPERATOR_NE: + case OperatorNotEqual: if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) { // // string compare @@ -217,23 +244,22 @@ TestOperation ( // numeric compare // if (Compare1[0] == L'-') { - Cmp1 = 0 - (INTN)StrDecimalToUintn(Compare1+1); + Cmp1 = 0 - (INTN)ShellStrToUintn(Compare1+1); } else { - Cmp1 = (INTN)StrDecimalToUintn(Compare1); + Cmp1 = (INTN)ShellStrToUintn(Compare1); } if (Compare2[0] == L'-') { - Cmp2 = 0 - (INTN)StrDecimalToUintn(Compare2+1); + Cmp2 = 0 - (INTN)ShellStrToUintn(Compare2+1); } else { - Cmp2 = (INTN)StrDecimalToUintn(Compare2); + Cmp2 = (INTN)ShellStrToUintn(Compare2); } if (Cmp1 != Cmp2) { return (TRUE); } } return (FALSE); - break; - case OPERATOR_UGE: - case OPERATOR_GE: + case OperatorUnsignedGreaterOrEqual: + case OperatorGreatorOrEqual: if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) { // // string compare @@ -246,16 +272,16 @@ TestOperation ( // numeric compare // if (Compare1[0] == L'-') { - Cmp1 = 0 - (INTN)StrDecimalToUintn(Compare1+1); + Cmp1 = 0 - (INTN)ShellStrToUintn(Compare1+1); } else { - Cmp1 = (INTN)StrDecimalToUintn(Compare1); + Cmp1 = (INTN)ShellStrToUintn(Compare1); } if (Compare2[0] == L'-') { - Cmp2 = 0 - (INTN)StrDecimalToUintn(Compare2+1); + Cmp2 = 0 - (INTN)ShellStrToUintn(Compare2+1); } else { - Cmp2 = (INTN)StrDecimalToUintn(Compare2); + Cmp2 = (INTN)ShellStrToUintn(Compare2); } - if (BinOp == OPERATOR_GE) { + if (BinOp == OperatorGreatorOrEqual) { if (Cmp1 >= Cmp2) { return (TRUE); } @@ -266,9 +292,8 @@ TestOperation ( } } return (FALSE); - break; - case OPERATOR_LE: - case OPERATOR_ULE: + case OperatorLessOrEqual: + case OperatorUnsignedLessOrEqual: if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) { // // string compare @@ -281,16 +306,16 @@ TestOperation ( // numeric compare // if (Compare1[0] == L'-') { - Cmp1 = 0 - (INTN)StrDecimalToUintn(Compare1+1); + Cmp1 = 0 - (INTN)ShellStrToUintn(Compare1+1); } else { - Cmp1 = (INTN)StrDecimalToUintn(Compare1); + Cmp1 = (INTN)ShellStrToUintn(Compare1); } if (Compare2[0] == L'-') { - Cmp2 = 0 - (INTN)StrDecimalToUintn(Compare2+1); + Cmp2 = 0 - (INTN)ShellStrToUintn(Compare2+1); } else { - Cmp2 = (INTN)StrDecimalToUintn(Compare2); + Cmp2 = (INTN)ShellStrToUintn(Compare2); } - if (BinOp == OPERATOR_LE) { + if (BinOp == OperatorLessOrEqual) { if (Cmp1 <= Cmp2) { return (TRUE); } @@ -301,15 +326,29 @@ TestOperation ( } } return (FALSE); - break; default: ASSERT(FALSE); 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. +**/ EFI_STATUS -EFIAPI ProcessStatement ( IN OUT BOOLEAN *PassingState, IN UINTN StartParameterNumber, @@ -328,14 +367,16 @@ ProcessStatement ( CHAR16 *Compare2; CHAR16 HexString[20]; CHAR16 *TempSpot; + BOOLEAN Match; - ASSERT((END_TAG_TYPE)OperatorToUse != END_TAG_THEN); + ASSERT((END_TAG_TYPE)OperatorToUse != EndTagThen); Status = EFI_SUCCESS; - BinOp = OPERATOR_MAX; + BinOp = OperatorMax; OperationResult = FALSE; + Match = FALSE; StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber]; - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"not")) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"not", &Match)) && Match) { NotPresent = TRUE; StatementWalker = gEfiShellParametersProtocol->Argv[++StartParameterNumber]; } else { @@ -345,16 +386,19 @@ ProcessStatement ( // // now check for 'boolfunc' operators // - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"isint")) { - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"isint", &Match)) && Match) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match + && StatementWalker[StrLen(StatementWalker)-1] == L')') { StatementWalker[StrLen(StatementWalker)-1] = CHAR_NULL; OperationResult = ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE); } else { Status = EFI_INVALID_PARAMETER; ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"isint"); } - } 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')') { + } else if ((!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exists", &Match)) && Match) || + (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exist", &Match)) && Match)) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && + StatementWalker[StrLen(StatementWalker)-1] == L')') { StatementWalker[StrLen(StatementWalker)-1] = CHAR_NULL; // // is what remains a file in CWD??? @@ -366,8 +410,9 @@ ProcessStatement ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"exist(s)"); Status = EFI_INVALID_PARAMETER; } - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"available")) { - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') { + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"available", &Match)) && Match) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && + StatementWalker[StrLen(StatementWalker)-1] == L')') { StatementWalker[StrLen(StatementWalker)-1] = CHAR_NULL; // // is what remains a file in the CWD or path??? @@ -377,8 +422,9 @@ ProcessStatement ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"available"); Status = EFI_INVALID_PARAMETER; } - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"profile")) { - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') { + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"profile", &Match)) && Match) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && + StatementWalker[StrLen(StatementWalker)-1] == L')') { // // Chop off that ')' // @@ -397,15 +443,15 @@ ProcessStatement ( // Compare1 = NULL; Compare2 = NULL; - BinOp = OPERATOR_MAX; + BinOp = OperatorMax; // // get the first item // StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber]; - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror")) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror", &Match)) && Match) { TempSpot = StrStr(StatementWalker, L")"); - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) { *TempSpot = CHAR_NULL; if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) { UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT); @@ -420,9 +466,9 @@ ProcessStatement ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"efierror"); Status = EFI_INVALID_PARAMETER; } - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror")) { + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror", &Match)) && Match) { TempSpot = StrStr(StatementWalker, L")"); - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) { *TempSpot = CHAR_NULL; if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) { UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>2)); @@ -437,9 +483,9 @@ ProcessStatement ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"pierror"); Status = EFI_INVALID_PARAMETER; } - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"oemerror")) { + } else if (!EFI_ERROR (IsNextFragment ((CONST CHAR16**)(&StatementWalker), L"oemerror", &Match)) && Match) { TempSpot = StrStr(StatementWalker, L")"); - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) { TempSpot = CHAR_NULL; if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) { UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>1)); @@ -472,28 +518,28 @@ ProcessStatement ( // ASSERT(StartParameterNumber+1Argv[StartParameterNumber+1]; - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"gt")) { - BinOp = OPERATOR_GT; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"lt")) { - BinOp = OPERATOR_LT; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"eq")) { - BinOp = OPERATOR_EQ; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ne")) { - BinOp = OPERATOR_NE; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ge")) { - BinOp = OPERATOR_GE; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"le")) { - BinOp = OPERATOR_LE; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"==")) { - BinOp = OPERATOR_EQ; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ugt")) { - BinOp = OPERATOR_UGT; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ult")) { - BinOp = OPERATOR_ULT; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"uge")) { - BinOp = OPERATOR_UGE; - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ule")) { - BinOp = OPERATOR_ULE; + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"gt", &Match)) && Match) { + BinOp = OperatorGreaterThan; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"lt", &Match)) && Match) { + BinOp = OperatorLessThan; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"eq", &Match)) && Match) { + BinOp = OperatorEqual; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ne", &Match)) && Match) { + BinOp = OperatorNotEqual; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ge", &Match)) && Match) { + BinOp = OperatorGreatorOrEqual; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"le", &Match)) && Match) { + BinOp = OperatorLessOrEqual; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"==", &Match)) && Match) { + BinOp = OperatorEqual; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ugt", &Match)) && Match) { + BinOp = OperatorUnisgnedGreaterThan; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ult", &Match)) && Match) { + BinOp = OperatorUnsignedLessThan; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"uge", &Match)) && Match) { + BinOp = OperatorUnsignedGreaterOrEqual; + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ule", &Match)) && Match) { + BinOp = OperatorUnsignedLessOrEqual; } else { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_INVALID_BINOP), gShellLevel1HiiHandle, StatementWalker); Status = EFI_INVALID_PARAMETER; @@ -504,9 +550,9 @@ ProcessStatement ( // ASSERT(StartParameterNumber+2<=EndParameterNumber); StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber+2]; - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror")) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror", &Match)) && Match) { TempSpot = StrStr(StatementWalker, L")"); - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) { TempSpot = CHAR_NULL; if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) { UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT); @@ -524,9 +570,9 @@ ProcessStatement ( // // can this be collapsed into the above? // - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror")) { + } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror", &Match)) && Match) { TempSpot = StrStr(StatementWalker, L")"); - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) { TempSpot = CHAR_NULL; if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) { UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>2)); @@ -541,9 +587,9 @@ ProcessStatement ( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"pierror"); Status = EFI_INVALID_PARAMETER; } - } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"oemerror")) { + } else if (!EFI_ERROR (IsNextFragment ((CONST CHAR16**)(&StatementWalker), L"oemerror", &Match)) && Match) { TempSpot = StrStr(StatementWalker, L")"); - if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) { + if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) { TempSpot = CHAR_NULL; if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) { UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>1)); @@ -566,7 +612,7 @@ ProcessStatement ( Compare2 = StrnCatGrow(&Compare2, NULL, StatementWalker, 0); } - if (Compare1 != NULL && Compare2 != NULL && BinOp != OPERATOR_MAX) { + if (Compare1 != NULL && Compare2 != NULL && BinOp != OperatorMax) { OperationResult = TestOperation(Compare1, Compare2, BinOp, CaseInsensitive, ForceStringCompare); } @@ -583,13 +629,13 @@ ProcessStatement ( OperationResult = (BOOLEAN)(!OperationResult); } switch(OperatorToUse) { - case END_TAG_OR: + case EndTagOr: *PassingState = (BOOLEAN)(*PassingState || OperationResult); break; - case END_TAG_AND: + case EndTagAnd: *PassingState = (BOOLEAN)(*PassingState && OperationResult); break; - case END_TAG_MAX: + case EndTagMax: *PassingState = (BOOLEAN)(OperationResult); break; default: @@ -599,20 +645,28 @@ ProcessStatement ( 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. +**/ BOOLEAN -EFIAPI BuildNextStatement ( IN UINTN ParameterNumber, OUT UINTN *EndParameter, OUT END_TAG_TYPE *EndTag ) { - CHAR16 *Buffer; - UINTN BufferSize; + *EndTag = EndTagMax; - *EndTag = END_TAG_MAX; - - for(Buffer = NULL, BufferSize = 0 + for( ; ParameterNumber < gEfiShellParametersProtocol->Argc ; ParameterNumber++ ) { @@ -621,32 +675,40 @@ BuildNextStatement ( gEfiShellParametersProtocol->Argv[ParameterNumber], L"or") == 0) { *EndParameter = ParameterNumber - 1; - *EndTag = END_TAG_OR; + *EndTag = EndTagOr; break; } else if (gUnicodeCollation->StriColl( gUnicodeCollation, gEfiShellParametersProtocol->Argv[ParameterNumber], L"and") == 0) { *EndParameter = ParameterNumber - 1; - *EndTag = END_TAG_AND; + *EndTag = EndTagAnd; break; } else if (gUnicodeCollation->StriColl( gUnicodeCollation, gEfiShellParametersProtocol->Argv[ParameterNumber], L"then") == 0) { *EndParameter = ParameterNumber - 1; - *EndTag = END_TAG_THEN; + *EndTag = EndTagThen; break; } } - if (*EndTag == END_TAG_MAX) { + if (*EndTag == EndTagMax) { return (FALSE); } return (TRUE); } +/** + Move the script file pointer to a different place in the script file. + This one is special since it handles the if/else/endif syntax. + + @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. +**/ BOOLEAN -EFIAPI MoveToTagSpecial ( IN SCRIPT_FILE *ScriptFile ) @@ -675,8 +737,15 @@ MoveToTagSpecial ( // CommandName = NULL; CommandName = StrnCatGrow(&CommandName, NULL, CommandNode->Cl, 0); + if (CommandName == NULL) { + continue; + } CommandWalker = CommandName; - while (CommandWalker[0] == L' ') { + + // + // Skip leading spaces and tabs. + // + while ((CommandWalker[0] == L' ') || (CommandWalker[0] == L'\t')) { CommandWalker++; } TempLocation = StrStr(CommandWalker, L" "); @@ -721,8 +790,15 @@ MoveToTagSpecial ( 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. +**/ EFI_STATUS -EFIAPI PerformResultOperation ( IN CONST BOOLEAN Result ) @@ -755,26 +831,37 @@ ShellCommandRunIf ( BOOLEAN CurrentValue; END_TAG_TYPE Ending; END_TAG_TYPE PreviousEnding; - + SCRIPT_FILE *CurrentScriptFile; Status = CommandInit(); ASSERT_EFI_ERROR(Status); if (!gEfiShellProtocol->BatchIsActive()) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"If"); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"if"); return (SHELL_UNSUPPORTED); } if (gEfiShellParametersProtocol->Argc < 3) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle, L"if"); return (SHELL_INVALID_PARAMETER); } // // Make sure that an End exists. // - if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, ShellCommandGetCurrentScriptFile(), TRUE, TRUE, FALSE)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"EnfIf", L"If", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line); + CurrentScriptFile = ShellCommandGetCurrentScriptFile(); + if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, CurrentScriptFile, TRUE, TRUE, FALSE)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SYNTAX_NO_MATCHING), + gShellLevel1HiiHandle, + L"EndIf", + L"If", + CurrentScriptFile!=NULL + && CurrentScriptFile->CurrentCommand!=NULL + ? CurrentScriptFile->CurrentCommand->Line:0); return (SHELL_DEVICE_ERROR); } @@ -822,7 +909,7 @@ ShellCommandRunIf ( ForceString = FALSE; } - for ( ShellStatus = SHELL_SUCCESS, CurrentValue = FALSE, Ending = END_TAG_MAX + for ( ShellStatus = SHELL_SUCCESS, CurrentValue = FALSE, Ending = EndTagMax ; CurrentParameter < gEfiShellParametersProtocol->Argc && ShellStatus == SHELL_SUCCESS ; CurrentParameter++) { if (gUnicodeCollation->StriColl( @@ -833,12 +920,12 @@ ShellCommandRunIf ( // we are at the then // if (CurrentParameter+1 != gEfiShellParametersProtocol->Argc) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TEXT_AFTER_THEN), gShellLevel1HiiHandle); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TEXT_AFTER_THEN), gShellLevel1HiiHandle, L"if"); ShellStatus = SHELL_INVALID_PARAMETER; } else { Status = PerformResultOperation(CurrentValue); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, L"if", gEfiShellParametersProtocol->Argv[CurrentParameter]); ShellStatus = SHELL_INVALID_PARAMETER; } } @@ -848,7 +935,18 @@ ShellCommandRunIf ( // build up the next statement for analysis // if (!BuildNextStatement(CurrentParameter, &EndParameter, &Ending)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"Then", L"If", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line); + CurrentScriptFile = ShellCommandGetCurrentScriptFile(); + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SYNTAX_NO_MATCHING), + gShellLevel1HiiHandle, + L"Then", + L"If", + CurrentScriptFile!=NULL + && CurrentScriptFile->CurrentCommand!=NULL + ? CurrentScriptFile->CurrentCommand->Line:0); ShellStatus = SHELL_INVALID_PARAMETER; } else { // @@ -862,10 +960,10 @@ ShellCommandRunIf ( // // Optomize to get out of the loop early... // - if ((Ending == END_TAG_OR && CurrentValue) || (Ending == END_TAG_AND && !CurrentValue)) { + if ((Ending == EndTagOr && CurrentValue) || (Ending == EndTagAnd && !CurrentValue)) { Status = PerformResultOperation(CurrentValue); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, L"if", gEfiShellParametersProtocol->Argv[CurrentParameter]); ShellStatus = SHELL_INVALID_PARAMETER; } break; @@ -877,7 +975,7 @@ ShellCommandRunIf ( // // Skip over the or or and parameter. // - if (Ending == END_TAG_OR || Ending == END_TAG_AND) { + if (Ending == EndTagOr || Ending == EndTagAnd) { CurrentParameter++; } } @@ -899,10 +997,14 @@ ShellCommandRunElse ( IN EFI_SYSTEM_TABLE *SystemTable ) { - ASSERT_EFI_ERROR(CommandInit()); + EFI_STATUS Status; + SCRIPT_FILE *CurrentScriptFile; + + Status = CommandInit (); + ASSERT_EFI_ERROR (Status); if (gEfiShellParametersProtocol->Argc > 1) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle, L"if"); return (SHELL_INVALID_PARAMETER); } @@ -911,18 +1013,49 @@ ShellCommandRunElse ( return (SHELL_UNSUPPORTED); } - - if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, ShellCommandGetCurrentScriptFile(), FALSE, TRUE, FALSE)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"If", L"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line); + CurrentScriptFile = ShellCommandGetCurrentScriptFile(); + + if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, CurrentScriptFile, FALSE, TRUE, FALSE)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SYNTAX_NO_MATCHING), + gShellLevel1HiiHandle, + L"If", + L"Else", + CurrentScriptFile!=NULL + && CurrentScriptFile->CurrentCommand!=NULL + ? CurrentScriptFile->CurrentCommand->Line:0); return (SHELL_DEVICE_ERROR); } - if (!MoveToTag(GetPreviousNode, L"if", L"else", NULL, ShellCommandGetCurrentScriptFile(), FALSE, TRUE, FALSE)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"If", L"Else", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line); + if (!MoveToTag(GetPreviousNode, L"if", L"else", NULL, CurrentScriptFile, FALSE, TRUE, FALSE)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SYNTAX_NO_MATCHING), + gShellLevel1HiiHandle, + L"If", + L"Else", + CurrentScriptFile!=NULL + && CurrentScriptFile->CurrentCommand!=NULL + ? CurrentScriptFile->CurrentCommand->Line:0); return (SHELL_DEVICE_ERROR); } - if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, ShellCommandGetCurrentScriptFile(), FALSE, FALSE, FALSE)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"EndIf", "Else", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line); + if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, CurrentScriptFile, FALSE, FALSE, FALSE)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SYNTAX_NO_MATCHING), + gShellLevel1HiiHandle, + L"EndIf", + "Else", + CurrentScriptFile!=NULL + && CurrentScriptFile->CurrentCommand!=NULL + ? CurrentScriptFile->CurrentCommand->Line:0); return (SHELL_DEVICE_ERROR); } @@ -942,10 +1075,14 @@ ShellCommandRunEndIf ( IN EFI_SYSTEM_TABLE *SystemTable ) { - ASSERT_EFI_ERROR(CommandInit()); + EFI_STATUS Status; + SCRIPT_FILE *CurrentScriptFile; + + Status = CommandInit (); + ASSERT_EFI_ERROR (Status); if (gEfiShellParametersProtocol->Argc > 1) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle, L"if"); return (SHELL_INVALID_PARAMETER); } @@ -954,8 +1091,19 @@ ShellCommandRunEndIf ( return (SHELL_UNSUPPORTED); } - if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, ShellCommandGetCurrentScriptFile(), FALSE, TRUE, FALSE)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"If", L"EndIf", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line); + CurrentScriptFile = ShellCommandGetCurrentScriptFile(); + if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, CurrentScriptFile, FALSE, TRUE, FALSE)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SYNTAX_NO_MATCHING), + gShellLevel1HiiHandle, + L"If", + L"EndIf", + CurrentScriptFile!=NULL + && CurrentScriptFile->CurrentCommand!=NULL + ? CurrentScriptFile->CurrentCommand->Line:0); return (SHELL_DEVICE_ERROR); }