]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellParametersProtocol.c
ShellPkg: Fix the bug that handling Ctrl-C improperly
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellParametersProtocol.c
index 1c1367bdf89c0dc2c9c4910049b2bc82e5888260..8d76fb4be7ff0910d333b1814ed471a9e23099f1 100644 (file)
@@ -2,9 +2,10 @@
   Member functions of EFI_SHELL_PARAMETERS_PROTOCOL and functions for creation,\r
   manipulation, and initialization of EFI_SHELL_PARAMETERS_PROTOCOL.\r
 \r
+  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
   Copyright (C) 2014, Red Hat, Inc.\r
   (C) Copyright 2013 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2017, 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
 #include "Shell.h"\r
 \r
+BOOLEAN AsciiRedirection = FALSE;\r
+\r
 /**\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
@@ -72,20 +74,22 @@ FindEndOfParameter(
 \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, 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
 EFI_STATUS\r
-EFIAPI\r
 GetNextParameter(\r
   IN OUT CHAR16   **Walker,\r
   IN OUT CHAR16   **TempParameter,\r
-  IN CONST UINTN  Length\r
+  IN CONST UINTN  Length,\r
+  IN BOOLEAN      StripQuotation\r
   )\r
 {\r
   CONST CHAR16 *NextDelim;\r
@@ -159,7 +163,11 @@ DEBUG_CODE_END();
       //\r
       // eliminate the unescaped quote\r
       //\r
-      CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r
+      if (StripQuotation) {\r
+        CopyMem ((CHAR16*)NextDelim, NextDelim + 1, StrSize (NextDelim + 1));\r
+         } else{\r
+        NextDelim++;\r
+         }\r
     }\r
   }\r
 \r
@@ -176,26 +184,30 @@ DEBUG_CODE_END();
   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, 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
+  @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
 **/\r
 EFI_STATUS\r
-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
@@ -206,18 +218,25 @@ 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
       ; Count++\r
       ) {\r
-    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size))) {\r
+    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size, TRUE))) {\r
       break;\r
     }\r
   }\r
@@ -227,30 +246,34 @@ ParseCommandLineToArgs(
   //\r
   (*Argv) = AllocateZeroPool((Count)*sizeof(CHAR16*));\r
   if (*Argv == NULL) {\r
-    SHELL_FREE_NON_NULL(TempParameter);\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
-    if (EFI_ERROR(GetNextParameter(&Walker, &TempParameter, Size))) {\r
-      SHELL_FREE_NON_NULL(TempParameter);\r
-      return (EFI_INVALID_PARAMETER);\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
-      SHELL_FREE_NON_NULL(TempParameter);\r
-      return (EFI_OUT_OF_RESOURCES);\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Done;\r
     }\r
     ((CHAR16**)(*Argv))[(*Argc)] = NewParam;\r
     (*Argc)++;\r
   }\r
   ASSERT(Count >= (*Argc));\r
+  Status = EFI_SUCCESS;\r
+  \r
+Done:\r
   SHELL_FREE_NON_NULL(TempParameter);\r
-  return (EFI_SUCCESS);\r
+  SHELL_FREE_NON_NULL(NewCommandLine);\r
+  return (Status);\r
 }\r
 \r
 /**\r
@@ -270,7 +293,6 @@ ParseCommandLineToArgs(
   @sa ParseCommandLineToArgs\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 CreatePopulateInstallShellParametersProtocol (\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  **NewShellParameters,\r
   IN OUT BOOLEAN                        *RootShellInstance\r
@@ -360,6 +382,7 @@ CreatePopulateInstallShellParametersProtocol (
     // Populate Argc and Argv\r
     //\r
     Status = ParseCommandLineToArgs(FullCommandLine,\r
+                                    TRUE,\r
                                     &(*NewShellParameters)->Argv,\r
                                     &(*NewShellParameters)->Argc);\r
 \r
@@ -411,7 +434,6 @@ CreatePopulateInstallShellParametersProtocol (
   @sa UninstallProtocolInterface\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 CleanUpShellParametersProtocol (\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *NewShellParameters\r
   )\r
@@ -458,7 +480,6 @@ CleanUpShellParametersProtocol (
   @return An error upon failure.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 IsUnicodeFile(\r
   IN CONST CHAR16 *FileName\r
   )\r
@@ -493,7 +514,6 @@ IsUnicodeFile(
   @param[in, out] TheString  A pointer to the string to update.\r
 **/\r
 VOID\r
-EFIAPI\r
 StripQuotes (\r
   IN OUT CHAR16 *TheString\r
   )\r
