2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <gpxe/keys.h>
23 #include <gpxe/editstring.h>
24 #include <readline/readline.h>
32 #define READLINE_MAX 256
35 * Synchronise console with edited string
37 * @v string Editable string
39 static void sync_console ( struct edit_string *string ) {
40 unsigned int mod_start = string->mod_start;
41 unsigned int mod_end = string->mod_end;
42 unsigned int cursor = string->last_cursor;
43 size_t len = strlen ( string->buf );
45 /* Expand region back to old cursor position if applicable */
46 if ( mod_start > string->last_cursor )
47 mod_start = string->last_cursor;
49 /* Expand region forward to new cursor position if applicable */
50 if ( mod_end < string->cursor )
51 mod_end = string->cursor;
53 /* Backspace to start of region */
54 while ( cursor > mod_start ) {
59 /* Print modified region */
60 while ( cursor < mod_end ) {
61 putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
65 /* Backspace to new cursor position */
66 while ( cursor > string->cursor ) {
73 * Read line from console
75 * @v prompt Prompt string
76 * @ret line Line read from console (excluding terminating newline)
78 * The returned line is allocated with malloc(); the caller must
79 * eventually call free() to release the storage.
81 char * readline ( const char *prompt ) {
82 char buf[READLINE_MAX];
83 struct edit_string string = {
85 .len = sizeof ( buf ),
92 printf ( "%s", prompt );
96 key = edit_string ( &string, getkey() );
97 sync_console ( &string );
102 line = strdup ( buf );
104 printf ( "Out of memory\n" );