Skip to content

Commit 14030c5

Browse files
jcarseyjcarsey
authored and
jcarsey
committedJan 27, 2015
ShellPkg: refine command line parsing
Correctly divide up parameters for Argc/Argv including quote ("), escape (^), and space ( ) processing. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jaben Carsey <Jaben.carsey@intel.com> Reviewed-by: Joe Peterson <joe.peterson@intel.com> Reviewed-by: Tapan Shah <tapandshah@hp.com> git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16673 6f19259b-4bc3-4df7-8a09-765794883524
1 parent fdd52bd commit 14030c5

File tree

3 files changed

+214
-119
lines changed

3 files changed

+214
-119
lines changed
 

‎ShellPkg/Application/Shell/Shell.c

+47-43
Original file line numberDiff line numberDiff line change
@@ -1870,12 +1870,12 @@ IsValidSplit(
18701870
return (EFI_OUT_OF_RESOURCES);
18711871
}
18721872
TempWalker = (CHAR16*)Temp;
1873-
GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine));
1874-
1875-
if (GetOperationType(FirstParameter) == Unknown_Invalid) {
1876-
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
1877-
SetLastError(SHELL_NOT_FOUND);
1878-
Status = EFI_NOT_FOUND;
1873+
if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine)))) {
1874+
if (GetOperationType(FirstParameter) == Unknown_Invalid) {
1875+
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
1876+
SetLastError(SHELL_NOT_FOUND);
1877+
Status = EFI_NOT_FOUND;
1878+
}
18791879
}
18801880
}
18811881

@@ -2030,24 +2030,25 @@ DoHelpUpdate(
20302030
Walker = *CmdLine;
20312031
while(Walker != NULL && *Walker != CHAR_NULL) {
20322032
LastWalker = Walker;
2033-
GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine));
2034-
if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {
2035-
LastWalker[0] = L' ';
2036-
LastWalker[1] = L' ';
2037-
NewCommandLine = AllocateZeroPool(StrSize(L"help ") + StrSize(*CmdLine));
2038-
if (NewCommandLine == NULL) {
2039-
Status = EFI_OUT_OF_RESOURCES;
2033+
if (!EFI_ERROR(GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine)))) {
2034+
if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {
2035+
LastWalker[0] = L' ';
2036+
LastWalker[1] = L' ';
2037+
NewCommandLine = AllocateZeroPool(StrSize(L"help ") + StrSize(*CmdLine));
2038+
if (NewCommandLine == NULL) {
2039+
Status = EFI_OUT_OF_RESOURCES;
2040+
break;
2041+
}
2042+
2043+
//
2044+
// We know the space is sufficient since we just calculated it.
2045+
//
2046+
StrnCpy(NewCommandLine, L"help ", 5);
2047+
StrnCat(NewCommandLine, *CmdLine, StrLen(*CmdLine));
2048+
SHELL_FREE_NON_NULL(*CmdLine);
2049+
*CmdLine = NewCommandLine;
20402050
break;
20412051
}
2042-
2043-
//
2044-
// We know the space is sufficient since we just calculated it.
2045-
//
2046-
StrnCpy(NewCommandLine, L"help ", 5);
2047-
StrnCat(NewCommandLine, *CmdLine, StrLen(*CmdLine));
2048-
SHELL_FREE_NON_NULL(*CmdLine);
2049-
*CmdLine = NewCommandLine;
2050-
break;
20512052
}
20522053
}
20532054

@@ -2499,27 +2500,30 @@ RunCommand(
24992500
return (EFI_OUT_OF_RESOURCES);
25002501
}
25012502
TempWalker = CleanOriginal;
2502-
GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal));
2503-
2504-
//
2505-
// Depending on the first parameter we change the behavior
2506-
//
2507-
switch (Type = GetOperationType(FirstParameter)) {
2508-
case File_Sys_Change:
2509-
Status = ChangeMappedDrive (FirstParameter);
2510-
break;
2511-
case Internal_Command:
2512-
case Script_File_Name:
2513-
case Efi_Application:
2514-
Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol);
2515-
break;
2516-
default:
2517-
//
2518-
// Whatever was typed, it was invalid.
2519-
//
2520-
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2521-
SetLastError(SHELL_NOT_FOUND);
2522-
break;
2503+
if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal)))) {
2504+
//
2505+
// Depending on the first parameter we change the behavior
2506+
//
2507+
switch (Type = GetOperationType(FirstParameter)) {
2508+
case File_Sys_Change:
2509+
Status = ChangeMappedDrive (FirstParameter);
2510+
break;
2511+
case Internal_Command:
2512+
case Script_File_Name:
2513+
case Efi_Application:
2514+
Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol);
2515+
break;
2516+
default:
2517+
//
2518+
// Whatever was typed, it was invalid.
2519+
//
2520+
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2521+
SetLastError(SHELL_NOT_FOUND);
2522+
break;
2523+
}
2524+
} else {
2525+
ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);
2526+
SetLastError(SHELL_NOT_FOUND);
25232527
}
25242528

25252529
SHELL_FREE_NON_NULL(CleanOriginal);

0 commit comments

Comments
 (0)
Please sign in to comment.