]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/FileHandleWrappers.c
ShellPkg: Remove trailing \r\n when redirect to env variable (EX: use ">v")
[mirror_edk2.git] / ShellPkg / Application / Shell / FileHandleWrappers.c
index 2ca13cb3f3f73ad482ca52c6e89bfa65bc69b6d0..ef8293c1de511914e690d5b1dc3028e6a2585a9e 100644 (file)
@@ -3,6 +3,7 @@
   StdIn, StdOut, StdErr, etc...).\r
 \r
   Copyright (c) 2009 - 2011, 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
@@ -950,8 +951,48 @@ FileInterfaceEnvClose(
   IN EFI_FILE_PROTOCOL *This\r
   )\r
 {\r
+  VOID*       NewBuffer;\r
+  UINTN       NewSize;\r
+  EFI_STATUS  Status;\r
+\r
+  //\r
+  // Most if not all UEFI commands will have an '\r\n' at the end of any output. \r
+  // Since the output was redirected to a variable, it does not make sense to \r
+  // keep this.  So, before closing, strip the trailing '\r\n' from the variable\r
+  // if it exists.\r
+  //\r
+  NewBuffer   = NULL;\r
+  NewSize     = 0;\r
+\r
+  Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    NewBuffer = AllocateZeroPool(NewSize + sizeof(CHAR16));\r
+    if (NewBuffer == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }  \r
+    Status = SHELL_GET_ENVIRONMENT_VARIABLE(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, &NewSize, NewBuffer);\r
+  }\r
+  \r
+  if (!EFI_ERROR(Status)) {\r
+    \r
+    if (StrSize(NewBuffer) > 6)\r
+    {\r
+      if ((((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 2] == CHAR_LINEFEED) \r
+           && (((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] == CHAR_CARRIAGE_RETURN)) {\r
+        ((CHAR16*)NewBuffer)[(StrSize(NewBuffer)/2) - 3] = CHAR_NULL;   \r
+      }\r
+    \r
+      if (IsVolatileEnv(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name)) {\r
+        Status = SHELL_SET_ENVIRONMENT_VARIABLE_V(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
+      } else {\r
+        Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(((EFI_FILE_PROTOCOL_ENVIRONMENT*)This)->Name, StrSize(NewBuffer), NewBuffer);\r
+      }\r
+    }\r
+  } \r
+  \r
+  SHELL_FREE_NON_NULL(NewBuffer);\r
   FreePool((EFI_FILE_PROTOCOL_ENVIRONMENT*)This);\r
-  return (EFI_SUCCESS);\r
+  return (Status);\r
 }\r
 \r
 /**\r