From 6f6792b82085c125df476ed5f8347afcbad1af5d Mon Sep 17 00:00:00 2001 From: Qiu Shumin Date: Fri, 26 Dec 2014 08:22:35 +0000 Subject: [PATCH] ShellPkg: Add code to handle the split ('|') in a double-quoted string. This patch update the code in function 'ContainsSplit', and make 'ContainsSplit' depend on 'FindNextInstance'. So we move 'FindNextInstance' in front of 'ContainsSplit'. Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Qiu Shumin Reviewed-by: Jaben Carsey git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@16560 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Application/Shell/Shell.c | 118 ++++++++++++++++++----------- 1 file changed, 75 insertions(+), 43 deletions(-) diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 41fa78004d..de46a72b44 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -100,6 +100,48 @@ TrimSpaces( return (EFI_SUCCESS); } +/** + Parse for the next instance of one string within another string. Can optionally make sure that + the string was not escaped (^ character) per the shell specification. + + @param[in] SourceString The string to search within + @param[in] FindString The string to look for + @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances +**/ +CHAR16* +EFIAPI +FindNextInstance( + IN CONST CHAR16 *SourceString, + IN CONST CHAR16 *FindString, + IN CONST BOOLEAN CheckForEscapeCharacter + ) +{ + CHAR16 *Temp; + if (SourceString == NULL) { + return (NULL); + } + Temp = StrStr(SourceString, FindString); + + // + // If nothing found, or we dont care about escape characters + // + if (Temp == NULL || !CheckForEscapeCharacter) { + return (Temp); + } + + // + // If we found an escaped character, try again on the remainder of the string + // + if ((Temp > (SourceString)) && *(Temp-1) == L'^') { + return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter); + } + + // + // we found the right character + // + return (Temp); +} + /** Find a command line contains a split operation @@ -142,7 +184,39 @@ ContainsSplit( ) { CONST CHAR16 *TempSpot; - TempSpot = FindSplit(CmdLine); + CONST CHAR16 *FirstQuote; + CONST CHAR16 *SecondQuote; + + FirstQuote = FindNextInstance (CmdLine, L"\"", TRUE); + SecondQuote = NULL; + TempSpot = FindSplit(CmdLine); + + if (FirstQuote == NULL || + TempSpot == NULL || + TempSpot == CHAR_NULL || + FirstQuote > TempSpot + ) { + return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)); + } + + while ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)) { + if (FirstQuote == NULL || FirstQuote > TempSpot) { + break; + } + SecondQuote = FindNextInstance (FirstQuote + 1, L"\"", TRUE); + if (SecondQuote == NULL) { + break; + } + if (SecondQuote < TempSpot) { + FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE); + continue; + } else { + FirstQuote = FindNextInstance (SecondQuote + 1, L"\"", TRUE); + TempSpot = FindSplit(TempSpot + 1); + continue; + } + } + return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)); } @@ -1232,48 +1306,6 @@ ShellConvertAlias( return (EFI_SUCCESS); } -/** - Parse for the next instance of one string within another string. Can optionally make sure that - the string was not escaped (^ character) per the shell specification. - - @param[in] SourceString The string to search within - @param[in] FindString The string to look for - @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances -**/ -CHAR16* -EFIAPI -FindNextInstance( - IN CONST CHAR16 *SourceString, - IN CONST CHAR16 *FindString, - IN CONST BOOLEAN CheckForEscapeCharacter - ) -{ - CHAR16 *Temp; - if (SourceString == NULL) { - return (NULL); - } - Temp = StrStr(SourceString, FindString); - - // - // If nothing found, or we dont care about escape characters - // - if (Temp == NULL || !CheckForEscapeCharacter) { - return (Temp); - } - - // - // If we found an escaped character, try again on the remainder of the string - // - if ((Temp > (SourceString)) && *(Temp-1) == L'^') { - return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter); - } - - // - // we found the right character - // - return (Temp); -} - /** This function will eliminate unreplaced (and therefore non-found) environment variables. -- 2.39.5