Browse Source

[readline] Allow a prefilled input string to be provided

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
4dedccfa1f
3 changed files with 11 additions and 3 deletions
  1. 9
    2
      src/hci/readline.c
  2. 1
    1
      src/hci/shell.c
  3. 1
    0
      src/include/readline/readline.h

+ 9
- 2
src/hci/readline.c View File

241
  * Read line from console (with history)
241
  * Read line from console (with history)
242
  *
242
  *
243
  * @v prompt		Prompt string
243
  * @v prompt		Prompt string
244
+ * @v prefill		Prefill string, or NULL for no prefill
244
  * @v history		History buffer, or NULL for no history
245
  * @v history		History buffer, or NULL for no history
245
  * @ret line		Line read from console (excluding terminating newline)
246
  * @ret line		Line read from console (excluding terminating newline)
246
  *
247
  *
247
  * The returned line is allocated with malloc(); the caller must
248
  * The returned line is allocated with malloc(); the caller must
248
  * eventually call free() to release the storage.
249
  * eventually call free() to release the storage.
249
  */
250
  */
250
-char * readline_history ( const char *prompt,
251
+char * readline_history ( const char *prompt, const char *prefill,
251
 			  struct readline_history *history ) {
252
 			  struct readline_history *history ) {
252
 	char buf[READLINE_MAX];
253
 	char buf[READLINE_MAX];
253
 	struct edit_string string;
254
 	struct edit_string string;
265
 	init_editstring ( &string, buf, sizeof ( buf ) );
266
 	init_editstring ( &string, buf, sizeof ( buf ) );
266
 	buf[0] = '\0';
267
 	buf[0] = '\0';
267
 
268
 
269
+	/* Prefill string, if applicable */
270
+	if ( prefill ) {
271
+		replace_string ( &string, prefill );
272
+		sync_console ( &string );
273
+	}
274
+
268
 	while ( 1 ) {
275
 	while ( 1 ) {
269
 		/* Handle keypress */
276
 		/* Handle keypress */
270
 		key = edit_string ( &string, getkey ( 0 ) );
277
 		key = edit_string ( &string, getkey ( 0 ) );
321
  * eventually call free() to release the storage.
328
  * eventually call free() to release the storage.
322
  */
329
  */
323
 char * readline ( const char *prompt ) {
330
 char * readline ( const char *prompt ) {
324
-	return readline_history ( prompt, NULL );
331
+	return readline_history ( prompt, NULL, NULL );
325
 }
332
 }

+ 1
- 1
src/hci/shell.c View File

86
 
86
 
87
 	/* Read and execute commands */
87
 	/* Read and execute commands */
88
 	do {
88
 	do {
89
-		line = readline_history ( shell_prompt, &history );
89
+		line = readline_history ( shell_prompt, NULL, &history );
90
 		if ( line ) {
90
 		if ( line ) {
91
 			rc = system ( line );
91
 			rc = system ( line );
92
 			free ( line );
92
 			free ( line );

+ 1
- 0
src/include/readline/readline.h View File

51
 
51
 
52
 extern void history_free ( struct readline_history *history );
52
 extern void history_free ( struct readline_history *history );
53
 extern char * __malloc readline_history ( const char *prompt,
53
 extern char * __malloc readline_history ( const char *prompt,
54
+					  const char *prefill,
54
 					  struct readline_history *history );
55
 					  struct readline_history *history );
55
 extern char * __malloc readline ( const char *prompt );
56
 extern char * __malloc readline ( const char *prompt );
56
 
57
 

Loading…
Cancel
Save