From 1bc3514bd83ec5e1407afa059dd52c0118e73b41 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 19 Dec 2006 19:28:12 +0000 Subject: [PATCH] Start of generic editable string support --- src/Makefile | 2 +- src/hci/editstring.c | 159 ++++++++++++++++++++++++++++++++++ src/include/gpxe/editstring.h | 22 +++++ 3 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/hci/editstring.c create mode 100644 src/include/gpxe/editstring.h diff --git a/src/Makefile b/src/Makefile index 78d6c10f..09bede17 100644 --- a/src/Makefile +++ b/src/Makefile @@ -144,7 +144,7 @@ SRCDIRS += drivers/bitbash SRCDIRS += interface/pxe SRCDIRS += tests SRCDIRS += crypto -SRCDIRS += hci/mucurses +SRCDIRS += hci hci/mucurses SRCDIRS += commandline SRCDIRS += commandline/commands diff --git a/src/hci/editstring.c b/src/hci/editstring.c new file mode 100644 index 00000000..a17b5761 --- /dev/null +++ b/src/hci/editstring.c @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2006 Michael Brown . + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include + +/** @file + * + * Editable strings + * + */ + +/** + * Insert and/or delete text within an editable string + * + * @v string Editable string + * @v delete_len Length of text to delete from current cursor position + * @v insert_text Text to insert at current cursor position, or NULL + */ +static void insert_delete ( struct edit_string *string, size_t delete_len, + const char *insert_text ) { + size_t old_len, max_delete_len, insert_len, max_insert_len; + + /* Calculate lengths */ + old_len = strlen ( string->buf ); + assert ( string->cursor <= old_len ); + max_delete_len = ( old_len - string->cursor ); + if ( delete_len > max_delete_len ) + delete_len = max_delete_len; + insert_len = ( insert_text ? strlen ( insert_text ) : 0 ); + max_insert_len = ( ( string->len - 1 ) - ( old_len - delete_len ) ); + if ( insert_len > max_insert_len ) + insert_len = max_insert_len; + + /* Move data following the cursor */ + memmove ( ( string->buf + string->cursor + insert_len ), + ( string->buf + string->cursor + delete_len ), + ( max_delete_len + 1 - delete_len ) ); + + /* Copy inserted text to cursor position */ + memcpy ( ( string->buf + string->cursor ), insert_text, insert_len ); + string->cursor += insert_len; +} + +/** + * Insert character at current cursor position + * + * @v string Editable string + * @v character Character to insert + */ +static void insert_character ( struct edit_string *string, + unsigned int character ) { + char insert_text[2] = { character, '\0' }; + insert_delete ( string, 0, insert_text ); +} + +/** + * Delete character at current cursor position + * + * @v string Editable string + */ +static void delete_character ( struct edit_string *string ) { + insert_delete ( string, 1, NULL ); +} + +/** + * Delete character to left of current cursor position + * + * @v string Editable string + */ +static void backspace ( struct edit_string *string ) { + if ( string->cursor > 0 ) { + string->cursor--; + delete_character ( string ); + } +} + +/** + * Delete to end of line + * + * @v string Editable string + */ +static void kill_eol ( struct edit_string *string ) { + insert_delete ( string, ~( ( size_t ) 0 ), NULL ); +} + +/** + * Edit editable string + * + * @v string Editable string + * @v key Key pressed by user + * @ret key Key returned to application, or zero + * + * Handles keypresses and updates the content of the editable string. + * Basic line editing facilities (delete/insert/cursor) are supported. + * If edit_string() understands and uses the keypress it will return + * zero, otherwise it will return the original key. + * + * This function does not update the display in any way. + */ +int edit_string ( struct edit_string *string, int key ) { + int retval = 0; + + if ( ( key >= 0x20 ) && ( key <= 0x7e ) ) { + /* Printable character; insert at current position */ + insert_character ( string, key ); + } else switch ( key ) { + case 0x08: /* Ctrl-H */ + /* Backspace */ + backspace ( string ); + break; + case 0x04: /* Ctrl-D */ + /* Delete character */ + delete_character ( string ); + break; + case 0x0b: /* Ctrl-K */ + kill_eol ( string ); + break; + case 0x01: /* Ctrl-A */ + /* Start of line */ + string->cursor = 0; + break; + case 0x05: /* Ctrl-E */ + /* End of line */ + string->cursor = strlen ( string->buf ); + break; + case 0x02: /* Ctrl-B */ + /* Cursor left */ + if ( string->cursor > 0 ) + string->cursor--; + break; + case 0x06: /* Ctrl-F */ + /* Cursor right */ + if ( string->cursor < ( string->len - 1 ) ) + string->cursor++; + break; + default: + retval = key; + break; + } + + return retval; +} diff --git a/src/include/gpxe/editstring.h b/src/include/gpxe/editstring.h new file mode 100644 index 00000000..7dc96d9a --- /dev/null +++ b/src/include/gpxe/editstring.h @@ -0,0 +1,22 @@ +#ifndef _EDITSTRING_H +#define _EDITSTRING_H + +/** @file + * + * Editable strings + * + */ + +/** An editable string */ +struct edit_string { + /** Buffer for string */ + char *buf; + /** Size of buffer (including terminating NUL) */ + size_t len; + /** Cursor position */ + unsigned int cursor; +}; + +extern int edit_string ( struct edit_string *string, int key ); + +#endif /* _EDITSTRING_H */