]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel3CommandsLib/Echo.c
ShellPkg: Fix echo to support displaying special characters
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel3CommandsLib / Echo.c
index a638de8ce22b13873265569d2b30a18548ebd02c..c98ee8500bbc247572e005a88fdd1c9e72d4987f 100644 (file)
@@ -2,7 +2,7 @@
   Main file for Echo shell level 3 function.\r
 \r
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved. <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
 \r
 #include <Library/ShellLib.h>\r
 \r
-STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
-  {L"-on", TypeFlag},\r
-  {L"-off", TypeFlag},\r
-  {NULL, TypeMax}\r
-  };\r
-\r
 /**\r
   Function for 'echo' command.\r
 \r
@@ -36,86 +30,73 @@ ShellCommandRunEcho (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  EFI_STATUS          Status;\r
-  LIST_ENTRY          *Package;\r
-  SHELL_STATUS        ShellStatus;\r
-  UINTN               ParamCount;\r
-  CHAR16              *ProblemParam;\r
+  CHAR16              *RawCmdLine;\r
+  SHELL_STATUS        Status;\r
   UINTN               Size;\r
-  CHAR16              *PrintString;\r
-\r
-  Size                = 0;\r
-  ProblemParam        = NULL;\r
-  PrintString         = NULL;\r
-  ShellStatus         = SHELL_SUCCESS;\r
-\r
-  //\r
-  // initialize the shell lib (we must be in non-auto-init...)\r
-  //\r
-  Status = ShellInitialize();\r
-  ASSERT_EFI_ERROR(Status);\r
-\r
-  //\r
-  // parse the command line\r
-  //\r
-  Status = ShellCommandLineParseEx (ParamList, &Package, &ProblemParam, TRUE, TRUE);\r
-  if (EFI_ERROR(Status)) {\r
-    if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel3HiiHandle, L"echo", ProblemParam);  \r
-      FreePool(ProblemParam);\r
-      ShellStatus = SHELL_INVALID_PARAMETER;\r
-    } else {\r
-      ASSERT(FALSE);\r
-    }\r
-  } else {\r
-    //\r
-    // check for "-?"\r
-    //\r
-    if (ShellCommandLineGetFlag(Package, L"-?")) {\r
-      ASSERT(FALSE);\r
+  CHAR16              *Walker;\r
+  CHAR16              *TempParameter;\r
+  BOOLEAN             OnFlag;\r
+  BOOLEAN             OffFlag;\r
+  UINTN               Count;\r
+\r
+  RawCmdLine = ShellGetRawCmdLine ();\r
+  if (RawCmdLine == NULL) {\r
+    return SHELL_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  OnFlag  = FALSE;\r
+  OffFlag = FALSE;\r
+\r
+  Size = StrSize (RawCmdLine);\r
+  TempParameter  = AllocateZeroPool(Size);\r
+  if (TempParameter == NULL) {\r
+    Status = SHELL_OUT_OF_RESOURCES;\r
+    goto Done;\r
+  }\r
+\r
+  for ( Count = 0\r
+      , Walker = RawCmdLine\r
+      ; Walker != NULL && *Walker != CHAR_NULL\r
+      ; Count++\r
+      ) {\r
+    if (EFI_ERROR (ShellGetNextParameter (&Walker, TempParameter, Size, FALSE))) {\r
+      break;\r
     }\r
-    if (ShellCommandLineGetFlag(Package, L"-on")) {\r
-      //\r
-      // Turn it on\r
-      //\r
-      ShellCommandSetEchoState(TRUE);\r
-    } else if (ShellCommandLineGetFlag(Package, L"-off")) {\r
-      //\r
-      // turn it off\r
-      //\r
-      ShellCommandSetEchoState(FALSE);\r
-    } else if (ShellCommandLineGetRawValue(Package, 1) == NULL) {\r
-      //\r
-      // output its current state\r
-      //\r
-      if (ShellCommandGetEchoState()) {\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ECHO_ON), gShellLevel3HiiHandle);\r
-      } else {\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ECHO_OFF), gShellLevel3HiiHandle);\r
+\r
+    if (Count == 1) {\r
+      if (gUnicodeCollation->StriColl(gUnicodeCollation, TempParameter, L"-on") == 0 ) {\r
+        OnFlag = TRUE;\r
       }\r
-    } else {\r
-      //\r
-      // print the line\r
-      //\r
-      for ( ParamCount = 1\r
-          ; ShellCommandLineGetRawValue(Package, ParamCount) != NULL\r
-          ; ParamCount++\r
-         ) {\r
-        StrnCatGrow(&PrintString, &Size, ShellCommandLineGetRawValue(Package, ParamCount), 0);\r
-        if (ShellCommandLineGetRawValue(Package, ParamCount+1) != NULL) {\r
-          StrnCatGrow(&PrintString, &Size, L" ", 0);\r
-        } \r
+      if (gUnicodeCollation->StriColl(gUnicodeCollation, TempParameter, L"-off") == 0 ) {\r
+        OffFlag = TRUE;\r
       }\r
-      ShellPrintEx(-1, -1, L"%s\r\n", PrintString);\r
-      SHELL_FREE_NON_NULL(PrintString);\r
+    }\r
+  }\r
+\r
+  if (OnFlag || OffFlag) {\r
+    if (Count != 2) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ECHO_INVALID_PARAM), gShellLevel3HiiHandle, L"echo", L"-on/-off");\r
+      Status = SHELL_INVALID_PARAMETER;\r
+      goto Done;\r
     }\r
 \r
-    //\r
-    // free the command line package\r
-    //\r
-    ShellCommandLineFreeVarList (Package);\r
+    ShellCommandSetEchoState(OnFlag);\r
+    Status = SHELL_SUCCESS;\r
+    goto Done;\r
   }\r
 \r
-  return (ShellStatus);\r
+  Walker = RawCmdLine + StrLen (L"echo");\r
+  if  (*Walker != CHAR_NULL) {\r
+    Walker++;\r
+    ShellPrintEx (-1, -1, L"%s\r\n", Walker);\r
+  }\r
+\r
+  Status = SHELL_SUCCESS;\r
+\r
+Done:\r
+  SHELL_FREE_NON_NULL (TempParameter);\r
+  SHELL_FREE_NON_NULL (RawCmdLine);\r
+  return Status;\r
+\r
 }\r
 \r