Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / readline.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <ipxe/console.h>
27 #include <ipxe/keys.h>
28 #include <ipxe/editstring.h>
29 #include <readline/readline.h>
30
31 /** @file
32  *
33  * Minimal readline
34  *
35  */
36
37 #define READLINE_MAX 256
38
39 /**
40  * Synchronise console with edited string
41  *
42  * @v string            Editable string
43  */
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 );
49
50         /* Expand region back to old cursor position if applicable */
51         if ( mod_start > string->last_cursor )
52                 mod_start = string->last_cursor;
53
54         /* Expand region forward to new cursor position if applicable */
55         if ( mod_end < string->cursor )
56                 mod_end = string->cursor;
57
58         /* Backspace to start of region */
59         while ( cursor > mod_start ) {
60                 putchar ( '\b' );
61                 cursor--;
62         }
63
64         /* Print modified region */
65         while ( cursor < mod_end ) {
66                 putchar ( ( cursor >= len ) ? ' ' : string->buf[cursor] );
67                 cursor++;
68         }
69
70         /* Backspace to new cursor position */
71         while ( cursor > string->cursor ) {
72                 putchar ( '\b' );
73                 cursor--;
74         }
75 }
76
77 /**
78  * Locate history entry
79  *
80  * @v history           History buffer
81  * @v depth             Depth within history buffer
82  * @ret entry           History entry
83  */
84 static struct readline_history_entry *
85 history_entry ( struct readline_history *history, unsigned int depth ) {
86         unsigned int offset;
87
88         offset = ( ( history->next - depth ) %
89                    ( sizeof ( history->entries ) /
90                      sizeof ( history->entries[0] ) ) );
91         return &history->entries[offset];
92 }
93
94 /**
95  * Read string from history buffer
96  *
97  * @v history           History buffer
98  * @v depth             Depth within history buffer
99  * @ret string          String
100  */
101 static const char * history_fetch ( struct readline_history *history,
102                                     unsigned int depth ) {
103         struct readline_history_entry *entry;
104
105         /* Return the temporary copy if it exists, otherwise return
106          * the persistent copy.
107          */
108         entry = history_entry ( history, depth );
109         return ( entry->temp ? entry->temp : entry->string );
110 }
111
112 /**
113  * Write temporary string copy to history buffer
114  *
115  * @v history           History buffer
116  * @v depth             Depth within history buffer
117  * @v string            String
118  */
119 static void history_store ( struct readline_history *history,
120                             unsigned int depth, const char *string ) {
121         struct readline_history_entry *entry;
122         char *temp;
123
124         /* Create temporary copy of string */
125         temp = strdup ( string );
126         if ( ! temp ) {
127                 /* Just discard the string; there's nothing we can do */
128                 DBGC ( history, "READLINE %p could not store string\n",
129                        history );
130                 return;
131         }
132
133         /* Store temporary copy */
134         entry = history_entry ( history, depth );
135         free ( entry->temp );
136         entry->temp = temp;
137 }
138
139 /**
140  * Move to new history depth
141  *
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
146  */
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 );
151
152         /* Depth checks */
153         if ( new_depth > READLINE_HISTORY_MAX_DEPTH )
154                 return NULL;
155         if ( ! new_string )
156                 return NULL;
157
158         /* Store temporary copy of old string at current depth */
159         history_store ( history, history->depth, old_string );
160
161         /* Update depth */
162         history->depth = new_depth;
163
164         /* Return new string */
165         return new_string;
166 }
167
168 /**
169  * Append new history entry
170  *
171  * @v history           History buffer
172  * @v string            String
173  */
174 static void history_append ( struct readline_history *history,
175                              const char *string ) {
176         struct readline_history_entry *entry;
177
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",
185                        history );
186                 return;
187         }
188
189         /* Increment history position */
190         history->next++;
191
192         /* Prepare empty "next" slot */
193         entry = history_entry ( history, 0 );
194         free ( entry->string );
195         entry->string = NULL;
196 }
197
198 /**
199  * Clean up history after editing
200  *
201  * @v history           History buffer
202  */
203 static void history_cleanup ( struct readline_history *history ) {
204         struct readline_history_entry *entry;
205         unsigned int i;
206
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 );
212                 entry->temp = NULL;
213         }
214
215         /* Reset depth */
216         history->depth = 0;
217
218         /* Sanity check */
219         entry = history_entry ( history, 0 );
220         assert ( entry->string == NULL );
221 }
222
223 /**
224  * Free history buffer
225  *
226  * @v history           History buffer
227  */
228 void history_free ( struct readline_history *history ) {
229         struct readline_history_entry *entry;
230         unsigned int i;
231
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 );
238         }
239 }
240
241 /**
242  * Read line from console (with history)
243  *
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
249  *
250  * The returned line is allocated with malloc(); the caller must
251  * eventually call free() to release the storage.
252  */
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;
257         int key;
258         int move_by;
259         const char *new_string;
260         int rc;
261
262         /* Avoid returning uninitialised data on error */
263         *line = NULL;
264
265         /* Display prompt, if applicable */
266         if ( prompt )
267                 printf ( "%s", prompt );
268
269         /* Ensure cursor is visible */
270         printf ( "\033[?25h" );
271
272         /* Initialise editable string */
273         memset ( &string, 0, sizeof ( string ) );
274         init_editstring ( &string, buf, sizeof ( buf ) );
275         buf[0] = '\0';
276
277         /* Prefill string, if applicable */
278         if ( prefill ) {
279                 replace_string ( &string, prefill );
280                 sync_console ( &string );
281         }
282
283         while ( 1 ) {
284                 /* Handle keypress */
285                 key = edit_string ( &string, getkey ( 0 ) );
286                 sync_console ( &string );
287                 move_by = 0;
288                 switch ( key ) {
289                 case CR:
290                 case LF:
291                         *line = strdup ( buf );
292                         rc = ( ( *line ) ? 0 : -ENOMEM );
293                         goto done;
294                 case CTRL_C:
295                         rc = -ECANCELED;
296                         goto done;
297                 case KEY_UP:
298                         move_by = 1;
299                         break;
300                 case KEY_DOWN:
301                         move_by = -1;
302                         break;
303                 default:
304                         /* Do nothing */
305                         break;
306                 }
307
308                 /* Handle history movement, if applicable */
309                 if ( move_by && history ) {
310                         new_string = history_move ( history, move_by, buf );
311                         if ( new_string ) {
312                                 replace_string ( &string, new_string );
313                                 sync_console ( &string );
314                         }
315                 }
316         }
317
318  done:
319         putchar ( '\n' );
320         if ( history ) {
321                 if ( *line && (*line)[0] )
322                         history_append ( history, *line );
323                 history_cleanup ( history );
324         }
325         assert ( ( rc == 0 ) ^ ( *line == NULL ) );
326         return rc;
327 }
328
329 /**
330  * Read line from console
331  *
332  * @v prompt            Prompt string
333  * @ret line            Line read from console (excluding terminating newline)
334  *
335  * The returned line is allocated with malloc(); the caller must
336  * eventually call free() to release the storage.
337  */
338 char * readline ( const char *prompt ) {
339         char *line;
340
341         readline_history ( prompt, NULL, NULL, &line );
342         return line;
343 }