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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
26 #include <ipxe/console.h>
27 #include <ipxe/keys.h>
28 #include <ipxe/editstring.h>
29 #include <readline/readline.h>
37 #define READLINE_MAX 256
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 * Locate history entry
80 * @v history History buffer
81 * @v depth Depth within history buffer
82 * @ret entry History entry
84 static struct readline_history_entry *
85 history_entry ( struct readline_history *history, unsigned int depth ) {
88 offset = ( ( history->next - depth ) %
89 ( sizeof ( history->entries ) /
90 sizeof ( history->entries[0] ) ) );
91 return &history->entries[offset];
95 * Read string from history buffer
97 * @v history History buffer
98 * @v depth Depth within history buffer
101 static const char * history_fetch ( struct readline_history *history,
102 unsigned int depth ) {
103 struct readline_history_entry *entry;
105 /* Return the temporary copy if it exists, otherwise return
106 * the persistent copy.
108 entry = history_entry ( history, depth );
109 return ( entry->temp ? entry->temp : entry->string );
113 * Write temporary string copy to history buffer
115 * @v history History buffer
116 * @v depth Depth within history buffer
119 static void history_store ( struct readline_history *history,
120 unsigned int depth, const char *string ) {
121 struct readline_history_entry *entry;
124 /* Create temporary copy of string */
125 temp = strdup ( string );
127 /* Just discard the string; there's nothing we can do */
128 DBGC ( history, "READLINE %p could not store string\n",
133 /* Store temporary copy */
134 entry = history_entry ( history, depth );
135 free ( entry->temp );
140 * Move to new history depth
142 * @v history History buffer
143 * @v offset Offset by which to change depth
144 * @v old_string String (possibly modified) at current depth
145 * @ret new_string String at new depth, or NULL for no movement
147 static const char * history_move ( struct readline_history *history,
148 int offset, const char *old_string ) {
149 unsigned int new_depth = ( history->depth + offset );
150 const char * new_string = history_fetch ( history, new_depth );
153 if ( new_depth > READLINE_HISTORY_MAX_DEPTH )
158 /* Store temporary copy of old string at current depth */
159 history_store ( history, history->depth, old_string );
162 history->depth = new_depth;
164 /* Return new string */
169 * Append new history entry
171 * @v history History buffer
174 static void history_append ( struct readline_history *history,
175 const char *string ) {
176 struct readline_history_entry *entry;
178 /* Store new entry */
179 entry = history_entry ( history, 0 );
180 assert ( entry->string == NULL );
181 entry->string = strdup ( string );
182 if ( ! entry->string ) {
183 /* Just discard the string; there's nothing we can do */
184 DBGC ( history, "READLINE %p could not append string\n",
189 /* Increment history position */
192 /* Prepare empty "next" slot */
193 entry = history_entry ( history, 0 );
194 free ( entry->string );
195 entry->string = NULL;
199 * Clean up history after editing
201 * @v history History buffer
203 static void history_cleanup ( struct readline_history *history ) {
204 struct readline_history_entry *entry;
207 /* Discard any temporary strings */
208 for ( i = 0 ; i < ( sizeof ( history->entries ) /
209 sizeof ( history->entries[0] ) ) ; i++ ) {
210 entry = &history->entries[i];
211 free ( entry->temp );
219 entry = history_entry ( history, 0 );
220 assert ( entry->string == NULL );
224 * Free history buffer
226 * @v history History buffer
228 void history_free ( struct readline_history *history ) {
229 struct readline_history_entry *entry;
232 /* Discard any temporary strings */
233 for ( i = 0 ; i < ( sizeof ( history->entries ) /
234 sizeof ( history->entries[0] ) ) ; i++ ) {
235 entry = &history->entries[i];
236 assert ( entry->temp == NULL );
237 free ( entry->string );
242 * Read line from console (with history)
244 * @v prompt Prompt string
245 * @v prefill Prefill string, or NULL for no prefill
246 * @v history History buffer, or NULL for no history
247 * @ret line Line read from console (excluding terminating newline)
248 * @ret rc Return status code
250 * The returned line is allocated with malloc(); the caller must
251 * eventually call free() to release the storage.
253 int readline_history ( const char *prompt, const char *prefill,
254 struct readline_history *history, char **line ) {
255 char buf[READLINE_MAX];
256 struct edit_string string;
259 const char *new_string;
262 /* Avoid returning uninitialised data on error */
265 /* Display prompt, if applicable */
267 printf ( "%s", prompt );
269 /* Ensure cursor is visible */
270 printf ( "\033[?25h" );
272 /* Initialise editable string */
273 memset ( &string, 0, sizeof ( string ) );
274 init_editstring ( &string, buf, sizeof ( buf ) );
277 /* Prefill string, if applicable */
279 replace_string ( &string, prefill );
280 sync_console ( &string );
284 /* Handle keypress */
285 key = edit_string ( &string, getkey ( 0 ) );
286 sync_console ( &string );
291 *line = strdup ( buf );
292 rc = ( ( *line ) ? 0 : -ENOMEM );
308 /* Handle history movement, if applicable */
309 if ( move_by && history ) {
310 new_string = history_move ( history, move_by, buf );
312 replace_string ( &string, new_string );
313 sync_console ( &string );
321 if ( *line && (*line)[0] )
322 history_append ( history, *line );
323 history_cleanup ( history );
325 assert ( ( rc == 0 ) ^ ( *line == NULL ) );
330 * Read line from console
332 * @v prompt Prompt string
333 * @ret line Line read from console (excluding terminating newline)
335 * The returned line is allocated with malloc(); the caller must
336 * eventually call free() to release the storage.
338 char * readline ( const char *prompt ) {
341 readline_history ( prompt, NULL, NULL, &line );