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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <gpxe/editstring.h>
30 * Insert and/or delete text within an editable string
32 * @v string Editable string
33 * @v delete_len Length of text to delete from current cursor position
34 * @v insert_text Text to insert at current cursor position, or NULL
36 static void insert_delete ( struct edit_string *string, size_t delete_len,
37 const char *insert_text ) {
38 size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
40 /* Calculate lengths */
41 old_len = strlen ( string->buf );
42 assert ( string->cursor <= old_len );
43 max_delete_len = ( old_len - string->cursor );
44 if ( delete_len > max_delete_len )
45 delete_len = max_delete_len;
46 insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
47 max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
48 if ( insert_len > max_insert_len )
49 insert_len = max_insert_len;
50 new_len = ( old_len - delete_len + insert_len );
52 /* Fill in edit history */
53 string->mod_start = string->cursor;
54 string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
56 /* Move data following the cursor */
57 memmove ( ( string->buf + string->cursor + insert_len ),
58 ( string->buf + string->cursor + delete_len ),
59 ( max_delete_len + 1 - delete_len ) );
61 /* Copy inserted text to cursor position */
62 memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
63 string->cursor += insert_len;
67 * Insert character at current cursor position
69 * @v string Editable string
70 * @v character Character to insert
72 static void insert_character ( struct edit_string *string,
73 unsigned int character ) {
74 char insert_text[2] = { character, '\0' };
75 insert_delete ( string, 0, insert_text );
79 * Delete character at current cursor position
81 * @v string Editable string
83 static void delete_character ( struct edit_string *string ) {
84 insert_delete ( string, 1, NULL );
88 * Delete character to left of current cursor position
90 * @v string Editable string
92 static void backspace ( struct edit_string *string ) {
93 if ( string->cursor > 0 ) {
95 delete_character ( string );
100 * Delete to end of line
102 * @v string Editable string
104 static void kill_eol ( struct edit_string *string ) {
105 insert_delete ( string, ~( ( size_t ) 0 ), NULL );
109 * Edit editable string
111 * @v string Editable string
112 * @v key Key pressed by user
113 * @ret key Key returned to application, or zero
115 * Handles keypresses and updates the content of the editable string.
116 * Basic line editing facilities (delete/insert/cursor) are supported.
117 * If edit_string() understands and uses the keypress it will return
118 * zero, otherwise it will return the original key.
120 * This function does not update the display in any way.
122 * The string's edit history will be updated to allow the caller to
123 * efficiently bring the display into sync with the string content.
125 int edit_string ( struct edit_string *string, int key ) {
128 /* Prepare edit history */
129 string->last_cursor = string->cursor;
130 string->mod_start = string->cursor;
131 string->mod_end = string->cursor;
134 if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
135 /* Printable character; insert at current position */
136 insert_character ( string, key );
137 } else switch ( key ) {
138 case 0x08: /* Ctrl-H */
140 backspace ( string );
142 case 0x04: /* Ctrl-D */
143 /* Delete character */
144 delete_character ( string );
146 case 0x0b: /* Ctrl-K */
149 case 0x01: /* Ctrl-A */
153 case 0x05: /* Ctrl-E */
155 string->cursor = strlen ( string->buf );
157 case 0x02: /* Ctrl-B */
159 if ( string->cursor > 0 )
162 case 0x06: /* Ctrl-F */
164 if ( string->cursor < ( string->len - 1 ) )