]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Fixes for the ‘type’ command:
authorChris Phillips <chrisp@hp.com>
Fri, 18 Oct 2013 20:53:06 +0000 (20:53 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 18 Oct 2013 20:53:06 +0000 (20:53 +0000)
- Better handling to skip byte order mark in Unicode files.
- Only display valid ASCII characters.
- Change to use ShellPrintEx() instead of Print().
- Print each character instead of %s to avoid possible overrun when not NULL terminated.
- Check for ExecutionBreak.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Chris Phillips <chrisp@hp.com>
reviewed-by: Jaben Carsey <jaben.carsey@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14788 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Library/UefiShellLevel3CommandsLib/Type.c

index 50974c3fb9499534ba2d100a70bcd525e934fd47..dffefd1234d2d5e794d2538ddb7c42211c64d83b 100644 (file)
@@ -1,6 +1,7 @@
 /** @file\r
   Main file for Type shell level 3 function.\r
 \r
+  Copyright (c) 2013, Hewlett-Packard Development Company, L.P.\r
   Copyright (c) 2009 - 2011, 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
 EFI_STATUS\r
 EFIAPI\r
 TypeFileByHandle (\r
-  IN EFI_HANDLE Handle,\r
-  BOOLEAN Ascii,\r
-  BOOLEAN UCS2\r
+  IN SHELL_FILE_HANDLE Handle,\r
+  IN BOOLEAN Ascii,\r
+  IN BOOLEAN UCS2\r
   )\r
 {\r
   UINTN       ReadSize;\r
   VOID        *Buffer;\r
+  VOID        *AllocatedBuffer;\r
   EFI_STATUS  Status;\r
   UINTN       LoopVar;\r
+  UINTN       LoopSize;\r
   CHAR16      AsciiChar;\r
+  CHAR16      Ucs2Char;\r
 \r
   ReadSize = PcdGet32(PcdShellFileOperationSize);\r
-  Buffer = AllocateZeroPool(ReadSize);\r
-  if (Buffer == NULL) {\r
+  AllocatedBuffer = AllocateZeroPool(ReadSize);\r
+  if (AllocatedBuffer == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
   Status = ShellSetFilePosition(Handle, 0);\r
   ASSERT_EFI_ERROR(Status);\r
 \r
-  while (ReadSize == ((UINTN)PcdGet32(PcdShellFileOperationSize))){\r
+  while (ReadSize == ((UINTN)PcdGet32(PcdShellFileOperationSize))) {\r
+    Buffer = AllocatedBuffer;\r
     ZeroMem(Buffer, ReadSize);\r
     Status = ShellReadFile(Handle, &ReadSize, Buffer);\r
     if (EFI_ERROR(Status)){\r
       break;\r
     }\r
 \r
-    if (!(Ascii|UCS2)){\r
+    if (!(Ascii|UCS2)) {\r
       if (*(UINT16*)Buffer == gUnicodeFileTag) {\r
         UCS2 = TRUE;\r
-        Buffer = ((UINT16*)Buffer) + 1;\r
       } else {\r
         Ascii = TRUE;\r
       }\r
     }\r
 \r
-    //\r
-    // We want to use plain Print function here! (no color support for files)\r
-    //\r
-    if (Ascii){\r
-      for (LoopVar = 0 ; LoopVar < ReadSize ; LoopVar++) {\r
-      AsciiChar = CHAR_NULL;\r
-      AsciiChar = ((CHAR8*)Buffer)[LoopVar];\r
-      if (AsciiChar == CHAR_NULL) {\r
-        AsciiChar = '.';\r
-      }\r
-      Print(L"%c", AsciiChar);\r
+    if (Ascii) {\r
+      LoopSize = ReadSize;\r
+      for (LoopVar = 0 ; LoopVar < LoopSize ; LoopVar++) {\r
+        //\r
+        // The valid range of ASCII characters is 0x20-0x7E.\r
+        // Display "." when there is an invalid character.\r
+        //\r
+        AsciiChar = CHAR_NULL;\r
+        AsciiChar = ((CHAR8*)Buffer)[LoopVar];\r
+        if (AsciiChar == '\r' || AsciiChar == '\n') {\r
+          //\r
+          // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)\r
+          // characters to be displayed as is.\r
+          // \r
+          if (AsciiChar == '\n' && ((CHAR8*)Buffer)[LoopVar-1] != '\r') {\r
+            //\r
+            // In case Line Feed (0xA) is encountered & Carriage Return (0xD)\r
+            // was not the previous character, print CR and LF. This is because\r
+            // Shell 2.0 requires carriage return with line feed for displaying\r
+            // each new line from left.\r
+            //\r
+            ShellPrintEx (-1, -1, L"\r\n");\r
+            continue;\r
+          }\r
+        } else {\r
+          //\r
+          // For all other characters which are not printable, display '.'\r
+          //\r
+          if (AsciiChar < 0x20 || AsciiChar >= 0x7F) {\r
+            AsciiChar = '.';\r
+          }\r
+        }\r
+        ShellPrintEx (-1, -1, L"%c", AsciiChar);\r
       }\r
     } else {\r
-      Print(L"%s", Buffer);\r
+      if (*(UINT16*)Buffer == gUnicodeFileTag) {\r
+        //\r
+        // For unicode files, skip displaying the byte order marker.\r
+        //\r
+        Buffer = ((UINT16*)Buffer) + 1;\r
+        LoopSize = (ReadSize / (sizeof (CHAR16))) - 1;\r
+      } else {\r
+        LoopSize = ReadSize / (sizeof (CHAR16));\r
+      }\r
+      \r
+      for (LoopVar = 0 ; LoopVar < LoopSize ; LoopVar++) {\r
+        //\r
+        // An invalid range of characters is 0x0-0x1F.\r
+        // Display "." when there is an invalid character.\r
+        //\r
+        Ucs2Char = CHAR_NULL;\r
+        Ucs2Char = ((CHAR16*)Buffer)[LoopVar];\r
+        if (Ucs2Char == '\r' || Ucs2Char == '\n') {\r
+          //\r
+          // Allow Line Feed (LF) (0xA) & Carriage Return (CR) (0xD)\r
+          // characters to be displayed as is.\r
+          // \r
+          if (Ucs2Char == '\n' && ((CHAR16*)Buffer)[LoopVar-1] != '\r') {\r
+            //\r
+            // In case Line Feed (0xA) is encountered & Carriage Return (0xD)\r
+            // was not the previous character, print CR and LF. This is because\r
+            // Shell 2.0 requires carriage return with line feed for displaying\r
+            // each new line from left.\r
+            //\r
+            ShellPrintEx (-1, -1, L"\r\n");\r
+            continue;\r
+          }\r
+        } \r
+        else if (Ucs2Char < 0x20) {\r
+          //\r
+          // For all other characters which are not printable, display '.'\r
+          //\r
+          Ucs2Char = L'.';\r
+        }\r
+        ShellPrintEx (-1, -1, L"%c", Ucs2Char);\r
+      }\r
+    }\r
+\r
+    if (ShellGetExecutionBreakFlag()) {\r
+      break;\r
     }\r
   }\r
-  Print(L"\r\n", Buffer);\r
+  FreePool (AllocatedBuffer);\r
+  ShellPrintEx (-1, -1, L"\r\n");\r
   return (Status);\r
 }\r
 \r
@@ -196,6 +267,11 @@ ShellCommandRunType (
                 ; !IsNull(&FileList->Link, &Node->Link) && !ShellGetExecutionBreakFlag()\r
                 ; Node = (EFI_SHELL_FILE_INFO*)GetNextNode(&FileList->Link, &Node->Link)\r
                ){\r
+\r
+              if (ShellGetExecutionBreakFlag()) {\r
+                break;\r
+              }\r
+\r
               //\r
               // make sure the file opened ok\r
               //\r
@@ -217,7 +293,7 @@ ShellCommandRunType (
               //\r
               // do it\r
               //\r
-              Status = TypeFileByHandle(Node->Handle, AsciiMode, UnicodeMode);\r
+              Status = TypeFileByHandle (Node->Handle, AsciiMode, UnicodeMode);\r
               if (EFI_ERROR(Status)) {\r
                 ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_TYP_ERROR), gShellLevel3HiiHandle, Node->FileName, Status);\r
                 ShellStatus = SHELL_INVALID_PARAMETER;\r