/** @file\r
This is THE shell (application)\r
\r
- Copyright (c) 2009 - 2013, Intel Corporation. All rights reserved.<BR>\r
+ Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
Copyright (c) 2013, Hewlett-Packard Development Company, L.P.\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
STATIC CONST CHAR16 mExecutableExtensions[] = L".NSH;.EFI";\r
STATIC CONST CHAR16 mStartupScript[] = L"startup.nsh";\r
\r
+/**\r
+ Cleans off leading and trailing spaces and tabs.\r
+\r
+ @param[in] String pointer to the string to trim them off.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+TrimSpaces(\r
+ IN CHAR16 **String\r
+ )\r
+{\r
+ ASSERT(String != NULL);\r
+ ASSERT(*String!= NULL);\r
+ //\r
+ // Remove any spaces and tabs at the beginning of the (*String).\r
+ //\r
+ while (((*String)[0] == L' ') || ((*String)[0] == L'\t')) {\r
+ CopyMem((*String), (*String)+1, StrSize((*String)) - sizeof((*String)[0]));\r
+ }\r
+\r
+ //\r
+ // Remove any spaces and tabs at the end of the (*String).\r
+ //\r
+ while ((StrLen (*String) > 0) && (((*String)[StrLen((*String))-1] == L' ') || ((*String)[StrLen((*String))-1] == L'\t'))) {\r
+ (*String)[StrLen((*String))-1] = CHAR_NULL;\r
+ }\r
+\r
+ return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+ Find a command line contains a split operation\r
+\r
+ @param[in] CmdLine The command line to parse.\r
+\r
+ @retval A pointer to the | character in CmdLine or NULL if not present.\r
+**/\r
+CONST CHAR16*\r
+EFIAPI\r
+FindSplit(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ CONST CHAR16 *TempSpot;\r
+ TempSpot = NULL;\r
+ if (StrStr(CmdLine, L"|") != NULL) {\r
+ for (TempSpot = CmdLine ; TempSpot != NULL && *TempSpot != CHAR_NULL ; TempSpot++) {\r
+ if (*TempSpot == L'^' && *(TempSpot+1) == L'|') {\r
+ TempSpot++;\r
+ } else if (*TempSpot == L'|') {\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ return (TempSpot);\r
+}\r
+\r
+/**\r
+ Determine if a command line contains a split operation\r
+\r
+ @param[in] CmdLine The command line to parse.\r
+\r
+ @retval TRUE CmdLine has a valid split.\r
+ @retval FALSE CmdLine does not have a valid split.\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+ContainsSplit(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ CONST CHAR16 *TempSpot;\r
+ TempSpot = FindSplit(CmdLine);\r
+ return (BOOLEAN) ((TempSpot != NULL) && (*TempSpot != CHAR_NULL));\r
+}\r
+\r
/**\r
Function to start monitoring for CTRL-S using SimpleTextInputEx. This \r
feature's enabled state was not known when the shell initially launched.\r
SimpleEx,\r
&KeyData,\r
NotificationFunction,\r
- &ShellInfoObject.CtrlSNotifyHandle2);\r
+ &ShellInfoObject.CtrlSNotifyHandle3);\r
} \r
KeyData.KeyState.KeyShiftState = EFI_SHIFT_STATE_VALID|EFI_RIGHT_CONTROL_PRESSED;\r
if (!EFI_ERROR(Status)) {\r
SimpleEx,\r
&KeyData,\r
NotificationFunction,\r
- &ShellInfoObject.CtrlSNotifyHandle2);\r
+ &ShellInfoObject.CtrlSNotifyHandle4);\r
}\r
return (Status);\r
}\r
// If we got a file, run it\r
//\r
if (!EFI_ERROR(Status) && FileHandle != NULL) {\r
- Status = RunScriptFileHandle (FileHandle, mStartupScript);\r
+ Status = RunScriptFile (mStartupScript, FileHandle, L"", ShellInfoObject.NewShellParametersProtocol);\r
ShellInfoObject.NewEfiShellProtocol->CloseFile(FileHandle);\r
} else {\r
FileStringPath = ShellFindFilePath(mStartupScript);\r
Status = EFI_SUCCESS;\r
ASSERT(FileHandle == NULL);\r
} else {\r
- Status = RunScriptFile(FileStringPath);\r
+ Status = RunScriptFile(FileStringPath, NULL, L"", ShellInfoObject.NewShellParametersProtocol);\r
FreePool(FileStringPath);\r
}\r
}\r
if (!EFI_ERROR (Status)) {\r
CmdLine[BufferSize / sizeof (CHAR16)] = CHAR_NULL;\r
Status = RunCommand(CmdLine);\r
- }\r
+ }\r
\r
//\r
// Done with this command\r
}\r
\r
/**\r
- Function will process and run a command line.\r
+ Take the original command line, substitute any variables, free \r
+ the original string, return the modified copy.\r
\r
- This will determine if the command line represents an internal shell \r
- command or dispatch an external application.\r
-\r
- @param[in] CmdLine The command line to parse.\r
+ @param[in] CmdLine pointer to the command line to update.\r
\r
- @retval EFI_SUCCESS The command was completed.\r
- @retval EFI_ABORTED The command's operation was aborted.\r
+ @retval EFI_SUCCESS the function was successful.\r
+ @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
**/\r
EFI_STATUS\r
EFIAPI\r
-RunCommand(\r
- IN CONST CHAR16 *CmdLine\r
+ShellSubstituteVariables(\r
+ IN CHAR16 **CmdLine\r
)\r
{\r
- EFI_STATUS Status;\r
- EFI_STATUS StatusCode;\r
- CHAR16 *CommandName;\r
- SHELL_STATUS ShellStatus;\r
- UINTN Argc;\r
- CHAR16 **Argv;\r
- BOOLEAN LastError;\r
- CHAR16 LeString[19];\r
- CHAR16 *PostAliasCmdLine;\r
- UINTN PostAliasSize;\r
- CHAR16 *PostVariableCmdLine;\r
- CHAR16 *CommandWithPath;\r
- CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
- CONST CHAR16 *TempLocation;\r
- CONST CHAR16 *TempLocation2;\r
- SHELL_FILE_HANDLE OriginalStdIn;\r
- SHELL_FILE_HANDLE OriginalStdOut;\r
- SHELL_FILE_HANDLE OriginalStdErr;\r
- SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r
- CHAR16 *TempLocation3;\r
- UINTN Count;\r
- UINTN Count2;\r
- CHAR16 *CleanOriginal;\r
- SPLIT_LIST *Split;\r
-\r
- ASSERT(CmdLine != NULL);\r
- if (StrLen(CmdLine) == 0) {\r
- return (EFI_SUCCESS);\r
- }\r
-\r
- CommandName = NULL;\r
- PostVariableCmdLine = NULL;\r
- PostAliasCmdLine = NULL;\r
- CommandWithPath = NULL;\r
- DevPath = NULL;\r
- Status = EFI_SUCCESS;\r
- CleanOriginal = NULL;\r
- Split = NULL;\r
-\r
- CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r
- if (CleanOriginal == NULL) {\r
+ CHAR16 *NewCmdLine;\r
+ NewCmdLine = ShellConvertVariables(*CmdLine);\r
+ SHELL_FREE_NON_NULL(*CmdLine);\r
+ if (NewCmdLine == NULL) {\r
return (EFI_OUT_OF_RESOURCES);\r
}\r
+ *CmdLine = NewCmdLine;\r
+ return (EFI_SUCCESS);\r
+}\r
\r
- //\r
- // Remove any spaces and tabs at the beginning of the string.\r
- //\r
- while ((CleanOriginal[0] == L' ') || (CleanOriginal[0] == L'\t')) {\r
- CopyMem(CleanOriginal, CleanOriginal+1, StrSize(CleanOriginal) - sizeof(CleanOriginal[0]));\r
- }\r
+/**\r
+ Take the original command line, substitute any alias in the first group of space delimited characters, free \r
+ the original string, return the modified copy.\r
\r
- //\r
- // Handle case that passed in command line is just 1 or more " " characters.\r
- //\r
- if (StrLen (CleanOriginal) == 0) {\r
- if (CleanOriginal != NULL) {\r
- FreePool(CleanOriginal);\r
- CleanOriginal = NULL;\r
- }\r
- return (EFI_SUCCESS);\r
- }\r
+ @param[in] CmdLine pointer to the command line to update.\r
+\r
+ @retval EFI_SUCCESS the function was successful.\r
+ @retval EFI_OUT_OF_RESOURCES a memory allocation failed.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ShellSubstituteAliases(\r
+ IN CHAR16 **CmdLine\r
+ )\r
+{\r
+ CHAR16 *NewCmdLine;\r
+ CHAR16 *CommandName;\r
+ EFI_STATUS Status;\r
+ UINTN PostAliasSize;\r
+ ASSERT(CmdLine != NULL);\r
+ ASSERT(*CmdLine!= NULL);\r
\r
- //\r
- // Remove any spaces at the end of the string.\r
- //\r
- while (CleanOriginal[StrLen(CleanOriginal)-1] == L' ') {\r
- CleanOriginal[StrLen(CleanOriginal)-1] = CHAR_NULL;\r
- }\r
\r
CommandName = NULL;\r
- if (StrStr(CleanOriginal, L" ") == NULL){\r
- StrnCatGrow(&CommandName, NULL, CleanOriginal, 0);\r
+ if (StrStr((*CmdLine), L" ") == NULL){\r
+ StrnCatGrow(&CommandName, NULL, (*CmdLine), 0);\r
} else {\r
- StrnCatGrow(&CommandName, NULL, CleanOriginal, StrStr(CleanOriginal, L" ") - CleanOriginal);\r
+ StrnCatGrow(&CommandName, NULL, (*CmdLine), StrStr((*CmdLine), L" ") - (*CmdLine));\r
}\r
\r
- ASSERT(PostAliasCmdLine == NULL);\r
+ //\r
+ // This cannot happen 'inline' since the CmdLine can need extra space.\r
+ //\r
+ NewCmdLine = NULL;\r
if (!ShellCommandIsCommandOnList(CommandName)) {\r
//\r
// Convert via alias\r
//\r
Status = ShellConvertAlias(&CommandName);\r
+ if (EFI_ERROR(Status)){\r
+ return (Status);\r
+ }\r
PostAliasSize = 0;\r
- PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, CommandName, 0);\r
- PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, &PostAliasSize, StrStr(CleanOriginal, L" "), 0);\r
- ASSERT_EFI_ERROR(Status);\r
+ NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, CommandName, 0);\r
+ if (NewCmdLine == NULL) {\r
+ SHELL_FREE_NON_NULL(CommandName);\r
+ SHELL_FREE_NON_NULL(*CmdLine);\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
+ NewCmdLine = StrnCatGrow(&NewCmdLine, &PostAliasSize, StrStr((*CmdLine), L" "), 0);\r
+ if (NewCmdLine == NULL) {\r
+ SHELL_FREE_NON_NULL(CommandName);\r
+ SHELL_FREE_NON_NULL(*CmdLine);\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
} else {\r
- PostAliasCmdLine = StrnCatGrow(&PostAliasCmdLine, NULL, CleanOriginal, 0);\r
+ NewCmdLine = StrnCatGrow(&NewCmdLine, NULL, (*CmdLine), 0);\r
}\r
\r
- if (CleanOriginal != NULL) {\r
- FreePool(CleanOriginal);\r
- CleanOriginal = NULL;\r
- }\r
+ SHELL_FREE_NON_NULL(*CmdLine);\r
+ SHELL_FREE_NON_NULL(CommandName);\r
+ \r
+ //\r
+ // re-assign the passed in double pointer to point to our newly allocated buffer\r
+ //\r
+ *CmdLine = NewCmdLine;\r
\r
- if (CommandName != NULL) {\r
- FreePool(CommandName);\r
- CommandName = NULL;\r
- }\r
+ return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+ Takes the Argv[0] part of the command line and determine the meaning of it.\r
\r
- PostVariableCmdLine = ShellConvertVariables(PostAliasCmdLine);\r
+ @param[in] CmdName pointer to the command line to update.\r
+ \r
+ @retval Internal_Command The name is an internal command.\r
+ @retval File_Sys_Change the name is a file system change.\r
+ @retval Script_File_Name the name is a NSH script file.\r
+ @retval Unknown_Invalid the name is unknown.\r
+ @retval Efi_Application the name is an application (.EFI).\r
+**/\r
+SHELL_OPERATION_TYPES\r
+EFIAPI\r
+GetOperationType(\r
+ IN CONST CHAR16 *CmdName\r
+ )\r
+{\r
+ CHAR16* FileWithPath;\r
+ CONST CHAR16* TempLocation;\r
+ CONST CHAR16* TempLocation2;\r
\r
+ FileWithPath = NULL;\r
//\r
- // we can now free the modified by alias command line\r
+ // test for an internal command.\r
//\r
- if (PostAliasCmdLine != NULL) {\r
- FreePool(PostAliasCmdLine);\r
- PostAliasCmdLine = NULL;\r
- }\r
-\r
- if (PostVariableCmdLine == NULL) {\r
- return (EFI_OUT_OF_RESOURCES);\r
- }\r
-\r
- while (PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] == L' ') {\r
- PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] = CHAR_NULL;\r
- }\r
- while (PostVariableCmdLine[0] == L' ') {\r
- CopyMem(PostVariableCmdLine, PostVariableCmdLine+1, StrSize(PostVariableCmdLine) - sizeof(PostVariableCmdLine[0]));\r
+ if (ShellCommandIsCommandOnList(CmdName)) {\r
+ return (Internal_Command);\r
}\r
\r
//\r
- // We dont do normal processing with a split command line (output from one command input to another)\r
+ // Test for file system change request. anything ending with : and cant have spaces.\r
//\r
- TempLocation3 = NULL;\r
- if (StrStr(PostVariableCmdLine, L"|") != NULL) {\r
- for (TempLocation3 = PostVariableCmdLine ; TempLocation3 != NULL && *TempLocation3 != CHAR_NULL ; TempLocation3++) {\r
- if (*TempLocation3 == L'^' && *(TempLocation3+1) == L'|') {\r
- TempLocation3++;\r
- } else if (*TempLocation3 == L'|') {\r
- break;\r
- }\r
+ if (CmdName[(StrLen(CmdName)-1)] == L':') {\r
+ if (StrStr(CmdName, L" ") != NULL) {\r
+ return (Unknown_Invalid);\r
}\r
+ return (File_Sys_Change);\r
}\r
- if (TempLocation3 != NULL && *TempLocation3 != CHAR_NULL) {\r
+\r
+ //\r
+ // Test for a file\r
+ //\r
+ if ((FileWithPath = ShellFindFilePathEx(CmdName, mExecutableExtensions)) != NULL) {\r
//\r
- // are we in an existing split???\r
+ // See if that file has a script file extension\r
//\r
- if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
- Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
- }\r
-\r
- if (Split == NULL) {\r
- Status = RunSplitCommand(PostVariableCmdLine, NULL, NULL);\r
- } else {\r
- Status = RunSplitCommand(PostVariableCmdLine, Split->SplitStdIn, Split->SplitStdOut);\r
- }\r
- if (EFI_ERROR(Status)) {\r
- ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, PostVariableCmdLine);\r
+ if (StrLen(FileWithPath) > 4) {\r
+ TempLocation = FileWithPath+StrLen(FileWithPath)-4;\r
+ TempLocation2 = mScriptExtension;\r
+ if (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0) {\r
+ SHELL_FREE_NON_NULL(FileWithPath);\r
+ return (Script_File_Name);\r
+ }\r
}\r
- } else {\r
\r
//\r
- // If this is a mapped drive change handle that...\r
+ // Was a file, but not a script. we treat this as an application.\r
//\r
- if (PostVariableCmdLine[(StrLen(PostVariableCmdLine)-1)] == L':' && StrStr(PostVariableCmdLine, L" ") == NULL) {\r
- Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, PostVariableCmdLine);\r
- if (EFI_ERROR(Status)) {\r
- ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, PostVariableCmdLine);\r
- }\r
- FreePool(PostVariableCmdLine);\r
- return (Status);\r
- }\r
+ SHELL_FREE_NON_NULL(FileWithPath);\r
+ return (Efi_Application);\r
+ }\r
+ \r
+ SHELL_FREE_NON_NULL(FileWithPath);\r
+ //\r
+ // No clue what this is... return invalid flag...\r
+ //\r
+ return (Unknown_Invalid);\r
+}\r
\r
- ///@todo update this section to divide into 3 ways - run internal command, run split (above), and run an external file...\r
- /// We waste a lot of time doing processing like StdIn,StdOut,Argv,Argc for things that are external files...\r
+/**\r
+ Determine if the first item in a command line is valid.\r
\r
+ @param[in] CmdLine The command line to parse.\r
\r
+ @retval EFI_SUCCESS The item is valid.\r
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
+ @retval EFI_NOT_FOUND The operation type is unknown or invalid.\r
+**/\r
+EFI_STATUS \r
+EFIAPI\r
+IsValidSplit(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ CHAR16 *Temp;\r
+ CHAR16 *FirstParameter;\r
+ CHAR16 *TempWalker;\r
+ EFI_STATUS Status;\r
\r
- Status = UpdateStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
- if (EFI_ERROR(Status)) {\r
- if (Status == EFI_NOT_FOUND) {\r
- ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_REDUNDA_REDIR), ShellInfoObject.HiiHandle);\r
- } else {\r
- ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_REDIR), ShellInfoObject.HiiHandle);\r
- }\r
- } else {\r
- while (PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] == L' ') {\r
- PostVariableCmdLine[StrLen(PostVariableCmdLine)-1] = CHAR_NULL;\r
- }\r
- while (PostVariableCmdLine[0] == L' ') {\r
- CopyMem(PostVariableCmdLine, PostVariableCmdLine+1, StrSize(PostVariableCmdLine) - sizeof(PostVariableCmdLine[0]));\r
- }\r
+ Temp = NULL;\r
\r
- //\r
- // get the argc and argv updated for internal commands\r
- //\r
- Status = UpdateArgcArgv(ShellInfoObject.NewShellParametersProtocol, PostVariableCmdLine, &Argv, &Argc);\r
- ASSERT_EFI_ERROR(Status);\r
+ Temp = StrnCatGrow(&Temp, NULL, CmdLine, 0);\r
+ if (Temp == NULL) {\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
\r
- for (Count = 0 ; Count < ShellInfoObject.NewShellParametersProtocol->Argc ; Count++) {\r
- if (StrStr(ShellInfoObject.NewShellParametersProtocol->Argv[Count], L"-?") == ShellInfoObject.NewShellParametersProtocol->Argv[Count]\r
- || (ShellInfoObject.NewShellParametersProtocol->Argv[0][0] == L'?' && ShellInfoObject.NewShellParametersProtocol->Argv[0][1] == CHAR_NULL)\r
- ) {\r
- //\r
- // We need to redo the arguments since a parameter was -?\r
- // move them all down 1 to the end, then up one then replace the first with help\r
- //\r
- FreePool(ShellInfoObject.NewShellParametersProtocol->Argv[Count]);\r
- ShellInfoObject.NewShellParametersProtocol->Argv[Count] = NULL;\r
- for (Count2 = Count ; (Count2 + 1) < ShellInfoObject.NewShellParametersProtocol->Argc ; Count2++) {\r
- ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = ShellInfoObject.NewShellParametersProtocol->Argv[Count2+1];\r
- }\r
- ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = NULL;\r
- for (Count2 = ShellInfoObject.NewShellParametersProtocol->Argc -1 ; Count2 > 0 ; Count2--) {\r
- ShellInfoObject.NewShellParametersProtocol->Argv[Count2] = ShellInfoObject.NewShellParametersProtocol->Argv[Count2-1];\r
- }\r
- ShellInfoObject.NewShellParametersProtocol->Argv[0] = NULL;\r
- ShellInfoObject.NewShellParametersProtocol->Argv[0] = StrnCatGrow(&ShellInfoObject.NewShellParametersProtocol->Argv[0], NULL, L"help", 0);\r
- break;\r
- }\r
- }\r
+ FirstParameter = StrStr(Temp, L"|");\r
+ if (FirstParameter != NULL) {\r
+ *FirstParameter = CHAR_NULL;\r
+ }\r
\r
- //\r
- // command or file?\r
- //\r
- if (ShellCommandIsCommandOnList(ShellInfoObject.NewShellParametersProtocol->Argv[0])) {\r
- //\r
- // Run the command (which was converted if it was an alias)\r
- //\r
- if (!EFI_ERROR(Status)) {\r
- Status = ShellCommandRunCommandHandler(ShellInfoObject.NewShellParametersProtocol->Argv[0], &ShellStatus, &LastError);\r
- ASSERT_EFI_ERROR(Status);\r
+ FirstParameter = NULL;\r
\r
- if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
- UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ShellStatus);\r
- } else {\r
- UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ShellStatus);\r
- }\r
- DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
- if (LastError) {\r
- InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
- }\r
- //\r
- // Pass thru the exitcode from the app.\r
- //\r
- if (ShellCommandGetExit()) {\r
- Status = ShellStatus;\r
- } else if (ShellStatus != 0 && IsScriptOnlyCommand(ShellInfoObject.NewShellParametersProtocol->Argv[0])) {\r
- Status = EFI_ABORTED;\r
- }\r
- }\r
- } else {\r
- //\r
- // run an external file (or script)\r
- //\r
- if (StrStr(ShellInfoObject.NewShellParametersProtocol->Argv[0], L":") != NULL) {\r
- ASSERT (CommandWithPath == NULL);\r
- if (ShellIsFile(ShellInfoObject.NewShellParametersProtocol->Argv[0]) == EFI_SUCCESS) {\r
- CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, ShellInfoObject.NewShellParametersProtocol->Argv[0], 0);\r
- }\r
- }\r
- if (CommandWithPath == NULL) {\r
- CommandWithPath = ShellFindFilePathEx(ShellInfoObject.NewShellParametersProtocol->Argv[0], mExecutableExtensions);\r
- }\r
- if (CommandWithPath == NULL || ShellIsDirectory(CommandWithPath) == EFI_SUCCESS) {\r
- ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, ShellInfoObject.NewShellParametersProtocol->Argv[0]);\r
+ //\r
+ // Process the command line\r
+ //\r
+ Status = ProcessCommandLineToFinal(&Temp);\r
\r
- if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
- UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", EFI_NOT_FOUND);\r
- } else {\r
- UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", EFI_NOT_FOUND);\r
- }\r
- DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
- InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
- } else {\r
- //\r
- // Check if it's a NSH (script) file.\r
- //\r
- TempLocation = CommandWithPath+StrLen(CommandWithPath)-4;\r
- TempLocation2 = mScriptExtension;\r
- if ((StrLen(CommandWithPath) > 4) && (StringNoCaseCompare((VOID*)(&TempLocation), (VOID*)(&TempLocation2)) == 0)) {\r
- Status = RunScriptFile (CommandWithPath);\r
- } else {\r
- DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r
- ASSERT(DevPath != NULL);\r
- Status = InternalShellExecuteDevicePath(\r
- &gImageHandle,\r
- DevPath,\r
- PostVariableCmdLine,\r
- NULL,\r
- &StatusCode\r
- );\r
+ if (!EFI_ERROR(Status)) {\r
+ FirstParameter = AllocateZeroPool(StrSize(CmdLine));\r
+ if (FirstParameter == NULL) {\r
+ SHELL_FREE_NON_NULL(Temp);\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
+ TempWalker = (CHAR16*)Temp;\r
+ GetNextParameter(&TempWalker, &FirstParameter);\r
\r
- //\r
- // Update last error status.\r
- //\r
- if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
- UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", StatusCode);\r
- } else {\r
- UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", StatusCode);\r
- }\r
- DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
- InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
- }\r
- }\r
- }\r
+ if (GetOperationType(FirstParameter) == Unknown_Invalid) {\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
+ SetLastError(SHELL_NOT_FOUND);\r
+ Status = EFI_NOT_FOUND;\r
+ }\r
+ }\r
\r
- //\r
- // Print some error info.\r
- //\r
- if (EFI_ERROR(Status)) {\r
- ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r
- }\r
+ SHELL_FREE_NON_NULL(Temp);\r
+ SHELL_FREE_NON_NULL(FirstParameter);\r
+ return Status;\r
+}\r
\r
- CommandName = StrnCatGrow(&CommandName, NULL, ShellInfoObject.NewShellParametersProtocol->Argv[0], 0);\r
+/**\r
+ Determine if a command line contains with a split contains only valid commands.\r
\r
- RestoreArgcArgv(ShellInfoObject.NewShellParametersProtocol, &Argv, &Argc);\r
+ @param[in] CmdLine The command line to parse.\r
\r
- RestoreStdInStdOutStdErr(ShellInfoObject.NewShellParametersProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
- }\r
- if (CommandName != NULL) {\r
- if (ShellCommandGetCurrentScriptFile() != NULL && !IsScriptOnlyCommand(CommandName)) {\r
- //\r
- // if this is NOT a scipt only command return success so the script won't quit.\r
- // prevent killing the script - this is the only place where we know the actual command name (after alias and variable replacement...)\r
- //\r
- Status = EFI_SUCCESS;\r
- }\r
- }\r
+ @retval EFI_SUCCESS CmdLine has only valid commands, application, or has no split.\r
+ @retval EFI_ABORTED CmdLine has at least one invalid command or application.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+VerifySplit(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ CONST CHAR16 *TempSpot;\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Verify up to the pipe or end character\r
+ //\r
+ Status = IsValidSplit(CmdLine);\r
+ if (EFI_ERROR(Status)) {\r
+ return (Status);\r
}\r
\r
- SHELL_FREE_NON_NULL(CommandName);\r
- SHELL_FREE_NON_NULL(CommandWithPath);\r
- SHELL_FREE_NON_NULL(PostVariableCmdLine);\r
+ //\r
+ // If this was the only item, then get out\r
+ //\r
+ if (!ContainsSplit(CmdLine)) {\r
+ return (EFI_SUCCESS);\r
+ }\r
\r
- return (Status);\r
+ //\r
+ // recurse to verify the next item\r
+ //\r
+ TempSpot = FindSplit(CmdLine)+1;\r
+ return (VerifySplit(TempSpot));\r
}\r
\r
-STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};\r
/**\r
- Function determins if the CommandName COULD be a valid command. It does not determine whether\r
- this is a valid command. It only checks for invalid characters.\r
+ Process a split based operation.\r
\r
- @param[in] CommandName The name to check\r
+ @param[in] CmdLine pointer to the command line to process\r
\r
- @retval TRUE CommandName could be a command name\r
- @retval FALSE CommandName could not be a valid command name\r
+ @retval EFI_SUCCESS The operation was successful\r
+ @return an error occured.\r
**/\r
-BOOLEAN\r
+EFI_STATUS\r
EFIAPI\r
-IsValidCommandName(\r
+ProcessNewSplitCommandLine(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ SPLIT_LIST *Split;\r
+ EFI_STATUS Status;\r
+\r
+ Status = VerifySplit(CmdLine);\r
+ if (EFI_ERROR(Status)) {\r
+ return (Status);\r
+ }\r
+\r
+ Split = NULL;\r
+\r
+ //\r
+ // are we in an existing split???\r
+ //\r
+ if (!IsListEmpty(&ShellInfoObject.SplitList.Link)) {\r
+ Split = (SPLIT_LIST*)GetFirstNode(&ShellInfoObject.SplitList.Link);\r
+ }\r
+\r
+ if (Split == NULL) {\r
+ Status = RunSplitCommand(CmdLine, NULL, NULL);\r
+ } else {\r
+ Status = RunSplitCommand(CmdLine, Split->SplitStdIn, Split->SplitStdOut);\r
+ }\r
+ if (EFI_ERROR(Status)) {\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_SPLIT), ShellInfoObject.HiiHandle, CmdLine);\r
+ }\r
+ return (Status);\r
+}\r
+\r
+/**\r
+ Handle a request to change the current file system.\r
+\r
+ @param[in] CmdLine The passed in command line.\r
+\r
+ @retval EFI_SUCCESS The operation was successful.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ChangeMappedDrive(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ Status = EFI_SUCCESS;\r
+\r
+ //\r
+ // make sure we are the right operation\r
+ //\r
+ ASSERT(CmdLine[(StrLen(CmdLine)-1)] == L':' && StrStr(CmdLine, L" ") == NULL);\r
+ \r
+ //\r
+ // Call the protocol API to do the work\r
+ //\r
+ Status = ShellInfoObject.NewEfiShellProtocol->SetCurDir(NULL, CmdLine);\r
+\r
+ //\r
+ // Report any errors\r
+ //\r
+ if (EFI_ERROR(Status)) {\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_INVALID_MAPPING), ShellInfoObject.HiiHandle, CmdLine);\r
+ }\r
+\r
+ return (Status);\r
+}\r
+\r
+/**\r
+ Reprocess the command line to direct all -? to the help command.\r
+\r
+ if found, will add "help" as argv[0], and move the rest later.\r
+\r
+ @param[in,out] CmdLine pointer to the command line to update\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+DoHelpUpdate(\r
+ IN OUT CHAR16 **CmdLine\r
+ )\r
+{\r
+ CHAR16 *CurrentParameter;\r
+ CHAR16 *Walker;\r
+ CHAR16 *LastWalker;\r
+ CHAR16 *NewCommandLine;\r
+ EFI_STATUS Status;\r
+\r
+ Status = EFI_SUCCESS;\r
+\r
+ CurrentParameter = AllocateZeroPool(StrSize(*CmdLine));\r
+ if (CurrentParameter == NULL) {\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
+\r
+ Walker = *CmdLine;\r
+ while(Walker != NULL && *Walker != CHAR_NULL) {\r
+ LastWalker = Walker;\r
+ GetNextParameter(&Walker, &CurrentParameter);\r
+ if (StrStr(CurrentParameter, L"-?") == CurrentParameter) {\r
+ LastWalker[0] = L' ';\r
+ LastWalker[1] = L' ';\r
+ NewCommandLine = AllocateZeroPool(StrSize(L"help ") + StrSize(*CmdLine));\r
+ if (NewCommandLine == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ break;\r
+ }\r
+ StrCpy(NewCommandLine, L"help ");\r
+ StrCat(NewCommandLine, *CmdLine);\r
+ SHELL_FREE_NON_NULL(*CmdLine);\r
+ *CmdLine = NewCommandLine;\r
+ break;\r
+ }\r
+ }\r
+\r
+ SHELL_FREE_NON_NULL(CurrentParameter);\r
+\r
+ return (Status);\r
+}\r
+\r
+/**\r
+ Function to update the shell variable "lasterror".\r
+\r
+ @param[in] ErrorCode the error code to put into lasterror.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SetLastError(\r
+ IN CONST SHELL_STATUS ErrorCode\r
+ )\r
+{\r
+ CHAR16 LeString[19];\r
+ if (sizeof(EFI_STATUS) == sizeof(UINT64)) {\r
+ UnicodeSPrint(LeString, sizeof(LeString), L"0x%Lx", ErrorCode);\r
+ } else {\r
+ UnicodeSPrint(LeString, sizeof(LeString), L"0x%x", ErrorCode);\r
+ }\r
+ DEBUG_CODE(InternalEfiShellSetEnv(L"debuglasterror", LeString, TRUE););\r
+ InternalEfiShellSetEnv(L"lasterror", LeString, TRUE);\r
+\r
+ return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+ Converts the command line to it's post-processed form. this replaces variables and alias' per UEFI Shell spec.\r
+\r
+ @param[in,out] CmdLine pointer to the command line to update\r
+\r
+ @retval EFI_SUCCESS The operation was successful\r
+ @retval EFI_OUT_OF_RESOURCES A memory allocation failed.\r
+ @return some other error occured\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+ProcessCommandLineToFinal(\r
+ IN OUT CHAR16 **CmdLine\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ TrimSpaces(CmdLine);\r
+\r
+ Status = ShellSubstituteAliases(CmdLine);\r
+ if (EFI_ERROR(Status)) {\r
+ return (Status);\r
+ }\r
+\r
+ TrimSpaces(CmdLine);\r
+\r
+ Status = ShellSubstituteVariables(CmdLine);\r
+ if (EFI_ERROR(Status)) {\r
+ return (Status);\r
+ }\r
+\r
+ TrimSpaces(CmdLine);\r
+\r
+ //\r
+ // update for help parsing\r
+ //\r
+ if (StrStr(*CmdLine, L"?") != NULL) {\r
+ //\r
+ // This may do nothing if the ? does not indicate help.\r
+ // Save all the details for in the API below.\r
+ //\r
+ Status = DoHelpUpdate(CmdLine);\r
+ }\r
+\r
+ TrimSpaces(CmdLine);\r
+\r
+ return (EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+ Run an internal shell command.\r
+\r
+ This API will upadate the shell's environment since these commands are libraries.\r
+ \r
+ @param[in] CmdLine the command line to run.\r
+ @param[in] FirstParameter the first parameter on the command line\r
+ @param[in] ParamProtocol the shell parameters protocol pointer\r
+\r
+ @retval EFI_SUCCESS The command was completed.\r
+ @retval EFI_ABORTED The command's operation was aborted.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RunInternalCommand(\r
+ IN CONST CHAR16 *CmdLine,\r
+ IN CHAR16 *FirstParameter,\r
+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
+)\r
+{\r
+ EFI_STATUS Status;\r
+ UINTN Argc;\r
+ CHAR16 **Argv;\r
+ SHELL_STATUS CommandReturnedStatus;\r
+ BOOLEAN LastError;\r
+\r
+ //\r
+ // get the argc and argv updated for internal commands\r
+ //\r
+ Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
+ if (!EFI_ERROR(Status)) {\r
+ //\r
+ // Run the internal command.\r
+ //\r
+ Status = ShellCommandRunCommandHandler(FirstParameter, &CommandReturnedStatus, &LastError);\r
+\r
+ if (!EFI_ERROR(Status)) {\r
+ //\r
+ // Update last error status.\r
+ // some commands do not update last error.\r
+ //\r
+ if (LastError) {\r
+ SetLastError(CommandReturnedStatus);\r
+ }\r
+\r
+ //\r
+ // Pass thru the exitcode from the app.\r
+ //\r
+ if (ShellCommandGetExit()) {\r
+ //\r
+ // An Exit was requested ("exit" command), pass its value up.\r
+ //\r
+ Status = CommandReturnedStatus;\r
+ } else if (CommandReturnedStatus != SHELL_SUCCESS && IsScriptOnlyCommand(FirstParameter)) {\r
+ //\r
+ // Always abort when a script only command fails for any reason\r
+ //\r
+ Status = EFI_ABORTED;\r
+ } else if (ShellCommandGetCurrentScriptFile() != NULL && CommandReturnedStatus == SHELL_ABORTED) {\r
+ //\r
+ // Abort when in a script and a command aborted\r
+ //\r
+ Status = EFI_ABORTED;\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
+ // This is safe even if the update API failed. In this case, it may be a no-op.\r
+ //\r
+ RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
+\r
+ //\r
+ // If a script is running and the command is not a scipt only command, then\r
+ // change return value to success so the script won't halt (unless aborted).\r
+ //\r
+ // Script only commands have to be able halt the script since the script will\r
+ // not operate if they are failing.\r
+ //\r
+ if ( ShellCommandGetCurrentScriptFile() != NULL\r
+ && !IsScriptOnlyCommand(FirstParameter)\r
+ && Status != EFI_ABORTED\r
+ ) {\r
+ Status = EFI_SUCCESS;\r
+ }\r
+\r
+ return (Status);\r
+}\r
+\r
+/**\r
+ Function to run the command or file.\r
+\r
+ @param[in] Type the type of operation being run.\r
+ @param[in] CmdLine the command line to run.\r
+ @param[in] FirstParameter the first parameter on the command line\r
+ @param[in] ParamProtocol the shell parameters protocol pointer\r
+\r
+ @retval EFI_SUCCESS The command was completed.\r
+ @retval EFI_ABORTED The command's operation was aborted.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RunCommandOrFile(\r
+ IN SHELL_OPERATION_TYPES Type,\r
+ IN CONST CHAR16 *CmdLine,\r
+ IN CHAR16 *FirstParameter,\r
+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
+)\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_STATUS StatusCode;\r
+ CHAR16 *CommandWithPath;\r
+ EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
+\r
+ Status = EFI_SUCCESS;\r
+ CommandWithPath = NULL;\r
+ DevPath = NULL;\r
+\r
+ switch (Type) {\r
+ case Internal_Command:\r
+ Status = RunInternalCommand(CmdLine, FirstParameter, ParamProtocol);\r
+ break;\r
+ case Script_File_Name:\r
+ case Efi_Application:\r
+ //\r
+ // Process a fully qualified path\r
+ //\r
+ if (StrStr(FirstParameter, L":") != NULL) {\r
+ ASSERT (CommandWithPath == NULL);\r
+ if (ShellIsFile(FirstParameter) == EFI_SUCCESS) {\r
+ CommandWithPath = StrnCatGrow(&CommandWithPath, NULL, FirstParameter, 0);\r
+ }\r
+ }\r
+\r
+ //\r
+ // Process a relative path and also check in the path environment variable\r
+ //\r
+ if (CommandWithPath == NULL) {\r
+ CommandWithPath = ShellFindFilePathEx(FirstParameter, mExecutableExtensions);\r
+ }\r
+\r
+ //\r
+ // This should be impossible now.\r
+ //\r
+ ASSERT(CommandWithPath != NULL);\r
+\r
+ //\r
+ // Make sure that path is not just a directory (or not found)\r
+ //\r
+ if (!EFI_ERROR(ShellIsDirectory(CommandWithPath))) {\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
+ SetLastError(SHELL_NOT_FOUND);\r
+ }\r
+ switch (Type) {\r
+ case Script_File_Name:\r
+ Status = RunScriptFile (CommandWithPath, NULL, CmdLine, ParamProtocol);\r
+ break;\r
+ case Efi_Application:\r
+ //\r
+ // Get the device path of the application image\r
+ //\r
+ DevPath = ShellInfoObject.NewEfiShellProtocol->GetDevicePathFromFilePath(CommandWithPath);\r
+ if (DevPath == NULL){\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ break;\r
+ }\r
+\r
+ //\r
+ // Execute the device path\r
+ //\r
+ Status = InternalShellExecuteDevicePath(\r
+ &gImageHandle,\r
+ DevPath,\r
+ CmdLine,\r
+ NULL,\r
+ &StatusCode\r
+ );\r
+\r
+ SHELL_FREE_NON_NULL(DevPath);\r
+\r
+ //\r
+ // Update last error status.\r
+ //\r
+ SetLastError((SHELL_STATUS) StatusCode);\r
+ break;\r
+ default:\r
+ //\r
+ // Do nothing.\r
+ //\r
+ break;\r
+ }\r
+ break;\r
+ default:\r
+ //\r
+ // Do nothing.\r
+ //\r
+ break;\r
+ }\r
+\r
+ SHELL_FREE_NON_NULL(CommandWithPath);\r
+\r
+ return (Status);\r
+}\r
+\r
+/**\r
+ Function to setup StdIn, StdErr, StdOut, and then run the command or file.\r
+\r
+ @param[in] Type the type of operation being run.\r
+ @param[in] CmdLine the command line to run.\r
+ @param[in] FirstParameter the first parameter on the command line.\r
+ @param[in] ParamProtocol the shell parameters protocol pointer\r
+\r
+ @retval EFI_SUCCESS The command was completed.\r
+ @retval EFI_ABORTED The command's operation was aborted.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SetupAndRunCommandOrFile(\r
+ IN SHELL_OPERATION_TYPES Type,\r
+ IN CHAR16 *CmdLine,\r
+ IN CHAR16 *FirstParameter,\r
+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
+)\r
+{\r
+ EFI_STATUS Status;\r
+ SHELL_FILE_HANDLE OriginalStdIn;\r
+ SHELL_FILE_HANDLE OriginalStdOut;\r
+ SHELL_FILE_HANDLE OriginalStdErr;\r
+ SYSTEM_TABLE_INFO OriginalSystemTableInfo;\r
+\r
+ //\r
+ // Update the StdIn, StdOut, and StdErr for redirection to environment variables, files, etc... unicode and ASCII\r
+ //\r
+ Status = UpdateStdInStdOutStdErr(ParamProtocol, CmdLine, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
+\r
+ //\r
+ // The StdIn, StdOut, and StdErr are set up.\r
+ // Now run the command, script, or application\r
+ //\r
+ if (!EFI_ERROR(Status)) {\r
+ Status = RunCommandOrFile(Type, CmdLine, FirstParameter, ParamProtocol);\r
+ }\r
+\r
+ //\r
+ // Now print errors\r
+ //\r
+ if (EFI_ERROR(Status)) {\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_ERROR), ShellInfoObject.HiiHandle, (VOID*)(Status));\r
+ }\r
+\r
+ //\r
+ // put back the original StdIn, StdOut, and StdErr\r
+ //\r
+ RestoreStdInStdOutStdErr(ParamProtocol, &OriginalStdIn, &OriginalStdOut, &OriginalStdErr, &OriginalSystemTableInfo);\r
+\r
+ return (Status);\r
+}\r
+\r
+/**\r
+ Function will process and run a command line.\r
+\r
+ This will determine if the command line represents an internal shell \r
+ command or dispatch an external application.\r
+\r
+ @param[in] CmdLine The command line to parse.\r
+\r
+ @retval EFI_SUCCESS The command was completed.\r
+ @retval EFI_ABORTED The command's operation was aborted.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+RunCommand(\r
+ IN CONST CHAR16 *CmdLine\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ CHAR16 *CleanOriginal;\r
+ CHAR16 *FirstParameter;\r
+ CHAR16 *TempWalker;\r
+ SHELL_OPERATION_TYPES Type;\r
+\r
+ ASSERT(CmdLine != NULL);\r
+ if (StrLen(CmdLine) == 0) {\r
+ return (EFI_SUCCESS);\r
+ }\r
+\r
+ Status = EFI_SUCCESS;\r
+ CleanOriginal = NULL;\r
+\r
+ CleanOriginal = StrnCatGrow(&CleanOriginal, NULL, CmdLine, 0);\r
+ if (CleanOriginal == NULL) {\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
+\r
+ TrimSpaces(&CleanOriginal);\r
+\r
+ //\r
+ // Handle case that passed in command line is just 1 or more " " characters.\r
+ //\r
+ if (StrLen (CleanOriginal) == 0) {\r
+ SHELL_FREE_NON_NULL(CleanOriginal);\r
+ return (EFI_SUCCESS);\r
+ }\r
+\r
+ Status = ProcessCommandLineToFinal(&CleanOriginal);\r
+ if (EFI_ERROR(Status)) {\r
+ SHELL_FREE_NON_NULL(CleanOriginal);\r
+ return (Status);\r
+ }\r
+\r
+ //\r
+ // We dont do normal processing with a split command line (output from one command input to another)\r
+ //\r
+ if (ContainsSplit(CleanOriginal)) {\r
+ Status = ProcessNewSplitCommandLine(CleanOriginal);\r
+ SHELL_FREE_NON_NULL(CleanOriginal);\r
+ return (Status);\r
+ } \r
+\r
+ //\r
+ // We need the first parameter information so we can determine the operation type\r
+ //\r
+ FirstParameter = AllocateZeroPool(StrSize(CleanOriginal));\r
+ if (FirstParameter == NULL) {\r
+ SHELL_FREE_NON_NULL(CleanOriginal);\r
+ return (EFI_OUT_OF_RESOURCES);\r
+ }\r
+ TempWalker = CleanOriginal;\r
+ GetNextParameter(&TempWalker, &FirstParameter);\r
+\r
+ //\r
+ // Depending on the first parameter we change the behavior\r
+ //\r
+ switch (Type = GetOperationType(FirstParameter)) {\r
+ case File_Sys_Change:\r
+ Status = ChangeMappedDrive(CleanOriginal);\r
+ break;\r
+ case Internal_Command:\r
+ case Script_File_Name:\r
+ case Efi_Application:\r
+ Status = SetupAndRunCommandOrFile(Type, CleanOriginal, FirstParameter, ShellInfoObject.NewShellParametersProtocol);\r
+ break;\r
+ default:\r
+ //\r
+ // Whatever was typed, it was invalid.\r
+ //\r
+ ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_SHELL_NOT_FOUND), ShellInfoObject.HiiHandle, FirstParameter);\r
+ SetLastError(SHELL_NOT_FOUND);\r
+ break;\r
+ }\r
+ \r
+ SHELL_FREE_NON_NULL(CleanOriginal);\r
+ SHELL_FREE_NON_NULL(FirstParameter);\r
+\r
+ return (Status);\r
+}\r
+\r
+STATIC CONST UINT16 InvalidChars[] = {L'*', L'?', L'<', L'>', L'\\', L'/', L'\"', 0x0001, 0x0002};\r
+/**\r
+ Function determins if the CommandName COULD be a valid command. It does not determine whether\r
+ this is a valid command. It only checks for invalid characters.\r
+\r
+ @param[in] CommandName The name to check\r
+\r
+ @retval TRUE CommandName could be a command name\r
+ @retval FALSE CommandName could not be a valid command name\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+IsValidCommandName(\r
IN CONST CHAR16 *CommandName\r
)\r
{\r
while(!ShellFileHandleEof(Handle)) {\r
CommandLine = ShellFileHandleReturnLine(Handle, &Ascii);\r
LineCount++;\r
- if (CommandLine == NULL || StrLen(CommandLine) == 0) {\r
+ if (CommandLine == NULL || StrLen(CommandLine) == 0 || CommandLine[0] == '#') {\r
+ SHELL_FREE_NON_NULL(CommandLine);\r
continue;\r
}\r
NewScriptFile->CurrentCommand = AllocateZeroPool(sizeof(SCRIPT_COMMAND_LIST));\r
if (NewScriptFile->CurrentCommand == NULL) {\r
+ SHELL_FREE_NON_NULL(CommandLine);\r
DeleteScriptFileStruct(NewScriptFile);\r
return (EFI_OUT_OF_RESOURCES);\r
}\r
Function to process a NSH script file.\r
\r
@param[in] ScriptPath Pointer to the script file name (including file system path).\r
+ @param[in] Handle the handle of the script file already opened.\r
+ @param[in] CmdLine the command line to run.\r
+ @param[in] ParamProtocol the shell parameters protocol pointer\r
\r
@retval EFI_SUCCESS the script completed sucessfully\r
**/\r
EFI_STATUS\r
EFIAPI\r
RunScriptFile (\r
- IN CONST CHAR16 *ScriptPath\r
+ IN CONST CHAR16 *ScriptPath,\r
+ IN SHELL_FILE_HANDLE Handle OPTIONAL,\r
+ IN CONST CHAR16 *CmdLine,\r
+ IN EFI_SHELL_PARAMETERS_PROTOCOL *ParamProtocol\r
)\r
{\r
EFI_STATUS Status;\r
SHELL_FILE_HANDLE FileHandle;\r
+ UINTN Argc;\r
+ CHAR16 **Argv;\r
\r
if (ShellIsFile(ScriptPath) != EFI_SUCCESS) {\r
return (EFI_INVALID_PARAMETER);\r
}\r
\r
- Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
- if (EFI_ERROR(Status)) {\r
- return (Status);\r
- }\r
+ //\r
+ // get the argc and argv updated for scripts\r
+ //\r
+ Status = UpdateArgcArgv(ParamProtocol, CmdLine, &Argv, &Argc);\r
+ if (!EFI_ERROR(Status)) {\r
\r
- Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
+ if (Handle == NULL) {\r
+ //\r
+ // open the file\r
+ //\r
+ Status = ShellOpenFileByName(ScriptPath, &FileHandle, EFI_FILE_MODE_READ, 0);\r
+ if (!EFI_ERROR(Status)) {\r
+ //\r
+ // run it\r
+ //\r
+ Status = RunScriptFileHandle(FileHandle, ScriptPath);\r
+\r
+ //\r
+ // now close the file\r
+ //\r
+ ShellCloseFile(&FileHandle);\r
+ }\r
+ } else {\r
+ Status = RunScriptFileHandle(Handle, ScriptPath);\r
+ }\r
+ }\r
\r
- ShellCloseFile(&FileHandle);\r
+ //\r
+ // This is guarenteed to be called after UpdateArgcArgv no matter what else happened.\r
+ // This is safe even if the update API failed. In this case, it may be a no-op.\r
+ //\r
+ RestoreArgcArgv(ParamProtocol, &Argv, &Argc);\r
\r
return (Status);\r
}\r