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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
30 #include <ipxe/console.h>
31 #include <ipxe/keys.h>
32 #include <ipxe/editstring.h>
33 #include <readline/readline.h>
41 #define READLINE_MAX 1024
44 * Synchronise console with edited string
46 * @v string Editable string
48 static void sync_console ( struct edit_string
*string
) {
49 unsigned int mod_start
= string
->mod_start
;
50 unsigned int mod_end
= string
->mod_end
;
51 unsigned int cursor
= string
->last_cursor
;
52 size_t len
= strlen ( string
->buf
);
54 /* Expand region back to old cursor position if applicable */
55 if ( mod_start
> string
->last_cursor
)
56 mod_start
= string
->last_cursor
;
58 /* Expand region forward to new cursor position if applicable */
59 if ( mod_end
< string
->cursor
)
60 mod_end
= string
->cursor
;
62 /* Backspace to start of region */
63 while ( cursor
> mod_start
) {
68 /* Print modified region */
69 while ( cursor
< mod_end
) {
70 putchar ( ( cursor
>= len
) ?
' ' : string
->buf
[cursor
] );
74 /* Backspace to new cursor position */
75 while ( cursor
> string
->cursor
) {
82 * Locate history entry
84 * @v history History buffer
85 * @v depth Depth within history buffer
86 * @ret entry History entry
88 static struct readline_history_entry
*
89 history_entry ( struct readline_history
*history
, unsigned int depth
) {
92 offset
= ( ( history
->next
- depth
) %
93 ( sizeof ( history
->entries
) /
94 sizeof ( history
->entries
[0] ) ) );
95 return &history
->entries
[offset
];
99 * Read string from history buffer
101 * @v history History buffer
102 * @v depth Depth within history buffer
105 static const char * history_fetch ( struct readline_history
*history
,
106 unsigned int depth
) {
107 struct readline_history_entry
*entry
;
109 /* Return the temporary copy if it exists, otherwise return
110 * the persistent copy.
112 entry
= history_entry ( history
, depth
);
113 return ( entry
->temp ? entry
->temp
: entry
->string
);
117 * Write temporary string copy to history buffer
119 * @v history History buffer
120 * @v depth Depth within history buffer
123 static void history_store ( struct readline_history
*history
,
124 unsigned int depth
, const char *string
) {
125 struct readline_history_entry
*entry
;
128 /* Create temporary copy of string */
129 temp
= strdup ( string
);
131 /* Just discard the string; there's nothing we can do */
132 DBGC ( history
, "READLINE %p could not store string\n",
137 /* Store temporary copy */
138 entry
= history_entry ( history
, depth
);
139 free ( entry
->temp
);
144 * Move to new history depth
146 * @v history History buffer
147 * @v offset Offset by which to change depth
148 * @v old_string String (possibly modified) at current depth
149 * @ret new_string String at new depth, or NULL for no movement
151 static const char * history_move ( struct readline_history
*history
,
152 int offset
, const char *old_string
) {
153 unsigned int new_depth
= ( history
->depth
+ offset
);
154 const char * new_string
= history_fetch ( history
, new_depth
);
157 if ( new_depth
> READLINE_HISTORY_MAX_DEPTH
)
162 /* Store temporary copy of old string at current depth */
163 history_store ( history
, history
->depth
, old_string
);
166 history
->depth
= new_depth
;
168 /* Return new string */
173 * Append new history entry
175 * @v history History buffer
178 static void history_append ( struct readline_history
*history
,
179 const char *string
) {
180 struct readline_history_entry
*entry
;
182 /* Store new entry */
183 entry
= history_entry ( history
, 0 );
184 assert ( entry
->string
== NULL
);
185 entry
->string
= strdup ( string
);
186 if ( ! entry
->string
) {
187 /* Just discard the string; there's nothing we can do */
188 DBGC ( history
, "READLINE %p could not append string\n",
193 /* Increment history position */
196 /* Prepare empty "next" slot */
197 entry
= history_entry ( history
, 0 );
198 free ( entry
->string
);
199 entry
->string
= NULL
;
203 * Clean up history after editing
205 * @v history History buffer
207 static void history_cleanup ( struct readline_history
*history
) {
208 struct readline_history_entry
*entry
;
211 /* Discard any temporary strings */
212 for ( i
= 0 ; i
< ( sizeof ( history
->entries
) /
213 sizeof ( history
->entries
[0] ) ) ; i
++ ) {
214 entry
= &history
->entries
[i
];
215 free ( entry
->temp
);
223 entry
= history_entry ( history
, 0 );
224 assert ( entry
->string
== NULL
);
228 * Free history buffer
230 * @v history History buffer
232 void history_free ( struct readline_history
*history
) {
233 struct readline_history_entry
*entry
;
236 /* Discard any temporary strings */
237 for ( i
= 0 ; i
< ( sizeof ( history
->entries
) /
238 sizeof ( history
->entries
[0] ) ) ; i
++ ) {
239 entry
= &history
->entries
[i
];
240 assert ( entry
->temp
== NULL
);
241 free ( entry
->string
);
246 * Read line from console (with history)
248 * @v prompt Prompt string
249 * @v prefill Prefill string, or NULL for no prefill
250 * @v history History buffer, or NULL for no history
251 * @v timeout Timeout period, in ticks (0=indefinite)
252 * @ret line Line read from console (excluding terminating newline)
253 * @ret rc Return status code
255 * The returned line is allocated with malloc(); the caller must
256 * eventually call free() to release the storage.
258 int readline_history ( const char *prompt
, const char *prefill
,
259 struct readline_history
*history
, unsigned long timeout
,
261 struct edit_string string
;
265 const char *new_string
;
268 /* Avoid returning uninitialised data on error */
271 /* Display prompt, if applicable */
273 printf ( "%s", prompt
);
275 /* Ensure cursor is visible */
276 printf ( "\033[?25h" );
278 /* Allocate buffer and initialise editable string */
279 buf
= zalloc ( READLINE_MAX
);
284 memset ( &string
, 0, sizeof ( string
) );
285 init_editstring ( &string
, buf
, READLINE_MAX
);
287 /* Prefill string, if applicable */
289 replace_string ( &string
, prefill
);
290 sync_console ( &string
);
296 key
= getkey ( timeout
);
303 /* Handle keypress */
304 key
= edit_string ( &string
, key
);
305 sync_console ( &string
);
310 /* Shrink string (ignoring failures) */
311 *line
= realloc ( buf
,
312 ( strlen ( buf
) + 1 /* NUL */ ) );
332 /* Handle history movement, if applicable */
333 if ( move_by
&& history
) {
334 new_string
= history_move ( history
, move_by
, buf
);
336 replace_string ( &string
, new_string
);
337 sync_console ( &string
);
346 if ( *line
&& (*line
)[0] )
347 history_append ( history
, *line
);
348 history_cleanup ( history
);
350 assert ( ( rc
== 0 ) ^ ( *line
== NULL
) );
355 * Read line from console
357 * @v prompt Prompt string
358 * @ret line Line read from console (excluding terminating newline)
360 * The returned line is allocated with malloc(); the caller must
361 * eventually call free() to release the storage.
363 char * readline ( const char *prompt
) {
366 readline_history ( prompt
, NULL
, NULL
, 0, &line
);