]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg: Refactor string manipulation
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index e152a7a3a08dcfb9f09e1e9b9d392f293c66606e..ff149009b14cda223df7d93e1c774da916129aa9 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
+  Copyright (c) 2013 Hewlett-Packard Development Company, L.P.\r
+  Copyright (c) 2009 - 2014, 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
@@ -13,8 +15,7 @@
 \r
 **/\r
 \r
-#include "ShellParametersProtocol.h"\r
-#include "ConsoleWrappers.h"\r
+#include "Shell.h"\r
 \r
 /**\r
   return the next parameter from a command line string;\r
@@ -34,8 +35,9 @@
 VOID\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
   )\r
 {\r
   CHAR16 *NextDelim;\r
@@ -81,16 +83,20 @@ GetNextParameter(
       //\r
       // found ""\r
       //\r
-      StrCpy(*TempParameter, L"");\r
+      *(*TempParameter) = CHAR_NULL;\r
       *Walker = NextDelim + 1;\r
     } else if (NextDelim != NULL) {\r
-      StrnCpy(*TempParameter, (*Walker)+1, NextDelim - ((*Walker)+1));\r
+\r
+      //\r
+      // Copy ensuring that both quotes are left in place.\r
+      //\r
+      StrnCpy(*TempParameter, (*Walker), NextDelim - *Walker + 1);\r
       *Walker = NextDelim + 1;\r
     } else {\r
       //\r
       // last one... someone forgot the training quote!\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
+      StrnCpy(*TempParameter, *Walker, Length/sizeof(CHAR16) - 1);\r
       *Walker = NULL;\r
     }\r
     for (TempLoc = *TempParameter ; TempLoc != NULL && *TempLoc != CHAR_NULL ; TempLoc++) {\r
@@ -112,7 +118,7 @@ GetNextParameter(
       //\r
       // last one.\r
       //\r
-      StrCpy(*TempParameter, *Walker);\r
+      StrnCpy(*TempParameter, *Walker, Length/sizeof(CHAR16) - 1);\r
       *Walker = NULL;\r
     }\r
     for (NextDelim = *TempParameter ; NextDelim != NULL && *NextDelim != CHAR_NULL ; NextDelim++) {\r
@@ -176,22 +182,16 @@ ParseCommandLineToArgs(
   for ( Count = 0\r
       , Walker = (CHAR16*)CommandLine\r
       ; Walker != NULL && *Walker != CHAR_NULL\r
-      ; GetNextParameter(&Walker, &TempParameter)\r
+      ; GetNextParameter(&Walker, &TempParameter, Size)\r
       , Count++\r
      );\r
 \r
-/*  Count = 0;\r
-  Walker = (CHAR16*)CommandLine;\r
-  while(Walker != NULL) {\r
-    GetNextParameter(&Walker, &TempParameter);\r
-    Count++;\r
-  }\r
-*/\r
   //\r
   // lets allocate the pointer array\r
   //\r
   (*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*));\r
   if (*Argv == NULL) {\r
+    SHELL_FREE_NON_NULL(TempParameter);\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
@@ -199,14 +199,17 @@ ParseCommandLineToArgs(
   Walker = (CHAR16*)CommandLine;\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
+    GetNextParameter(&Walker, &TempParameter, Size);\r
+    NewParam = AllocateCopyPool(StrSize(TempParameter), TempParameter);\r
+    if (NewParam == NULL){\r
+      SHELL_FREE_NON_NULL(TempParameter);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
     ((CHAR16**)(*Argv))[(*Argc)] = NewParam;\r
     (*Argc)++;\r
   }\r
   ASSERT(Count >= (*Argc));\r
+  SHELL_FREE_NON_NULL(TempParameter);\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -289,10 +292,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,9 +315,7 @@ 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
@@ -538,6 +539,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,7 +668,6 @@ UpdateStdInStdOutStdErr(
   BOOLEAN           OutAppend;\r
   BOOLEAN           ErrAppend;\r
   UINTN             Size;\r
-  CHAR16            TagBuffer[2];\r
   SPLIT_LIST        *Split;\r
   CHAR16            *FirstLocation;\r
 \r
@@ -888,7 +972,7 @@ UpdateStdInStdOutStdErr(
   //\r
   // re-populate the string to support any filenames that were in quotes.\r
   //\r
-  StrCpy(CommandLineCopy, NewCommandLine);\r
+  StrnCpy(CommandLineCopy, NewCommandLine, StrLen(NewCommandLine));\r
 \r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
     && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
@@ -914,17 +998,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 +1079,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 +1087,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 +1108,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 +1130,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 +1148,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 +1164,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,7 +1197,15 @@ UpdateStdInStdOutStdErr(
           &TempHandle,\r
           EFI_FILE_MODE_READ,\r
           0);\r
-        if (!InUnicode && !EFI_ERROR(Status)) {\r
+        if (InUnicode) {\r
+          //\r
+          // Chop off the 0xFEFF if it's there...\r
+          //\r
+          RemoveFileTag(&TempHandle);\r
+        } else if (!EFI_ERROR(Status)) {\r
+          //\r
+          // Create the ASCII->Unicode conversion layer\r
+          //\r
           TempHandle = CreateFileInterfaceFile(TempHandle, FALSE);\r
         }\r
         if (!EFI_ERROR(Status)) {\r
@@ -1134,8 +1220,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