很多时候,我们的程序需要和用户进行简单的交互,当然可以设计菜单之类的,但是从代码的角度来说依然很复杂,我们并不希望为了获得一个确认而大费周章。这时候,可以直接使用 Shell 提供的ShellPromptForResponse 函数。
函数原型在ShellLib.h 中:
/** Prompt the user and return the resultant answer to the requestor. This function will display the requested question on the shell prompt and then wait for an apropriate answer to be input from the console. If the SHELL_PROMPT_REQUEST_TYPE is SHELL_PROMPT_REQUEST_TYPE_YESNO, ShellPromptResponseTypeQuitContinue or SHELL_PROMPT_REQUEST_TYPE_YESNOCANCEL then *Response is of type SHELL_PROMPT_RESPONSE. If the SHELL_PROMPT_REQUEST_TYPE is ShellPromptResponseTypeFreeform then *Response is of type CHAR16*. In either case *Response must be callee freed if Response was not NULL; @param Type What type of question is asked. This is used to filter the input to prevent invalid answers to question. @param Prompt The pointer to a string prompt used to request input. @param Response The pointer to Response, which will be populated upon return. @retval EFI_SUCCESS The operation was successful. @retval EFI_UNSUPPORTED The operation is not supported as requested. @retval EFI_INVALID_PARAMETER A parameter was invalid. @return other The operation failed. **/ EFI_STATUS EFIAPI ShellPromptForResponse ( IN SHELL_PROMPT_REQUEST_TYPE Type, IN CHAR16 *Prompt OPTIONAL, IN OUT VOID **Response OPTIONAL );
简单的说这个函数提供了3种用法:
1. 使用ShellPromptResponseTypeFreeform 参数,接收全部输入的字符串;
2. 使用ShellPromptResponseTypeYesNo 这样,用户输入 y 表示 Yes, n 表示 No
3. 使用ShellPromptResponseTypeEnterContinue 进行按键等待的
#include <Uefi.h> #include <Library/UefiLib.h> #include <Library/ShellCEntryLib.h> #include "ShellLib.h" extern EFI_BOOT_SERVICES *gBS; extern EFI_SYSTEM_TABLE *gST; extern EFI_RUNTIME_SERVICES *gRT; SHELL_PROMPT_RESPONSE EFIAPI Resp() { } int EFIAPI main ( IN int Argc, IN CHAR16 **Argv ) { CHAR16 *InputStr = NULL; ShellPromptForResponse( ShellPromptResponseTypeFreeform, L"Please input->", (VOID**)&InputStr); Print(L"Get input by freeform %s\n",InputStr); ShellPromptForResponse( ShellPromptResponseTypeEnterContinue, L"Press Enter to continue...", NULL); ShellPromptForResponse( ShellPromptResponseTypeAnyKeyContinue, L"Press any key to continue...", NULL); SHELL_PROMPT_RESPONSE *Resp=NULL; ShellPromptForResponse(ShellPromptResponseTypeYesNo, L"Type 'y' or 'n'->",(VOID**)&Resp); switch (*(SHELL_PROMPT_RESPONSE*)Resp) { case ShellPromptResponseNo: Print(L"Choose No\n"); break; case ShellPromptResponseYes: Print(L"Choose Yes\n"); break; default: break; } return EFI_SUCCESS; }
运行结果:
可以看到,这个函数还可以用来实现回车键继续或者任意键继续的功能。
关于这个函数,具体的实现代码可以在 \ShellPkg\Library\UefiShellLib\UefiShellLib.c 中看到,有兴趣的朋友可以深入研究一下。
完整的代码下载
InputTest
請問要如何在shell Code 間找到相對應的 Funciton?
ShellPromptForResponse 函数在 \ShellPkg\Library\UefiShellLib\UefiShellLib.c