]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/Shell.c
Fix an issue that execute "map -r" in NT32 simulator cannot reinstate iSCSI session...
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
index e817a1114267b7ec7e84f9e8ecc33f0aae8f090e..3019c7d4101eca1225462b990f9568cf437754de 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   This is THE shell (application)\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2013, Hewlett-Packard Development Company, L.P.\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
@@ -23,22 +24,24 @@ SHELL_INFO ShellInfoObject = {
   FALSE,\r
   FALSE,\r
   {\r
-    0,\r
-    0,\r
-    0,\r
-    0,\r
-    0,\r
-    0,\r
-    0,\r
-    0,\r
-    0,\r
+    {{\r
+      0,\r
+      0,\r
+      0,\r
+      0,\r
+      0,\r
+      0,\r
+      0,\r
+      0,\r
+      0\r
+    }},\r
     0,\r
     NULL,\r
     NULL\r
   },\r
-  {0,0},\r
+  {{NULL, NULL}, NULL},\r
   {\r
-    {0,0},\r
+    {{NULL, NULL}, NULL},\r
     0,\r
     0,\r
     TRUE\r
@@ -50,8 +53,12 @@ SHELL_INFO ShellInfoObject = {
   NULL,\r
   NULL,\r
   NULL,\r
-  {0,0,NULL,NULL},\r
-  {0,0},\r
+  {{NULL, NULL}, NULL, NULL},\r
+  {{NULL, NULL}, NULL, NULL},\r
+  NULL,\r
+  NULL,\r
+  NULL,\r
+  NULL,\r
   NULL,\r
   NULL,\r
   NULL,\r
@@ -375,7 +382,7 @@ UefiMain (
         Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r
       }\r
 \r
-      if (!ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
+      if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
         //\r
         // begin the UI waiting loop\r
         //\r
@@ -435,7 +442,7 @@ UefiMain (
   }\r
   if (ShellInfoObject.NewEfiShellProtocol != NULL){\r
     if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){\r
-      ShellInfoObject.NewEfiShellProtocol->SetEnv(L"cwd", L"", TRUE);\r
+      InternalEfiShellSetEnv(L"cwd", NULL, TRUE);\r
     }\r
     CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol);\r
     DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;);\r
@@ -475,6 +482,9 @@ UefiMain (
     DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
   }\r
 \r
+  if (ShellCommandGetExit()) {\r
+    return ((EFI_STATUS)ShellCommandGetExitCode());\r
+  }\r
   return (Status);\r
 }\r
 \r
@@ -566,8 +576,8 @@ IsScriptOnlyCommand(
   loaded image protocol installed on it.  The FilePath will point to the device path\r
   for the file that was loaded.\r
 \r
-  @param[in,out] DevPath       On a sucessful return the device path to the loaded image.\r
-  @param[in,out] FilePath      On a sucessful return the device path to the file.\r
+  @param[in, out] DevPath       On a sucessful return the device path to the loaded image.\r
+  @param[in, out] FilePath      On a sucessful return the device path to the file.\r
 \r
   @retval EFI_SUCCESS           The 2 device paths were sucessfully returned.\r
   @retval other                 A error from gBS->HandleProtocol.\r
@@ -633,6 +643,7 @@ STATIC CONST SHELL_PARAM_ITEM mShellParamList[] = {
   {L"-noversion",     TypeFlag},\r
   {L"-startup",       TypeFlag},\r
   {L"-delay",         TypeValue},\r
+  {L"-_exit",         TypeFlag},\r
   {NULL, TypeMax}\r
   };\r
 \r
@@ -740,6 +751,7 @@ ProcessCommandLine(
   ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap        = ShellCommandLineGetFlag(Package, L"-nomap");\r
   ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion    = ShellCommandLineGetFlag(Package, L"-noversion");\r
   ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay        = ShellCommandLineGetFlag(Package, L"-delay");\r
+  ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit         = ShellCommandLineGetFlag(Package, L"-_exit");\r
 \r
   ShellInfoObject.ShellInitSettings.Delay = 5;\r
 \r
@@ -824,6 +836,7 @@ DoStartupScript(
     return (EFI_SUCCESS);\r
   }\r
 \r
+  gST->ConOut->EnableCursor(gST->ConOut, FALSE);\r
   //\r
   // print out our warning and see if they press a key\r
   //\r
@@ -838,6 +851,7 @@ DoStartupScript(
     }\r
   }\r
   ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle);\r
+  gST->ConOut->EnableCursor(gST->ConOut, TRUE);\r
 \r
   //\r
   // ESC was pressed\r
@@ -854,15 +868,19 @@ DoStartupScript(
     FileStringPath = NULL;\r
     NewSize = 0;\r
     FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0);\r
-    TempSpot = StrStr(FileStringPath, L";");\r
-    if (TempSpot != NULL) {\r
-      *TempSpot = CHAR_NULL;\r
+    if (FileStringPath == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+    } else {\r
+      TempSpot = StrStr(FileStringPath, L";");\r
+      if (TempSpot != NULL) {\r
+        *TempSpot = CHAR_NULL;\r
+      }\r
+      FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);\r
+      PathRemoveLastItem(FileStringPath);\r
+      FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);\r
+      Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);\r
+      FreePool(FileStringPath);\r
     }\r
