diff options
author | Michael Brown | 2012-10-25 08:21:49 +0200 |
---|---|---|
committer | Michael Brown | 2012-10-25 08:24:43 +0200 |
commit | 2c011d77ae0147b8cc68bc4f3c577ebf55b83f16 (patch) | |
tree | 4eaa0afca5fcaa5963799408d29eb60b456fc512 /src/hci/readline.c | |
parent | [netdevice] Clear network device setting before unregistering (diff) | |
download | ipxe-2c011d77ae0147b8cc68bc4f3c577ebf55b83f16.tar.gz ipxe-2c011d77ae0147b8cc68bc4f3c577ebf55b83f16.tar.xz ipxe-2c011d77ae0147b8cc68bc4f3c577ebf55b83f16.zip |
[readline] Allow readline_history() to return a meaningful status
Signed-off-by: Michael Brown <mcb30@ipxe.org>
Diffstat (limited to 'src/hci/readline.c')
-rw-r--r-- | src/hci/readline.c | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/hci/readline.c b/src/hci/readline.c index a199fb0e..d67980b2 100644 --- a/src/hci/readline.c +++ b/src/hci/readline.c @@ -22,6 +22,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); #include <stdio.h> #include <string.h> #include <stdlib.h> +#include <errno.h> #include <ipxe/console.h> #include <ipxe/keys.h> #include <ipxe/editstring.h> @@ -244,18 +245,22 @@ void history_free ( struct readline_history *history ) { * @v prefill Prefill string, or NULL for no prefill * @v history History buffer, or NULL for no history * @ret line Line read from console (excluding terminating newline) + * @ret rc Return status code * * The returned line is allocated with malloc(); the caller must * eventually call free() to release the storage. */ -char * readline_history ( const char *prompt, const char *prefill, - struct readline_history *history ) { +int readline_history ( const char *prompt, const char *prefill, + struct readline_history *history, char **line ) { char buf[READLINE_MAX]; struct edit_string string; int key; int move_by; const char *new_string; - char *line; + int rc; + + /* Avoid returning uninitialised data on error */ + *line = NULL; /* Display prompt, if applicable */ if ( prompt ) @@ -280,12 +285,11 @@ char * readline_history ( const char *prompt, const char *prefill, switch ( key ) { case CR: case LF: - line = strdup ( buf ); - if ( ! line ) - printf ( "\nOut of memory" ); + *line = strdup ( buf ); + rc = ( ( *line ) ? 0 : -ENOMEM ); goto done; case CTRL_C: - line = NULL; + rc = -ECANCELED; goto done; case KEY_UP: move_by = 1; @@ -311,11 +315,12 @@ char * readline_history ( const char *prompt, const char *prefill, done: putchar ( '\n' ); if ( history ) { - if ( line && line[0] ) - history_append ( history, line ); + if ( *line && (*line)[0] ) + history_append ( history, *line ); history_cleanup ( history ); } - return line; + assert ( ( rc == 0 ) ^ ( *line == NULL ) ); + return rc; } /** @@ -328,5 +333,8 @@ char * readline_history ( const char *prompt, const char *prefill, * eventually call free() to release the storage. */ char * readline ( const char *prompt ) { - return readline_history ( prompt, NULL, NULL ); + char *line; + + readline_history ( prompt, NULL, NULL, &line ); + return line; } |