@@ -545,7 +565,6 @@ CalculateEfiHdrCrc (
   @return       The modified FileName.\r
 **/\r
 CHAR16*\r
-EFIAPI\r
 FixFileName (\r
   IN CHAR16 *FileName\r
   )\r
@@ -588,7 +607,6 @@ FixFileName (
   @return       The modified FileName.\r
 **/\r
 CHAR16*\r
-EFIAPI\r
 FixVarName (\r
   IN CHAR16 *FileName\r
   )\r
@@ -617,7 +635,6 @@ FixVarName (
   @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
@@ -681,7 +698,6 @@ WriteFileTag (
   @retval   EFI_OUT_OF_RESOURCES        A memory allocation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 UpdateStdInStdOutStdErr(\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters,\r
   IN CHAR16                             *NewCommandLine,\r
@@ -710,9 +726,11 @@ UpdateStdInStdOutStdErr(
   UINTN             Size;\r
   SPLIT_LIST        *Split;\r
   CHAR16            *FirstLocation;\r
+  BOOLEAN           Volatile;\r
 \r
   OutUnicode      = TRUE;\r
   InUnicode       = TRUE;\r
+  AsciiRedirection = FALSE;\r
   ErrUnicode      = TRUE;\r
   StdInVarName    = NULL;\r
   StdOutVarName   = NULL;\r
@@ -991,6 +1009,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
@@ -1015,9 +1034,9 @@ UpdateStdInStdOutStdErr(
   StrnCpyS(CommandLineCopy, StrSize(CommandLineCopy)/sizeof(CHAR16), NewCommandLine, StrLen(NewCommandLine));\r
 \r
   if (FirstLocation != CommandLineCopy + StrLen(CommandLineCopy)\r
-    && ((UINTN)(FirstLocation - CommandLineCopy) < StrLen(NewCommandLine))\r
+    && (((UINTN)FirstLocation - (UINTN)CommandLineCopy)/sizeof(CHAR16) < StrLen(NewCommandLine))\r
     ){\r
-    *(NewCommandLine + (UINTN)(FirstLocation - CommandLineCopy)) = CHAR_NULL;\r
+    *(NewCommandLine + ((UINTN)FirstLocation - (UINTN)CommandLineCopy)/sizeof(CHAR16)) = CHAR_NULL;\r
   }\r
 \r
   if (!EFI_ERROR(Status)) {\r
@@ -1083,8 +1102,8 @@ UpdateStdInStdOutStdErr(
       //\r
       // Check for no volatile environment variables\r
       //\r
-      ||(StdErrVarName  != NULL && !IsVolatileEnv(StdErrVarName))\r
-      ||(StdOutVarName  != NULL && !IsVolatileEnv(StdOutVarName))\r
+      ||(StdErrVarName  != NULL && !EFI_ERROR (IsVolatileEnv (StdErrVarName, &Volatile)) && !Volatile)\r
+      ||(StdOutVarName  != NULL && !EFI_ERROR (IsVolatileEnv (StdOutVarName, &Volatile)) && !Volatile)\r
       //\r
       // Cant redirect during a reconnect operation.\r
       //\r
@@ -1145,7 +1164,7 @@ UpdateStdInStdOutStdErr(
         if (TempHandle == NULL) {\r
           Status = EFI_INVALID_PARAMETER;\r
         } else {\r
-          if (StrStr(StdOutFileName, L"NUL")==StdOutFileName) {\r
+          if (gUnicodeCollation->MetaiMatch (gUnicodeCollation, StdOutFileName, L"NUL")) {\r
             //no-op\r
           } else if (!OutAppend && OutUnicode && !EFI_ERROR(Status)) {\r
             Status = WriteFileTag (TempHandle);\r
@@ -1237,18 +1256,13 @@ UpdateStdInStdOutStdErr(
           &TempHandle,\r
           EFI_FILE_MODE_READ,\r
           0);\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
+          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
@@ -1283,7 +1297,6 @@ UpdateStdInStdOutStdErr(
   @param[in] SystemTableInfo           Pointer to old system table information.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 RestoreStdInStdOutStdErr (\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters,\r
   IN  SHELL_FILE_HANDLE                 *OldStdIn,\r
@@ -1352,6 +1365,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
@@ -1359,15 +1373,18 @@ RestoreStdInStdOutStdErr (
   @retval   EFI_OUT_OF_RESOURCES        A memory allocation failed.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 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
@@ -1376,7 +1393,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
@@ -1389,7 +1414,6 @@ UpdateArgcArgv(
   @param[in] OldArgc                    pointer to old number of items in Argv list\r
 **/\r
 VOID\r
-EFIAPI\r
 RestoreArgcArgv(\r
   IN OUT EFI_SHELL_PARAMETERS_PROTOCOL  *ShellParameters,\r
   IN CHAR16                             ***OldArgv,\r