]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[readline] Add an optional timeout to readline_history()
authorMichael Brown <mcb30@ipxe.org>
Fri, 23 Apr 2021 11:27:57 +0000 (12:27 +0100)
committerMichael Brown <mcb30@ipxe.org>
Fri, 23 Apr 2021 11:27:57 +0000 (12:27 +0100)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/hci/commands/nvo_cmd.c
src/hci/readline.c
src/hci/shell.c
src/include/readline/readline.h

index ac0d60651e1be4593666d14f5828fe1448ddac60..88e9d471495955752d48e8b0bc69f59bcf34e594 100644 (file)
@@ -241,7 +241,7 @@ static int read_value ( struct named_setting *setting, char **args __unused,
                              NULL, &setting->setting, &existing );
 
        /* Read new value */
-       if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
+       if ( ( rc = readline_history ( NULL, existing, NULL, 0, value ) ) != 0 )
                goto err_readline;
 
  err_readline:
index 83a2e0b906a08f7f5a16b92c31e0a524d177fbc0..852c4503abbb3889aac475f5f1ad7a348193d3c1 100644 (file)
@@ -248,6 +248,7 @@ void history_free ( struct readline_history *history ) {
  * @v prompt           Prompt string
  * @v prefill          Prefill string, or NULL for no prefill
  * @v history          History buffer, or NULL for no history
+ * @v timeout          Timeout period, in ticks (0=indefinite)
  * @ret line           Line read from console (excluding terminating newline)
  * @ret rc             Return status code
  *
@@ -255,7 +256,8 @@ void history_free ( struct readline_history *history ) {
  * eventually call free() to release the storage.
  */
 int readline_history ( const char *prompt, const char *prefill,
-                      struct readline_history *history, char **line ) {
+                      struct readline_history *history, unsigned long timeout,
+                      char **line ) {
        char buf[READLINE_MAX];
        struct edit_string string;
        int key;
@@ -285,8 +287,17 @@ int readline_history ( const char *prompt, const char *prefill,
        }
 
        while ( 1 ) {
+
+               /* Get keypress */
+               key = getkey ( timeout );
+               if ( key < 0 ) {
+                       rc = -ETIMEDOUT;
+                       goto done;
+               }
+               timeout = 0;
+
                /* Handle keypress */
-               key = edit_string ( &string, getkey ( 0 ) );
+               key = edit_string ( &string, key );
                sync_console ( &string );
                move_by = 0;
                switch ( key ) {
@@ -342,6 +353,6 @@ int readline_history ( const char *prompt, const char *prefill,
 char * readline ( const char *prompt ) {
        char *line;
 
-       readline_history ( prompt, NULL, NULL, &line );
+       readline_history ( prompt, NULL, NULL, 0, &line );
        return line;
 }
index 276eb35279ffdf408d0a233c8d9aeb507faa3919..8ecf73a6f9d3861258a261ca857c4ce6f9291818 100644 (file)
@@ -91,7 +91,7 @@ int shell ( void ) {
 
        /* Read and execute commands */
        do {
-               readline_history ( shell_prompt, NULL, &history, &line );
+               readline_history ( shell_prompt, NULL, &history, 0, &line );
                if ( line ) {
                        rc = system ( line );
                        free ( line );
index afafbbdf56febd51ad6034e9e8308c39f979d58b..3caf28b47172630b1cacd5195a6e3572ce313b33 100644 (file)
@@ -51,7 +51,8 @@ struct readline_history {
 
 extern void history_free ( struct readline_history *history );
 extern int readline_history ( const char *prompt, const char *prefill,
-                             struct readline_history *history, char **line );
+                             struct readline_history *history,
+                             unsigned long timeout, char **line );
 extern char * __malloc readline ( const char *prompt );
 
 #endif /* _READLINE_H */