]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel1CommandsLib/If.c
ShellPkg/Dp: Add null pointer check
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel1CommandsLib / If.c
index a3c4482bb3bfc3284347a7deba775f65e1ee3f0c..35c5ca6835817f942e6044d72c7641c0eb278558 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for If and else shell level 1 function.\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
+  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -39,17 +40,19 @@ typedef enum {
 /**\r
   Extract the next fragment, if there is one.\r
 \r
-  @param[in,out] Statement    The current remaining statement.\r
-  @param[in] Fragment         The current fragment.\r
+  @param[in, out] Statement    The current remaining statement.\r
+  @param[in] Fragment          The current fragment.\r
+  @param[out] Match            TRUE when there is another Fragment in Statement,\r
+                               FALSE otherwise.\r
 \r
-  @retval FALSE   There is not another fragment.\r
-  @retval TRUE    There is another fragment.\r
+  @retval EFI_SUCCESS          The match operation is performed successfully.\r
+  @retval EFI_OUT_OF_RESOURCES Out of resources.\r
 **/\r
-BOOLEAN\r
-EFIAPI\r
+EFI_STATUS\r
 IsNextFragment (\r
   IN OUT CONST CHAR16     **Statement,\r
-  IN CONST CHAR16         *Fragment\r
+  IN CONST CHAR16         *Fragment,\r
+  OUT BOOLEAN             *Match\r
   )\r
 {\r
   CHAR16                  *Tester;\r
@@ -57,7 +60,9 @@ IsNextFragment (
   Tester = NULL;\r
 \r
   Tester = StrnCatGrow(&Tester, NULL, *Statement, StrLen(Fragment));\r
-  ASSERT(Tester != NULL);\r
+  if (Tester == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   Tester[StrLen(Fragment)] = CHAR_NULL;\r
   if (gUnicodeCollation->StriColl(\r
         gUnicodeCollation,\r
@@ -70,11 +75,12 @@ IsNextFragment (
     while (*Statement[0] == L' ') {\r
       (*Statement)++;\r
     }\r
-    FreePool(Tester);\r
-    return (TRUE);\r
+    *Match = TRUE;\r
+  } else {\r
+    *Match = FALSE;\r
   }\r
   FreePool(Tester);\r
-  return (FALSE);\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -86,7 +92,6 @@ IsNextFragment (
   @retval FALSE   String is not a valid profile.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 IsValidProfile (\r
   IN CONST CHAR16 *String\r
   )\r
@@ -95,6 +100,7 @@ IsValidProfile (
   CONST CHAR16  *TempLocation;\r
 \r
   ProfilesString = ShellGetEnvironmentVariable(L"profiles");\r
+  ASSERT(ProfilesString != NULL);\r
   TempLocation = StrStr(ProfilesString, String);\r
   if ((TempLocation != NULL) && (*(TempLocation-1) == L';') && (*(TempLocation+StrLen(String)) == L';')) {\r
     return (TRUE);\r
@@ -114,7 +120,6 @@ IsValidProfile (
   @return     The result of the comparison.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 TestOperation (\r
   IN CONST CHAR16             *Compare1,\r
   IN CONST CHAR16             *Compare2,\r
@@ -164,7 +169,6 @@ TestOperation (
       }\r
     }\r
     return (FALSE);\r
-    break;\r
   case OperatorUnsignedLessThan:\r
   case OperatorLessThan:\r
     if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {\r
@@ -200,7 +204,6 @@ TestOperation (
 \r
     }\r
     return (FALSE);\r
-    break;\r
   case OperatorEqual:\r
     if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {\r
       //\r
@@ -228,7 +231,6 @@ TestOperation (
       }\r
     }\r
     return (FALSE);\r
-    break;\r
   case OperatorNotEqual:\r
     if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {\r
       //\r
@@ -256,7 +258,6 @@ TestOperation (
       }\r
     }\r
     return (FALSE);\r
-    break;\r
   case OperatorUnsignedGreaterOrEqual:\r
   case OperatorGreatorOrEqual:\r
     if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {\r
@@ -291,7 +292,6 @@ TestOperation (
       }\r
     }\r
     return (FALSE);\r
-    break;\r
   case OperatorLessOrEqual:\r
   case OperatorUnsignedLessOrEqual:\r
     if (ForceStringCompare || !ShellIsHexOrDecimalNumber(Compare1, FALSE, FALSE) || !ShellIsHexOrDecimalNumber(Compare2, FALSE, FALSE)) {\r
@@ -326,7 +326,6 @@ TestOperation (
       }\r
     }\r
     return (FALSE);\r
-    break;\r
   default:\r
     ASSERT(FALSE);\r
     return (FALSE);\r
@@ -336,21 +335,20 @@ TestOperation (
 /**\r
   Process an if statement and determine if its is valid or not.\r
 \r
-  @param[in,out] PassingState     Opon entry, the current state.  Upon exit, \r
-                                  the new state.\r
-  @param[in] StartParameterNumber The number of the first parameter of\r
-                                  this statement.\r
-  @param[in] EndParameterNumber   The number of the final parameter of\r
-                                  this statement.\r
-  @param[in] OperatorToUse        The type of termination operator.\r
-  @param[in] CaseInsensitive      TRUE for case insensitive, FALSE otherwise.\r
-  @param[in] ForceStringCompare   TRUE for all string based, FALSE otherwise.\r
+  @param[in, out] PassingState     Opon entry, the current state.  Upon exit, \r
+                                   the new state.\r
+  @param[in] StartParameterNumber  The number of the first parameter of\r
+                                   this statement.\r
+  @param[in] EndParameterNumber    The number of the final parameter of\r
+                                   this statement.\r
+  @param[in] OperatorToUse         The type of termination operator.\r
+  @param[in] CaseInsensitive       TRUE for case insensitive, FALSE otherwise.\r
+  @param[in] ForceStringCompare    TRUE for all string based, FALSE otherwise.\r
 \r
   @retval EFI_INVALID_PARAMETER   A parameter was invalid.\r
   @retval EFI_SUCCESS             The operation was successful.                                  \r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 ProcessStatement (\r
   IN OUT BOOLEAN          *PassingState,\r
   IN UINTN                StartParameterNumber,\r
@@ -369,14 +367,16 @@ ProcessStatement (
   CHAR16                  *Compare2;\r
   CHAR16                  HexString[20];\r
   CHAR16                  *TempSpot;\r
+  BOOLEAN                 Match;\r
 \r
   ASSERT((END_TAG_TYPE)OperatorToUse != EndTagThen);\r
 \r
   Status          = EFI_SUCCESS;\r
   BinOp           = OperatorMax;\r
   OperationResult = FALSE;\r
+  Match           = FALSE;\r
   StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber];\r
-  if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"not")) {\r
+  if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"not", &Match)) && Match) {\r
     NotPresent      = TRUE;\r
     StatementWalker = gEfiShellParametersProtocol->Argv[++StartParameterNumber];\r
   } else {\r
@@ -386,16 +386,19 @@ ProcessStatement (
   //\r
   // now check for 'boolfunc' operators\r
   //\r
-  if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"isint")) {\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
+  if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"isint", &Match)) && Match) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match\r
+        && StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
       StatementWalker[StrLen(StatementWalker)-1] = CHAR_NULL;\r
       OperationResult = ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE);\r
     } else {\r
       Status = EFI_INVALID_PARAMETER;\r
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"isint");\r
     }\r
-  } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exists") || IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exist")) {\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
+  } else if ((!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exists", &Match)) && Match) ||\r
+             (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"exist", &Match)) && Match)) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match &&\r
+        StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
       StatementWalker[StrLen(StatementWalker)-1] = CHAR_NULL;\r
       //\r
       // is what remains a file in CWD???\r
@@ -407,8 +410,9 @@ ProcessStatement (
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"exist(s)");\r
       Status = EFI_INVALID_PARAMETER;\r
     }\r
-  } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"available")) {\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
+  } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"available", &Match)) && Match) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match &&\r
+        StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
       StatementWalker[StrLen(StatementWalker)-1] = CHAR_NULL;\r
       //\r
       // is what remains a file in the CWD or path???\r
@@ -418,8 +422,9 @@ ProcessStatement (
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"available");\r
       Status = EFI_INVALID_PARAMETER;\r
     }\r
-  } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"profile")) {\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
+  } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"profile", &Match)) && Match) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match &&\r
+        StatementWalker[StrLen(StatementWalker)-1] == L')') {\r
       //\r
       // Chop off that ')'\r
       //\r
@@ -444,9 +449,9 @@ ProcessStatement (
     // get the first item\r
     //\r
     StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber];\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror")) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror", &Match)) && Match) {\r
       TempSpot = StrStr(StatementWalker, L")");\r
-      if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) {\r
+      if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) {\r
         *TempSpot = CHAR_NULL;\r
         if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) {\r
           UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT);\r
@@ -461,9 +466,9 @@ ProcessStatement (
         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"efierror");\r
         Status = EFI_INVALID_PARAMETER;\r
       }\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror", &Match)) && Match) {\r
       TempSpot = StrStr(StatementWalker, L")");\r
-      if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) {\r
+      if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) {\r
         *TempSpot = CHAR_NULL;\r
         if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) {\r
           UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>2));\r
@@ -478,9 +483,9 @@ ProcessStatement (
         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"pierror");\r
         Status = EFI_INVALID_PARAMETER;\r
       }\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"oemerror")) {\r
+    } else if (!EFI_ERROR (IsNextFragment ((CONST CHAR16**)(&StatementWalker), L"oemerror", &Match)) && Match) {\r
       TempSpot = StrStr(StatementWalker, L")");\r
-      if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) {\r
+      if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) {\r
         TempSpot = CHAR_NULL;\r
         if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) {\r
           UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>1));\r
@@ -513,27 +518,27 @@ ProcessStatement (
     //\r
     ASSERT(StartParameterNumber+1<EndParameterNumber);\r
     StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber+1];\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"gt")) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"gt", &Match)) && Match) {\r
       BinOp = OperatorGreaterThan;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"lt")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"lt", &Match)) && Match) {\r
       BinOp = OperatorLessThan;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"eq")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"eq", &Match)) && Match) {\r
       BinOp = OperatorEqual;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ne")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ne", &Match)) && Match) {\r
       BinOp = OperatorNotEqual;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ge")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ge", &Match)) && Match) {\r
       BinOp = OperatorGreatorOrEqual;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"le")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"le", &Match)) && Match) {\r
       BinOp = OperatorLessOrEqual;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"==")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"==", &Match)) && Match) {\r
       BinOp = OperatorEqual;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ugt")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ugt", &Match)) && Match) {\r
       BinOp = OperatorUnisgnedGreaterThan;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ult")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ult", &Match)) && Match) {\r
       BinOp = OperatorUnsignedLessThan;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"uge")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"uge", &Match)) && Match) {\r
       BinOp = OperatorUnsignedGreaterOrEqual;\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ule")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"ule", &Match)) && Match) {\r
       BinOp = OperatorUnsignedLessOrEqual;\r
     } else {\r
       ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_INVALID_BINOP), gShellLevel1HiiHandle, StatementWalker);\r
@@ -545,9 +550,9 @@ ProcessStatement (
     //\r
     ASSERT(StartParameterNumber+2<=EndParameterNumber);\r
     StatementWalker = gEfiShellParametersProtocol->Argv[StartParameterNumber+2];\r
-    if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror")) {\r
+    if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"efierror", &Match)) && Match) {\r
       TempSpot = StrStr(StatementWalker, L")");\r
-      if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) {\r
+      if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) {\r
         TempSpot = CHAR_NULL;\r
         if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) {\r
           UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT);\r
@@ -565,9 +570,9 @@ ProcessStatement (
     //\r
     // can this be collapsed into the above?\r
     //\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror")) {\r
+    } else if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"pierror", &Match)) && Match) {\r
       TempSpot = StrStr(StatementWalker, L")");\r
-      if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) {\r
+      if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) {\r
         TempSpot = CHAR_NULL;\r
         if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) {\r
           UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>2));\r
@@ -582,9 +587,9 @@ ProcessStatement (
         ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_IN), gShellLevel1HiiHandle, L"pierror");\r
         Status = EFI_INVALID_PARAMETER;\r
       }\r
-    } else if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"oemerror")) {\r
+    } else if (!EFI_ERROR (IsNextFragment ((CONST CHAR16**)(&StatementWalker), L"oemerror", &Match)) && Match) {\r
       TempSpot = StrStr(StatementWalker, L")");\r
-      if (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(") && TempSpot != NULL) {\r
+      if (!EFI_ERROR (IsNextFragment((CONST CHAR16**)(&StatementWalker), L"(", &Match)) && Match && TempSpot != NULL) {\r
         TempSpot = CHAR_NULL;\r
         if (ShellIsHexOrDecimalNumber(StatementWalker, FALSE, FALSE)) {\r
           UnicodeSPrint(HexString, sizeof(HexString), L"0x%x", ShellStrToUintn(StatementWalker)|MAX_BIT|(MAX_BIT>>1));\r
@@ -653,19 +658,15 @@ ProcessStatement (
   @retval FALSE   A valid statement was not found.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 BuildNextStatement (\r
   IN UINTN          ParameterNumber,\r
   OUT UINTN         *EndParameter,\r
   OUT END_TAG_TYPE  *EndTag\r
   )\r
 {\r
-  CHAR16    *Buffer;\r
-  UINTN     BufferSize;\r
-\r
   *EndTag = EndTagMax;\r
 \r
-  for(Buffer = NULL, BufferSize = 0\r
+  for(\r
     ; ParameterNumber < gEfiShellParametersProtocol->Argc\r
     ; ParameterNumber++\r
    ) {\r
@@ -708,7 +709,6 @@ BuildNextStatement (
   @retval FALSE    Something went wrong.\r
 **/\r
 BOOLEAN\r
-EFIAPI\r
 MoveToTagSpecial (\r
   IN SCRIPT_FILE                *ScriptFile\r
   )\r
@@ -737,8 +737,15 @@ MoveToTagSpecial (
     //\r
     CommandName   = NULL;\r
     CommandName   = StrnCatGrow(&CommandName, NULL, CommandNode->Cl, 0);\r
+    if (CommandName == NULL) {\r
+      continue;\r
+    }\r
     CommandWalker = CommandName;\r
-    while (CommandWalker[0] == L' ') {\r
+\r
+    //\r
+    // Skip leading spaces and tabs.\r
+    //\r
+    while ((CommandWalker[0] == L' ') || (CommandWalker[0] == L'\t')) {\r
       CommandWalker++;\r
     }\r
     TempLocation  = StrStr(CommandWalker, L" ");\r
@@ -792,7 +799,6 @@ MoveToTagSpecial (
   @retval EFI_NOT_FOUND     The ending tag could not be found.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 PerformResultOperation (\r
   IN CONST BOOLEAN Result\r
   )\r
@@ -825,36 +831,37 @@ ShellCommandRunIf (
   BOOLEAN             CurrentValue;\r
   END_TAG_TYPE        Ending;\r
   END_TAG_TYPE        PreviousEnding;\r
-\r
+  SCRIPT_FILE         *CurrentScriptFile;\r
 \r
   Status = CommandInit();\r
   ASSERT_EFI_ERROR(Status);\r
 \r
   if (!gEfiShellProtocol->BatchIsActive()) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"If");\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_NO_SCRIPT), gShellLevel1HiiHandle, L"if");  \r
     return (SHELL_UNSUPPORTED);\r
   }\r
 \r
   if (gEfiShellParametersProtocol->Argc < 3) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel1HiiHandle, L"if");  \r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
   //\r
   // Make sure that an End exists.\r
   //\r
-  if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, ShellCommandGetCurrentScriptFile(), TRUE, TRUE, FALSE)) {\r
+  CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
+  if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, CurrentScriptFile, TRUE, TRUE, FALSE)) {\r
     ShellPrintHiiEx(\r
       -1, \r
       -1, \r
       NULL, \r
       STRING_TOKEN (STR_SYNTAX_NO_MATCHING), \r
       gShellLevel1HiiHandle, \r
-      L"EnfIf", \r
+      L"EndIf", \r
       L"If", \r
-      ShellCommandGetCurrentScriptFile()!=NULL\r
-        &&ShellCommandGetCurrentScriptFile()->CurrentCommand!=NULL\r
-          ?ShellCommandGetCurrentScriptFile()->CurrentCommand->Line:0);\r
+      CurrentScriptFile!=NULL \r
+        && CurrentScriptFile->CurrentCommand!=NULL\r
+        ? CurrentScriptFile->CurrentCommand->Line:0);\r
     return (SHELL_DEVICE_ERROR);\r
   }\r
 \r
@@ -913,12 +920,12 @@ ShellCommandRunIf (
       // we are at the then\r
       //\r
       if (CurrentParameter+1 != gEfiShellParametersProtocol->Argc) {\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TEXT_AFTER_THEN), gShellLevel1HiiHandle);\r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TEXT_AFTER_THEN), gShellLevel1HiiHandle, L"if");  \r
         ShellStatus = SHELL_INVALID_PARAMETER;\r
       } else {\r
         Status = PerformResultOperation(CurrentValue);\r
         if (EFI_ERROR(Status)) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, L"if", gEfiShellParametersProtocol->Argv[CurrentParameter]);  \r
           ShellStatus = SHELL_INVALID_PARAMETER;\r
         }\r
       }\r
@@ -928,7 +935,18 @@ ShellCommandRunIf (
       // build up the next statement for analysis\r
       //\r
       if (!BuildNextStatement(CurrentParameter, &EndParameter, &Ending)) {\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_NO_MATCHING), gShellLevel1HiiHandle, L"Then", L"If", ShellCommandGetCurrentScriptFile()->CurrentCommand->Line);\r
+        CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
+        ShellPrintHiiEx(\r
+          -1, \r
+          -1, \r
+          NULL, \r
+          STRING_TOKEN (STR_SYNTAX_NO_MATCHING), \r
+          gShellLevel1HiiHandle, \r
+          L"Then", \r
+          L"If",\r
+          CurrentScriptFile!=NULL \r
+            && CurrentScriptFile->CurrentCommand!=NULL\r
+            ? CurrentScriptFile->CurrentCommand->Line:0);\r
         ShellStatus = SHELL_INVALID_PARAMETER;\r
       } else {\r
         //\r
@@ -945,7 +963,7 @@ ShellCommandRunIf (
           if ((Ending == EndTagOr && CurrentValue) || (Ending == EndTagAnd && !CurrentValue)) {\r
             Status = PerformResultOperation(CurrentValue);\r
             if (EFI_ERROR(Status)) {\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, gEfiShellParametersProtocol->Argv[CurrentParameter]);\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SYNTAX_AFTER_BAD), gShellLevel1HiiHandle, L"if", gEfiShellParametersProtocol->Argv[CurrentParameter]);  \r
               ShellStatus = SHELL_INVALID_PARAMETER;\r
             }\r
             break;\r
@@ -979,10 +997,14 @@ ShellCommandRunElse (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  ASSERT_EFI_ERROR(CommandInit());\r
+  EFI_STATUS  Status;\r
+  SCRIPT_FILE *CurrentScriptFile;\r
+\r
+  Status = CommandInit ();\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   if (gEfiShellParametersProtocol->Argc > 1) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle, L"if");  \r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
@@ -991,8 +1013,9 @@ ShellCommandRunElse (
     return (SHELL_UNSUPPORTED);\r
   }\r
 \r
+  CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
 \r
-  if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, ShellCommandGetCurrentScriptFile(), FALSE, TRUE, FALSE)) {\r
+  if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, CurrentScriptFile, FALSE, TRUE, FALSE)) {\r
     ShellPrintHiiEx(\r
       -1, \r
       -1, \r
@@ -1001,12 +1024,12 @@ ShellCommandRunElse (
       gShellLevel1HiiHandle, \r
       L"If", \r
       L"Else", \r
-      ShellCommandGetCurrentScriptFile()!=NULL\r
-        &&ShellCommandGetCurrentScriptFile()->CurrentCommand!=NULL\r
-          ?ShellCommandGetCurrentScriptFile()->CurrentCommand->Line:0);\r
+      CurrentScriptFile!=NULL \r
+        && CurrentScriptFile->CurrentCommand!=NULL\r
+        ? CurrentScriptFile->CurrentCommand->Line:0);\r
     return (SHELL_DEVICE_ERROR);\r
   }\r
