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