From 2aebb209beb8aae5249fdbc63c05a9639a1344bf Mon Sep 17 00:00:00 2001 From: AtieP Date: Sun, 10 Apr 2022 14:07:37 +0200 Subject: [PATCH] [readline] Add TAB support Signed-off-by: AtieP This commit adds TAB support, so when you're typing a command and you press TAB, similar commands are shown. --- src/hci/readline.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/hci/readline.c b/src/hci/readline.c index ecc72d43f..dc60cdd21 100644 --- a/src/hci/readline.c +++ b/src/hci/readline.c @@ -27,6 +27,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); #include #include #include +#include #include #include #include @@ -324,6 +325,38 @@ int readline_history ( const char *prompt, const char *prefill, case KEY_DOWN: move_by = -1; break; + case TAB: { + int new_line_printed, matches_found; + struct command *cmd; + new_line_printed = matches_found = 0; + + for_each_table_entry ( cmd, COMMANDS ) { + if ( strncmp ( buf, cmd->name, strlen ( buf ) ) == 0 ) { + if ( !new_line_printed) { + printf("\n"); + new_line_printed = 1; + matches_found = 1; + } + printf ( "%s ", cmd->name ); + } + } + + if ( matches_found ) { + printf ( "\n" ); + + /* Printing the prompt and creating a new editstring to print + the already typed command */ + + if ( prompt ) + printf ( "%s", prompt ); + + memset ( &string, 0, sizeof ( string ) ); + init_editstring ( &string, buf, READLINE_MAX ); + replace_string ( &string, buf ); + sync_console ( &string ); + } + continue; + } default: /* Do nothing */ break;