-  if (!MoveToTag(GetPreviousNode, L"if", L"else", NULL, ShellCommandGetCurrentScriptFile(), FALSE, TRUE, FALSE)) {\r
+  if (!MoveToTag(GetPreviousNode, L"if", L"else", NULL, CurrentScriptFile, FALSE, TRUE, FALSE)) {\r
     ShellPrintHiiEx(\r
       -1, \r
       -1, \r
@@ -1015,13 +1038,13 @@ ShellCommandRunElse (
       gShellLevel1HiiHandle, \r
       L"If", \r
       L"Else", \r
-      ShellCommandGetCurrentScriptFile()!=NULL\r
-        &&ShellCommandGetCurrentScriptFile()->CurrentCommand!=NULL\r
-          ?ShellCommandGetCurrentScriptFile()->CurrentCommand->Line:0);\r
+      CurrentScriptFile!=NULL \r
+        && CurrentScriptFile->CurrentCommand!=NULL\r
+        ? CurrentScriptFile->CurrentCommand->Line:0);\r
     return (SHELL_DEVICE_ERROR);\r
   }\r
 \r
-  if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, ShellCommandGetCurrentScriptFile(), FALSE, FALSE, FALSE)) {\r
+  if (!MoveToTag(GetNextNode, L"endif", L"if", NULL, CurrentScriptFile, FALSE, FALSE, FALSE)) {\r
     ShellPrintHiiEx(\r
       -1, \r
       -1, \r
@@ -1030,9 +1053,9 @@ ShellCommandRunElse (
       gShellLevel1HiiHandle, \r
       L"EndIf", \r
       "Else", \r
-      ShellCommandGetCurrentScriptFile()!=NULL\r
-        &&ShellCommandGetCurrentScriptFile()->CurrentCommand!=NULL\r
-          ?ShellCommandGetCurrentScriptFile()->CurrentCommand->Line:0);\r
+      CurrentScriptFile!=NULL \r
+        && CurrentScriptFile->CurrentCommand!=NULL\r
+        ? CurrentScriptFile->CurrentCommand->Line:0);\r
     return (SHELL_DEVICE_ERROR);\r
   }\r
 \r
@@ -1052,10 +1075,14 @@ ShellCommandRunEndIf (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  ASSERT_EFI_ERROR(CommandInit());\r
+  EFI_STATUS  Status;\r
+  SCRIPT_FILE *CurrentScriptFile;\r
+\r
+  Status = CommandInit ();\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   if (gEfiShellParametersProtocol->Argc > 1) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellLevel1HiiHandle, L"if");  \r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
 \r
@@ -1064,7 +1091,8 @@ ShellCommandRunEndIf (
     return (SHELL_UNSUPPORTED);\r
   }\r
 \r
-  if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, ShellCommandGetCurrentScriptFile(), FALSE, TRUE, FALSE)) {\r
+  CurrentScriptFile = ShellCommandGetCurrentScriptFile();\r
+  if (!MoveToTag(GetPreviousNode, L"if", L"endif", NULL, CurrentScriptFile, FALSE, TRUE, FALSE)) {\r
     ShellPrintHiiEx(\r
       -1, \r
       -1, \r
@@ -1073,9 +1101,9 @@ ShellCommandRunEndIf (
       gShellLevel1HiiHandle, \r
       L"If", \r
       L"EndIf", \r
-      ShellCommandGetCurrentScriptFile()!=NULL\r
-        &&ShellCommandGetCurrentScriptFile()->CurrentCommand!=NULL\r
-          ?ShellCommandGetCurrentScriptFile()->CurrentCommand->Line:0);\r
+      CurrentScriptFile!=NULL \r
+        && CurrentScriptFile->CurrentCommand!=NULL\r
+        ? CurrentScriptFile->CurrentCommand->Line:0);\r
     return (SHELL_DEVICE_ERROR);\r
   }\r
 \r