/* Read existing value */
if ( ( rc = fetchf_named_setting_copy ( name, &existing ) ) < 0 )
- return rc;
+ goto err_existing;
/* Read new value */
- *value = readline_history ( NULL, existing, NULL );
+ if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
+ goto err_new;
- /* Free existing value */
- free ( existing );
+ /* Success */
+ rc = 0;
- return 0;
+ err_new:
+ free ( existing );
+ err_existing:
+ return rc;
}
/**
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <errno.h>
#include <ipxe/console.h>
#include <ipxe/keys.h>
#include <ipxe/editstring.h>
* @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 )
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;
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;
}
/**
* 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;
}
};
extern void history_free ( struct readline_history *history );
-extern char * __malloc readline_history ( const char *prompt,
- const char *prefill,
- struct readline_history *history );
+extern int readline_history ( const char *prompt, const char *prefill,
+ struct readline_history *history, char **line );
extern char * __malloc readline ( const char *prompt );
#endif /* _READLINE_H */