]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg: Refactor Alias replacement into separate function
authorJaben Carsey <jaben.carsey@intel.com>
Thu, 12 Dec 2013 21:16:38 +0000 (21:16 +0000)
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 12 Dec 2013 21:16:38 +0000 (21:16 +0000)
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jaben Carsey <jaben.carsey@intel.com>
Reviewed-by: Erik Bjorge <erik.c.bjorge@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14973 6f19259b-4bc3-4df7-8a09-765794883524

ShellPkg/Application/Shell/Shell.c

index 7af8153b4d560bcc4a211b599ff4ffbb73179560..1c617abff509301a48410c0b4611d56b3fc2dd63 100644 (file)
@@ -1430,6 +1430,77 @@ RunSplitCommand(
   return (Status);\r
 }\r
 \r
+/**\r
+  Take the original command line, substitute any alias in the first group of space delimited characters, free \r
+  the original string, return the modified copy\r
+\r
+  @param[in] CmdLine  pointer to the command line to update\r
+  @param[out]CmdName  upon successful return the name of the command to be run\r
+\r
+  @retval EFI_SUCCESS           the function was successful\r
+  @retval EFI_OUT_OF_RESOURCES  a memory allocation failed\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ShellSubstituteAliases(\r
+  IN CHAR16 **CmdLine\r
+  )\r
+{\r
+  CHAR16      *NewCmdLine;\r
+  CHAR16      *CommandName;\r
+  EFI_STATUS  Status;\r
+  UINTN       PostAliasSize;\r
+  ASSERT(CmdLine != NULL);\r
+  ASSERT(*CmdLine!= NULL);\r
+\r
+\r
+  CommandName = NULL;\r
+  if (StrStr((*CmdLine), L" ") == NULL){\r
+    StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r
+  } else {\r
+    StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r
+  }\r
+\r
+  //\r
+  // This cannot happen 'inline' since the CmdLine can need extra space.\r
+  //\r
+  NewCmdLine = NULL;\r
+  if (!ShellCommandIsCommandOnList(CommandName)) {\r
+    //\r
+    // Convert via alias\r
+    //\r
+    Status = ShellConvertAlias(&CommandName);\r
+    if (EFI_ERROR(Status)){\r
+      return (Status);\r
+    }\r
+    PostAliasSize = 0;\r
+    NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r
+    if (NewCmdLine == NULL) {\r
+      SHELL_FREE_NON_NULL(CommandName);\r
+      SHELL_FREE_NON_NULL(*CmdLine);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+    NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r
+    if (NewCmdLine == NULL) {\r
+      SHELL_FREE_NON_NULL(CommandName);\r
+      SHELL_FREE_NON_NULL(*CmdLine);\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+  } else {\r
+    NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r
+  }\r
+\r
+  SHELL_FREE_NON_NULL(*CmdLine);\r
+  SHELL_FREE_NON_NULL(CommandName);\r
\r
+  //\r
+  // re-assign the passed in double pointer to point to our newly allocated buffer\r
+  //\r
+  *CmdLine = NewCmdLine;\r
+\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
 /**\r
   Function will process and run a command line.\r
 \r
@@ -1455,8 +1526,6 @@ RunCommand(
   CHAR16                    **Argv;\r
   BOOLEAN                   LastError;\r
   CHAR16                    LeString[19];\r
-  CHAR16                    *PostAliasCmdLine;\r
-  UINTN                     PostAliasSize;\r
   CHAR16                    *PostVariableCmdLine;\r
   CHAR16                    *CommandWithPath;\r
   CONST EFI_DEVICE_PATH_PROTOCOL  *DevPath;\r
@@ -1478,7 +1547,6 @@ RunCommand(
 \r
   CommandName         = NULL;\r
   PostVariableCmdLine = NULL;\r
-  PostAliasCmdLine    = NULL;\r
   CommandWithPath     = NULL;\r
   DevPath             = NULL;\r
   Status              = EFI_SUCCESS;\r
@@ -1503,46 +1571,12 @@ RunCommand(
     return (EFI_SUCCESS);\r
   }\r
 \r
-  CommandName = NULL;\r
-  if (StrStr(CleanOriginal, L" ") == NULL){\r
-    StrnCatGrow(&CommandName, NULL, CleanOriginal, 0);\r
-  } else {\r
-    StrnCatGrow(&CommandName, NULL, CleanOriginal, StrStr(CleanOriginal, L" ") - CleanOriginal);\r
-  }\r
-\r
-  ASSERT(PostAliasCmdLine == NULL);\r
-  if (!ShellCommandIsCommandOnList(CommandName)) {\r
-    //\r
-    // Convert via alias\r
-    //\r
-    Status = ShellConvertAlias(&CommandName);\r
-    PostAliasSize = 0;\r
-    PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, CommandName, 0);\r
-    PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, StrStr(CleanOriginal, L" "), 0);\r
-    ASSERT_EFI_ERROR(Status);\r
-  } else {\r
-    PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, NULL, CleanOriginal, 0);\r
-  }\r
-\r
-  if (CleanOriginal != NULL) {\r
-    FreePool(CleanOriginal);\r
-    CleanOriginal = NULL;\r
-  }\r
-\r
-  if (CommandName != NULL) {\r
-    FreePool(CommandName);\r
-    CommandName = NULL;\r
+  Status = ShellSubstituteAliases(&CleanOriginal);\r
+  if (EFI_ERROR(Status)) {\r
+    return (Status);\r
   }\r
 \r
-  PostVariableCmdLine = ShellConvertVariables(PostAliasCmdLine);\r
-\r
-  //\r
-  // we can now free the modified by alias command line\r
-  //\r
-  if (PostAliasCmdLine != NULL) {\r
-    FreePool(PostAliasCmdLine);\r
-    PostAliasCmdLine = NULL;\r
-  }\r
+  PostVariableCmdLine = ShellConvertVariables(CleanOriginal);\r
 \r
   if (PostVariableCmdLine == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
@@ -1739,6 +1773,7 @@ RunCommand(
   SHELL_FREE_NON_NULL(CommandName);\r
   SHELL_FREE_NON_NULL(CommandWithPath);\r
   SHELL_FREE_NON_NULL(PostVariableCmdLine);\r
+  SHELL_FREE_NON_NULL(CleanOriginal);\r
 \r
   return (Status);\r
 }\r