#include"wsh_shell_promptwait.h"voidWshShellPromptWait_Flush(WshShellPromptWait_t*pWait){WSH_SHELL_ASSERT(pWait);if(!pWait)return;pWait->Handler=NULL;pWait->Ctx=NULL;pWait->RejectedNum=0;}#if WSH_SHELL_PROMPT_WAITvoidWshShellPromptWait_Attach(WshShellPromptWait_t*pWait,WshShellPromptWait_Handler_thandler,void*pCtx){WSH_SHELL_ASSERT(pWait&&handler);if(!pWait||!handler)return;pWait->Handler=handler;pWait->Ctx=pCtx;/* Every wait starts with a fresh hint budget. */pWait->RejectedNum=0;}WshShell_Bool_tWshShellPromptWait_HintIsNeeded(constWshShellPromptWait_t*pcWait){if(!pcWait)returnfalse;returnpcWait->RejectedNum<WSH_SHELL_PROMPT_WAIT_HINT_RETRIES;}WSH_SHELL_RET_STATE_tWshShellPromptWait_Handle(WshShellPromptWait_t*pWait,WshShell_Char_tsymbol){WSH_SHELL_ASSERT(pWait);if(!pWait)returnWSH_SHELL_RET_STATE_ERR_PARAM;if(pWait->Handler){/* The handler sees every symbol, Ctrl+C included, so it can undo * whatever the wait was guarding. */WshShell_Bool_tres=pWait->Handler(symbol,pWait);/* Ctrl+C then always escapes the wait, whatever the handler returned: * a handler can never trap the shell. The cancel handler in the main * symbol dispatcher runs afterwards as usual. */if(symbol==WSH_SHELL_SYM_CANCEL){WshShellPromptWait_Flush(pWait);returnWSH_SHELL_RET_STATE_ERR_EMPTY;}/* Counted after the handler ran, so the handler still sees the budget * for the keystroke it is refusing right now. Saturates at the limit: * nothing above it changes behaviour, and it cannot overflow. */if(!res){if(pWait->RejectedNum<WSH_SHELL_PROMPT_WAIT_HINT_RETRIES)pWait->RejectedNum++;WSH_SHELL_PRINT("%c",WSH_SHELL_SYM_SOUND);}returnres==true?WSH_SHELL_RET_STATE_SUCCESS:WSH_SHELL_RET_STATE_ERR_BUSY;}returnWSH_SHELL_RET_STATE_ERR_EMPTY;}WshShell_Bool_tWshShellPromptWait_Enter(WshShell_Char_tsymbol,WshShellPromptWait_t*pWait){WSH_SHELL_ASSERT(pWait);/* Ctrl+C is force-flushed by the caller; nothing to undo, stay quiet. */if(symbol==WSH_SHELL_SYM_CANCEL)returntrue;if(symbol=='\r'||symbol=='\n'){WshShellPromptWait_Flush(pWait);returntrue;}else{if(WshShellPromptWait_HintIsNeeded(pWait))WSH_SHELL_PRINT_SYS("Press <Enter> to continue...\r\n");returnfalse;}}/* The signature is pinned by WshShellPromptWait_Handler_t: handlers are stored in * that type, and a const parameter would no longer match it. */// cppcheck-suppress constParameterPointerWshShell_Bool_tWshShellPromptWait_YesNo(WshShell_Char_tsymbol,WshShellPromptWait_t*pWait){WSH_SHELL_ASSERT(pWait);/* Ctrl+C is force-flushed by the caller; nothing to undo, stay quiet. */if(symbol==WSH_SHELL_SYM_CANCEL)returntrue;if(symbol=='Y'||symbol=='y'){WSH_SHELL_PRINT_SYS("Yes selected\r\n");}elseif(symbol=='N'||symbol=='n'){WSH_SHELL_PRINT_SYS("No selected\r\n");}else{if(WshShellPromptWait_HintIsNeeded(pWait))WSH_SHELL_PRINT_SYS("Invalid input, press Y or N\r\n");returnfalse;}returntrue;}#else /* WSH_SHELL_PROMPT_WAIT */voidWshShellPromptWait_Attach(WshShellPromptWait_t*pWait,WshShellPromptWait_Handler_thandler,void*pCtx){return;}WSH_SHELL_RET_STATE_tWshShellPromptWait_Handle(WshShellPromptWait_t*pWait,WshShell_Char_tsymbol){returnWSH_SHELL_RET_STATE_SUCCESS;}WshShell_Bool_tWshShellPromptWait_Enter(WshShell_Char_tsymbol,WshShellPromptWait_t*pWait){returntrue;}WshShell_Bool_tWshShellPromptWait_YesNo(WshShell_Char_tsymbol,WshShellPromptWait_t*pWait){returntrue;}WshShell_Bool_tWshShellPromptWait_HintIsNeeded(constWshShellPromptWait_t*pcWait){returnfalse;}#endif /* WSH_SHELL_PROMPT_WAIT */