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.
19 FILE_LICENCE ( GPL2_OR_LATER
);
25 #include <gpxe/keys.h>
26 #include <gpxe/editstring.h>
27 #include <readline/readline.h>
35 #define READLINE_MAX 256
37 static void sync_console ( struct edit_string
*string
) __nonnull
;
40 * Synchronise console with edited string
42 * @v string Editable string
44 static void sync_console ( struct edit_string
*string
) {
45 unsigned int mod_start
= string
->mod_start
;
46 unsigned int mod_end
= string
->mod_end
;
47 unsigned int cursor
= string
->last_cursor
;
48 size_t len
= strlen ( string
->buf
);
50 /* Expand region back to old cursor position if applicable */
51 if ( mod_start
> string
->last_cursor
)
52 mod_start
= string
->last_cursor
;
54 /* Expand region forward to new cursor position if applicable */
55 if ( mod_end
< string
->cursor
)
56 mod_end
= string
->cursor
;
58 /* Backspace to start of region */
59 while ( cursor
> mod_start
) {
64 /* Print modified region */
65 while ( cursor
< mod_end
) {
66 putchar ( ( cursor
>= len
) ? ' ' : string
->buf
[cursor
] );
70 /* Backspace to new cursor position */
71 while ( cursor
> string
->cursor
) {
78 * Read line from console
80 * @v prompt Prompt string
81 * @ret line Line read from console (excluding terminating newline)
83 * The returned line is allocated with malloc(); the caller must
84 * eventually call free() to release the storage.
86 char * readline ( const char *prompt
) {
87 char buf
[READLINE_MAX
];
88 struct edit_string string
;
93 printf ( "%s", prompt
);
95 memset ( &string
, 0, sizeof ( string
) );
97 string
.len
= sizeof ( buf
);
101 key
= edit_string ( &string
, getkey() );
102 sync_console ( &string
);
107 line
= strdup ( buf
);
109 printf ( "Out of memory\n" );