X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShell.c;h=1c617abff509301a48410c0b4611d56b3fc2dd63;hb=ca53c0af0037ad585f7455efc9ee1656bec8d176;hp=ddd5d1712a704d72580bf9336bad3d6caf2f08ce;hpb=3c865f2064d37eaccd1693b878596d5138b0b38e;p=mirror_edk2.git diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index ddd5d1712a..1c617abff5 100644 --- a/ShellPkg/Application/Shell/Shell.c +++ b/ShellPkg/Application/Shell/Shell.c @@ -1,7 +1,8 @@ /** @file This is THE shell (application) - Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.
+ Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.
+ Copyright (c) 2013, 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 @@ -23,22 +24,24 @@ SHELL_INFO ShellInfoObject = { FALSE, FALSE, { - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, + {{ + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0 + }}, 0, NULL, NULL }, - {0,0}, + {{NULL, NULL}, NULL}, { - {0,0}, + {{NULL, NULL}, NULL}, 0, 0, TRUE @@ -50,14 +53,176 @@ SHELL_INFO ShellInfoObject = { NULL, NULL, NULL, - {0,0,NULL,NULL}, - {0,0}, + {{NULL, NULL}, NULL, NULL}, + {{NULL, NULL}, NULL, NULL}, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + FALSE }; STATIC CONST CHAR16 mScriptExtension[] = L".NSH"; STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI"; STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh"; +/** + Cleans off leading and trailing spaces and tabs + + @param[in] String pointer to the string to trim them off +**/ +EFI_STATUS +EFIAPI +TrimSpaces( + IN CHAR16 **String + ) +{ + ASSERT(String != NULL); + ASSERT(*String!= NULL); + // + // Remove any spaces and tabs at the beginning of the (*String). + // + while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) { + CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0])); + } + + // + // Remove any spaces at the end of the (*String). + // + while ((*String)[StrLen((*String))-1] == L' ') { + (*String)[StrLen((*String))-1] = CHAR_NULL; + } + + return (EFI_SUCCESS); +} + +/** + Find a command line contains a split operation + + @param[in] CmdLine The command line to parse. + + @retval A pointer to the | character in CmdLine or NULL if not present. +**/ +CONST +CHAR16* +EFIAPI +FindSplit( + IN CONST CHAR16 *CmdLine + ) +{ + 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; + } + } + } + return (TempSpot); +} + +/** + Determine if a command line contains a split operation + + @param[in] CmdLine The command line to parse. + + @retval TRUE CmdLine has a valid split. + @retval FALSE CmdLine does not have a valid split. +**/ +BOOLEAN +EFIAPI +ContainsSplit( + IN CONST CHAR16 *CmdLine + ) +{ + CONST CHAR16 *TempSpot; + TempSpot = FindSplit(CmdLine); + return (TempSpot != NULL && *TempSpot != CHAR_NULL); +} + +/** + Function to start monitoring for CTRL-S using SimpleTextInputEx. This + 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. +**/ +EFI_STATUS +EFIAPI +InternalEfiShellStartCtrlSMonitor( + VOID + ) +{ + EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *SimpleEx; + EFI_KEY_DATA KeyData; + EFI_STATUS Status; + + Status = gBS->OpenProtocol( + gST->ConsoleInHandle, + &gEfiSimpleTextInputExProtocolGuid, + (VOID**)&SimpleEx, + gImageHandle, + NULL, + EFI_OPEN_PROTOCOL_GET_PROTOCOL); + if (EFI_ERROR(Status)) { + ShellPrintHiiEx( + -1, + -1, + NULL, + STRING_TOKEN (STR_SHELL_NO_IN_EX), + ShellInfoObject.HiiHandle); + return (EFI_SUCCESS); + } + + KeyData.KeyState.KeyToggleState = 0; + KeyData.Key.ScanCode = 0; + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED; + KeyData.Key.UnicodeChar = L's'; + + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle1); + + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle2); + } + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_LEFT_CONTROL_PRESSED; + KeyData.Key.UnicodeChar = 19; + + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle3); + } + KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; + if (!EFI_ERROR(Status)) { + Status = SimpleEx->RegisterKeyNotify( + SimpleEx, + &KeyData, + NotificationFunction, + &ShellInfoObject.CtrlSNotifyHandle4); + } + return (Status); +} + + + /** The entry point for the application. @@ -75,17 +240,11 @@ UefiMain ( IN EFI_SYSTEM_TABLE *SystemTable ) { - EFI_STATUS Status; - CHAR16 *TempString; - UINTN Size; -// EFI_INPUT_KEY Key; - -// gST->ConOut->OutputString(gST->ConOut, L"ReadKeyStroke Calling\r\n"); -// -// Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); -// -// gST->ConOut->OutputString(gST->ConOut, L"ReadKeyStroke Done\r\n"); -// gBS->Stall (1000000); + EFI_STATUS Status; + CHAR16 *TempString; + UINTN Size; + EFI_HANDLE ConInHandle; + EFI_SIMPLE_TEXT_INPUT_PROTOCOL *OldConIn; if (PcdGet8(PcdShellSupportLevel) > 3) { return (EFI_UNSUPPORTED); @@ -215,11 +374,28 @@ UefiMain ( 0, gST->ConOut->Mode->CursorRow, NULL, - STRING_TOKEN (STR_VER_OUTPUT_MAIN), + STRING_TOKEN (STR_VER_OUTPUT_MAIN_SHELL), ShellInfoObject.HiiHandle, SupportLevel[PcdGet8(PcdShellSupportLevel)], gEfiShellProtocol->MajorVersion, - gEfiShellProtocol->MinorVersion, + gEfiShellProtocol->MinorVersion + ); + + ShellPrintHiiEx ( + -1, + -1, + NULL, + STRING_TOKEN (STR_VER_OUTPUT_MAIN_SUPPLIER), + ShellInfoObject.HiiHandle, + (CHAR16 *) PcdGetPtr (PcdShellSupplier) + ); + + ShellPrintHiiEx ( + -1, + -1, + NULL, + STRING_TOKEN (STR_VER_OUTPUT_MAIN_UEFI), + ShellInfoObject.HiiHandle, (gST->Hdr.Revision&0xffff0000)>>16, (gST->Hdr.Revision&0x0000ffff), gST->FirmwareVendor, @@ -271,9 +447,26 @@ UefiMain ( // // Set up the event for CTRL-C monitoring... // + Status = InernalEfiShellStartMonitor(); + } + + if (!EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { + // + // Set up the event for CTRL-S monitoring... + // + Status = InternalEfiShellStartCtrlSMonitor(); + } - ///@todo add support for using SimpleInputEx here - // if SimpleInputEx is not available display a warning. + if (!EFI_ERROR(Status) && ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { + // + // close off the gST->ConIn + // + OldConIn = gST->ConIn; + ConInHandle = gST->ConsoleInHandle; + gST->ConIn = CreateSimpleTextInOnFile((SHELL_FILE_HANDLE)&FileInterfaceNulFile, &gST->ConsoleInHandle); + } else { + OldConIn = NULL; + ConInHandle = NULL; } if (!EFI_ERROR(Status) && PcdGet8(PcdShellSupportLevel) >= 1) { @@ -283,7 +476,7 @@ UefiMain ( Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath); } - if ((PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { + if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit && !ShellCommandGetExit() && (PcdGet8(PcdShellSupportLevel) >= 3 || PcdGetBool(PcdShellForceConsole)) && !EFI_ERROR(Status) && !ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { // // begin the UI waiting loop // @@ -303,12 +496,22 @@ UefiMain ( ShellInfoObject.ConsoleInfo->Enabled = TRUE; ShellInfoObject.ConsoleInfo->RowCounter = 0; + // + // Reset the CTRL-C event (yes we ignore the return values) + // + Status = gBS->CheckEvent (ShellInfoObject.NewEfiShellProtocol->ExecutionBreak); + // // Display Prompt // Status = DoShellPrompt(); } while (!ShellCommandGetExit()); } + if (OldConIn != NULL && ConInHandle != NULL) { + CloseSimpleTextInOnFile (gST->ConIn); + gST->ConIn = OldConIn; + gST->ConsoleInHandle = ConInHandle; + } } } @@ -319,11 +522,6 @@ UefiMain ( gBS->CloseEvent(ShellInfoObject.UserBreakTimer); DEBUG_CODE(ShellInfoObject.UserBreakTimer = NULL;); } - - if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){ - ShellInfoObject.NewEfiShellProtocol->SetEnv(L"cwd", L"", TRUE); - } - if (ShellInfoObject.ImageDevPath != NULL) { FreePool(ShellInfoObject.ImageDevPath); DEBUG_CODE(ShellInfoObject.ImageDevPath = NULL;); @@ -337,6 +535,9 @@ UefiMain ( DEBUG_CODE(ShellInfoObject.NewShellParametersProtocol = NULL;); } if (ShellInfoObject.NewEfiShellProtocol != NULL){ + if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){ + InternalEfiShellSetEnv(L"cwd", NULL, TRUE); + } CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol); DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;); } @@ -375,6 +576,9 @@ UefiMain ( DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;); } + if (ShellCommandGetExit()) { + return ((EFI_STATUS)ShellCommandGetExitCode()); + } return (Status); } @@ -460,16 +664,14 @@ IsScriptOnlyCommand( return (FALSE); } - - /** This function will populate the 2 device path protocol parameters based on the global gImageHandle. The DevPath will point to the device path for the handle that has 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 sucessful return the device path to the loaded image. + @param[in, out] FilePath On a sucessful return the device path to the file. @retval EFI_SUCCESS The 2 device paths were sucessfully returned. @retval other A error from gBS->HandleProtocol. @@ -510,6 +712,11 @@ GetDevicePathsForImageAndFile ( if (!EFI_ERROR (Status)) { *DevPath = DuplicateDevicePath (ImageDevicePath); *FilePath = DuplicateDevicePath (LoadedImage->FilePath); + gBS->CloseProtocol( + LoadedImage->DeviceHandle, + &gEfiDevicePathProtocolGuid, + gImageHandle, + NULL); } gBS->CloseProtocol( gImageHandle, @@ -530,8 +737,10 @@ STATIC CONST SHELL_PARAM_ITEM mShellParamList[] = { {L"-noversion", TypeFlag}, {L"-startup", TypeFlag}, {L"-delay", TypeValue}, + {L"-_exit", TypeFlag}, {NULL, TypeMax} }; + /** Process all Uefi Shell 2.0 command line options. @@ -572,6 +781,7 @@ ProcessCommandLine( UINTN Count; UINTN LoopVar; CHAR16 *ProblemParam; + UINT64 Intermediate; Package = NULL; ProblemParam = NULL; @@ -582,7 +792,7 @@ ProcessCommandLine( Size = 0; TempConst = ShellCommandLineGetRawValue(Package, Count++); if (TempConst != NULL && StrLen(TempConst)) { - ShellInfoObject.ShellInitSettings.FileName = AllocatePool(StrSize(TempConst)); + ShellInfoObject.ShellInitSettings.FileName = AllocateZeroPool(StrSize(TempConst)); if (ShellInfoObject.ShellInitSettings.FileName == NULL) { return (EFI_OUT_OF_RESOURCES); } @@ -635,18 +845,21 @@ ProcessCommandLine( ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoMap = ShellCommandLineGetFlag(Package, L"-nomap"); ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoVersion = ShellCommandLineGetFlag(Package, L"-noversion"); ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay = ShellCommandLineGetFlag(Package, L"-delay"); + ShellInfoObject.ShellInitSettings.BitUnion.Bits.Exit = ShellCommandLineGetFlag(Package, L"-_exit"); + + ShellInfoObject.ShellInitSettings.Delay = 5; - if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay) { + if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoInterrupt) { + ShellInfoObject.ShellInitSettings.Delay = 0; + } else if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.Delay) { TempConst = ShellCommandLineGetValue(Package, L"-delay"); - if (TempConst != NULL) { - ShellInfoObject.ShellInitSettings.Delay = StrDecimalToUintn (TempConst); - } else { - ShellInfoObject.ShellInitSettings.Delay = 5; + if (TempConst != NULL && *TempConst == L':') { + TempConst++; + } + if (TempConst != NULL && !EFI_ERROR(ShellConvertStringToUint64(TempConst, &Intermediate, FALSE, FALSE))) { + ShellInfoObject.ShellInitSettings.Delay = (UINTN)Intermediate; } - } else { - ShellInfoObject.ShellInitSettings.Delay = 5; } - ShellCommandLineFreeVarList(Package); return (Status); @@ -676,7 +889,9 @@ DoStartupScript( EFI_DEVICE_PATH_PROTOCOL *NewPath; EFI_DEVICE_PATH_PROTOCOL *NamePath; CHAR16 *FileStringPath; + CHAR16 *TempSpot; UINTN NewSize; + CONST CHAR16 *MapName; Key.UnicodeChar = CHAR_NULL; Key.ScanCode = 0; @@ -715,18 +930,22 @@ DoStartupScript( return (EFI_SUCCESS); } + gST->ConOut->EnableCursor(gST->ConOut, FALSE); // // print out our warning and see if they press a key // - for ( Status = EFI_UNSUPPORTED, Delay = (ShellInfoObject.ShellInitSettings.Delay * 10) - ; Delay > 0 && EFI_ERROR(Status) + for ( Status = EFI_UNSUPPORTED, Delay = ShellInfoObject.ShellInitSettings.Delay + ; Delay != 0 && EFI_ERROR(Status) ; Delay-- ){ - ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay/10); - gBS->Stall (100000); - Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); + ShellPrintHiiEx(0, gST->ConOut->Mode->CursorRow, NULL, STRING_TOKEN (STR_SHELL_STARTUP_QUESTION), ShellInfoObject.HiiHandle, Delay); + gBS->Stall (1000000); + if (!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoConsoleIn) { + Status = gST->ConIn->ReadKeyStroke (gST->ConIn, &Key); + } } ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CRLF), ShellInfoObject.HiiHandle); + gST->ConOut->EnableCursor(gST->ConOut, TRUE); // // ESC was pressed @@ -735,25 +954,43 @@ DoStartupScript( return (EFI_SUCCESS); } - NamePath = FileDevicePath (NULL, mStartupScript); // - // Try the first location + // Try the first location (must be file system) // - NewPath = AppendDevicePathNode (ImagePath, NamePath); - Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0); + MapName = ShellInfoObject.NewEfiShellProtocol->GetMapFromDevicePath(&ImagePath); + if (MapName != NULL) { + FileStringPath = NULL; + NewSize = 0; + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0); + if (FileStringPath == NULL) { + Status = EFI_OUT_OF_RESOURCES; + } else { + TempSpot = StrStr(FileStringPath, L";"); + if (TempSpot != NULL) { + *TempSpot = CHAR_NULL; + } + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0); + PathRemoveLastItem(FileStringPath); + FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, mStartupScript, 0); + Status = ShellInfoObject.NewEfiShellProtocol->OpenFileByName(FileStringPath, &FileHandle, EFI_FILE_MODE_READ); + FreePool(FileStringPath); + } + } if (EFI_ERROR(Status)) { + NamePath = FileDevicePath (NULL, mStartupScript); + NewPath = AppendDevicePathNode (ImagePath, NamePath); + FreePool(NamePath); + // - // Try the second location + // Try the location // - FreePool(NewPath); - NewPath = AppendDevicePathNode (FilePath , NamePath); Status = InternalOpenFileDevicePath(NewPath, &FileHandle, EFI_FILE_MODE_READ, 0); + FreePool(NewPath); } - // // If we got a file, run it // - if (!EFI_ERROR(Status)) { + if (!EFI_ERROR(Status) && FileHandle != NULL) { Status = RunScriptFileHandle (FileHandle, mStartupScript); ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle); } else { @@ -770,8 +1007,6 @@ DoStartupScript( } } - FreePool(NamePath); - FreePool(NewPath); return (Status); } @@ -893,10 +1128,10 @@ AddLineToCommandHistory( Checks if a string is an alias for another command. If yes, then it replaces the alias name with the correct command name. - @param[in,out] CommandString Upon entry the potential alias. Upon return the - command name if it was an alias. If it was not - an alias it will be unchanged. This function may - change the buffer to fit the command name. + @param[in, out] CommandString Upon entry the potential alias. Upon return the + command name if it was an alias. If it was not + an alias it will be unchanged. This function may + change the buffer to fit the command name. @retval EFI_SUCCESS The name was changed. @retval EFI_SUCCESS The name was not an alias. @@ -915,7 +1150,7 @@ ShellConvertAlias( return (EFI_SUCCESS); } FreePool(*CommandString); - *CommandString = AllocatePool(StrSize(NewString)); + *CommandString = AllocateZeroPool(StrSize(NewString)); if (*CommandString == NULL) { return (EFI_OUT_OF_RESOURCES); } @@ -945,6 +1180,7 @@ ShellConvertVariables ( CHAR16 *NewCommandLine1; CHAR16 *NewCommandLine2; CHAR16 *Temp; + CHAR16 *Temp2; UINTN ItemSize; CHAR16 *ItemTemp; SCRIPT_FILE *CurrentScriptFile; @@ -1002,15 +1238,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... // @@ -1042,8 +1269,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); @@ -1089,7 +1359,16 @@ RunSplitCommand( NextCommandLine = StrnCatGrow(&NextCommandLine, &Size1, StrStr(CmdLine, L"|")+1, 0); OurCommandLine = StrnCatGrow(&OurCommandLine , &Size2, CmdLine , StrStr(CmdLine, L"|") - CmdLine); - if (NextCommandLine[0] != CHAR_NULL && + + if (NextCommandLine == NULL || OurCommandLine == NULL) { + SHELL_FREE_NON_NULL(OurCommandLine); + SHELL_FREE_NON_NULL(NextCommandLine); + return (EFI_OUT_OF_RESOURCES); + } else if (StrStr(OurCommandLine, L"|") != NULL || Size1 == 0 || Size2 == 0) { + 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' ' ){ @@ -1110,7 +1389,6 @@ RunSplitCommand( ASSERT(Split->SplitStdOut != NULL); InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link); - ASSERT(StrStr(OurCommandLine, L"|") == NULL); Status = RunCommand(OurCommandLine); // @@ -1152,6 +1430,77 @@ RunSplitCommand( return (Status); } +/** + Take the original command line, substitute any alias in the first group of space delimited characters, free + the original string, return the modified copy + + @param[in] CmdLine pointer to the command line to update + @param[out]CmdName upon successful return the name of the command to be run + + @retval EFI_SUCCESS the function was successful + @retval EFI_OUT_OF_RESOURCES a memory allocation failed +**/ +EFI_STATUS +EFIAPI +ShellSubstituteAliases( + IN CHAR16 **CmdLine + ) +{ + CHAR16 *NewCmdLine; + CHAR16 *CommandName; + EFI_STATUS Status; + UINTN PostAliasSize; + ASSERT(CmdLine != NULL); + ASSERT(*CmdLine!= NULL); + + + CommandName = NULL; + if (StrStr((*CmdLine), L" ") == NULL){ + StrnCatGrow(&CommandName, NULL, (*CmdLine), 0); + } else { + StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine)); + } + + // + // This cannot happen 'inline' since the CmdLine can need extra space. + // + NewCmdLine = NULL; + if (!ShellCommandIsCommandOnList(CommandName)) { + // + // Convert via alias + // + Status = ShellConvertAlias(&CommandName); + if (EFI_ERROR(Status)){ + return (Status); + } + PostAliasSize = 0; + NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0); + if (NewCmdLine == NULL) { + SHELL_FREE_NON_NULL(CommandName); + SHELL_FREE_NON_NULL(*CmdLine); + return (EFI_OUT_OF_RESOURCES); + } + NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0); + if (NewCmdLine == NULL) { + SHELL_FREE_NON_NULL(CommandName); + SHELL_FREE_NON_NULL(*CmdLine); + return (EFI_OUT_OF_RESOURCES); + } + } else { + NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0); + } + + SHELL_FREE_NON_NULL(*CmdLine); + SHELL_FREE_NON_NULL(CommandName); + + // + // re-assign the passed in double pointer to point to our newly allocated buffer + // + *CmdLine = NewCmdLine; + + return (EFI_SUCCESS); +} + /** Function will process and run a command line. @@ -1170,23 +1519,22 @@ RunCommand( ) { EFI_STATUS Status; + EFI_STATUS StatusCode; CHAR16 *CommandName; SHELL_STATUS ShellStatus; UINTN Argc; CHAR16 **Argv; BOOLEAN LastError; - CHAR16 LeString[11]; - CHAR16 *PostAliasCmdLine; - UINTN PostAliasSize; + CHAR16 LeString[19]; CHAR16 *PostVariableCmdLine; CHAR16 *CommandWithPath; - EFI_DEVICE_PATH_PROTOCOL *DevPath; + CONST EFI_DEVICE_PATH_PROTOCOL *DevPath; CONST CHAR16 *TempLocation; CONST CHAR16 *TempLocation2; SHELL_FILE_HANDLE OriginalStdIn; SHELL_FILE_HANDLE OriginalStdOut; SHELL_FILE_HANDLE OriginalStdErr; - CHAR16 *TempLocation3; + SYSTEM_TABLE_INFO OriginalSystemTableInfo; UINTN Count; UINTN Count2; CHAR16 *CleanOriginal; @@ -1199,7 +1547,6 @@ RunCommand( CommandName = NULL; PostVariableCmdLine = NULL; - PostAliasCmdLine = NULL; CommandWithPath = NULL; DevPath = NULL; Status = EFI_SUCCESS; @@ -1207,79 +1554,40 @@ RunCommand( Split = NULL; CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0); - while (CleanOriginal[StrLen(CleanOriginal)-1] == L' ') { - CleanOriginal[StrLen(CleanOriginal)-1] = CHAR_NULL; - } - while (CleanOriginal[0] == L' ') { - CopyMem(CleanOriginal, CleanOriginal+1, StrSize(CleanOriginal) - sizeof(CleanOriginal[0])); - } - - CommandName = NULL; - if (StrStr(CleanOriginal, L" ") == NULL){ - StrnCatGrow(&CommandName, NULL, CleanOriginal, 0); - } else { - StrnCatGrow(&CommandName, NULL, CleanOriginal, StrStr(CleanOriginal, L" ") - CleanOriginal); + if (CleanOriginal == NULL) { + return (EFI_OUT_OF_RESOURCES); } - ASSERT(PostAliasCmdLine == NULL); - if (!ShellCommandIsCommandOnList(CommandName)) { - // - // Convert via alias - // - Status = ShellConvertAlias(&CommandName); - PostAliasSize = 0; - PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, CommandName, 0); - PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, StrStr(CleanOriginal, L" "), 0); - ASSERT_EFI_ERROR(Status); - } else { - PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, NULL, CleanOriginal, 0); - } + TrimSpaces(&CleanOriginal); - if (CleanOriginal != NULL) { - FreePool(CleanOriginal); - CleanOriginal = NULL; + // + // Handle case that passed in command line is just 1 or more " " characters. + // + if (StrLen (CleanOriginal) == 0) { + if (CleanOriginal != NULL) { + FreePool(CleanOriginal); + CleanOriginal = NULL; + } + return (EFI_SUCCESS); } - if (CommandName != NULL) { - FreePool(CommandName); - CommandName = NULL; + Status = ShellSubstituteAliases(&CleanOriginal); + if (EFI_ERROR(Status)) { + return (Status); } - PostVariableCmdLine = ShellConvertVariables(PostAliasCmdLine); - - // - // we can now free the modified by alias command line - // - if (PostAliasCmdLine != NULL) { - FreePool(PostAliasCmdLine); - PostAliasCmdLine = NULL; - } + PostVariableCmdLine = ShellConvertVariables(CleanOriginal); if (PostVariableCmdLine == NULL) { return (EFI_OUT_OF_RESOURCES); } - while (PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] == L' ') { - PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] = CHAR_NULL; - } - while (PostVariableCmdLine[0] == L' ') { - CopyMem(PostVariableCmdLine, PostVariableCmdLine+1, StrSize(PostVariableCmdLine) - sizeof(PostVariableCmdLine[0])); - } + TrimSpaces(&PostVariableCmdLine); // // We dont do normal processing with a split command line (output from one command input to another) // - TempLocation3 = NULL; - if (StrStr(PostVariableCmdLine, L"|") != NULL) { - for (TempLocation3 = PostVariableCmdLine ; TempLocation3 != NULL && *TempLocation3 != CHAR_NULL ; TempLocation3++) { - if (*TempLocation3 == L'^' && *(TempLocation3+1) == L'|') { - TempLocation3++; - } else if (*TempLocation3 == L'|') { - break; - } - } - } - if (TempLocation3 != NULL && *TempLocation3 != CHAR_NULL) { + if (ContainsSplit(PostVariableCmdLine)) { // // are we in an existing split??? // @@ -1292,6 +1600,9 @@ RunCommand( } else { Status = RunSplitCommand(PostVariableCmdLine, Split->SplitStdIn, Split->SplitStdOut); } + if (EFI_ERROR(Status)) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, PostVariableCmdLine); + } } else { // @@ -1311,32 +1622,16 @@ RunCommand( - Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr); + Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle); - } else { - // - // remove the < and/or > from the command line now - // - for (TempLocation3 = PostVariableCmdLine ; TempLocation3 != NULL && *TempLocation3 != CHAR_NULL ; TempLocation3++) { - if (*TempLocation3 == L'^') { - if (*(TempLocation3+1) == L'<' || *(TempLocation3+1) == L'>') { - CopyMem(TempLocation3, TempLocation3+1, StrSize(TempLocation3) - sizeof(TempLocation3[0])); - } - } else if (*TempLocation3 == L'>') { - *TempLocation3 = CHAR_NULL; - } else if ((*TempLocation3 == L'1' || *TempLocation3 == L'2')&&(*(TempLocation3+1) == L'>')) { - *TempLocation3 = CHAR_NULL; - } - } - - while (PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] == L' ') { - PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] = CHAR_NULL; - } - while (PostVariableCmdLine[0] == L' ') { - CopyMem(PostVariableCmdLine, PostVariableCmdLine+1, StrSize(PostVariableCmdLine) - sizeof(PostVariableCmdLine[0])); + if (Status == EFI_NOT_FOUND) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle); + } else { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle); } - + } else { + TrimSpaces(&PostVariableCmdLine); + // // get the argc and argv updated for internal commands // @@ -1376,10 +1671,15 @@ RunCommand( if (!EFI_ERROR(Status)) { Status = ShellCommandRunCommandHandler(ShellInfoObject.NewShellParametersProtocol->Argv[0], &ShellStatus, &LastError); ASSERT_EFI_ERROR(Status); - UnicodeSPrint(LeString, sizeof(LeString)*sizeof(LeString[0]), L"0x%08x", ShellStatus); - DEBUG_CODE(InternalEfiShellSetEnv(L"DebugLasterror", LeString, TRUE);); + + if (sizeof(EFI_STATUS) == sizeof(UINT64)) { + UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellStatus); + } else { + UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ShellStatus); + } + DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE);); if (LastError) { - InternalEfiShellSetEnv(L"Lasterror", LeString, TRUE); + InternalEfiShellSetEnv(L"lasterror", LeString, TRUE); } // // Pass thru the exitcode from the app. @@ -1405,6 +1705,14 @@ RunCommand( } if (CommandWithPath == NULL || ShellIsDirectory(CommandWithPath) == EFI_SUCCESS) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, ShellInfoObject.NewShellParametersProtocol->Argv[0]); + + if (sizeof(EFI_STATUS) == sizeof(UINT64)) { + UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", EFI_NOT_FOUND); + } else { + UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", EFI_NOT_FOUND); + } + DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE);); + InternalEfiShellSetEnv(L"lasterror", LeString, TRUE); } else { // // Check if it's a NSH (script) file. @@ -1421,16 +1729,35 @@ RunCommand( DevPath, PostVariableCmdLine, NULL, - NULL + &StatusCode ); + + // + // Update last error status. + // + if (sizeof(EFI_STATUS) == sizeof(UINT64)) { + UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", StatusCode); + } else { + UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", StatusCode); + } + DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE);); + InternalEfiShellSetEnv(L"lasterror", LeString, TRUE); } } } + + // + // Print some error info. + // + if (EFI_ERROR(Status)) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status)); + } + CommandName = StrnCatGrow(&CommandName, NULL, ShellInfoObject.NewShellParametersProtocol->Argv[0], 0); RestoreArgcArgv(ShellInfoObject.NewShellParametersProtocol, &Argv, &Argc); - RestoreStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr); + RestoreStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo); } if (CommandName != NULL) { if (ShellCommandGetCurrentScriptFile() != NULL && !IsScriptOnlyCommand(CommandName)) { @@ -1446,7 +1773,7 @@ RunCommand( SHELL_FREE_NON_NULL(CommandName); SHELL_FREE_NON_NULL(CommandWithPath); SHELL_FREE_NON_NULL(PostVariableCmdLine); - SHELL_FREE_NON_NULL(DevPath); + SHELL_FREE_NON_NULL(CleanOriginal); return (Status); } @@ -1507,8 +1834,10 @@ RunScriptFileHandle ( SCRIPT_COMMAND_LIST *LastCommand; BOOLEAN Ascii; BOOLEAN PreScriptEchoState; + BOOLEAN PreCommandEchoState; CONST CHAR16 *CurDir; UINTN LineCount; + CHAR16 LeString[50]; ASSERT(!ShellCommandGetScriptExit()); @@ -1585,12 +1914,12 @@ RunScriptFileHandle ( // // Now enumerate through the commands and run each one. // - CommandLine = AllocatePool(PcdGet16(PcdShellPrintBufferSize)); + CommandLine = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize)); if (CommandLine == NULL) { DeleteScriptFileStruct(NewScriptFile); return (EFI_OUT_OF_RESOURCES); } - CommandLine2 = AllocatePool(PcdGet16(PcdShellPrintBufferSize)); + CommandLine2 = AllocateZeroPool(PcdGet16(PcdShellPrintBufferSize)); if (CommandLine2 == NULL) { FreePool(CommandLine); DeleteScriptFileStruct(NewScriptFile); @@ -1685,23 +2014,54 @@ RunScriptFileHandle ( // } else { if (CommandLine3 != NULL && StrLen(CommandLine3) > 0) { - if (ShellCommandGetEchoState()) { - CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd"); - if (CurDir != NULL && StrLen(CurDir) > 1) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir); - } else { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle); + if (CommandLine3[0] == L'@') { + // + // We need to save the current echo state + // and disable echo for just this command. + // + PreCommandEchoState = ShellCommandGetEchoState(); + ShellCommandSetEchoState(FALSE); + Status = RunCommand(CommandLine3+1); + + // + // If command was "@echo -off" or "@echo -on" then don't restore echo state + // + if (StrCmp (L"@echo -off", CommandLine3) != 0 && + StrCmp (L"@echo -on", CommandLine3) != 0) { + // + // Now restore the pre-'@' echo state. + // + ShellCommandSetEchoState(PreCommandEchoState); + } + } else { + if (ShellCommandGetEchoState()) { + CurDir = ShellInfoObject.NewEfiShellProtocol->GetEnv(L"cwd"); + if (CurDir != NULL && StrLen(CurDir) > 1) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_CURDIR), ShellInfoObject.HiiHandle, CurDir); + } else { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_SHELL), ShellInfoObject.HiiHandle); + } + ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2); } - ShellPrintEx(-1, -1, L"%s\r\n", CommandLine2); + Status = RunCommand(CommandLine3); } - Status = RunCommand(CommandLine3); } if (ShellCommandGetScriptExit()) { - ShellCommandRegisterExit(FALSE); + // + // ShellCommandGetExitCode() always returns a UINT64 + // + 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; break; } + if (ShellGetExecutionBreakFlag()) { + break; + } if (EFI_ERROR(Status)) { break; } @@ -1726,11 +2086,17 @@ RunScriptFileHandle ( } } - ShellCommandSetEchoState(PreScriptEchoState); FreePool(CommandLine); FreePool(CommandLine2); ShellCommandSetNewScript (NULL); + + // + // Only if this was the last script reset the state. + // + if (ShellCommandGetCurrentScriptFile()==NULL) { + ShellCommandSetEchoState(PreScriptEchoState); + } return (EFI_SUCCESS); }