X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShell.c;h=40ae68d5a81f978976856de8c4b30f804aea62fa;hb=d2a0d2e6acea4d6a3dbe31b68a9a7fe7511cb478;hp=df101a32af0157e16eeac13a085e67e62397796b;hpb=0477054bc0f5c891a098280cffde0aed549262b3;p=mirror_edk2.git diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index df101a32af..40ae68d5a8 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1,8 +1,8 @@ /** @file This is THE shell (application) - Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.
- Copyright (c) 2013, Hewlett-Packard Development Company, L.P. + Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.
+ (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -101,30 +101,92 @@ TrimSpaces( } /** - Find a command line contains a split operation + 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] CmdLine The command line to parse. - - @retval A pointer to the | character in CmdLine or NULL if not present. + @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 **/ -CONST CHAR16* +CHAR16* EFIAPI -FindSplit( - IN CONST CHAR16 *CmdLine +FindNextInstance( + IN CONST CHAR16 *SourceString, + IN CONST CHAR16 *FindString, + IN CONST BOOLEAN CheckForEscapeCharacter ) { - CONST CHAR16 *TempSpot; - TempSpot = NULL; - if (StrStr(CmdLine, L"|") != NULL) { - for (TempSpot = CmdLine ; TempSpot != NULL && *TempSpot != CHAR_NULL ; TempSpot++) { - if (*TempSpot == L'^' && *(TempSpot+1) == L'|') { - TempSpot++; - } else if (*TempSpot == L'|') { - break; + CHAR16 *Temp; + if (SourceString == NULL) { + return (NULL); + } + Temp = StrStr(SourceString, FindString); + + // + // If nothing found, or we don't 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); +} + +/** + Check whether the string between a pair of % is a valid environment variable name. + + @param[in] BeginPercent pointer to the first percent. + @param[in] EndPercent pointer to the last percent. + + @retval TRUE is a valid environment variable name. + @retval FALSE is NOT a valid environment variable name. +**/ +BOOLEAN +IsValidEnvironmentVariableName( + IN CONST CHAR16 *BeginPercent, + IN CONST CHAR16 *EndPercent + ) +{ + CONST CHAR16 *Walker; + + Walker = NULL; + + ASSERT (BeginPercent != NULL); + ASSERT (EndPercent != NULL); + ASSERT (BeginPercent < EndPercent); + + if ((BeginPercent + 1) == EndPercent) { + return FALSE; + } + + for (Walker = BeginPercent + 1; Walker < EndPercent; Walker++) { + if ( + (*Walker >= L'0' && *Walker <= L'9') || + (*Walker >= L'A' && *Walker <= L'Z') || + (*Walker >= L'a' && *Walker <= L'z') || + (*Walker == L'_') + ) { + if (Walker == BeginPercent + 1 && (*Walker >= L'0' && *Walker <= L'9')) { + return FALSE; + } else { + continue; } + } else { + return FALSE; } } - return (TempSpot); + + return TRUE; } /** @@ -142,7 +204,39 @@ ContainsSplit( ) { CONST CHAR16 *TempSpot; - TempSpot = FindSplit(CmdLine); + CONST CHAR16 *FirstQuote; + CONST CHAR16 *SecondQuote; + + FirstQuote = FindNextInstance (CmdLine, L"\"", TRUE); + SecondQuote = NULL; + TempSpot = FindFirstCharacter(CmdLine, L"|", L'^'); + + 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 = FindFirstCharacter(TempSpot + 1, L"|", L'^'); + continue; + } + } + return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL)); } @@ -151,7 +245,7 @@ ContainsSplit( feature's enabled state was not known when the shell initially launched. @retval EFI_SUCCESS The feature is enabled. - @retval EFI_OUT_OF_RESOURCES There is not enough mnemory available. + @retval EFI_OUT_OF_RESOURCES There is not enough memory available. **/ EFI_STATUS EFIAPI @@ -244,9 +338,6 @@ UefiMain ( UINTN Size; EFI_HANDLE ConInHandle; EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn; - UINTN ExitDataSize; - CHAR16 *ExitData; - SHELL_STATUS ExitStatus; if (PcdGet8(PcdShellSupportLevel) > 3) { return (EFI_UNSUPPORTED); @@ -300,12 +391,6 @@ UefiMain ( // install our console logger. This will keep a log of the output for back-browsing // Status = ConsoleLoggerInstall(ShellInfoObject.LogScreenCount, &ShellInfoObject.ConsoleInfo); - if(EFI_ERROR (Status)) { - ExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT)); - } else { - ExitStatus = SHELL_SUCCESS; - } - if (!EFI_ERROR(Status)) { // // Enable the cursor to be visible @@ -359,7 +444,10 @@ UefiMain ( // // Check the command line // - Status = ProcessCommandLine(); + Status = ProcessCommandLine (); + if (EFI_ERROR (Status)) { + goto FreeResources; + } // // If shell support level is >= 1 create the mappings and paths @@ -416,7 +504,7 @@ UefiMain ( // Display the mapping // if (PcdGet8(PcdShellSupportLevel) >= 2 && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap) { - Status = RunCommand(L"map", NULL); + Status = RunCommand(L"map"); ASSERT_EFI_ERROR(Status); } @@ -482,11 +570,7 @@ UefiMain ( // // process the startup script or launch the called app. // - Status = DoStartupScript( - ShellInfoObject.ImageDevPath, - ShellInfoObject.FileDevPath, - &ExitStatus - ); + Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath); } if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { @@ -506,6 +590,7 @@ UefiMain ( // Reset page break back to default. // ShellInfoObject.PageBreakEnabled = PcdGetBool(PcdShellPageBreakDefault); + ASSERT (ShellInfoObject.ConsoleInfo != NULL); ShellInfoObject.ConsoleInfo->Enabled = TRUE; ShellInfoObject.ConsoleInfo->RowCounter = 0; @@ -519,7 +604,6 @@ UefiMain ( // Status = DoShellPrompt(); } while (!ShellCommandGetExit()); - ExitStatus = (SHELL_STATUS) ShellCommandGetExitCode(); } if (OldConIn != NULL && ConInHandle != NULL) { CloseSimpleTextInOnFile (gST->ConIn); @@ -591,39 +675,16 @@ FreeResources: DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;); } - if (!EFI_ERROR (Status)) { - // If the command exited with an error, we pass this error out in the ExitData - // so that it can be retrieved by the EfiShellExecute function (which may - // start the shell with gBS->StartImage) - if (ExitStatus != SHELL_SUCCESS) { - // Allocate a buffer for exit data to pass to gBS->Exit(). - // This buffer will contain the empty string immediately followed by - // the shell's exit status. (The empty string is required by the UEFI spec) - ExitDataSize = (sizeof (CHAR16) + sizeof (SHELL_STATUS)); - ExitData = AllocatePool (ExitDataSize); - if (ExitData == NULL) { - return EFI_OUT_OF_RESOURCES; - } - ExitData[0] = '\0'; - // Use CopyMem to avoid alignment faults - CopyMem ((ExitData + 1), &ExitStatus, sizeof (ExitStatus)); - - gBS->Exit (ImageHandle, EFI_ABORTED, ExitDataSize, ExitData); - - ASSERT (FALSE); - return EFI_SUCCESS; - } else { - return EFI_SUCCESS; - } - } else { - return Status; + if (ShellCommandGetExit()) { + return ((EFI_STATUS)ShellCommandGetExitCode()); } + return (Status); } /** Sets all the alias' that were registered with the ShellCommandLib library. - @retval EFI_SUCCESS all init commands were run sucessfully. + @retval EFI_SUCCESS all init commands were run successfully. **/ EFI_STATUS EFIAPI @@ -708,10 +769,10 @@ IsScriptOnlyCommand( loaded image protocol installed on it. The FilePath will point to the device path for the file that was loaded. - @param[in, out] DevPath On a sucessful return the device path to the loaded image. - @param[in, out] FilePath On a sucessful return the device path to the file. + @param[in, out] DevPath On a successful return the device path to the loaded image. + @param[in, out] FilePath On a successful return the device path to the file. - @retval EFI_SUCCESS The 2 device paths were sucessfully returned. + @retval EFI_SUCCESS The 2 device paths were successfully returned. @retval other A error from gBS->HandleProtocol. @sa HandleProtocol @@ -832,9 +893,13 @@ ProcessCommandLine( ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = FALSE; ShellInfoObject.ShellInitSettings.Delay = 5; - // Start LoopVar at 1 to ignore Argv[0] which is the name of this binary - // (probably "Shell.efi") - for (LoopVar = 1 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) { + // + // Start LoopVar at 0 to parse only optional arguments at Argv[0] + // and parse other parameters from Argv[1]. This is for use case that + // UEFI Shell boot option is created, and OptionalData is provided + // that starts with shell command-line options. + // + for (LoopVar = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) { CurrentArg = gEfiShellParametersProtocol->Argv[LoopVar]; if (UnicodeCollation->StriColl ( UnicodeCollation, @@ -914,7 +979,7 @@ ProcessCommandLine( ) == 0) { ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = TRUE; } else if (StrnCmp (L"-", CurrentArg, 1) == 0) { - // Unrecognised option + // Unrecognized option ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), ShellInfoObject.HiiHandle, @@ -922,7 +987,14 @@ ProcessCommandLine( ); return EFI_INVALID_PARAMETER; } else { - ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(CurrentArg)); + // + // First argument should be Shell.efi image name + // + if (LoopVar == 0) { + continue; + } + + ShellInfoObject.ShellInitSettings.FileName = AllocateCopyPool(StrSize(CurrentArg), CurrentArg); if (ShellInfoObject.ShellInitSettings.FileName == NULL) { return (EFI_OUT_OF_RESOURCES); } @@ -930,12 +1002,10 @@ ProcessCommandLine( // We found `file-name`. // ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoStartup = 1; - - StrCpy (ShellInfoObject.ShellInitSettings.FileName, CurrentArg); LoopVar++; // Add `file-name-options` - for ( ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) { + for (Size = 0 ; LoopVar < gEfiShellParametersProtocol->Argc ; LoopVar++) { ASSERT((ShellInfoObject.ShellInitSettings.FileOptions == NULL && Size == 0) || (ShellInfoObject.ShellInitSettings.FileOptions != NULL)); StrnCatGrow(&ShellInfoObject.ShellInitSettings.FileOptions, &Size, @@ -973,19 +1043,17 @@ ProcessCommandLine( @param ImagePath the path to the image for shell. first place to look for the startup script @param FilePath the path to the file for shell. second place to look for the startup script. - @param[out] ExitStatus The exit code of the script. Ignored if NULL. - @retval EFI_SUCCESS the variable is initialized. **/ EFI_STATUS EFIAPI DoStartupScript( - IN EFI_DEVICE_PATH_PROTOCOL *ImagePath, - IN EFI_DEVICE_PATH_PROTOCOL *FilePath, - OUT SHELL_STATUS *ExitStatus + IN EFI_DEVICE_PATH_PROTOCOL *ImagePath, + IN EFI_DEVICE_PATH_PROTOCOL *FilePath ) { EFI_STATUS Status; + EFI_STATUS CalleeStatus; UINTN Delay; EFI_INPUT_KEY Key; SHELL_FILE_HANDLE FileHandle; @@ -1012,12 +1080,15 @@ DoStartupScript( if (FileStringPath == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(FileStringPath, ShellInfoObject.ShellInitSettings.FileName); + StrCpyS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileName); if (ShellInfoObject.ShellInitSettings.FileOptions != NULL) { - StrCat(FileStringPath, L" "); - StrCat(FileStringPath, ShellInfoObject.ShellInitSettings.FileOptions); + StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), L" ", NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1); + StrnCatS(FileStringPath, NewSize/sizeof(CHAR16), ShellInfoObject.ShellInitSettings.FileOptions, NewSize/sizeof(CHAR16) - StrLen(FileStringPath) -1); + } + Status = RunShellCommand(FileStringPath, &CalleeStatus); + if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit == TRUE) { + ShellCommandRegisterExit(gEfiShellProtocol->BatchIsActive(), (UINT64)CalleeStatus); } - Status = RunCommand(FileStringPath, ExitStatus); FreePool(FileStringPath); return (Status); @@ -1094,30 +1165,18 @@ DoStartupScript( // If we got a file, run it // if (!EFI_ERROR(Status) && FileHandle != NULL) { - Status = RunScriptFile ( - mStartupScript, - FileHandle, - L"", - ShellInfoObject.NewShellParametersProtocol, - ExitStatus - ); + Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol); ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle); } else { FileStringPath = ShellFindFilePath(mStartupScript); if (FileStringPath == NULL) { // - // we return success since we dont need to have a startup script + // we return success since we don't need to have a startup script // Status = EFI_SUCCESS; ASSERT(FileHandle == NULL); } else { - Status = RunScriptFile( - FileStringPath, - NULL, - L"", - ShellInfoObject.NewShellParametersProtocol, - ExitStatus - ); + Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol); FreePool(FileStringPath); } } @@ -1146,6 +1205,7 @@ DoShellPrompt ( CONST CHAR16 *CurDir; UINTN BufferSize; EFI_STATUS Status; + LIST_ENTRY OldBufferList; CurDir = NULL; @@ -1159,6 +1219,7 @@ DoShellPrompt ( return EFI_OUT_OF_RESOURCES; } + SaveBufferList(&OldBufferList); CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd"); // @@ -1182,12 +1243,13 @@ DoShellPrompt ( // if (!EFI_ERROR (Status)) { CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL; - Status = RunCommand(CmdLine, NULL); + Status = RunCommand(CmdLine); } // // Done with this command // + RestoreBufferList(&OldBufferList); FreePool (CmdLine); return Status; } @@ -1217,6 +1279,36 @@ AddBufferToFreeList( return (Buffer); } + +/** + Create a new buffer list and stores the old one to OldBufferList + + @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList. +**/ +VOID +SaveBufferList ( + OUT LIST_ENTRY *OldBufferList + ) +{ + CopyMem (OldBufferList, &ShellInfoObject.BufferToFreeList.Link, sizeof (LIST_ENTRY)); + InitializeListHead (&ShellInfoObject.BufferToFreeList.Link); +} + +/** + Restore previous nodes into BufferToFreeList . + + @param OldBufferList The temporary list head used to store the nodes in BufferToFreeList. +**/ +VOID +RestoreBufferList ( + IN OUT LIST_ENTRY *OldBufferList + ) +{ + FreeBufferList (&ShellInfoObject.BufferToFreeList); + CopyMem (&ShellInfoObject.BufferToFreeList.Link, OldBufferList, sizeof (LIST_ENTRY)); +} + + /** Add a buffer to the Line History List @@ -1229,14 +1321,40 @@ AddLineToCommandHistory( ) { BUFFER_LIST *Node; + BUFFER_LIST *Walker; + UINT16 MaxHistoryCmdCount; + UINT16 Count; + + Count = 0; + MaxHistoryCmdCount = PcdGet16(PcdShellMaxHistoryCommandCount); + + if (MaxHistoryCmdCount == 0) { + return ; + } + Node = AllocateZeroPool(sizeof(BUFFER_LIST)); ASSERT(Node != NULL); - Node->Buffer = AllocateZeroPool(StrSize(Buffer)); + Node->Buffer = AllocateCopyPool(StrSize(Buffer), Buffer); ASSERT(Node->Buffer != NULL); - StrCpy(Node->Buffer, Buffer); - InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link); + for ( Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link) + ; !IsNull(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link) + ; Walker = (BUFFER_LIST*)GetNextNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Walker->Link) + ){ + Count++; + } + if (Count < MaxHistoryCmdCount){ + InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link); + } else { + Walker = (BUFFER_LIST*)GetFirstNode(&ShellInfoObject.ViewingSettings.CommandHistory.Link); + RemoveEntryList(&Walker->Link); + if (Walker->Buffer != NULL) { + FreePool(Walker->Buffer); + } + FreePool(Walker); + InsertTailList(&ShellInfoObject.ViewingSettings.CommandHistory.Link, &Node->Link); + } } /** @@ -1265,11 +1383,77 @@ ShellConvertAlias( return (EFI_SUCCESS); } FreePool(*CommandString); - *CommandString = AllocateZeroPool(StrSize(NewString)); + *CommandString = AllocateCopyPool(StrSize(NewString), NewString); if (*CommandString == NULL) { return (EFI_OUT_OF_RESOURCES); } - StrCpy(*CommandString, NewString); + return (EFI_SUCCESS); +} + +/** + This function will eliminate unreplaced (and therefore non-found) environment variables. + + @param[in,out] CmdLine The command line to update. +**/ +EFI_STATUS +EFIAPI +StripUnreplacedEnvironmentVariables( + IN OUT CHAR16 *CmdLine + ) +{ + CHAR16 *FirstPercent; + CHAR16 *FirstQuote; + CHAR16 *SecondPercent; + CHAR16 *SecondQuote; + CHAR16 *CurrentLocator; + + for (CurrentLocator = CmdLine ; CurrentLocator != NULL ; ) { + FirstQuote = FindNextInstance(CurrentLocator, L"\"", TRUE); + FirstPercent = FindNextInstance(CurrentLocator, L"%", TRUE); + SecondPercent = FirstPercent!=NULL?FindNextInstance(FirstPercent+1, L"%", TRUE):NULL; + if (FirstPercent == NULL || SecondPercent == NULL) { + // + // If we ever don't have 2 % we are done. + // + break; + } + + if (FirstQuote!= NULL && FirstQuote < FirstPercent) { + SecondQuote = FindNextInstance(FirstQuote+1, L"\"", TRUE); + // + // Quote is first found + // + + if (SecondQuote < FirstPercent) { + // + // restart after the pair of " + // + CurrentLocator = SecondQuote + 1; + } else /* FirstPercent < SecondQuote */{ + // + // Restart on the first percent + // + CurrentLocator = FirstPercent; + } + continue; + } + + if (FirstQuote == NULL || SecondPercent < FirstQuote) { + if (IsValidEnvironmentVariableName(FirstPercent, SecondPercent)) { + // + // We need to remove from FirstPercent to SecondPercent + // + CopyMem(FirstPercent, SecondPercent + 1, StrSize(SecondPercent + 1)); + // + // don't need to update the locator. both % characters are gone. + // + } else { + CurrentLocator = SecondPercent + 1; + } + continue; + } + CurrentLocator = FirstQuote; + } return (EFI_SUCCESS); } @@ -1281,7 +1465,7 @@ ShellConvertAlias( @param[in] OriginalCommandLine The original command line - @retval NULL An error ocurred. + @retval NULL An error occurred. @return The new command line with no environment variables present. **/ CHAR16* @@ -1295,7 +1479,6 @@ ShellConvertVariables ( CHAR16 *NewCommandLine1; CHAR16 *NewCommandLine2; CHAR16 *Temp; - CHAR16 *Temp2; UINTN ItemSize; CHAR16 *ItemTemp; SCRIPT_FILE *CurrentScriptFile; @@ -1323,7 +1506,7 @@ ShellConvertVariables ( ; Temp = StrStr(Temp+1, AliasListNode->Alias) ){ // - // we need a preceeding and if there is space no ^ preceeding (if no space ignore) + // we need a preceding and if there is space no ^ preceding (if no space ignore) // if ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2)) { NewSize += StrSize(AliasListNode->CommandString); @@ -1344,7 +1527,7 @@ ShellConvertVariables ( ; Temp = StrStr(Temp+1, MasterEnvList) ){ // - // we need a preceeding and following % and if there is space no ^ preceeding (if no space ignore) + // we need a preceding and following % and if there is space no ^ preceding (if no space ignore) // if (*(Temp-1) == L'%' && *(Temp+StrLen(MasterEnvList)) == L'%' && ((((Temp-OriginalCommandLine)>2) && *(Temp-2) != L'^') || ((Temp-OriginalCommandLine)<=2))) { @@ -1356,7 +1539,7 @@ ShellConvertVariables ( // // now do the replacements... // - NewCommandLine1 = AllocateZeroPool(NewSize); + NewCommandLine1 = AllocateCopyPool(NewSize, OriginalCommandLine); NewCommandLine2 = AllocateZeroPool(NewSize); ItemTemp = AllocateZeroPool(ItemSize+(2*sizeof(CHAR16))); if (NewCommandLine1 == NULL || NewCommandLine2 == NULL || ItemTemp == NULL) { @@ -1365,16 +1548,24 @@ ShellConvertVariables ( SHELL_FREE_NON_NULL(ItemTemp); return (NULL); } - StrCpy(NewCommandLine1, OriginalCommandLine); for (MasterEnvList = EfiShellGetEnv(NULL) - ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL //&& *(MasterEnvList+1) != CHAR_NULL + ; MasterEnvList != NULL && *MasterEnvList != CHAR_NULL ; MasterEnvList += StrLen(MasterEnvList) + 1 ){ - StrCpy(ItemTemp, L"%"); - StrCat(ItemTemp, MasterEnvList); - StrCat(ItemTemp, L"%"); + StrCpyS( ItemTemp, + ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), + L"%" + ); + StrCatS( ItemTemp, + ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), + MasterEnvList + ); + StrCatS( ItemTemp, + ((ItemSize+(2*sizeof(CHAR16)))/sizeof(CHAR16)), + L"%" + ); ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, ItemTemp, EfiShellGetEnv(MasterEnvList), TRUE, FALSE); - StrCpy(NewCommandLine1, NewCommandLine2); + StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2); } if (CurrentScriptFile != NULL) { for (AliasListNode = (ALIAS_LIST*)GetFirstNode(&CurrentScriptFile->SubstList) @@ -1382,52 +1573,20 @@ ShellConvertVariables ( ; AliasListNode = (ALIAS_LIST*)GetNextNode(&CurrentScriptFile->SubstList, &AliasListNode->Link) ){ ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, AliasListNode->Alias, AliasListNode->CommandString, TRUE, FALSE); - StrCpy(NewCommandLine1, NewCommandLine2); + StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), 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)); - } - } + // + // Remove non-existent environment variables + // + StripUnreplacedEnvironmentVariables(NewCommandLine1); + // // Now cleanup any straggler intentionally ignored "%" characters // ShellCopySearchAndReplace(NewCommandLine1, NewCommandLine2, NewSize, L"^%", L"%", TRUE, FALSE); - StrCpy(NewCommandLine1, NewCommandLine2); + StrCpyS(NewCommandLine1, NewSize/sizeof(CHAR16), NewCommandLine2); FreePool(NewCommandLine2); FreePool(ItemTemp); @@ -1442,9 +1601,6 @@ ShellConvertVariables ( @param[in] StdIn The pointer to the Standard input. @param[in] StdOut The pointer to the Standard output. - @param[out] ExitStatus The exit code of the last command in the pipeline. - Ignored if NULL. - @retval EFI_SUCCESS The split command is executed successfully. @retval other Some error occurs when executing the split command. **/ @@ -1453,8 +1609,7 @@ EFIAPI RunSplitCommand( IN CONST CHAR16 *CmdLine, IN SHELL_FILE_HANDLE *StdIn, - IN SHELL_FILE_HANDLE *StdOut, - OUT SHELL_STATUS *ExitStatus + IN SHELL_FILE_HANDLE *StdOut ) { EFI_STATUS Status; @@ -1487,11 +1642,18 @@ RunSplitCommand( SHELL_FREE_NON_NULL(OurCommandLine); SHELL_FREE_NON_NULL(NextCommandLine); return (EFI_INVALID_PARAMETER); - } else if (NextCommandLine[0] != CHAR_NULL && - NextCommandLine[0] == L'a' && - NextCommandLine[1] == L' ' - ){ + } else if (NextCommandLine[0] == L'a' && + (NextCommandLine[1] == L' ' || NextCommandLine[1] == CHAR_NULL) + ){ CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0])); + while (NextCommandLine[0] == L' ') { + CopyMem(NextCommandLine, NextCommandLine+1, StrSize(NextCommandLine) - sizeof(NextCommandLine[0])); + } + if (NextCommandLine[0] == CHAR_NULL) { + SHELL_FREE_NON_NULL(OurCommandLine); + SHELL_FREE_NON_NULL(NextCommandLine); + return (EFI_INVALID_PARAMETER); + } Unicode = FALSE; } else { Unicode = TRUE; @@ -1508,7 +1670,7 @@ RunSplitCommand( ASSERT(Split->SplitStdOut != NULL); InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link); - Status = RunCommand(OurCommandLine, NULL); + Status = RunCommand(OurCommandLine); // // move the output from the first to the in to the second. @@ -1523,7 +1685,7 @@ RunSplitCommand( ShellInfoObject.NewEfiShellProtocol->SetFilePosition(ConvertShellHandleToEfiFileProtocol(Split->SplitStdIn), 0); if (!EFI_ERROR(Status)) { - Status = RunCommand(NextCommandLine, ExitStatus); + Status = RunCommand(NextCommandLine); } // @@ -1674,10 +1836,12 @@ GetOperationType( } // - // Test for file system change request. anything ending with : and cant have spaces. + // Test for file system change request. anything ending with first : and cant have spaces. // if (CmdName[(StrLen(CmdName)-1)] == L':') { - if (StrStr(CmdName, L" ") != NULL) { + if ( StrStr(CmdName, L" ") != NULL + || StrLen(StrStr(CmdName, L":")) > 1 + ) { return (Unknown_Invalid); } return (File_Sys_Change); @@ -1759,12 +1923,12 @@ IsValidSplit( return (EFI_OUT_OF_RESOURCES); } TempWalker = (CHAR16*)Temp; - GetNextParameter(&TempWalker, &FirstParameter); - - if (GetOperationType(FirstParameter) == Unknown_Invalid) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter); - SetLastError(SHELL_NOT_FOUND); - Status = EFI_NOT_FOUND; + if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CmdLine), TRUE))) { + if (GetOperationType(FirstParameter) == Unknown_Invalid) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter); + SetLastError(SHELL_NOT_FOUND); + Status = EFI_NOT_FOUND; + } } } @@ -1791,42 +1955,46 @@ VerifySplit( EFI_STATUS Status; // - // Verify up to the pipe or end character + // If this was the only item, then get out // - Status = IsValidSplit(CmdLine); - if (EFI_ERROR(Status)) { - return (Status); + if (!ContainsSplit(CmdLine)) { + return (EFI_SUCCESS); } // - // If this was the only item, then get out + // Verify up to the pipe or end character // - if (!ContainsSplit(CmdLine)) { - return (EFI_SUCCESS); + Status = IsValidSplit(CmdLine); + if (EFI_ERROR(Status)) { + return (Status); } // // recurse to verify the next item // - TempSpot = FindSplit(CmdLine)+1; + TempSpot = FindFirstCharacter(CmdLine, L"|", L'^') + 1; + if (*TempSpot == L'a' && + (*(TempSpot + 1) == L' ' || *(TempSpot + 1) == CHAR_NULL) + ) { + // If it's an ASCII pipe '|a' + TempSpot += 1; + } + return (VerifySplit(TempSpot)); } /** Process a split based operation. - @param[in] CmdLine Pointer to the command line to process - @param[out] ExitStatus The exit status of the command. Ignored if NULL. - Invalid if this function returns an error. + @param[in] CmdLine pointer to the command line to process @retval EFI_SUCCESS The operation was successful - @return an error occured. + @return an error occurred. **/ EFI_STATUS EFIAPI ProcessNewSplitCommandLine( - IN CONST CHAR16 *CmdLine, - OUT SHELL_STATUS *ExitStatus + IN CONST CHAR16 *CmdLine ) { SPLIT_LIST *Split; @@ -1847,14 +2015,9 @@ ProcessNewSplitCommandLine( } if (Split == NULL) { - Status = RunSplitCommand(CmdLine, NULL, NULL, ExitStatus); + Status = RunSplitCommand(CmdLine, NULL, NULL); } else { - Status = RunSplitCommand( - CmdLine, - Split->SplitStdIn, - Split->SplitStdOut, - ExitStatus - ); + Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut); } if (EFI_ERROR(Status)) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine); @@ -1913,9 +2076,9 @@ DoHelpUpdate( { CHAR16 *CurrentParameter; CHAR16 *Walker; - CHAR16 *LastWalker; CHAR16 *NewCommandLine; EFI_STATUS Status; + UINTN NewCmdLineSize; Status = EFI_SUCCESS; @@ -1926,21 +2089,26 @@ DoHelpUpdate( Walker = *CmdLine; while(Walker != NULL && *Walker != CHAR_NULL) { - LastWalker = Walker; - GetNextParameter(&Walker, &CurrentParameter); - if (StrStr(CurrentParameter, L"-?") == CurrentParameter) { - LastWalker[0] = L' '; - LastWalker[1] = L' '; - NewCommandLine = AllocateZeroPool(StrSize(L"help ") + StrSize(*CmdLine)); - if (NewCommandLine == NULL) { - Status = EFI_OUT_OF_RESOURCES; + if (!EFI_ERROR(GetNextParameter(&Walker, &CurrentParameter, StrSize(*CmdLine), TRUE))) { + if (StrStr(CurrentParameter, L"-?") == CurrentParameter) { + CurrentParameter[0] = L' '; + CurrentParameter[1] = L' '; + NewCmdLineSize = StrSize(L"help ") + StrSize(*CmdLine); + NewCommandLine = AllocateZeroPool(NewCmdLineSize); + if (NewCommandLine == NULL) { + Status = EFI_OUT_OF_RESOURCES; + break; + } + + // + // We know the space is sufficient since we just calculated it. + // + StrnCpyS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), L"help ", 5); + StrnCatS(NewCommandLine, NewCmdLineSize/sizeof(CHAR16), *CmdLine, StrLen(*CmdLine)); + SHELL_FREE_NON_NULL(*CmdLine); + *CmdLine = NewCommandLine; break; } - StrCpy(NewCommandLine, L"help "); - StrCat(NewCommandLine, *CmdLine); - SHELL_FREE_NON_NULL(*CmdLine); - *CmdLine = NewCommandLine; - break; } } @@ -1979,7 +2147,7 @@ SetLastError( @retval EFI_SUCCESS The operation was successful @retval EFI_OUT_OF_RESOURCES A memory allocation failed. - @return some other error occured + @return some other error occurred **/ EFI_STATUS EFIAPI @@ -2001,6 +2169,7 @@ ProcessCommandLineToFinal( if (EFI_ERROR(Status)) { return (Status); } + ASSERT (*CmdLine != NULL); TrimSpaces(CmdLine); @@ -2023,13 +2192,12 @@ ProcessCommandLineToFinal( /** Run an internal shell command. - This API will upadate the shell's environment since these commands are libraries. + This API will update the shell's environment since these commands are libraries. @param[in] CmdLine the command line to run. @param[in] FirstParameter the first parameter on the command line @param[in] ParamProtocol the shell parameters protocol pointer - - @param[out] ExitStatus The exit code of the command. Ignored if NULL. + @param[out] CommandStatus the status from the command line. @retval EFI_SUCCESS The command was completed. @retval EFI_ABORTED The command's operation was aborted. @@ -2040,7 +2208,7 @@ RunInternalCommand( IN CONST CHAR16 *CmdLine, IN CHAR16 *FirstParameter, IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol, - OUT SHELL_STATUS *ExitStatus OPTIONAL + OUT EFI_STATUS *CommandStatus ) { EFI_STATUS Status; @@ -2048,11 +2216,24 @@ RunInternalCommand( CHAR16 **Argv; SHELL_STATUS CommandReturnedStatus; BOOLEAN LastError; + CHAR16 *Walker; + CHAR16 *NewCmdLine; + + NewCmdLine = AllocateCopyPool (StrSize (CmdLine), CmdLine); + if (NewCmdLine == NULL) { + return EFI_OUT_OF_RESOURCES; + } + + for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) { + if (*Walker == L'^' && *(Walker+1) == L'#') { + CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0])); + } + } // // get the argc and argv updated for internal commands // - Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc); + Status = UpdateArgcArgv(ParamProtocol, NewCmdLine, Internal_Command, &Argv, &Argc); if (!EFI_ERROR(Status)) { // // Run the internal command. @@ -2060,6 +2241,14 @@ RunInternalCommand( Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError); if (!EFI_ERROR(Status)) { + if (CommandStatus != NULL) { + if (CommandReturnedStatus != SHELL_SUCCESS) { + *CommandStatus = (EFI_STATUS)(CommandReturnedStatus | MAX_BIT); + } else { + *CommandStatus = EFI_SUCCESS; + } + } + // // Update last error status. // some commands do not update last error. @@ -2067,9 +2256,6 @@ RunInternalCommand( if (LastError) { SetLastError(CommandReturnedStatus); } - if (ExitStatus != NULL) { - *ExitStatus = CommandReturnedStatus; - } // // Pass thru the exitcode from the app. @@ -2094,13 +2280,13 @@ RunInternalCommand( } // - // This is guarenteed to be called after UpdateArgcArgv no matter what else happened. + // This is guaranteed to be called after UpdateArgcArgv no matter what else happened. // This is safe even if the update API failed. In this case, it may be a no-op. // RestoreArgcArgv(ParamProtocol, &Argv, &Argc); // - // If a script is running and the command is not a scipt only command, then + // If a script is running and the command is not a script only command, then // change return value to success so the script won't halt (unless aborted). // // Script only commands have to be able halt the script since the script will @@ -2113,6 +2299,7 @@ RunInternalCommand( Status = EFI_SUCCESS; } + FreePool (NewCmdLine); return (Status); } @@ -2123,9 +2310,7 @@ RunInternalCommand( @param[in] CmdLine the command line to run. @param[in] FirstParameter the first parameter on the command line @param[in] ParamProtocol the shell parameters protocol pointer - - @param[out] ExitStatus The exit code of the command or file. - Ignored if NULL. + @param[out] CommandStatus the status from the command line. @retval EFI_SUCCESS The command was completed. @retval EFI_ABORTED The command's operation was aborted. @@ -2137,10 +2322,11 @@ RunCommandOrFile( IN CONST CHAR16 *CmdLine, IN CHAR16 *FirstParameter, IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol, - OUT SHELL_STATUS *ExitStatus + OUT EFI_STATUS *CommandStatus ) { EFI_STATUS Status; + EFI_STATUS StartStatus; CHAR16 *CommandWithPath; EFI_DEVICE_PATH_PROTOCOL *DevPath; SHELL_STATUS CalleeExitStatus; @@ -2152,12 +2338,7 @@ RunCommandOrFile( switch (Type) { case Internal_Command: - Status = RunInternalCommand( - CmdLine, - FirstParameter, - ParamProtocol, - &CalleeExitStatus - ); + Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol, CommandStatus); break; case Script_File_Name: case Efi_Application: @@ -2192,13 +2373,7 @@ RunCommandOrFile( } switch (Type) { case Script_File_Name: - Status = RunScriptFile ( - CommandWithPath, - NULL, - CmdLine, - ParamProtocol, - &CalleeExitStatus - ); + Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol); break; case Efi_Application: // @@ -2218,8 +2393,7 @@ RunCommandOrFile( DevPath, CmdLine, NULL, - NULL, - NULL + &StartStatus ); SHELL_FREE_NON_NULL(DevPath); @@ -2227,7 +2401,11 @@ RunCommandOrFile( if(EFI_ERROR (Status)) { CalleeExitStatus = (SHELL_STATUS) (Status & (~MAX_BIT)); } else { - CalleeExitStatus = SHELL_SUCCESS; + CalleeExitStatus = (SHELL_STATUS) StartStatus; + } + + if (CommandStatus != NULL) { + *CommandStatus = CalleeExitStatus; } // @@ -2252,10 +2430,6 @@ RunCommandOrFile( SHELL_FREE_NON_NULL(CommandWithPath); - if (ExitStatus != NULL) { - *ExitStatus = CalleeExitStatus; - } - return (Status); } @@ -2266,9 +2440,7 @@ RunCommandOrFile( @param[in] CmdLine the command line to run. @param[in] FirstParameter the first parameter on the command line. @param[in] ParamProtocol the shell parameters protocol pointer - - @param[out] ExitStatus The exit code of the command or file. - Ignored if NULL. + @param[out] CommandStatus the status from the command line. @retval EFI_SUCCESS The command was completed. @retval EFI_ABORTED The command's operation was aborted. @@ -2280,7 +2452,7 @@ SetupAndRunCommandOrFile( IN CHAR16 *CmdLine, IN CHAR16 *FirstParameter, IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol, - OUT SHELL_STATUS *ExitStatus + OUT EFI_STATUS *CommandStatus ) { EFI_STATUS Status; @@ -2299,13 +2471,8 @@ SetupAndRunCommandOrFile( // Now run the command, script, or application // if (!EFI_ERROR(Status)) { - Status = RunCommandOrFile( - Type, - CmdLine, - FirstParameter, - ParamProtocol, - ExitStatus - ); + TrimSpaces(&CmdLine); + Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol, CommandStatus); } // @@ -2330,16 +2497,16 @@ SetupAndRunCommandOrFile( command or dispatch an external application. @param[in] CmdLine The command line to parse. - @param[out] ExitStatus The exit code of the command. Ignored if NULL. + @param[out] CommandStatus The status from the command line. @retval EFI_SUCCESS The command was completed. @retval EFI_ABORTED The command's operation was aborted. **/ EFI_STATUS EFIAPI -RunCommand( - IN CONST CHAR16 *CmdLine, - OUT SHELL_STATUS *ExitStatus +RunShellCommand( + IN CONST CHAR16 *CmdLine, + OUT EFI_STATUS *CommandStatus ) { EFI_STATUS Status; @@ -2363,6 +2530,24 @@ RunCommand( TrimSpaces(&CleanOriginal); + // + // NULL out comments (leveraged from RunScriptFileHandle() ). + // The # character on a line is used to denote that all characters on the same line + // and to the right of the # are to be ignored by the shell. + // Afterwards, again remove spaces, in case any were between the last command-parameter and '#'. + // + for (TempWalker = CleanOriginal; TempWalker != NULL && *TempWalker != CHAR_NULL; TempWalker++) { + if (*TempWalker == L'^') { + if (*(TempWalker + 1) == L'#') { + TempWalker++; + } + } else if (*TempWalker == L'#') { + *TempWalker = CHAR_NULL; + } + } + + TrimSpaces(&CleanOriginal); + // // Handle case that passed in command line is just 1 or more " " characters. // @@ -2378,10 +2563,10 @@ RunCommand( } // - // We dont do normal processing with a split command line (output from one command input to another) + // We don't do normal processing with a split command line (output from one command input to another) // if (ContainsSplit(CleanOriginal)) { - Status = ProcessNewSplitCommandLine(CleanOriginal, ExitStatus); + Status = ProcessNewSplitCommandLine(CleanOriginal); SHELL_FREE_NON_NULL(CleanOriginal); return (Status); } @@ -2395,33 +2580,30 @@ RunCommand( return (EFI_OUT_OF_RESOURCES); } TempWalker = CleanOriginal; - GetNextParameter(&TempWalker, &FirstParameter); - - // - // Depending on the first parameter we change the behavior - // - switch (Type = GetOperationType(FirstParameter)) { - case File_Sys_Change: - Status = ChangeMappedDrive(CleanOriginal); - break; - case Internal_Command: - case Script_File_Name: - case Efi_Application: - Status = SetupAndRunCommandOrFile( - Type, - CleanOriginal, - FirstParameter, - ShellInfoObject.NewShellParametersProtocol, - ExitStatus - ); - break; - default: - // - // Whatever was typed, it was invalid. - // - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter); - SetLastError(SHELL_NOT_FOUND); - break; + if (!EFI_ERROR(GetNextParameter(&TempWalker, &FirstParameter, StrSize(CleanOriginal), TRUE))) { + // + // Depending on the first parameter we change the behavior + // + switch (Type = GetOperationType(FirstParameter)) { + case File_Sys_Change: + Status = ChangeMappedDrive (FirstParameter); + break; + case Internal_Command: + case Script_File_Name: + case Efi_Application: + Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol, CommandStatus); + break; + default: + // + // Whatever was typed, it was invalid. + // + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter); + SetLastError(SHELL_NOT_FOUND); + break; + } + } else { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter); + SetLastError(SHELL_NOT_FOUND); } SHELL_FREE_NON_NULL(CleanOriginal); @@ -2430,9 +2612,30 @@ RunCommand( return (Status); } +/** + Function will process and run a command line. + + This will determine if the command line represents an internal shell + command or dispatch an external application. + + @param[in] CmdLine The command line to parse. + + @retval EFI_SUCCESS The command was completed. + @retval EFI_ABORTED The command's operation was aborted. +**/ +EFI_STATUS +EFIAPI +RunCommand( + IN CONST CHAR16 *CmdLine + ) +{ + return (RunShellCommand(CmdLine, NULL)); +} + + STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002}; /** - Function determins if the CommandName COULD be a valid command. It does not determine whether + Function determines if the CommandName COULD be a valid command. It does not determine whether this is a valid command. It only checks for invalid characters. @param[in] CommandName The name to check @@ -2468,21 +2671,19 @@ IsValidCommandName( @param[in] Handle The handle to the already opened file. @param[in] Name The name of the script file. - @param[out] ExitStatus The exit code of the script. Ignored if NULL. - - @retval EFI_SUCCESS the script completed sucessfully + @retval EFI_SUCCESS the script completed successfully **/ EFI_STATUS EFIAPI RunScriptFileHandle ( - IN SHELL_FILE_HANDLE Handle, - IN CONST CHAR16 *Name, - OUT SHELL_STATUS *ExitStatus + IN SHELL_FILE_HANDLE Handle, + IN CONST CHAR16 *Name ) { EFI_STATUS Status; SCRIPT_FILE *NewScriptFile; UINTN LoopVar; + UINTN PrintBuffSize; CHAR16 *CommandLine; CHAR16 *CommandLine2; CHAR16 *CommandLine3; @@ -2493,13 +2694,12 @@ RunScriptFileHandle ( CONST CHAR16 *CurDir; UINTN LineCount; CHAR16 LeString[50]; - SHELL_STATUS CalleeExitStatus; + LIST_ENTRY OldBufferList; ASSERT(!ShellCommandGetScriptExit()); - - CalleeExitStatus = SHELL_SUCCESS; PreScriptEchoState = ShellCommandGetEchoState(); + PrintBuffSize = PcdGet16(PcdShellPrintBufferSize); NewScriptFile = (SCRIPT_FILE*)AllocateZeroPool(sizeof(SCRIPT_FILE)); if (NewScriptFile == NULL) { @@ -2574,12 +2774,12 @@ RunScriptFileHandle ( // // Now enumerate through the commands and run each one. // - CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize)); + CommandLine = AllocateZeroPool(PrintBuffSize); if (CommandLine == NULL) { DeleteScriptFileStruct(NewScriptFile); return (EFI_OUT_OF_RESOURCES); } - CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize)); + CommandLine2 = AllocateZeroPool(PrintBuffSize); if (CommandLine2 == NULL) { FreePool(CommandLine); DeleteScriptFileStruct(NewScriptFile); @@ -2591,15 +2791,23 @@ RunScriptFileHandle ( ; // conditional increment in the body of the loop ){ ASSERT(CommandLine2 != NULL); - StrCpy(CommandLine2, NewScriptFile->CurrentCommand->Cl); + StrnCpyS( CommandLine2, + PrintBuffSize/sizeof(CHAR16), + NewScriptFile->CurrentCommand->Cl, + PrintBuffSize/sizeof(CHAR16) - 1 + ); + + SaveBufferList(&OldBufferList); // // NULL out comments // for (CommandLine3 = CommandLine2 ; CommandLine3 != NULL && *CommandLine3 != CHAR_NULL ; CommandLine3++) { if (*CommandLine3 == L'^') { - if (*(CommandLine3+1) == L'#' || *(CommandLine3+1) == L':') { + if ( *(CommandLine3+1) == L':') { CopyMem(CommandLine3, CommandLine3+1, StrSize(CommandLine3) - sizeof(CommandLine3[0])); + } else if (*(CommandLine3+1) == L'#') { + CommandLine3++; } } else if (*CommandLine3 == L'#') { *CommandLine3 = CHAR_NULL; @@ -2610,7 +2818,11 @@ RunScriptFileHandle ( // // Due to variability in starting the find and replace action we need to have both buffers the same. // - StrCpy(CommandLine, CommandLine2); + StrnCpyS( CommandLine, + PrintBuffSize/sizeof(CHAR16), + CommandLine2, + PrintBuffSize/sizeof(CHAR16) - 1 + ); // // Remove the %0 to %9 from the command line (if we have some arguments) @@ -2618,51 +2830,55 @@ RunScriptFileHandle ( if (NewScriptFile->Argv != NULL) { switch (NewScriptFile->Argc) { default: - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", NewScriptFile->Argv[9], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", NewScriptFile->Argv[9], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 9: - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", NewScriptFile->Argv[8], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", NewScriptFile->Argv[8], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 8: - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", NewScriptFile->Argv[7], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", NewScriptFile->Argv[7], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 7: - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", NewScriptFile->Argv[6], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", NewScriptFile->Argv[6], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 6: - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", NewScriptFile->Argv[5], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", NewScriptFile->Argv[5], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 5: - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", NewScriptFile->Argv[4], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", NewScriptFile->Argv[4], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 4: - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", NewScriptFile->Argv[3], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", NewScriptFile->Argv[3], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 3: - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", NewScriptFile->Argv[2], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", NewScriptFile->Argv[2], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 2: - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", NewScriptFile->Argv[1], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", NewScriptFile->Argv[1], FALSE, FALSE); ASSERT_EFI_ERROR(Status); case 1: - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%0", NewScriptFile->Argv[0], FALSE, TRUE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%0", NewScriptFile->Argv[0], FALSE, FALSE); ASSERT_EFI_ERROR(Status); break; case 0: break; } } - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%1", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%2", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%3", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%4", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%5", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%6", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%7", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PcdGet16 (PcdShellPrintBufferSize), L"%8", L"\"\"", FALSE, FALSE); - Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PcdGet16 (PcdShellPrintBufferSize), L"%9", L"\"\"", FALSE, FALSE); - - StrCpy(CommandLine2, CommandLine); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%1", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%2", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%3", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%4", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%5", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%6", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%7", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine, CommandLine2, PrintBuffSize, L"%8", L"\"\"", FALSE, FALSE); + Status = ShellCopySearchAndReplace(CommandLine2, CommandLine, PrintBuffSize, L"%9", L"\"\"", FALSE, FALSE); + + StrnCpyS( CommandLine2, + PrintBuffSize/sizeof(CHAR16), + CommandLine, + PrintBuffSize/sizeof(CHAR16) - 1 + ); LastCommand = NewScriptFile->CurrentCommand; @@ -2681,7 +2897,7 @@ RunScriptFileHandle ( // PreCommandEchoState = ShellCommandGetEchoState(); ShellCommandSetEchoState(FALSE); - Status = RunCommand(CommandLine3+1, NULL); + Status = RunCommand(CommandLine3+1); // // If command was "@echo -off" or "@echo -on" then don't restore echo state @@ -2703,7 +2919,7 @@ RunScriptFileHandle ( } ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2); } - Status = RunCommand(CommandLine3, NULL); + Status = RunCommand(CommandLine3); } } @@ -2711,24 +2927,25 @@ RunScriptFileHandle ( // // ShellCommandGetExitCode() always returns a UINT64 // - CalleeExitStatus = (SHELL_STATUS) ShellCommandGetExitCode(); - UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", CalleeExitStatus); + UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellCommandGetExitCode()); DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE);); InternalEfiShellSetEnv(L"lasterror", LeString, TRUE); ShellCommandRegisterExit(FALSE, 0); Status = EFI_SUCCESS; + RestoreBufferList(&OldBufferList); break; } if (ShellGetExecutionBreakFlag()) { + RestoreBufferList(&OldBufferList); break; } if (EFI_ERROR(Status)) { - CalleeExitStatus = (SHELL_STATUS) Status; + RestoreBufferList(&OldBufferList); break; } if (ShellCommandGetExit()) { - CalleeExitStatus = (SHELL_STATUS) ShellCommandGetExitCode(); + RestoreBufferList(&OldBufferList); break; } } @@ -2747,6 +2964,7 @@ RunScriptFileHandle ( NewScriptFile->CurrentCommand->Reset = TRUE; } } + RestoreBufferList(&OldBufferList); } @@ -2760,11 +2978,6 @@ RunScriptFileHandle ( if (ShellCommandGetCurrentScriptFile()==NULL) { ShellCommandSetEchoState(PreScriptEchoState); } - - if (ExitStatus != NULL) { - *ExitStatus = CalleeExitStatus; - } - return (EFI_SUCCESS); } @@ -2776,18 +2989,15 @@ RunScriptFileHandle ( @param[in] CmdLine the command line to run. @param[in] ParamProtocol the shell parameters protocol pointer - @param[out] ExitStatus The exit code of the script. Ignored if NULL. - - @retval EFI_SUCCESS the script completed sucessfully + @retval EFI_SUCCESS the script completed successfully **/ EFI_STATUS EFIAPI RunScriptFile ( - IN CONST CHAR16 *ScriptPath, - IN SHELL_FILE_HANDLE Handle OPTIONAL, - IN CONST CHAR16 *CmdLine, - IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol, - OUT SHELL_STATUS *ExitStatus + IN CONST CHAR16 *ScriptPath, + IN SHELL_FILE_HANDLE Handle OPTIONAL, + IN CONST CHAR16 *CmdLine, + IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol ) { EFI_STATUS Status; @@ -2802,7 +3012,7 @@ RunScriptFile ( // // get the argc and argv updated for scripts // - Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc); + Status = UpdateArgcArgv(ParamProtocol, CmdLine, Script_File_Name, &Argv, &Argc); if (!EFI_ERROR(Status)) { if (Handle == NULL) { @@ -2814,7 +3024,7 @@ RunScriptFile ( // // run it // - Status = RunScriptFileHandle(FileHandle, ScriptPath, ExitStatus); + Status = RunScriptFileHandle(FileHandle, ScriptPath); // // now close the file @@ -2822,15 +3032,50 @@ RunScriptFile ( ShellCloseFile(&FileHandle); } } else { - Status = RunScriptFileHandle(Handle, ScriptPath, ExitStatus); + Status = RunScriptFileHandle(Handle, ScriptPath); } } // - // This is guarenteed to be called after UpdateArgcArgv no matter what else happened. + // This is guaranteed to be called after UpdateArgcArgv no matter what else happened. // This is safe even if the update API failed. In this case, it may be a no-op. // RestoreArgcArgv(ParamProtocol, &Argv, &Argc); return (Status); } + +/** + Return the pointer to the first occurrence of any character from a list of characters. + + @param[in] String the string to parse + @param[in] CharacterList the list of character to look for + @param[in] EscapeCharacter An escape character to skip + + @return the location of the first character in the string + @retval CHAR_NULL no instance of any character in CharacterList was found in String +**/ +CONST CHAR16* +EFIAPI +FindFirstCharacter( + IN CONST CHAR16 *String, + IN CONST CHAR16 *CharacterList, + IN CONST CHAR16 EscapeCharacter + ) +{ + UINT32 WalkChar; + UINT32 WalkStr; + + for (WalkStr = 0; WalkStr < StrLen(String); WalkStr++) { + if (String[WalkStr] == EscapeCharacter) { + WalkStr++; + continue; + } + for (WalkChar = 0; WalkChar < StrLen(CharacterList); WalkChar++) { + if (String[WalkStr] == CharacterList[WalkChar]) { + return (&String[WalkStr]); + } + } + } + return (String + StrLen(String)); +}