examples: cli: Add password prompt (hidden input) example

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2018-11-17 11:50:26 +01:00
parent 2137b9df9f
commit 0b7142eb8e

View File

@ -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);