]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/Shell.c
Add missing braces around initializer.
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
index a56967189bba7ca7e4b7c09e55ac983a18c0a26f..3e4706c288dbd2cbac12ce28af1c34fbbfb73e10 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   This is THE shell (application)\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2012, 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
@@ -23,22 +23,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,12 +52,17 @@ 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
   NULL,\r
-  NULL\r
+  FALSE\r
 };\r
 \r
 STATIC CONST CHAR16 mScriptExtension[]      = L".NSH";\r
@@ -155,17 +162,11 @@ UefiMain (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  EFI_STATUS  Status;\r
-  CHAR16      *TempString;\r
-  UINTN       Size;\r
-//    EFI_INPUT_KEY                 Key;\r
-\r
-//  gST->ConOut->OutputString(gST->ConOut, L"ReadKeyStroke Calling\r\n");\r
-//\r
-//  Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key);\r
-//\r
-//  gST->ConOut->OutputString(gST->ConOut, L"ReadKeyStroke Done\r\n");\r
-//  gBS->Stall (1000000);\r
+  EFI_STATUS                      Status;\r
+  CHAR16                          *TempString;\r
+  UINTN                           Size;\r
+  EFI_HANDLE                      ConInHandle;\r
+  EFI_SIMPLE_TEXT_INPUT_PROTOCOL  *OldConIn;\r
 \r
   if (PcdGet8(PcdShellSupportLevel) > 3) {\r
     return (EFI_UNSUPPORTED);\r
@@ -361,6 +362,18 @@ UefiMain (
         Status = InternalEfiShellStartCtrlSMonitor();\r
       }\r
 \r
+      if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) {\r
+        //\r
+        // close off the gST->ConIn\r
+        //\r
+        OldConIn      = gST->ConIn;\r
+        ConInHandle   = gST->ConsoleInHandle;\r
+        gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle);\r
+      } else {\r
+        OldConIn      = NULL;\r
+        ConInHandle   = NULL;\r
+      }\r
+\r
       if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) {\r
         //\r
         // process the startup script or launch the called app.\r
@@ -399,6 +412,11 @@ UefiMain (
           Status = DoShellPrompt();\r
         } while (!ShellCommandGetExit());\r
       }\r
+      if (OldConIn != NULL && ConInHandle != NULL) {\r
+        CloseSimpleTextInOnFile (gST->ConIn);\r
+        gST->ConIn            = OldConIn;\r
+        gST->ConsoleInHandle  = ConInHandle;\r
+      }\r
     }\r
   }\r
 \r
@@ -463,6 +481,9 @@ UefiMain (
     DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
   }\r
 \r
+  if (ShellCommandGetExit()) {\r
+    return ((EFI_STATUS)ShellCommandGetExitCode());\r
+  }\r
   return (Status);\r
 }\r
 \r
@@ -548,16 +569,14 @@ IsScriptOnlyCommand(
   return (FALSE);\r
 }\r
 \r
-\r
-\r
 /**\r
   This function will populate the 2 device path protocol parameters based on the\r
   global gImageHandle.  The DevPath will point to the device path for the handle that has\r
   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
@@ -598,6 +617,11 @@ GetDevicePathsForImageAndFile (
     if (!EFI_ERROR (Status)) {\r
       *DevPath  = DuplicateDevicePath (ImageDevicePath);\r
       *FilePath = DuplicateDevicePath (LoadedImage->FilePath);\r
+      gBS->CloseProtocol(\r
+                  LoadedImage->DeviceHandle,\r
+                  &gEfiDevicePathProtocolGuid,\r
+                  gImageHandle,\r
+                  NULL);\r
     }\r
     gBS->CloseProtocol(\r
                 gImageHandle,\r
@@ -620,6 +644,7 @@ STATIC CONST SHELL_PARAM_ITEM mShellParamList[] = {
   {L"-delay",         TypeValue},\r
   {NULL, TypeMax}\r
   };\r
+\r
 /**\r
   Process all Uefi Shell 2.0 command line options.\r
 \r
@@ -808,6 +833,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
@@ -822,6 +848,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
@@ -838,15 +865,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
-    ChopLastSlash(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
@@ -1000,10 +1031,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
@@ -1196,7 +1227,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
@@ -1217,7 +1257,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
@@ -1277,6 +1316,7 @@ RunCommand(
   )\r
 {\r
   EFI_STATUS                Status;\r
+  EFI_STATUS                StatusCode;\r
   CHAR16                    *CommandName;\r
   SHELL_STATUS              ShellStatus;\r
   UINTN                     Argc;\r
@@ -1315,6 +1355,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
@@ -1400,6 +1443,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
@@ -1518,11 +1564,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)*sizeof(LeString[0]), L"0x%08x", 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
@@ -1603,8 +1664,10 @@ RunScriptFileHandle (
   SCRIPT_COMMAND_LIST *LastCommand;\r
   BOOLEAN             Ascii;\r
   BOOLEAN             PreScriptEchoState;\r
+  BOOLEAN             PreCommandEchoState;\r
   CONST CHAR16        *CurDir;\r
   UINTN               LineCount;\r
+  CHAR16              LeString[50];\r
 \r
   ASSERT(!ShellCommandGetScriptExit());\r
 \r
@@ -1781,23 +1844,45 @@ 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
+          if (CommandLine3[0] == L'@') {\r
+            //\r
+            // We need to save the current echo state\r
+            // and disable echo for just this command.\r
+            //\r
+            PreCommandEchoState = ShellCommandGetEchoState();\r
+            ShellCommandSetEchoState(FALSE);\r
+            Status = RunCommand(CommandLine3+1);\r
+\r
+            //\r
+            // Now restore the pre-'@' echo state.\r
+            //\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
-            ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2);\r
+            Status = RunCommand(CommandLine3);\r
           }\r
-          Status = RunCommand(CommandLine3);\r
         }\r
 \r
         if (ShellCommandGetScriptExit()) {\r
-          ShellCommandRegisterExit(FALSE);\r
+          UnicodeSPrint(LeString, sizeof(LeString)*sizeof(LeString[0]), 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