]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/Shell.c
ShellPkg: Refactor updating command line for help use
[mirror_edk2.git] / ShellPkg / Application / Shell / Shell.c
index 0cf09ec76b70b07d5aa62e227514449f52984888..6822f52e814a07c978e9934c970cd18c7d629655 100644 (file)
@@ -1655,6 +1655,57 @@ ChangeMappedDrive(
   return (Status);\r
 }\r
 \r
+/**\r
+  Reprocess the command line to direct all -? to the help command.\r
+\r
+  if found, will add "help" as argv[0], and move the rest later.\r
+\r
+  @param[in,out] Argc  The pointer to argc to update \r
+  @param[in,out] Argv  The pointer to argv to update (this is a pointer to an array of string pointers)\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DoHelpUpdateArgcArgv(\r
+  IN OUT UINTN *Argc,\r
+  IN OUT CHAR16 ***Argv\r
+  )\r
+{\r
+  UINTN Count;\r
+  UINTN Count2;\r
+  //\r
+  // Check each parameter\r
+  //\r
+  for (Count = 0 ; Count < (*Argc) ; Count++) {\r
+    //\r
+    // if it's "-?" or if the first parameter is "?"\r
+    //\r
+    if (StrStr((*Argv)[Count], L"-?") == (*Argv)[Count] \r
+    ||  ((*Argv)[0][0] == L'?' && (*Argv)[0][1] == CHAR_NULL)\r
+      ) {\r
+      //\r
+      // We need to redo the arguments since a parameter was -?\r
+      // move them all down 1 to the end, then up one then replace the first with help\r
+      //\r
+      FreePool((*Argv)[Count]);\r
+      (*Argv)[Count] = NULL;\r
+      for (Count2 = Count ; (Count2 + 1) < (*Argc) ; Count2++) {\r
+        (*Argv)[Count2] = (*Argv)[Count2+1];\r
+      }\r
+      (*Argv)[Count2] = NULL;\r
+      for (Count2 = (*Argc) -1 ; Count2 > 0 ; Count2--) {\r
+        (*Argv)[Count2] = (*Argv)[Count2-1];\r
+      }\r
+      (*Argv)[0] = NULL;\r
+      (*Argv)[0] = StrnCatGrow(&(*Argv)[0], NULL, L"help", 0);\r
+      if ((*Argv)[0] == NULL) {\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
+      break;\r
+    }\r
+  }\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
 /**\r
   Function will process and run a command line.\r
 \r
@@ -1688,8 +1739,6 @@ RunCommand(
   SHELL_FILE_HANDLE         OriginalStdOut;\r
   SHELL_FILE_HANDLE         OriginalStdErr;\r
   SYSTEM_TABLE_INFO         OriginalSystemTableInfo;\r
-  UINTN                     Count;\r
-  UINTN                     Count2;\r
   CHAR16                    *CleanOriginal;\r
 \r
   ASSERT(CmdLine != NULL);\r
@@ -1770,27 +1819,10 @@ RunCommand(
       Status = UpdateArgcArgv(ShellInfoObject.NewShellParametersProtocol, CleanOriginal, &Argv, &Argc);\r
       ASSERT_EFI_ERROR(Status);\r
 \r
-      for (Count = 0 ; Count < ShellInfoObject.NewShellParametersProtocol->Argc ; Count++) {\r
-        if (StrStr(ShellInfoObject.NewShellParametersProtocol->Argv[Count], L"-?") == ShellInfoObject.NewShellParametersProtocol->Argv[Count]\r
-        ||  (ShellInfoObject.NewShellParametersProtocol->Argv[0][0] == L'?' && ShellInfoObject.NewShellParametersProtocol->Argv[0][1] == CHAR_NULL)\r
-          ) {\r
-          //\r
-          // We need to redo the arguments since a parameter was -?\r
-          // move them all down 1 to the end, then up one then replace the first with help\r
-          //\r
-          FreePool(ShellInfoObject.NewShellParametersProtocol->Argv[Count]);\r
-          ShellInfoObject.NewShellParametersProtocol->Argv[Count] = NULL;\r
-          for (Count2 = Count ; (Count2 + 1) < ShellInfoObject.NewShellParametersProtocol->Argc ; Count2++) {\r
-            ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = ShellInfoObject.NewShellParametersProtocol->Argv[Count2+1];\r
-          }\r
-          ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = NULL;\r
-          for (Count2 = ShellInfoObject.NewShellParametersProtocol->Argc -1 ; Count2 > 0 ; Count2--) {\r
-            ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = ShellInfoObject.NewShellParametersProtocol->Argv[Count2-1];\r
-          }\r
-          ShellInfoObject.NewShellParametersProtocol->Argv[0] = NULL;\r
-          ShellInfoObject.NewShellParametersProtocol->Argv[0] = StrnCatGrow(&ShellInfoObject.NewShellParametersProtocol->Argv[0], NULL, L"help", 0);\r
-          break;\r
-        }\r
+      if (StrStr(CleanOriginal, L"?") != NULL) {\r
+        Status = DoHelpUpdateArgcArgv(\r
+          &ShellInfoObject.NewShellParametersProtocol->Argc,\r
+          &ShellInfoObject.NewShellParametersProtocol->Argv);\r
       }\r
 \r
       //\r