return (EFI_SUCCESS);\r
}\r
\r
+/**\r
+ Parse for the next instance of one string within another string. Can optionally make sure that \r
+ the string was not escaped (^ character) per the shell specification.\r
+\r
+ @param[in] SourceString The string to search within\r
+ @param[in] FindString The string to look for\r
+ @param[in] CheckForEscapeCharacter TRUE to skip escaped instances of FinfString, otherwise will return even escaped instances\r
+**/\r
+CHAR16*\r
+EFIAPI\r
+FindNextInstance(\r
+ IN CONST CHAR16 *SourceString,\r
+ IN CONST CHAR16 *FindString,\r
+ IN CONST BOOLEAN CheckForEscapeCharacter\r
+ )\r
+{\r
+ CHAR16 *Temp;\r
+ if (SourceString == NULL) {\r
+ return (NULL);\r
+ }\r
+ Temp = StrStr(SourceString, FindString);\r
+\r
+ //\r
+ // If nothing found, or we dont care about escape characters\r
+ //\r
+ if (Temp == NULL || !CheckForEscapeCharacter) {\r
+ return (Temp);\r
+ }\r
+\r
+ //\r
+ // If we found an escaped character, try again on the remainder of the string\r
+ //\r
+ if ((Temp > (SourceString)) && *(Temp-1) == L'^') {\r
+ return FindNextInstance(Temp+1, FindString, CheckForEscapeCharacter);\r
+ }\r
+\r
+ //\r
+ // we found the right character\r
+ //\r
+ return (Temp);\r
+}\r
+\r
+/**\r
+ This function will eliminate unreplaced (and therefore non-found) environment variables.\r
+\r
+ @param[in,out] CmdLine The command line to update.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StripUnreplacedEnvironmentVariables(\r
+ IN OUT CHAR16 *CmdLine\r
+ )\r
+{\r
+ CHAR16 *FirstPercent;\r
+ CHAR16 *FirstQuote;\r
+ CHAR16 *SecondPercent;\r
+ CHAR16 *SecondQuote;\r
+ CHAR16 *CurrentLocator;\r
+\r
+ for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) {\r
+ FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE);\r
+ FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE);\r
+ SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL;\r
+ if (FirstPercent == NULL || SecondPercent == NULL) {\r
+ //\r
+ // If we ever dont have 2 % we are done.\r
+ //\r
+ break;\r
+ }\r
+\r
+ if (FirstQuote < FirstPercent) {\r
+ SecondQuote = FirstQuote!= NULL?FindNextInstance(FirstQuote+1, L"\"", TRUE):NULL;\r
+ //\r
+ // Quote is first found\r
+ //\r
+\r
+ if (SecondQuote < FirstPercent) {\r
+ //\r
+ // restart after the pair of "\r
+ //\r
+ CurrentLocator = SecondQuote + 1;\r
+ } else /* FirstPercent < SecondQuote */{\r
+ //\r
+ // Restart on the first percent\r
+ //\r
+ CurrentLocator = FirstPercent;\r
+ }\r
+ continue;\r
+ }\r
+ ASSERT(FirstPercent < FirstQuote);\r
+ if (SecondPercent < FirstQuote) {\r
+ //\r
+ // We need to remove from FirstPercent to SecondPercent\r
+ //\r
+ CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1));\r
+\r
+ //\r
+ // dont need to update the locator. both % characters are gone.\r
+ //\r
+ continue;\r
+ }\r
+ ASSERT(FirstQuote < SecondPercent);\r
+ CurrentLocator = FirstQuote;\r
+ }\r
+ return (EFI_SUCCESS);\r
+}\r
+\r
/**\r
Function allocates a new command line and replaces all instances of environment\r
variable names that are correctly preset to their values.\r
CHAR16 *NewCommandLine1;\r
CHAR16 *NewCommandLine2;\r
CHAR16 *Temp;\r
- CHAR16 *Temp2;\r
UINTN ItemSize;\r
CHAR16 *ItemTemp;\r
SCRIPT_FILE *CurrentScriptFile;\r
//\r
// Remove non-existant environment variables in scripts only\r
//\r
- for (Temp = NewCommandLine1 ; Temp != NULL ; ) {\r
- Temp = StrStr(Temp, L"%");\r
- if (Temp == NULL) {\r
- break;\r
- }\r
- while (*(Temp - 1) == L'^') {\r
- Temp = StrStr(Temp + 1, L"%");\r
- if (Temp == NULL) {\r
- break;\r
- }\r
- }\r
- if (Temp == NULL) {\r
- break;\r
- }\r
- \r
- Temp2 = StrStr(Temp + 1, L"%");\r
- if (Temp2 == NULL) {\r
- break;\r
- }\r
- while (*(Temp2 - 1) == L'^') {\r
- Temp2 = StrStr(Temp2 + 1, L"%");\r
- if (Temp2 == NULL) {\r
- break;\r
- }\r
- }\r
- if (Temp2 == NULL) {\r
- break;\r
- }\r
- \r
- Temp2++;\r
- CopyMem(Temp, Temp2, StrSize(Temp2));\r
- }\r
-\r
+ StripUnreplacedEnvironmentVariables(NewCommandLine1);\r
}\r
\r
//\r