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
20 FILE_LICENCE ( GPL2_OR_LATER );
25 #include <ipxe/keys.h>
26 #include <ipxe/editstring.h>
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;
47 * Insert and/or delete text within an editable string
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
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;
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 );
69 /* Fill in edit history */
70 string->mod_start = string->cursor;
71 string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
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 ) );
78 /* Copy inserted text to cursor position */
79 memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
80 string->cursor += insert_len;
84 * Insert character at current cursor position
86 * @v string Editable string
87 * @v character Character to insert
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 );
96 * Delete character at current cursor position
98 * @v string Editable string
100 static void delete_character ( struct edit_string *string ) {
101 insert_delete ( string, 1, NULL );
105 * Delete character to left of current cursor position
107 * @v string Editable string
109 static void backspace ( struct edit_string *string ) {
110 if ( string->cursor > 0 ) {
112 delete_character ( string );
117 * Move to start of previous word
119 * @v string Editable string
121 static void previous_word ( struct edit_string *string ) {
122 while ( string->cursor &&
123 isspace ( string->buf[ string->cursor - 1 ] ) ) {
126 while ( string->cursor &&
127 ( ! isspace ( string->buf[ string->cursor - 1 ] ) ) ) {
133 * Delete to end of previous word
135 * @v string Editable string
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 );
144 * Delete to start of line
146 * @v string Editable string
148 static void kill_sol ( struct edit_string *string ) {
149 size_t old_cursor = string->cursor;
151 insert_delete ( string, old_cursor, NULL );
155 * Delete to end of line
157 * @v string Editable string
159 static void kill_eol ( struct edit_string *string ) {
160 insert_delete ( string, ~( ( size_t ) 0 ), NULL );
164 * Replace editable string
166 * @v string Editable string
167 * @v replacement Replacement string
169 void replace_string ( struct edit_string *string, const char *replacement ) {
171 insert_delete ( string, ~( ( size_t ) 0 ), replacement );
175 * Edit editable string
177 * @v string Editable string
178 * @v key Key pressed by user
179 * @ret key Key returned to application, or zero
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.
186 * This function does not update the display in any way.
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.
191 int edit_string ( struct edit_string *string, int key ) {
193 size_t len = strlen ( string->buf );
195 /* Prepare edit history */
196 string->last_cursor = string->cursor;
197 string->mod_start = string->cursor;
198 string->mod_end = string->cursor;
201 if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
202 /* Printable character; insert at current position */
203 insert_character ( string, key );
204 } else switch ( key ) {
207 backspace ( string );
211 /* Delete character */
212 delete_character ( string );
216 kill_word ( string );
219 /* Delete to start of line */
223 /* Delete to end of line */
234 string->cursor = len;
239 if ( string->cursor > 0 )
245 if ( string->cursor < len )