Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / editstring.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 <assert.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <ipxe/keys.h>
26 #include <ipxe/editstring.h>
27
28 /** @file
29  *
30  * Editable strings
31  *
32  */
33
34 static void insert_delete ( struct edit_string *string, size_t delete_len,
35                             const char *insert_text ) 
36                             __attribute__ (( nonnull (1) ));
37 static void insert_character ( struct edit_string *string,
38                                unsigned int character ) __nonnull;
39 static void delete_character ( struct edit_string *string ) __nonnull;
40 static void backspace ( struct edit_string *string ) __nonnull;
41 static void previous_word ( struct edit_string *string ) __nonnull;
42 static void kill_word ( struct edit_string *string ) __nonnull;
43 static void kill_sol ( struct edit_string *string ) __nonnull;
44 static void kill_eol ( struct edit_string *string ) __nonnull;
45
46 /**
47  * Insert and/or delete text within an editable string
48  *
49  * @v string            Editable string
50  * @v delete_len        Length of text to delete from current cursor position
51  * @v insert_text       Text to insert at current cursor position, or NULL
52  */
53 static void insert_delete ( struct edit_string *string, size_t delete_len,
54                             const char *insert_text ) {
55         size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
56
57         /* Calculate lengths */
58         old_len = strlen ( string->buf );
59         assert ( string->cursor <= old_len );
60         max_delete_len = ( old_len - string->cursor );
61         if ( delete_len > max_delete_len )
62                 delete_len = max_delete_len;
63         insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
64         max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
65         if ( insert_len > max_insert_len )
66                 insert_len = max_insert_len;
67         new_len = ( old_len - delete_len + insert_len );
68
69         /* Fill in edit history */
70         string->mod_start = string->cursor;
71         string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
72
73         /* Move data following the cursor */
74         memmove ( ( string->buf + string->cursor + insert_len ),
75                   ( string->buf + string->cursor + delete_len ),
76                   ( max_delete_len + 1 - delete_len ) );
77
78         /* Copy inserted text to cursor position */
79         memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
80         string->cursor += insert_len;
81 }
82
83 /**
84  * Insert character at current cursor position
85  *
86  * @v string            Editable string
87  * @v character         Character to insert
88  */
89 static void insert_character ( struct edit_string *string,
90                               unsigned int character ) {
91         char insert_text[2] = { character, '\0' };
92         insert_delete ( string, 0, insert_text );
93 }
94
95 /**
96  * Delete character at current cursor position
97  *
98  * @v string            Editable string
99  */
100 static void delete_character ( struct edit_string *string ) {
101         insert_delete ( string, 1, NULL );
102 }
103
104 /**
105  * Delete character to left of current cursor position
106  *
107  * @v string            Editable string
108  */
109 static void backspace ( struct edit_string *string ) {
110         if ( string->cursor > 0 ) {
111                 string->cursor--;
112                 delete_character ( string );
113         }
114 }
115
116 /**
117  * Move to start of previous word
118  *
119  * @v string            Editable string
120  */
121 static void previous_word ( struct edit_string *string ) {
122         while ( string->cursor &&
123                 isspace ( string->buf[ string->cursor - 1 ] ) ) {
124                 string->cursor--;
125         }
126         while ( string->cursor &&
127                 ( ! isspace ( string->buf[ string->cursor - 1 ] ) ) ) {
128                 string->cursor--;
129         }
130 }
131
132 /**
133  * Delete to end of previous word
134  *
135  * @v string            Editable string
136  */
137 static void kill_word ( struct edit_string *string ) {
138         size_t old_cursor = string->cursor;
139         previous_word ( string );
140         insert_delete ( string, ( old_cursor - string->cursor ), NULL );
141 }
142
143 /**
144  * Delete to start of line
145  *
146  * @v string            Editable string
147  */
148 static void kill_sol ( struct edit_string *string ) {
149         size_t old_cursor = string->cursor;
150         string->cursor = 0;
151         insert_delete ( string, old_cursor, NULL );
152 }
153
154 /**
155  * Delete to end of line
156  *
157  * @v string            Editable string
158  */
159 static void kill_eol ( struct edit_string *string ) {
160         insert_delete ( string, ~( ( size_t ) 0 ), NULL );
161 }
162
163 /**
164  * Replace editable string
165  *
166  * @v string            Editable string
167  * @v replacement       Replacement string
168  */
169 void replace_string ( struct edit_string *string, const char *replacement ) {
170         string->cursor = 0;
171         insert_delete ( string, ~( ( size_t ) 0 ), replacement );
172 }
173
174 /**
175  * Edit editable string
176  *
177  * @v string            Editable string
178  * @v key               Key pressed by user
179  * @ret key             Key returned to application, or zero
180  *
181  * Handles keypresses and updates the content of the editable string.
182  * Basic line editing facilities (delete/insert/cursor) are supported.
183  * If edit_string() understands and uses the keypress it will return
184  * zero, otherwise it will return the original key.
185  *
186  * This function does not update the display in any way.
187  *
188  * The string's edit history will be updated to allow the caller to
189  * efficiently bring the display into sync with the string content.
190  */
191 int edit_string ( struct edit_string *string, int key ) {
192         int retval = 0;
193         size_t len = strlen ( string->buf );
194
195         /* Prepare edit history */
196         string->last_cursor = string->cursor;
197         string->mod_start = string->cursor;
198         string->mod_end = string->cursor;
199
200         /* Interpret key */
201         if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
202                 /* Printable character; insert at current position */
203                 insert_character ( string, key );
204         } else switch ( key ) {
205         case KEY_BACKSPACE:
206                 /* Backspace */
207                 backspace ( string );
208                 break;
209         case KEY_DC:
210         case CTRL_D:
211                 /* Delete character */
212                 delete_character ( string );
213                 break;
214         case CTRL_W:
215                 /* Delete word */
216                 kill_word ( string );
217                 break;
218         case CTRL_U:
219                 /* Delete to start of line */
220                 kill_sol ( string );
221                 break;
222         case CTRL_K:
223                 /* Delete to end of line */
224                 kill_eol ( string );
225                 break;
226         case KEY_HOME:
227         case CTRL_A:
228                 /* Start of line */
229                 string->cursor = 0;
230                 break;
231         case KEY_END:
232         case CTRL_E:
233                 /* End of line */
234                 string->cursor = len;
235                 break;
236         case KEY_LEFT:
237         case CTRL_B:
238                 /* Cursor left */
239                 if ( string->cursor > 0 )
240                         string->cursor--;
241                 break;
242         case KEY_RIGHT:
243         case CTRL_F:
244                 /* Cursor right */
245                 if ( string->cursor < len )
246                         string->cursor++;
247                 break;
248         default:
249                 retval = key;
250                 break;
251         }
252
253         return retval;
254 }