|
@@ -22,6 +22,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
22
|
22
|
#include <stdio.h>
|
23
|
23
|
#include <string.h>
|
24
|
24
|
#include <stdlib.h>
|
|
25
|
+#include <errno.h>
|
25
|
26
|
#include <ipxe/console.h>
|
26
|
27
|
#include <ipxe/keys.h>
|
27
|
28
|
#include <ipxe/editstring.h>
|
|
@@ -244,18 +245,22 @@ void history_free ( struct readline_history *history ) {
|
244
|
245
|
* @v prefill Prefill string, or NULL for no prefill
|
245
|
246
|
* @v history History buffer, or NULL for no history
|
246
|
247
|
* @ret line Line read from console (excluding terminating newline)
|
|
248
|
+ * @ret rc Return status code
|
247
|
249
|
*
|
248
|
250
|
* The returned line is allocated with malloc(); the caller must
|
249
|
251
|
* eventually call free() to release the storage.
|
250
|
252
|
*/
|
251
|
|
-char * readline_history ( const char *prompt, const char *prefill,
|
252
|
|
- struct readline_history *history ) {
|
|
253
|
+int readline_history ( const char *prompt, const char *prefill,
|
|
254
|
+ struct readline_history *history, char **line ) {
|
253
|
255
|
char buf[READLINE_MAX];
|
254
|
256
|
struct edit_string string;
|
255
|
257
|
int key;
|
256
|
258
|
int move_by;
|
257
|
259
|
const char *new_string;
|
258
|
|
- char *line;
|
|
260
|
+ int rc;
|
|
261
|
+
|
|
262
|
+ /* Avoid returning uninitialised data on error */
|
|
263
|
+ *line = NULL;
|
259
|
264
|
|
260
|
265
|
/* Display prompt, if applicable */
|
261
|
266
|
if ( prompt )
|
|
@@ -280,12 +285,11 @@ char * readline_history ( const char *prompt, const char *prefill,
|
280
|
285
|
switch ( key ) {
|
281
|
286
|
case CR:
|
282
|
287
|
case LF:
|
283
|
|
- line = strdup ( buf );
|
284
|
|
- if ( ! line )
|
285
|
|
- printf ( "\nOut of memory" );
|
|
288
|
+ *line = strdup ( buf );
|
|
289
|
+ rc = ( ( *line ) ? 0 : -ENOMEM );
|
286
|
290
|
goto done;
|
287
|
291
|
case CTRL_C:
|
288
|
|
- line = NULL;
|
|
292
|
+ rc = -ECANCELED;
|
289
|
293
|
goto done;
|
290
|
294
|
case KEY_UP:
|
291
|
295
|
move_by = 1;
|
|
@@ -311,11 +315,12 @@ char * readline_history ( const char *prompt, const char *prefill,
|
311
|
315
|
done:
|
312
|
316
|
putchar ( '\n' );
|
313
|
317
|
if ( history ) {
|
314
|
|
- if ( line && line[0] )
|
315
|
|
- history_append ( history, line );
|
|
318
|
+ if ( *line && (*line)[0] )
|
|
319
|
+ history_append ( history, *line );
|
316
|
320
|
history_cleanup ( history );
|
317
|
321
|
}
|
318
|
|
- return line;
|
|
322
|
+ assert ( ( rc == 0 ) ^ ( *line == NULL ) );
|
|
323
|
+ return rc;
|
319
|
324
|
}
|
320
|
325
|
|
321
|
326
|
/**
|
|
@@ -328,5 +333,8 @@ char * readline_history ( const char *prompt, const char *prefill,
|
328
|
333
|
* eventually call free() to release the storage.
|
329
|
334
|
*/
|
330
|
335
|
char * readline ( const char *prompt ) {
|
331
|
|
- return readline_history ( prompt, NULL, NULL );
|
|
336
|
+ char *line;
|
|
337
|
+
|
|
338
|
+ readline_history ( prompt, NULL, NULL, &line );
|
|
339
|
+ return line;
|
332
|
340
|
}
|