-    FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0);\r
-    PathRemoveLastItem(FileStringPath);\r
-    FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0);\r
-    Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ);\r
-    FreePool(FileStringPath);\r
   }\r
   if (EFI_ERROR(Status)) {\r
     NamePath = FileDevicePath (NULL, mStartupScript);\r
@@ -1016,10 +1034,10 @@ AddLineToCommandHistory(
   Checks if a string is an alias for another command.  If yes, then it replaces the alias name\r
   with the correct command name.\r
 \r
-  @param[in,out] CommandString    Upon entry the potential alias.  Upon return the\r
-                                  command name if it was an alias.  If it was not\r
-                                  an alias it will be unchanged.  This function may\r
-                                  change the buffer to fit the command name.\r
+  @param[in, out] CommandString    Upon entry the potential alias.  Upon return the\r
+                                   command name if it was an alias.  If it was not\r
+                                   an alias it will be unchanged.  This function may\r
+                                   change the buffer to fit the command name.\r
 \r
   @retval EFI_SUCCESS             The name was changed.\r
   @retval EFI_SUCCESS             The name was not an alias.\r
@@ -1068,6 +1086,7 @@ ShellConvertVariables (
   CHAR16              *NewCommandLine1;\r
   CHAR16              *NewCommandLine2;\r
   CHAR16              *Temp;\r
+  CHAR16              *Temp2;\r
   UINTN               ItemSize;\r
   CHAR16              *ItemTemp;\r
   SCRIPT_FILE         *CurrentScriptFile;\r
@@ -1125,15 +1144,6 @@ ShellConvertVariables (
     }\r
   }\r
 \r
-  //\r
-  // Quick out if none were found...\r
-  //\r
-  if (NewSize == StrSize(OriginalCommandLine)) {\r
-    ASSERT(Temp == NULL);\r
-    Temp = StrnCatGrow(&Temp, NULL, OriginalCommandLine, 0);\r
-    return (Temp);\r
-  }\r
-\r
   //\r
   // now do the replacements...\r
   //\r
@@ -1165,8 +1175,51 @@ ShellConvertVariables (
     ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE);\r
     StrCpy(NewCommandLine1, NewCommandLine2);\r
     }\r
+\r
+    //\r
+    // Remove non-existant environment variables in scripts only\r
+    //\r
+    for (Temp = NewCommandLine1 ; Temp != NULL ; ) {\r
+      Temp = StrStr(Temp, L"%");\r
+      if (Temp == NULL) {\r
+        break;\r
+      }\r
+      while (*(Temp - 1) == L'^') {\r
+        Temp = StrStr(Temp + 1, L"%");\r
+        if (Temp == NULL) {\r
+          break;\r
+       }\r
+      }\r
+      if (Temp == NULL) {\r
+        break;\r
+      }\r
+      \r
+      Temp2 = StrStr(Temp + 1, L"%");\r
+      if (Temp2 == NULL) {\r
+        break;\r
+      }\r
+      while (*(Temp2 - 1) == L'^') {\r
+        Temp2 = StrStr(Temp2 + 1, L"%");\r
+        if (Temp2 == NULL) {\r
+          break;\r
+        }\r
+      }\r
+      if (Temp2 == NULL) {\r
+        break;\r
+      }\r
+      \r
+      Temp2++;\r
+      CopyMem(Temp, Temp2, StrSize(Temp2));\r
+    }\r
+\r
   }\r
 \r
