From df07baea2f9d1e40bd8fb3e445286d231241023a Mon Sep 17 00:00:00 2001 From: Jaben Carsey Date: Wed, 21 Aug 2013 18:11:23 +0000 Subject: [PATCH] ShellPkg: update behavior with undefined environment variables Undefined environment variables are now removed during script execution. Excepted environment variables are now correctly un-excepted right before processing continues Contributed-under: TianoCore Contribution Agreement 1.0 Signed-off-by: Jaben Carsey reviewed-by: Matthews, Robert git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@14585 6f19259b-4bc3-4df7-8a09-765794883524 --- ShellPkg/Application/Shell/Shell.c | 53 +++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index 00c7bb58cf..3df55e1a1c 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1085,6 +1085,7 @@ ShellConvertVariables ( CHAR16 *NewCommandLine1; CHAR16 *NewCommandLine2; CHAR16 *Temp; + CHAR16 *Temp2; UINTN ItemSize; CHAR16 *ItemTemp; SCRIPT_FILE *CurrentScriptFile; @@ -1142,15 +1143,6 @@ ShellConvertVariables ( } } - // - // Quick out if none were found... - // - if (NewSize == StrSize(OriginalCommandLine)) { - ASSERT(Temp == NULL); - Temp = StrnCatGrow(&Temp, NULL, OriginalCommandLine, 0); - return (Temp); - } - // // now do the replacements... // @@ -1182,8 +1174,51 @@ ShellConvertVariables ( ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE); StrCpy(NewCommandLine1, NewCommandLine2); } + + // + // Remove non-existant environment variables in scripts only + // + for (Temp = NewCommandLine1 ; Temp != NULL ; ) { + Temp = StrStr(Temp, L"%"); + if (Temp == NULL) { + break; + } + while (*(Temp - 1) == L'^') { + Temp = StrStr(Temp + 1, L"%"); + if (Temp == NULL) { + break; + } + } + if (Temp == NULL) { + break; + } + + Temp2 = StrStr(Temp + 1, L"%"); + if (Temp2 == NULL) { + break; + } + while (*(Temp2 - 1) == L'^') { + Temp2 = StrStr(Temp2 + 1, L"%"); + if (Temp2 == NULL) { + break; + } + } + if (Temp2 == NULL) { + break; + } + + Temp2++; + CopyMem(Temp, Temp2, StrSize(Temp2)); + } + } + // + // Now cleanup any straggler intentionally ignored "%" characters + // + ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE); + StrCpy(NewCommandLine1, NewCommandLine2); + FreePool(NewCommandLine2); FreePool(ItemTemp); -- 2.39.2