From 0b7142eb8e2c7b34d799cd82850abf09cecc1617 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 17 Nov 2018 11:50:26 +0100 Subject: [PATCH] examples: cli: Add password prompt (hidden input) example Signed-off-by: Joachim Nilsson --- examples/cli.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/examples/cli.c b/examples/cli.c index 9cd900b..3bcafed 100644 --- a/examples/cli.c +++ b/examples/cli.c @@ -60,7 +60,7 @@ static int my_rl_list_possib(char *token, char ***av) { int i, num, total = 0; char **copy; - + for (num = 0; list[num]; num++) ; @@ -119,20 +119,46 @@ el_status_t do_suspend(void) int main(void) { char *line; - char *prompt = "cli> "; + char *prompt = "cli> "; + char *passwd = "Enter password: "; /* Setup callbacks */ rl_set_complete_func(&my_rl_complete); rl_set_list_possib_func(&my_rl_list_possib); + el_bind_key('?', list_possible); el_bind_key(CTL('C'), do_break); el_bind_key(CTL('D'), do_exit); el_bind_key(CTL('Z'), do_suspend); read_history(HISTORY); - while ((line = readline(prompt)) != NULL) { - printf("\t\t\t|%s|\n", line); - free(line); + while ((line = readline(prompt))) { + int next = 0; + + /* Use el_no_echo when reading passwords and similar */ + if (!strncmp(line, "unlock", 6)) { + el_no_echo = 1; + while ((line = readline(passwd))) { + if (strncmp(line, "secret", 6)) { + printf("\nWrong password, please try again, it's secret.\n"); + free(line); + continue; + } + + el_no_echo = 0; + + printf("\nAchievement unlocked!\n"); + free(line); + next = 1; + break; + } + } + + if (next) + continue; + + printf("\t\t\t|%s|\n", line); + free(line); } write_history(HISTORY);