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/keys.h>
22 #include <gpxe/editstring.h>
31 * Insert and/or delete text within an editable string
33 * @v string Editable string
34 * @v delete_len Length of text to delete from current cursor position
35 * @v insert_text Text to insert at current cursor position, or NULL
37 static void insert_delete ( struct edit_string *string, size_t delete_len,
38 const char *insert_text ) {
39 size_t old_len, max_delete_len, insert_len, max_insert_len, new_len;
41 /* Calculate lengths */
42 old_len = strlen ( string->buf );
43 assert ( string->cursor <= old_len );
44 max_delete_len = ( old_len - string->cursor );
45 if ( delete_len > max_delete_len )
46 delete_len = max_delete_len;
47 insert_len = ( insert_text ? strlen ( insert_text ) : 0 );
48 max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) );
49 if ( insert_len > max_insert_len )
50 insert_len = max_insert_len;
51 new_len = ( old_len - delete_len + insert_len );
53 /* Fill in edit history */
54 string->mod_start = string->cursor;
55 string->mod_end = ( ( new_len > old_len ) ? new_len : old_len );
57 /* Move data following the cursor */
58 memmove ( ( string->buf + string->cursor + insert_len ),
59 ( string->buf + string->cursor + delete_len ),
60 ( max_delete_len + 1 - delete_len ) );
62 /* Copy inserted text to cursor position */
63 memcpy ( ( string->buf + string->cursor ), insert_text, insert_len );
64 string->cursor += insert_len;
68 * Insert character at current cursor position
70 * @v string Editable string
71 * @v character Character to insert
73 static void insert_character ( struct edit_string *string,
74 unsigned int character ) {
75 char insert_text[2] = { character, '\0' };
76 insert_delete ( string, 0, insert_text );
80 * Delete character at current cursor position
82 * @v string Editable string
84 static void delete_character ( struct edit_string *string ) {
85 insert_delete ( string, 1, NULL );
89 * Delete character to left of current cursor position
91 * @v string Editable string
93 static void backspace ( struct edit_string *string ) {
94 if ( string->cursor > 0 ) {
96 delete_character ( string );
101 * Delete to end of line
103 * @v string Editable string
105 static void kill_eol ( struct edit_string *string ) {
106 insert_delete ( string, ~( ( size_t ) 0 ), NULL );
110 * Edit editable string
112 * @v string Editable string
113 * @v key Key pressed by user
114 * @ret key Key returned to application, or zero
116 * Handles keypresses and updates the content of the editable string.
117 * Basic line editing facilities (delete/insert/cursor) are supported.
118 * If edit_string() understands and uses the keypress it will return
119 * zero, otherwise it will return the original key.
121 * This function does not update the display in any way.
123 * The string's edit history will be updated to allow the caller to
124 * efficiently bring the display into sync with the string content.
126 int edit_string ( struct edit_string *string, int key ) {
128 size_t len = strlen ( string->buf );
130 /* Prepare edit history */
131 string->last_cursor = string->cursor;
132 string->mod_start = string->cursor;
133 string->mod_end = string->cursor;
136 if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) {
137 /* Printable character; insert at current position */
138 insert_character ( string, key );
139 } else switch ( key ) {
142 backspace ( string );
146 /* Delete character */
147 delete_character ( string );
150 /* Delete to end of line */
161 string->cursor = len;
166 if ( string->cursor > 0 )
172 if ( string->cursor < len )