X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=ShellPkg%2FApplication%2FShell%2FShell.c;h=ef757dec0fa724f87b490cd45fde513fed3a16d1;hb=6ba2921da3c0b7db303b6db44810158a1fefd737;hp=a56967189bba7ca7e4b7c09e55ac983a18c0a26f;hpb=fb84495a7894307e0b7b5b803ac033efeabfb058;p=mirror_edk2.git diff --git a/ShellPkg/Application/Shell/Shell.c b/ShellPkg/Application/Shell/Shell.c index a56967189b..ef757dec0f 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 - 2011, 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,18 +53,100 @@ 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, + 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. @@ -123,7 +208,7 @@ InternalEfiShellStartCtrlSMonitor( SimpleEx, &KeyData, NotificationFunction, - &ShellInfoObject.CtrlSNotifyHandle2); + &ShellInfoObject.CtrlSNotifyHandle3); } KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED; if (!EFI_ERROR(Status)) { @@ -131,7 +216,7 @@ InternalEfiShellStartCtrlSMonitor( SimpleEx, &KeyData, NotificationFunction, - &ShellInfoObject.CtrlSNotifyHandle2); + &ShellInfoObject.CtrlSNotifyHandle4); } return (Status); } @@ -155,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); @@ -295,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, @@ -361,6 +457,18 @@ UefiMain ( Status = InternalEfiShellStartCtrlSMonitor(); } + 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) { // // process the startup script or launch the called app. @@ -368,7 +476,7 @@ UefiMain ( Status = DoStartupScript(ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath); } - if (!ShellCommandGetExit() && (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 // @@ -399,6 +507,11 @@ UefiMain ( Status = DoShellPrompt(); } while (!ShellCommandGetExit()); } + if (OldConIn != NULL && ConInHandle != NULL) { + CloseSimpleTextInOnFile (gST->ConIn); + gST->ConIn = OldConIn; + gST->ConsoleInHandle = ConInHandle; + } } } @@ -423,7 +536,7 @@ UefiMain ( } if (ShellInfoObject.NewEfiShellProtocol != NULL){ if (ShellInfoObject.NewEfiShellProtocol->IsRootShell()){ - ShellInfoObject.NewEfiShellProtocol->SetEnv(L"cwd", L"", TRUE); + InternalEfiShellSetEnv(L"cwd", NULL, TRUE); } CleanUpShellProtocol(ShellInfoObject.NewEfiShellProtocol); DEBUG_CODE(ShellInfoObject.NewEfiShellProtocol = NULL;); @@ -463,6 +576,9 @@ UefiMain ( DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;); } + if (ShellCommandGetExit()) { + return ((EFI_STATUS)ShellCommandGetExitCode()); + } return (Status); } @@ -548,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. @@ -598,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, @@ -618,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. @@ -724,6 +845,7 @@ 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; @@ -808,20 +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 + 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); + 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 @@ -838,15 +962,19 @@ DoStartupScript( FileStringPath = NULL; NewSize = 0; FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, MapName, 0); - TempSpot = StrStr(FileStringPath, L";"); - if (TempSpot != NULL) { - *TempSpot = CHAR_NULL; + 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); } - FileStringPath = StrnCatGrow(&FileStringPath, &NewSize, ((FILEPATH_DEVICE_PATH*)FilePath)->PathName, 0); - ChopLastSlash(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); @@ -1000,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. @@ -1052,6 +1180,7 @@ ShellConvertVariables ( CHAR16 *NewCommandLine1; CHAR16 *NewCommandLine2; CHAR16 *Temp; + CHAR16 *Temp2; UINTN ItemSize; CHAR16 *ItemTemp; SCRIPT_FILE *CurrentScriptFile; @@ -1109,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... // @@ -1149,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); @@ -1196,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' ' ){ @@ -1217,7 +1389,6 @@ RunSplitCommand( ASSERT(Split->SplitStdOut != NULL); InsertHeadList(&ShellInfoObject.SplitList.Link, &Split->Link); - ASSERT(StrStr(OurCommandLine, L"|") == NULL); Status = RunCommand(OurCommandLine); // @@ -1259,6 +1430,164 @@ RunSplitCommand( return (Status); } +/** + Take the original command line, substitute any variables, 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 +ShellSubstituteVariables( + IN CHAR16 **CmdLine + ) +{ + CHAR16 *NewCmdLine; + NewCmdLine = ShellConvertVariables(*CmdLine); + SHELL_FREE_NON_NULL(*CmdLine); + if (NewCmdLine == NULL) { + return (EFI_OUT_OF_RESOURCES); + } + *CmdLine = NewCmdLine; + return (EFI_SUCCESS); +} + +/** + 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); +} + +/** + Takes the Argv[0] part of the command line and determine the meaning of it. +**/ +SHELL_OPERATION_TYPES +EFIAPI +GetOperationType( + IN CONST CHAR16 *CmdName + ) +{ + CHAR16* FileWithPath; + CONST CHAR16* TempLocation; + CONST CHAR16* TempLocation2; + + FileWithPath = NULL; + // + // test for an internal command. + // + if (ShellCommandIsCommandOnList(CmdName)) { + return (INTERNAL_COMMAND); + } + + // + // Test for file system change request. anything ending with : and cant have spaces. + // + if (CmdName[(StrLen(CmdName)-1)] == L':') { + if (StrStr(CmdName, L" ") != NULL) { + return (UNKNOWN_INVALID); + } + return (FILE_SYS_CHANGE); + } + + // + // Test for a file + // + if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) { + // + // See if that file has a script file extension + // + if (StrLen(FileWithPath) > 4) { + TempLocation = FileWithPath+StrLen(FileWithPath)-4; + TempLocation2 = mScriptExtension; + if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) { + SHELL_FREE_NON_NULL(FileWithPath); + return (SCRIPT_FILE_NAME); + } + } + + // + // Was a file, but not a script. we treat this as an application. + // + SHELL_FREE_NON_NULL(FileWithPath); + return (EFI_APPLICATION); + } + + SHELL_FREE_NON_NULL(FileWithPath); + // + // No clue what this is... return invalid flag... + // + return (UNKNOWN_INVALID); +} + /** Function will process and run a command line. @@ -1277,15 +1606,13 @@ 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 *PostVariableCmdLine; + CHAR16 LeString[19]; CHAR16 *CommandWithPath; CONST EFI_DEVICE_PATH_PROTOCOL *DevPath; CONST CHAR16 *TempLocation; @@ -1294,7 +1621,6 @@ RunCommand( SHELL_FILE_HANDLE OriginalStdOut; SHELL_FILE_HANDLE OriginalStdErr; SYSTEM_TABLE_INFO OriginalSystemTableInfo; - CHAR16 *TempLocation3; UINTN Count; UINTN Count2; CHAR16 *CleanOriginal; @@ -1306,8 +1632,6 @@ RunCommand( } CommandName = NULL; - PostVariableCmdLine = NULL; - PostAliasCmdLine = NULL; CommandWithPath = NULL; DevPath = NULL; Status = EFI_SUCCESS; @@ -1315,79 +1639,39 @@ 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); - } - - 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); - } - - if (CleanOriginal != NULL) { - FreePool(CleanOriginal); - CleanOriginal = NULL; - } - - if (CommandName != NULL) { - FreePool(CommandName); - CommandName = NULL; + if (CleanOriginal == NULL) { + return (EFI_OUT_OF_RESOURCES); } - PostVariableCmdLine = ShellConvertVariables(PostAliasCmdLine); + TrimSpaces(&CleanOriginal); // - // we can now free the modified by alias command line + // Handle case that passed in command line is just 1 or more " " characters. // - if (PostAliasCmdLine != NULL) { - FreePool(PostAliasCmdLine); - PostAliasCmdLine = NULL; + if (StrLen (CleanOriginal) == 0) { + if (CleanOriginal != NULL) { + FreePool(CleanOriginal); + CleanOriginal = NULL; + } + return (EFI_SUCCESS); } - if (PostVariableCmdLine == NULL) { - return (EFI_OUT_OF_RESOURCES); + Status = ShellSubstituteAliases(&CleanOriginal); + if (EFI_ERROR(Status)) { + return (Status); } - 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])); + Status = ShellSubstituteVariables(&CleanOriginal); + if (EFI_ERROR(Status)) { + return (Status); } + TrimSpaces(&CleanOriginal); + // // 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(CleanOriginal)) { // // are we in an existing split??? // @@ -1396,21 +1680,24 @@ RunCommand( } if (Split == NULL) { - Status = RunSplitCommand(PostVariableCmdLine, NULL, NULL); + Status = RunSplitCommand(CleanOriginal, NULL, NULL); } else { - Status = RunSplitCommand(PostVariableCmdLine, Split->SplitStdIn, Split->SplitStdOut); + Status = RunSplitCommand(CleanOriginal, Split->SplitStdIn, Split->SplitStdOut); + } + if (EFI_ERROR(Status)) { + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CleanOriginal); } } else { // // If this is a mapped drive change handle that... // - if (PostVariableCmdLine[(StrLen(PostVariableCmdLine)-1)] == L':' && StrStr(PostVariableCmdLine, L" ") == NULL) { - Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, PostVariableCmdLine); + if (CleanOriginal[(StrLen(CleanOriginal)-1)] == L':' && StrStr(CleanOriginal, L" ") == NULL) { + Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CleanOriginal); if (EFI_ERROR(Status)) { - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, PostVariableCmdLine); + ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CleanOriginal); } - FreePool(PostVariableCmdLine); + FreePool(CleanOriginal); return (Status); } @@ -1419,7 +1706,7 @@ RunCommand( - Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo); + Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, CleanOriginal, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo); if (EFI_ERROR(Status)) { if (Status == EFI_NOT_FOUND) { ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle); @@ -1427,17 +1714,12 @@ RunCommand( ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle); } } else { - 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(&CleanOriginal); + // // get the argc and argv updated for internal commands // - Status = UpdateArgcArgv(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &Argv, &Argc); + Status = UpdateArgcArgv(ShellInfoObject.NewShellParametersProtocol, CleanOriginal, &Argv, &Argc); ASSERT_EFI_ERROR(Status); for (Count = 0 ; Count < ShellInfoObject.NewShellParametersProtocol->Argc ; Count++) { @@ -1473,10 +1755,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. @@ -1502,6 +1789,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. @@ -1516,13 +1811,32 @@ RunCommand( Status = InternalShellExecuteDevicePath( &gImageHandle, DevPath, - PostVariableCmdLine, + CleanOriginal, 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); @@ -1542,7 +1856,7 @@ RunCommand( SHELL_FREE_NON_NULL(CommandName); SHELL_FREE_NON_NULL(CommandWithPath); - SHELL_FREE_NON_NULL(PostVariableCmdLine); + SHELL_FREE_NON_NULL(CleanOriginal); return (Status); } @@ -1603,8 +1917,10 @@ RunScriptFileHandle ( SCRIPT_COMMAND_LIST *LastCommand; BOOLEAN Ascii; BOOLEAN PreScriptEchoState; + BOOLEAN PreCommandEchoState; CONST CHAR16 *CurDir; UINTN LineCount; + CHAR16 LeString[50]; ASSERT(!ShellCommandGetScriptExit()); @@ -1781,23 +2097,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; }