]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/Shell.c
ShellPkg: Fix command line parsing for script files (%1, %1, etc...)
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
index 81982f135405a9ac442fcb3728e4bf981584aa63..134ec587d525ac106f03d0a38c3a62d888dbb70b 100644 (file)
@@ -990,7 +990,7 @@ DoStartupScript(
   // If we got a file, run it\r
   //\r
   if (!EFI_ERROR(Status) && FileHandle != NULL) {\r
-    Status = RunScriptFileHandle (FileHandle, mStartupScript);\r
+    Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);\r
     ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);\r
   } else {\r
     FileStringPath = ShellFindFilePath(mStartupScript);\r
@@ -1001,7 +1001,7 @@ DoStartupScript(
       Status = EFI_SUCCESS;\r
       ASSERT(FileHandle == NULL);\r
     } else {\r
-      Status = RunScriptFile(FileStringPath);\r
+      Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);\r
       FreePool(FileStringPath);\r
     }\r
   }\r
@@ -2048,7 +2048,7 @@ RunCommandOrFile(
       }\r
       switch (Type) {\r
         case   Script_File_Name:\r
-          Status = RunScriptFile (CommandWithPath);\r
+          Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);\r
           break;\r
         case   Efi_Application:\r
           //\r
@@ -2578,30 +2578,62 @@ RunScriptFileHandle (
   Function to process a NSH script file.\r
 \r
   @param[in] ScriptPath         Pointer to the script file name (including file system path).\r
+  @param[in] Handle             the handle of the script file already opened.\r
+  @param[in] CmdLine            the command line to run.\r
+  @param[in] ParamProtocol      the shell parameters protocol pointer\r
 \r
   @retval EFI_SUCCESS           the script completed sucessfully\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 RunScriptFile (\r
-  IN CONST CHAR16 *ScriptPath\r
+  IN CONST CHAR16                   *ScriptPath,\r
+  IN SHELL_FILE_HANDLE              Handle OPTIONAL,\r
+  IN CONST CHAR16                   *CmdLine,\r
+  IN EFI_SHELL_PARAMETERS_PROTOCOL  *ParamProtocol\r
   )\r
 {\r
   EFI_STATUS          Status;\r
   SHELL_FILE_HANDLE   FileHandle;\r
+  UINTN                     Argc;\r
+  CHAR16                    **Argv;\r
 \r
   if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
-  Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
-  if (EFI_ERROR(Status)) {\r
-    return (Status);\r
-  }\r
+  //\r
+  // get the argc and argv updated for scripts\r
+  //\r
+  Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
+  if (!EFI_ERROR(Status)) {\r
 \r
-  Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
+    if (Handle == NULL) {\r
+      //\r
+      // open the file\r
+      //\r
+      Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
+      if (!EFI_ERROR(Status)) {\r
+        //\r
+        // run it\r
+        //\r
+        Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
 \r
-  ShellCloseFile(&FileHandle);\r
+        //\r
+        // now close the file\r
+        //\r
+        ShellCloseFile(&FileHandle);\r
+      }\r
+    } else {\r
+      Status = RunScriptFileHandle(Handle, ScriptPath);\r
+    }\r
+  }\r
+\r
+  //\r
+  // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
+  // This is safe even if the update API failed.  In this case, it may be a no-op.\r
+  //\r
+  RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
 \r
   return (Status);\r
 }\r