]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg: Use DOS format end of line.
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index c8ac00bc813b4f4082d30d6731794e0ad46f5832..b3767bbbf2b9b7d407a896b2d188f08cbda27dd8 100644 (file)
@@ -2,7 +2,9 @@
   Member functions of EFI_SHELL_PARAMETERS_PROTOCOL and functions for creation,\r
   manipulation, and initialization of EFI_SHELL_PARAMETERS_PROTOCOL.\r
 \r
-  Copyright (c) 2009 - 2012, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (C) 2014, Red Hat, Inc.\r
+  (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<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
 **/\r
 \r
-#include "ShellParametersProtocol.h"\r
-#include "ConsoleWrappers.h"\r
+#include "Shell.h"\r
+\r
+BOOLEAN AsciiRedirection = FALSE;\r
 \r
 /**\r
-  return the next parameter from a command line string;\r
+  Return the next parameter's end from a command line string.\r
+\r
+  @param[in] String        the string to parse\r
+**/\r
+CONST CHAR16*\r
+EFIAPI\r
+FindEndOfParameter(\r
+  IN CONST CHAR16 *String\r
+  )\r
+{\r
+  CONST CHAR16 *First;\r
+  CONST CHAR16 *CloseQuote;\r
+\r
+  First = FindFirstCharacter(String, L" \"", L'^');\r
+\r
+  //\r
+  // nothing, all one parameter remaining\r
+  //\r
+  if (*First == CHAR_NULL) {\r
+    return (First);\r
+  }\r
+\r
+  //\r
+  // If space before a quote (or neither found, i.e. both CHAR_NULL),\r
+  // then that's the end.\r
+  //\r
+  if (*First == L' ') {\r
+    return (First);\r
+  }\r
+\r
+  CloseQuote = FindFirstCharacter (First+1, L"\"", L'^');\r
+\r
+  //\r
+  // We did not find a terminator...\r
+  //\r
+  if (*CloseQuote == CHAR_NULL) {\r
+    return (NULL);\r
+  }\r
+\r
+  return (FindEndOfParameter (CloseQuote+1));\r
+}\r
+\r
+/**\r
+  Return the next parameter from a command line string.\r
 \r
   This function moves the next parameter from Walker into TempParameter and moves\r
   Walker up past that parameter for recursive calling.  When the final parameter\r
   Temp Parameter must be large enough to hold the parameter before calling this\r
   function.\r
 \r
-  @param[in, out] Walker        pointer to string of command line.  Adjusted to\r
-                                reminaing command line on return\r
-  @param[in, out] TempParameter pointer to string of command line item extracted.\r
+  This will also remove all remaining ^ characters after processing.\r
+\r
+  @param[in, out] Walker          pointer to string of command line.  Adjusted to\r
+                                  reminaing command line on return\r
+  @param[in, out] TempParameter   pointer to string of command line item extracted.\r
+  @param[in]      Length          buffer size of TempParameter.\r
+  @param[in]      StripQuotation  if TRUE then strip the quotation marks surrounding\r
+                                  the parameters.\r
 \r
+  @return   EFI_INALID_PARAMETER  A required parameter was NULL or pointed to a NULL or empty string.\r
+  @return   EFI_NOT_FOUND         A closing " could not be found on the specified string\r
 **/\r
-VOID\r
+EFI_STATUS\r
 EFIAPI\r
 GetNextParameter(\r
-  CHAR16 **Walker,\r
-  CHAR16 **TempParameter\r
+  IN OUT CHAR16   **Walker,\r
+  IN OUT CHAR16   **TempParameter,\r
+  IN CONST UINTN  Length,\r
+  IN BOOLEAN      StripQuotation\r
   )\r
 {\r
-  CHAR16 *NextDelim;\r
-  CHAR16 *TempLoc;\r
+  CONST CHAR16 *NextDelim;\r
+\r
+  if (Walker           == NULL\r
+    ||*Walker          == NULL\r
+    ||TempParameter    == NULL\r
+    ||*TempParameter   == NULL\r
+    ){\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
-  ASSERT(Walker           != NULL);\r
-  ASSERT(*Walker          != NULL);\r
-  ASSERT(TempParameter    != NULL);\r
-  ASSERT(*TempParameter   != NULL);\r
 \r
   //\r
   // make sure we dont have any leading spaces\r
@@ -57,77 +115,64 @@ GetNextParameter(
   // make sure we still have some params now...\r
   //\r
   if (StrLen(*Walker) == 0) {\r
-    ASSERT((*Walker)[0] == CHAR_NULL);\r
-    *Walker = NULL;\r
-    return;\r
+DEBUG_CODE_BEGIN();\r
+    *Walker        = NULL;\r
+DEBUG_CODE_END();\r
+    return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
+  NextDelim = FindEndOfParameter(*Walker);\r
+\r
+  if (NextDelim == NULL){\r
+DEBUG_CODE_BEGIN();\r
+    *Walker        = NULL;\r
+DEBUG_CODE_END();\r
+    return (EFI_NOT_FOUND);\r
+  }\r
+\r
+  StrnCpyS(*TempParameter, Length / sizeof(CHAR16), (*Walker), NextDelim - *Walker);\r
+\r
   //\r
-  // we have a quoted parameter\r
-  // could be the last parameter, but SHOULD have a trailing quote\r
+  // Add a CHAR_NULL if we didnt get one via the copy\r
   //\r
-  if ((*Walker)[0] == L'\"') {\r
-    NextDelim = NULL;\r
-    for (TempLoc = *Walker + 1 ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
-      if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
-        TempLoc++;\r
-      } else if (*TempLoc == L'\"') {\r
-        NextDelim = TempLoc;\r
-        break;\r
-      }\r
-    }\r
+  if (*NextDelim != CHAR_NULL) {\r
+    (*TempParameter)[NextDelim - *Walker] = CHAR_NULL;\r
+  }\r
+\r
+  //\r
+  // Update Walker for the next iteration through the function\r
+  //\r
+  *Walker = (CHAR16*)NextDelim;\r
+\r
+  //\r
+  // Remove any non-escaped quotes in the string\r
+  // Remove any remaining escape characters in the string\r
+  //\r
+  for (NextDelim = FindFirstCharacter(*TempParameter, L"\"^", CHAR_NULL) \r
+    ; *NextDelim != CHAR_NULL \r
+    ; NextDelim = FindFirstCharacter(NextDelim, L"\"^", CHAR_NULL)\r
+    ) {\r
+    if (*NextDelim == L'^') {\r
 \r
-    if (NextDelim - ((*Walker)+1) == 0) {\r
-      //\r
-      // found ""\r
-      //\r
-      StrCpy(*TempParameter, L"");\r
-      *Walker = NextDelim + 1;\r
-    } else if (NextDelim != NULL) {\r
-      StrnCpy(*TempParameter, (*Walker)+1, NextDelim - ((*Walker)+1));\r
-      *Walker = NextDelim + 1;\r
-    } else {\r
       //\r
-      // last one... someone forgot the training quote!\r
+      // eliminate the escape ^\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
-      *Walker = NULL;\r
-    }\r
-    for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
-      if (*TempLoc == L'^' && *(TempLoc+1) == L'\"') {\r
-        CopyMem(TempLoc, TempLoc+1, StrSize(TempLoc) - sizeof(TempLoc[0]));\r
-      }\r
-    }\r
-  } else {\r
-    //\r
-    // we have a regular parameter (no quote) OR\r
-    // we have the final parameter (no trailing space)\r
-    //\r
-    NextDelim = StrStr((*Walker), L" ");\r
-    if (NextDelim != NULL) {\r
-      StrnCpy(*TempParameter, *Walker, NextDelim - (*Walker));\r
-      (*TempParameter)[NextDelim - (*Walker)] = CHAR_NULL;\r
-      *Walker = NextDelim+1;\r
-    } else {\r
+      CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r
+      NextDelim++;\r
+    } else if (*NextDelim == L'\"') {\r
+\r
       //\r
-      // last one.\r
+      // eliminate the unescaped quote\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
-      *Walker = NULL;\r
-    }\r
-    for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) {\r
-      if (*NextDelim == L'^' && *(NextDelim+1) == L'^') {\r
-        CopyMem(NextDelim, NextDelim+1, StrSize(NextDelim) - sizeof(NextDelim[0]));\r
-      }\r
-    }\r
-    while ((*TempParameter)[StrLen(*TempParameter)-1] == L' ') {\r
-      (*TempParameter)[StrLen(*TempParameter)-1] = CHAR_NULL;\r
-    }\r
-    while ((*TempParameter)[0] == L' ') {\r
-      CopyMem(*TempParameter, (*TempParameter)+1, StrSize(*TempParameter) - sizeof((*TempParameter)[0]));\r
+      if (StripQuotation) {\r
+        CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r
+         } else{\r
+        NextDelim++;\r
+         }\r
     }\r
   }\r
-  return;\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -137,9 +182,14 @@ GetNextParameter(
   parameters for inclusion in EFI_SHELL_PARAMETERS_PROTOCOL.  this supports space\r
   delimited and quote surrounded parameter definition.\r
 \r
-  @param[in] CommandLine         String of command line to parse\r
-  @param[in, out] Argv           pointer to array of strings; one for each parameter\r
-  @param[in, out] Argc           pointer to number of strings in Argv array\r
+  All special character processing (alias, environment variable, redirection, \r
+  etc... must be complete before calling this API.\r
+\r
+  @param[in] CommandLine          String of command line to parse\r
+  @param[in] StripQuotation       if TRUE then strip the quotation marks surrounding\r
+                                  the parameters.\r
+  @param[in, out] Argv            pointer to array of strings; one for each parameter\r
+  @param[in, out] Argc            pointer to number of strings in Argv array\r
 \r
   @return EFI_SUCCESS           the operation was sucessful\r
   @return EFI_OUT_OF_RESOURCES  a memory allocation failed.\r
@@ -148,15 +198,18 @@ EFI_STATUS
 EFIAPI\r
 ParseCommandLineToArgs(\r
   IN CONST CHAR16 *CommandLine,\r
-  IN OUT CHAR16 ***Argv,\r
-  IN OUT UINTN *Argc\r
+  IN BOOLEAN      StripQuotation,\r
+  IN OUT CHAR16   ***Argv,\r
+  IN OUT UINTN    *Argc\r
   )\r
 {\r
   UINTN       Count;\r
   CHAR16      *TempParameter;\r
   CHAR16      *Walker;\r
   CHAR16      *NewParam;\r
+  CHAR16      *NewCommandLine;\r
   UINTN       Size;\r
+  EFI_STATUS  Status;\r
 \r
   ASSERT(Argc != NULL);\r
   ASSERT(Argv != NULL);\r
@@ -167,47 +220,62 @@ ParseCommandLineToArgs(
     return (EFI_SUCCESS);\r
   }\r
 \r
-  Size = StrSize(CommandLine);\r
+  NewCommandLine = AllocateCopyPool(StrSize(CommandLine), CommandLine);\r
+  if (NewCommandLine == NULL){\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
+\r
+  TrimSpaces(&NewCommandLine);\r
+  Size = StrSize(NewCommandLine);\r
   TempParameter = AllocateZeroPool(Size);\r
   if (TempParameter == NULL) {\r
+    SHELL_FREE_NON_NULL(NewCommandLine);\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
   for ( Count = 0\r
-      , Walker = (CHAR16*)CommandLine\r
+      , Walker = (CHAR16*)NewCommandLine\r
       ; Walker != NULL && *Walker != CHAR_NULL\r
-      ; GetNextParameter(&Walker, &TempParameter)\r
-      , Count++\r
-     );\r
-\r
-/*  Count = 0;\r
-  Walker = (CHAR16*)CommandLine;\r
-  while(Walker != NULL) {\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    Count++;\r
+      ; Count++\r
+      ) {\r
+    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size, TRUE))) {\r
+      break;\r
+    }\r
   }\r
-*/\r
+\r
   //\r
   // lets allocate the pointer array\r
   //\r
   (*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*));\r
   if (*Argv == NULL) {\r
-    return (EFI_OUT_OF_RESOURCES);\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Done;\r
   }\r
 \r
   *Argc = 0;\r
-  Walker = (CHAR16*)CommandLine;\r
+  Walker = (CHAR16*)NewCommandLine;\r
   while(Walker != NULL && *Walker != CHAR_NULL) {\r
     SetMem16(TempParameter, Size, CHAR_NULL);\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    NewParam = AllocateZeroPool(StrSize(TempParameter));\r
-    ASSERT(NewParam != NULL);\r
-    StrCpy(NewParam, TempParameter);\r
+    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size, StripQuotation))) {\r
+      Status = EFI_INVALID_PARAMETER;\r
+      goto Done;\r
+    }\r
+\r
+    NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter);\r
+    if (NewParam == NULL){\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Done;\r
+    }\r
     ((CHAR16**)(*Argv))[(*Argc)] = NewParam;\r
     (*Argc)++;\r
   }\r
   ASSERT(Count >= (*Argc));\r
-  return (EFI_SUCCESS);\r
+  Status = EFI_SUCCESS;\r
+  \r
+Done:\r
+  SHELL_FREE_NON_NULL(TempParameter);\r
+  SHELL_FREE_NON_NULL(NewCommandLine);\r
+  return (Status);\r
 }\r
 \r
 /**\r
@@ -289,10 +357,10 @@ CreatePopulateInstallShellParametersProtocol (
   //\r
   // Build the full command line\r
   //\r
-  Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, &FullCommandLine);\r
+  Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     FullCommandLine = AllocateZeroPool(Size + LoadedImage->LoadOptionsSize);\r
-    Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, &FullCommandLine);\r
+    Status = SHELL_GET_ENVIRONMENT_VARIABLE(L"ShellOpt", &Size, FullCommandLine);\r
   }\r
   if (Status == EFI_NOT_FOUND) {\r
     //\r
@@ -312,13 +380,12 @@ CreatePopulateInstallShellParametersProtocol (
     FullCommandLine = AllocateZeroPool(Size);\r
   }\r
   if (FullCommandLine != NULL) {\r
-    if (LoadedImage->LoadOptionsSize != 0){\r
-      StrCpy(FullCommandLine, LoadedImage->LoadOptions);\r
-    }\r
+    CopyMem (FullCommandLine, LoadedImage->LoadOptions, LoadedImage->LoadOptionsSize);\r
     //\r
     // Populate Argc and Argv\r
     //\r
     Status = ParseCommandLineToArgs(FullCommandLine,\r
+                                    TRUE,\r
                                     &(*NewShellParameters)->Argv,\r
                                     &(*NewShellParameters)->Argc);\r
 \r
@@ -538,6 +605,90 @@ FixFileName (
   return (Copy);\r
 }\r
 \r
+/**\r
+  Fix a string to only have the environment variable name, removing starting at the first space of whatever is quoted and removing the leading and trailing %.\r
+\r
+  @param[in]  FileName    The filename to start with.\r
+\r
+  @retval NULL  FileName was invalid.\r
+  @return       The modified FileName.\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FixVarName (\r
+  IN CHAR16 *FileName\r
+  )\r
+{\r
+  CHAR16  *Copy;\r
+  CHAR16  *TempLocation;\r
+\r
+  Copy = FileName;\r
+\r
+  if (FileName[0] == L'%') {\r
+    Copy = FileName+1;\r
+    if ((TempLocation = StrStr(Copy , L"%")) != NULL) {\r
+      TempLocation[0] = CHAR_NULL;\r
+    }    \r
+  }\r
+\r
+  return (FixFileName(Copy));\r
+}\r
+\r
+/**\r
+  Remove the unicode file tag from the begining of the file buffer since that will not be\r
+  used by StdIn.\r
+  \r
+  @param[in]  Handle    Pointer to the handle of the file to be processed.\r
+  \r
+  @retval EFI_SUCCESS   The unicode file tag has been moved successfully.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RemoveFileTag(\r
+  IN SHELL_FILE_HANDLE *Handle\r
+  )\r
+{\r
+  UINTN             CharSize;\r
+  CHAR16            CharBuffer;\r
+\r
+  CharSize    = sizeof(CHAR16);\r
+  CharBuffer  = 0;\r
+  gEfiShellProtocol->ReadFile(*Handle, &CharSize, &CharBuffer);\r
+  if (CharBuffer != gUnicodeFileTag) {\r
+    gEfiShellProtocol->SetFilePosition(*Handle, 0);\r
+  }\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+  Write the unicode file tag to the specified file.\r
+\r
+  It is the caller's responsibility to ensure that\r
+  ShellInfoObject.NewEfiShellProtocol has been initialized before calling this\r
+  function.\r
+\r
+  @param[in] FileHandle  The file to write the unicode file tag to.\r
+\r
+  @return  Status code from ShellInfoObject.NewEfiShellProtocol->WriteFile.\r
+**/\r
+EFI_STATUS\r
+WriteFileTag (\r
+  IN SHELL_FILE_HANDLE FileHandle\r
+  )\r
+{\r
+  CHAR16     FileTag;\r
+  UINTN      Size;\r
+  EFI_STATUS Status;\r
+\r
+  FileTag = gUnicodeFileTag;\r
+  Size = sizeof FileTag;\r
+  Status = ShellInfoObject.NewEfiShellProtocol->WriteFile (FileHandle, &Size,\r
+                                                  &FileTag);\r
+  ASSERT (EFI_ERROR (Status) || Size == sizeof FileTag);\r
+  return Status;\r
+}\r
+\r
+\r
 /**\r
   Funcion will replace the current StdIn and StdOut in the ShellParameters protocol\r
   structure by parsing NewCommandLine.  The current values are returned to the\r
@@ -583,12 +734,12 @@ UpdateStdInStdOutStdErr(
   BOOLEAN           OutAppend;\r
   BOOLEAN           ErrAppend;\r
   UINTN             Size;\r
-  CHAR16            TagBuffer[2];\r
   SPLIT_LIST        *Split;\r
   CHAR16            *FirstLocation;\r
 \r
   OutUnicode      = TRUE;\r
   InUnicode       = TRUE;\r
+  AsciiRedirection = FALSE;\r
   ErrUnicode      = TRUE;\r
   StdInVarName    = NULL;\r
   StdOutVarName   = NULL;\r
@@ -609,8 +760,8 @@ UpdateStdInStdOutStdErr(
   SystemTableInfo->ConInHandle    = gST->ConsoleInHandle;\r
   SystemTableInfo->ConOut         = gST->ConOut;\r
   SystemTableInfo->ConOutHandle   = gST->ConsoleOutHandle;\r
-  SystemTableInfo->ConErr         = gST->StdErr;\r
-  SystemTableInfo->ConErrHandle   = gST->StandardErrorHandle;\r
+  SystemTableInfo->ErrOut         = gST->StdErr;\r
+  SystemTableInfo->ErrOutHandle   = gST->StandardErrorHandle;\r
   *OldStdIn                       = ShellParameters->StdIn;\r
   *OldStdOut                      = ShellParameters->StdOut;\r
   *OldStdErr                      = ShellParameters->StdErr;\r
@@ -867,6 +1018,7 @@ UpdateStdInStdOutStdErr(
     } else {\r
       StdInFileName   = CommandLineWalker += 4;\r
       InUnicode       = FALSE;\r
+      AsciiRedirection = TRUE;\r
     }\r
     if (StrStr(CommandLineWalker, L" <a ") != NULL) {\r
       Status = EFI_NOT_FOUND;\r
@@ -888,7 +1040,7 @@ UpdateStdInStdOutStdErr(
   //\r
   // re-populate the string to support any filenames that were in quotes.\r
   //\r
-  StrCpy(CommandLineCopy, NewCommandLine);\r
+  StrnCpyS(CommandLineCopy, StrSize(CommandLineCopy)/sizeof(CHAR16), NewCommandLine, StrLen(NewCommandLine));\r
 \r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
     && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
@@ -914,17 +1066,17 @@ UpdateStdInStdOutStdErr(
       }\r
     }\r
     if (StdErrVarName  != NULL) {\r
-      if ((StdErrVarName     = FixFileName(StdErrVarName)) == NULL) {\r
+      if ((StdErrVarName     = FixVarName(StdErrVarName)) == NULL) {\r
         Status = EFI_INVALID_PARAMETER;\r
       }\r
     }\r
     if (StdOutVarName  != NULL) {\r
-      if ((StdOutVarName     = FixFileName(StdOutVarName)) == NULL) {\r
+      if ((StdOutVarName     = FixVarName(StdOutVarName)) == NULL) {\r
         Status = EFI_INVALID_PARAMETER;\r
       }\r
     }\r
     if (StdInVarName   != NULL) {\r
-      if ((StdInVarName      = FixFileName(StdInVarName)) == NULL) {\r
+      if ((StdInVarName      = FixVarName(StdInVarName)) == NULL) {\r
         Status = EFI_INVALID_PARAMETER;\r
       }\r
     }\r
@@ -995,13 +1147,7 @@ UpdateStdInStdOutStdErr(
         }\r
         Status = ShellOpenFileByName(StdErrFileName, &TempHandle, EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ|EFI_FILE_MODE_CREATE,0);\r
         if (!ErrAppend && ErrUnicode && !EFI_ERROR(Status)) {\r
-          //\r
-          // Write out the gUnicodeFileTag\r
-          //\r
-          Size = sizeof(CHAR16);\r
-          TagBuffer[0] = gUnicodeFileTag;\r
-          TagBuffer[1] = CHAR_NULL;\r
-          ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer);\r
+          Status = WriteFileTag (TempHandle);\r
         }\r
         if (!ErrUnicode && !EFI_ERROR(Status)) {\r
           TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
@@ -1009,7 +1155,7 @@ UpdateStdInStdOutStdErr(
         }\r
         if (!EFI_ERROR(Status)) {\r
           ShellParameters->StdErr = TempHandle;\r
-          gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle);\r
+          gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);\r
         }\r
       }\r
 \r
@@ -1030,20 +1176,20 @@ UpdateStdInStdOutStdErr(
           if (StrStr(StdOutFileName, L"NUL")==StdOutFileName) {\r
             //no-op\r
           } else if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) {\r
-            //\r
-            // Write out the gUnicodeFileTag\r
-            //\r
-            Size = sizeof(CHAR16);\r
-            TagBuffer[0] = gUnicodeFileTag;\r
-            TagBuffer[1] = CHAR_NULL;\r
-            ShellInfoObject.NewEfiShellProtocol->WriteFile(TempHandle, &Size, TagBuffer);\r
+            Status = WriteFileTag (TempHandle);\r
           } else if (OutAppend) {\r
-            //\r
-            // Move to end of file\r
-            //\r
             Status = ShellInfoObject.NewEfiShellProtocol->GetFileSize(TempHandle, &FileSize);\r
             if (!EFI_ERROR(Status)) {\r
-              Status = ShellInfoObject.NewEfiShellProtocol->SetFilePosition(TempHandle, FileSize);\r
+              //\r
+              // When appending to a new unicode file, write the file tag.\r
+              // Otherwise (ie. when appending to a new ASCII file, or an\r
+              // existent file with any encoding), just seek to the end.\r
+              //\r
+              Status = (FileSize == 0 && OutUnicode) ?\r
+                         WriteFileTag (TempHandle) :\r
+                         ShellInfoObject.NewEfiShellProtocol->SetFilePosition (\r
+                                                                TempHandle,\r
+                                                                FileSize);\r
             }\r
           }\r
           if (!OutUnicode && !EFI_ERROR(Status)) {\r
@@ -1052,7 +1198,7 @@ UpdateStdInStdOutStdErr(
           }\r
           if (!EFI_ERROR(Status)) {\r
             ShellParameters->StdOut = TempHandle;\r
-            gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle);\r
+            gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);\r
           }\r
         }\r
       }\r
@@ -1070,7 +1216,7 @@ UpdateStdInStdOutStdErr(
         TempHandle = CreateFileInterfaceEnv(StdOutVarName);\r
         ASSERT(TempHandle != NULL);\r
         ShellParameters->StdOut = TempHandle;\r
-        gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle);\r
+        gST->ConOut = CreateSimpleTextOutOnFile(TempHandle, &gST->ConsoleOutHandle, gST->ConOut);\r
       }\r
 \r
       //\r
@@ -1086,7 +1232,7 @@ UpdateStdInStdOutStdErr(
         TempHandle = CreateFileInterfaceEnv(StdErrVarName);\r
         ASSERT(TempHandle != NULL);\r
         ShellParameters->StdErr = TempHandle;\r
-        gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle);\r
+        gST->StdErr = CreateSimpleTextOutOnFile(TempHandle, &gST->StandardErrorHandle, gST->StdErr);\r
       }\r
 \r
       //\r
@@ -1119,10 +1265,13 @@ UpdateStdInStdOutStdErr(
           &TempHandle,\r
           EFI_FILE_MODE_READ,\r
           0);\r
-        if (!InUnicode && !EFI_ERROR(Status)) {\r
-          TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
-        }\r
         if (!EFI_ERROR(Status)) {\r
+          if (!InUnicode) {\r
+            //\r
+            // Create the ASCII->Unicode conversion layer\r
+            //\r
+            TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
+          }\r
           ShellParameters->StdIn = TempHandle;\r
           gST->ConIn = CreateSimpleTextInOnFile(TempHandle, &gST->ConsoleInHandle);\r
         }\r
@@ -1134,8 +1283,15 @@ UpdateStdInStdOutStdErr(
   CalculateEfiHdrCrc(&gST->Hdr);\r
 \r
   if (gST->ConIn == NULL ||gST->ConOut == NULL) {\r
-    return (EFI_OUT_OF_RESOURCES);\r
+    Status = EFI_OUT_OF_RESOURCES;\r
   }\r
+\r
+  if (Status == EFI_NOT_FOUND) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle);\r
+  } else if (EFI_ERROR(Status)) {\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle);\r
+  }\r
+\r
   return (Status);\r
 }\r
 \r
@@ -1200,10 +1356,10 @@ RestoreStdInStdOutStdErr (
     gST->ConOut               = SystemTableInfo->ConOut;\r
     gST->ConsoleOutHandle     = SystemTableInfo->ConOutHandle;\r
   }\r
-  if (gST->StdErr != SystemTableInfo->ConErr) {\r
+  if (gST->StdErr != SystemTableInfo->ErrOut) {\r
     CloseSimpleTextOutOnFile(gST->StdErr);\r
-    gST->StdErr               = SystemTableInfo->ConErr;\r
-    gST->StandardErrorHandle  = SystemTableInfo->ConErrHandle;\r
+    gST->StdErr               = SystemTableInfo->ErrOut;\r
+    gST->StandardErrorHandle  = SystemTableInfo->ErrOutHandle;\r
   }\r
 \r
   CalculateEfiHdrCrc(&gST->Hdr);\r
@@ -1219,6 +1375,7 @@ RestoreStdInStdOutStdErr (
 \r
   @param[in, out] ShellParameters        Pointer to parameter structure to modify.\r
   @param[in] NewCommandLine              The new command line to parse and use.\r
+  @param[in] Type                        The type of operation.\r
   @param[out] OldArgv                    Pointer to old list of parameters.\r
   @param[out] OldArgc                    Pointer to old number of items in Argv list.\r
 \r
@@ -1230,11 +1387,15 @@ EFIAPI
 UpdateArgcArgv(\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters,\r
   IN CONST CHAR16                       *NewCommandLine,\r
+  IN SHELL_OPERATION_TYPES              Type,\r
   OUT CHAR16                            ***OldArgv OPTIONAL,\r
   OUT UINTN                             *OldArgc OPTIONAL\r
   )\r
 {\r
+  BOOLEAN                 StripParamQuotation;\r
+  \r
   ASSERT(ShellParameters != NULL);\r
+  StripParamQuotation = TRUE;\r
 \r
   if (OldArgc != NULL) {\r
     *OldArgc = ShellParameters->Argc;\r
@@ -1243,7 +1404,15 @@ UpdateArgcArgv(
     *OldArgv = ShellParameters->Argv;\r
   }\r
 \r
-  return (ParseCommandLineToArgs(NewCommandLine, &(ShellParameters->Argv), &(ShellParameters->Argc)));\r
+  if (Type == Script_File_Name) {\r
+    StripParamQuotation = FALSE;\r
+  }\r
+  \r
+  return ParseCommandLineToArgs( NewCommandLine, \r
+                                 StripParamQuotation, \r
+                                 &(ShellParameters->Argv), \r
+                                 &(ShellParameters->Argc)\r
+                                );\r
 }\r
 \r
 /**\r