]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
Update the comments in function headers to follow Doxygen special documentation block...
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index 9e502256746c7e81ce648054b1149f8e3056b61a..7809a0e0d720708ada43364ac5283bc769f58e4b 100644 (file)
@@ -2,7 +2,8 @@
   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 - 2011, Intel Corporation. All rights reserved.<BR>\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 +14,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
@@ -192,6 +192,7 @@ ParseCommandLineToArgs(
   //\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
@@ -207,6 +208,7 @@ ParseCommandLineToArgs(
     (*Argc)++;\r
   }\r
   ASSERT(Count >= (*Argc));\r
+  SHELL_FREE_NON_NULL(TempParameter);\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -289,10 +291,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 +314,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
@@ -495,6 +495,104 @@ CalculateEfiHdrCrc (
   Hdr->CRC32 = Crc;\r
 }\r
 \r
+/**\r
+  Fix a string to only have the file name, removing starting at the first space of whatever is quoted.\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
+FixFileName (\r
+  IN CHAR16 *FileName\r
+  )\r
+{\r
+  CHAR16  *Copy;\r
+  CHAR16  *TempLocation;\r
+\r
+  if (FileName == NULL) {\r
+    return (NULL);\r
+  }\r
+\r
+  if (FileName[0] == L'\"') {\r
+    Copy = FileName+1;\r
+    if ((TempLocation = StrStr(Copy , L"\"")) != NULL) {\r
+      TempLocation[0] = CHAR_NULL;\r
+    }    \r
+  } else {\r
+    Copy = FileName;\r
+    while(Copy[0] == L' ') {\r
+      Copy++;\r
+    }\r
+    if ((TempLocation = StrStr(Copy , L" ")) != NULL) {\r
+      TempLocation[0] = CHAR_NULL;\r
+    }    \r
+  }\r
+\r
+  if (Copy[0] == CHAR_NULL) {\r
+    return (NULL);\r
+  }\r
+\r
+  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
   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
@@ -566,8 +664,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
@@ -842,6 +940,11 @@ UpdateStdInStdOutStdErr(
     }\r
   }\r
 \r
+  //\r
+  // re-populate the string to support any filenames that were in quotes.\r
+  //\r
+  StrCpy(CommandLineCopy, NewCommandLine);\r
+\r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
     && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
     ){\r
@@ -849,23 +952,36 @@ UpdateStdInStdOutStdErr(
   }\r
 \r
   if (!EFI_ERROR(Status)) {\r
-    if (StdErrFileName != NULL && (CommandLineWalker = StrStr(StdErrFileName, L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+\r
+    if (StdErrFileName != NULL) {\r
+      if ((StdErrFileName    = FixFileName(StdErrFileName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdOutFileName != NULL && (CommandLineWalker = StrStr(StdOutFileName, L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdOutFileName != NULL) {\r
+      if ((StdOutFileName    = FixFileName(StdOutFileName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdInFileName  != NULL && (CommandLineWalker = StrStr(StdInFileName , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdInFileName  != NULL) {\r
+      if ((StdInFileName     = FixFileName(StdInFileName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdErrVarName  != NULL && (CommandLineWalker = StrStr(StdErrVarName , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdErrVarName  != NULL) {\r
+      if ((StdErrVarName     = FixVarName(StdErrVarName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdOutVarName  != NULL && (CommandLineWalker = StrStr(StdOutVarName , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdOutVarName  != NULL) {\r
+      if ((StdOutVarName     = FixVarName(StdOutVarName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
-    if (StdInVarName   != NULL && (CommandLineWalker = StrStr(StdInVarName  , L" ")) != NULL) {\r
-      CommandLineWalker[0] = CHAR_NULL;\r
+    if (StdInVarName   != NULL) {\r
+      if ((StdInVarName      = FixVarName(StdInVarName)) == NULL) {\r
+        Status = EFI_INVALID_PARAMETER;\r
+      }\r
     }\r
 \r
     //\r
@@ -948,7 +1064,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
@@ -991,7 +1107,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
@@ -1009,7 +1125,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
@@ -1025,7 +1141,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
@@ -1058,7 +1174,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
@@ -1073,8 +1197,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
@@ -1139,10 +1270,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