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 );
29 #include <ipxe/keys.h>
30 #include <ipxe/editstring.h>
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;
51 * Insert and/or delete text within an editable string
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
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;
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 );
73 /* Fill in edit history */
74 string->mod_start = string->cursor;
75 string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
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 ) );
82 /* Copy inserted text to cursor position */
83 memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
84 string->cursor += insert_len;
88 * Insert character at current cursor position
90 * @v string Editable string
91 * @v character Character to insert
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 );
100 * Delete character at current cursor position
102 * @v string Editable string
104 static void delete_character ( struct edit_string *string ) {
105 insert_delete ( string, 1, NULL );
109 * Delete character to left of current cursor position
111 * @v string Editable string
113 static void backspace ( struct edit_string *string ) {
114 if ( string->cursor > 0 ) {
116 delete_character ( string );
121 * Move to start of previous word
123 * @v string Editable string
125 static void previous_word ( struct edit_string *string ) {
126 while ( string->cursor &&
127 isspace ( string->buf[ string->cursor - 1 ] ) ) {
130 while ( string->cursor &&
131 ( ! isspace ( string->buf[ string->cursor - 1 ] ) ) ) {
137 * Delete to end of previous word
139 * @v string Editable string
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 );
148 * Delete to start of line
150 * @v string Editable string
152 static void kill_sol ( struct edit_string *string ) {
153 size_t old_cursor = string->cursor;
155 insert_delete ( string, old_cursor, NULL );
159 * Delete to end of line
161 * @v string Editable string
163 static void kill_eol ( struct edit_string *string ) {
164 insert_delete ( string, ~( ( size_t ) 0 ), NULL );
168 * Replace editable string
170 * @v string Editable string
171 * @v replacement Replacement string
173 void replace_string ( struct edit_string *string, const char *replacement ) {
175 insert_delete ( string, ~( ( size_t ) 0 ), replacement );
179 * Edit editable string
181 * @v string Editable string
182 * @v key Key pressed by user
183 * @ret key Key returned to application, or zero
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.
190 * This function does not update the display in any way.
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.
195 int edit_string ( struct edit_string *string, int key ) {
197 size_t len = strlen ( string->buf );
199 /* Prepare edit history */
200 string->last_cursor = string->cursor;
201 string->mod_start = string->cursor;
202 string->mod_end = string->cursor;
205 if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
206 /* Printable character; insert at current position */
207 insert_character ( string, key );
208 } else switch ( key ) {
211 backspace ( string );
215 /* Delete character */
216 delete_character ( string );
220 kill_word ( string );
223 /* Delete to start of line */
227 /* Delete to end of line */
238 string->cursor = len;
243 if ( string->cursor > 0 )
249 if ( string->cursor < len )