summaryrefslogtreecommitdiff
path: root/ShellPkg/Application/Shell
diff options
context:
space:
mode:
authorJaben Carsey <jaben.carsey@intel.com>2013-12-12 21:16:38 +0000
committerjcarsey <jcarsey@6f19259b-4bc3-4df7-8a09-765794883524>2013-12-12 21:16:38 +0000
commitca53c0af0037ad585f7455efc9ee1656bec8d176 (patch)
tree92b2426b492e70bb4069287c1e0cd87e967fe9ec /ShellPkg/Application/Shell
parentad2bc85412dcc566dae32cd4b18da356ace1ea3d (diff)
downloadedk2-platforms-ca53c0af0037ad585f7455efc9ee1656bec8d176.tar.xz
ShellPkg: Refactor Alias replacement into separate function
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
Diffstat (limited to 'ShellPkg/Application/Shell')
-rw-r--r--ShellPkg/Application/Shell/Shell.c117
1 files changed, 76 insertions, 41 deletions
diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c
index 7af8153b4d..1c617abff5 100644
--- a/ShellPkg/Application/Shell/Shell.c
+++ b/ShellPkg/Application/Shell/Shell.c
@@ -1431,6 +1431,77 @@ RunSplitCommand(
}
/**
+ Take the original command line, substitute any alias in the first group of space delimited characters, free
+ the original string, return the modified copy
+
+ @param[in] CmdLine pointer to the command line to update
+ @param[out]CmdName upon successful return the name of the command to be run
+
+ @retval EFI_SUCCESS the function was successful
+ @retval EFI_OUT_OF_RESOURCES a memory allocation failed
+**/
+EFI_STATUS
+EFIAPI
+ShellSubstituteAliases(
+ IN CHAR16 **CmdLine
+ )
+{
+ CHAR16 *NewCmdLine;
+ CHAR16 *CommandName;
+ EFI_STATUS Status;
+ UINTN PostAliasSize;
+ ASSERT(CmdLine != NULL);
+ ASSERT(*CmdLine!= NULL);
+
+
+ CommandName = NULL;
+ if (StrStr((*CmdLine), L" ") == NULL){
+ StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);
+ } else {
+ StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));
+ }
+
+ //
+ // This cannot happen 'inline' since the CmdLine can need extra space.
+ //
+ NewCmdLine = NULL;
+ if (!ShellCommandIsCommandOnList(CommandName)) {
+ //
+ // Convert via alias
+ //
+ Status = ShellConvertAlias(&CommandName);
+ if (EFI_ERROR(Status)){
+ return (Status);
+ }
+ PostAliasSize = 0;
+ NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);
+ if (NewCmdLine == NULL) {
+ SHELL_FREE_NON_NULL(CommandName);
+ SHELL_FREE_NON_NULL(*CmdLine);
+ return (EFI_OUT_OF_RESOURCES);
+ }
+ NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);
+ if (NewCmdLine == NULL) {
+ SHELL_FREE_NON_NULL(CommandName);
+ SHELL_FREE_NON_NULL(*CmdLine);
+ return (EFI_OUT_OF_RESOURCES);
+ }
+ } else {
+ NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);
+ }
+
+ SHELL_FREE_NON_NULL(*CmdLine);
+ SHELL_FREE_NON_NULL(CommandName);
+
+ //
+ // re-assign the passed in double pointer to point to our newly allocated buffer
+ //
+ *CmdLine = NewCmdLine;
+
+ return (EFI_SUCCESS);
+}
+
+/**
Function will process and run a command line.
This will determine if the command line represents an internal shell
@@ -1455,8 +1526,6 @@ RunCommand(
CHAR16 **Argv;
BOOLEAN LastError;
CHAR16 LeString[19];
- CHAR16 *PostAliasCmdLine;
- UINTN PostAliasSize;
CHAR16 *PostVariableCmdLine;
CHAR16 *CommandWithPath;
CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;
@@ -1478,7 +1547,6 @@ RunCommand(
CommandName = NULL;
PostVariableCmdLine = NULL;
- PostAliasCmdLine = NULL;
CommandWithPath = NULL;
DevPath = NULL;
Status = EFI_SUCCESS;
@@ -1503,46 +1571,12 @@ RunCommand(
return (EFI_SUCCESS);
}
- CommandName = NULL;
- if (StrStr(CleanOriginal, L" ") == NULL){
- StrnCatGrow(&CommandName, NULL, CleanOriginal, 0);
- } else {
- StrnCatGrow(&CommandName, NULL, CleanOriginal, StrStr(CleanOriginal, L" ") - CleanOriginal);
- }
-
- ASSERT(PostAliasCmdLine == NULL);
- if (!ShellCommandIsCommandOnList(CommandName)) {
- //
- // Convert via alias
- //
- Status = ShellConvertAlias(&CommandName);
- PostAliasSize = 0;
- PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, CommandName, 0);
- PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, StrStr(CleanOriginal, L" "), 0);
- ASSERT_EFI_ERROR(Status);
- } else {
- PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, NULL, CleanOriginal, 0);
- }
-
- if (CleanOriginal != NULL) {
- FreePool(CleanOriginal);
- CleanOriginal = NULL;
- }
-
- if (CommandName != NULL) {
- FreePool(CommandName);
- CommandName = NULL;
+ Status = ShellSubstituteAliases(&CleanOriginal);
+ if (EFI_ERROR(Status)) {
+ return (Status);
}
- PostVariableCmdLine = ShellConvertVariables(PostAliasCmdLine);
-
- //
- // we can now free the modified by alias command line
- //
- if (PostAliasCmdLine != NULL) {
- FreePool(PostAliasCmdLine);
- PostAliasCmdLine = NULL;
- }
+ PostVariableCmdLine = ShellConvertVariables(CleanOriginal);
if (PostVariableCmdLine == NULL) {
return (EFI_OUT_OF_RESOURCES);
@@ -1739,6 +1773,7 @@ RunCommand(
SHELL_FREE_NON_NULL(CommandName);
SHELL_FREE_NON_NULL(CommandWithPath);
SHELL_FREE_NON_NULL(PostVariableCmdLine);
+ SHELL_FREE_NON_NULL(CleanOriginal);
return (Status);
}