]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Fix echo to support displaying special characters
authorRuiyu Ni <ruiyu.ni@intel.com>
Mon, 18 Jul 2016 09:50:20 +0000 (17:50 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Wed, 20 Jul 2016 02:28:00 +0000 (10:28 +0800)
Run 'echo -t' without the patch will get the result:
  echo: Unknown flag - '-t'
  The expected result is to display '-t' literally.
  This patch adds special handle for 'echo'. 'echo' will not use the
  general parameter parsing library.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
ShellPkg/Library/UefiShellLevel3CommandsLib/Echo.c
ShellPkg/Library/UefiShellLevel3CommandsLib/UefiShellLevel3CommandsLib.uni

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
index fc9c5d447ccc3a13fd69862f9e2eed847c9cfa0d..eeb870a395e3506b1e52abced7704729f28c9896 100644 (file)
@@ -1,7 +1,7 @@
 // /**\r
 //\r
 // (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>\r
-// Copyright (c) 2009 - 2013, 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
@@ -55,6 +55,7 @@
 \r
 #string STR_ECHO_ON               #language en-US "Echo is on.\r\n"\r
 #string STR_ECHO_OFF              #language en-US "Echo is off.\r\n"\r
+#string STR_ECHO_INVALID_PARAM    #language en-US "%H%s%N: Invalid  - too many parameters after '%s'\r\n"\r
 \r
 #string STR_PAUSE_PROMPT          #language en-US "Enter 'q' to quit, any other key to continue:\r\n"\r
 \r