+  //\r
+  // Now cleanup any straggler intentionally ignored "%" characters\r
+  //\r
+  ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE);\r
+  StrCpy(NewCommandLine1, NewCommandLine2);\r
+  \r
   FreePool(NewCommandLine2);\r
   FreePool(ItemTemp);\r
 \r
@@ -1212,7 +1265,16 @@ RunSplitCommand(
 \r
   NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0);\r
   OurCommandLine  = StrnCatGrow(&OurCommandLine , &Size2, CmdLine                , StrStr(CmdLine, L"|") - CmdLine);\r
-  if (NextCommandLine[0] != CHAR_NULL &&\r
+\r
+  if (NextCommandLine == NULL || OurCommandLine == NULL) {\r
+    SHELL_FREE_NON_NULL(OurCommandLine);\r
+    SHELL_FREE_NON_NULL(NextCommandLine);\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) {\r
+    SHELL_FREE_NON_NULL(OurCommandLine);\r
+    SHELL_FREE_NON_NULL(NextCommandLine);\r
+    return (EFI_INVALID_PARAMETER);\r
+  } else if (NextCommandLine[0] != CHAR_NULL &&\r
       NextCommandLine[0] == L'a' &&\r
       NextCommandLine[1] == L' '\r
      ){\r
@@ -1233,7 +1295,6 @@ RunSplitCommand(
   ASSERT(Split->SplitStdOut != NULL);\r
   InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link);\r
 \r
-  ASSERT(StrStr(OurCommandLine, L"|") == NULL);\r
   Status = RunCommand(OurCommandLine);\r
 \r
   //\r
@@ -1293,12 +1354,13 @@ RunCommand(
   )\r
 {\r
   EFI_STATUS                Status;\r
+  EFI_STATUS                StatusCode;\r
   CHAR16                    *CommandName;\r
   SHELL_STATUS              ShellStatus;\r
   UINTN                     Argc;\r
   CHAR16                    **Argv;\r
   BOOLEAN                   LastError;\r
-  CHAR16                    LeString[11];\r
+  CHAR16                    LeString[19];\r
   CHAR16                    *PostAliasCmdLine;\r
   UINTN                     PostAliasSize;\r
   CHAR16                    *PostVariableCmdLine;\r
@@ -1331,6 +1393,9 @@ RunCommand(
   Split               = NULL;\r
 \r
   CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r
+  if (CleanOriginal == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
   while (CleanOriginal[StrLen(CleanOriginal)-1] == L' ') {\r
     CleanOriginal[StrLen(CleanOriginal)-1] = CHAR_NULL;\r
   }\r
@@ -1416,6 +1481,9 @@ RunCommand(
     } else {\r
       Status = RunSplitCommand(PostVariableCmdLine, Split->SplitStdIn, Split->SplitStdOut);\r
     }\r
+    if (EFI_ERROR(Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, PostVariableCmdLine);\r
+    }\r
   } else {\r
 \r
     //\r
@@ -1489,7 +1557,7 @@ RunCommand(
         if (!EFI_ERROR(Status))  {\r
           Status = ShellCommandRunCommandHandler(ShellInfoObject.NewShellParametersProtocol->Argv[0], &ShellStatus, &LastError);\r
           ASSERT_EFI_ERROR(Status);\r
-          UnicodeSPrint(LeString, sizeof(LeString)*sizeof(LeString[0]), L"0x%08x", ShellStatus);\r
+          UnicodeSPrint(LeString, sizeof(LeString), L"0x%08Lx", ShellStatus);\r
           DEBUG_CODE(InternalEfiShellSetEnv(L"DebugLasterror", LeString, TRUE););\r
           if (LastError) {\r
             InternalEfiShellSetEnv(L"Lasterror", LeString, TRUE);\r
@@ -1534,11 +1602,26 @@ RunCommand(
               DevPath,\r
               PostVariableCmdLine,\r
               NULL,\r
-              NULL\r
+              &StatusCode\r
              );\r
+\r
+            //\r
+            // Updatet last error status.\r
+            //\r
+            UnicodeSPrint(LeString, sizeof(LeString), L"0x%08Lx", StatusCode);\r
+            DEBUG_CODE(InternalEfiShellSetEnv(L"DebugLasterror", LeString, TRUE););\r
+            InternalEfiShellSetEnv(L"Lasterror", LeString, TRUE);\r
           }\r
         }\r
       }\r
+\r
+      //\r
+      // Print some error info.\r
+      //\r
+      if (EFI_ERROR(Status)) {\r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r
+      }\r
+\r
       CommandName = StrnCatGrow(&CommandName, NULL, ShellInfoObject.NewShellParametersProtocol->Argv[0], 0);\r
 \r
       RestoreArgcArgv(ShellInfoObject.NewShellParametersProtocol, &Argv, &Argc);\r
@@ -1622,6 +1705,7 @@ RunScriptFileHandle (
   BOOLEAN             PreCommandEchoState;\r
   CONST CHAR16        *CurDir;\r
   UINTN               LineCount;\r
+  CHAR16              LeString[50];\r
 \r
   ASSERT(!ShellCommandGetScriptExit());\r
 \r
@@ -1798,15 +1882,6 @@ RunScriptFileHandle (
         //\r
       } else {\r
         if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) {\r
-          if (ShellCommandGetEchoState()) {\r
-            CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
-            if (CurDir != NULL && StrLen(CurDir) > 1) {\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
-            } else {\r
-              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
-            }\r
-            ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
-          }\r
           if (CommandLine3[0] == L'@') {\r
             //\r
             // We need to save the current echo state\r
@@ -1821,15 +1896,31 @@ RunScriptFileHandle (
             //\r
             ShellCommandSetEchoState(PreCommandEchoState);\r
           } else {\r
+            if (ShellCommandGetEchoState()) {\r
+              CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd");\r
+              if (CurDir != NULL && StrLen(CurDir) > 1) {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir);\r
+              } else {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle);\r
+              }\r
+              ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
+            }\r
             Status = RunCommand(CommandLine3);\r
           }\r
         }\r
 \r
         if (ShellCommandGetScriptExit()) {\r
-          ShellCommandRegisterExit(FALSE);\r
+          UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode());\r
+          DEBUG_CODE(InternalEfiShellSetEnv(L"DebugLasterror", LeString, TRUE););\r
+          InternalEfiShellSetEnv(L"Lasterror", LeString, TRUE);\r
+\r
+          ShellCommandRegisterExit(FALSE, 0);\r
           Status = EFI_SUCCESS;\r
           break;\r
         }\r
+        if (ShellGetExecutionBreakFlag()) {\r
+          break;\r
+        }\r
         if (EFI_ERROR(Status)) {\r
           break;\r
         }\r