Use symbolic key names, and avoid moving beyond end of string

pull/1/head
Michael Brown 2006-12-20 22:21:09 +00:00
parent 46c3eeba2a
commit 4b2b8b02ab
1 changed files with 17 additions and 9 deletions

View File

@ -18,6 +18,7 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <gpxe/keys.h>
#include <gpxe/editstring.h> #include <gpxe/editstring.h>
/** @file /** @file
@ -124,6 +125,7 @@ static void kill_eol ( struct edit_string *string ) {
*/ */
int edit_string ( struct edit_string *string, int key ) { int edit_string ( struct edit_string *string, int key ) {
int retval = 0; int retval = 0;
size_t len = strlen ( string->buf );
/* Prepare edit history */ /* Prepare edit history */
string->last_cursor = string->cursor; string->last_cursor = string->cursor;
@ -135,33 +137,39 @@ int edit_string ( struct edit_string *string, int key ) {
/* Printable character; insert at current position */ /* Printable character; insert at current position */
insert_character ( string, key ); insert_character ( string, key );
} else switch ( key ) { } else switch ( key ) {
case 0x08: /* Ctrl-H */ case KEY_BACKSPACE:
/* Backspace */ /* Backspace */
backspace ( string ); backspace ( string );
break; break;
case 0x04: /* Ctrl-D */ case KEY_DC:
case CTRL_D:
/* Delete character */ /* Delete character */
delete_character ( string ); delete_character ( string );
break; break;
case 0x0b: /* Ctrl-K */ case CTRL_K:
/* Delete to end of line */
kill_eol ( string ); kill_eol ( string );
break; break;
case 0x01: /* Ctrl-A */ case KEY_HOME:
case CTRL_A:
/* Start of line */ /* Start of line */
string->cursor = 0; string->cursor = 0;
break; break;
case 0x05: /* Ctrl-E */ case KEY_END:
case CTRL_E:
/* End of line */ /* End of line */
string->cursor = strlen ( string->buf ); string->cursor = len;
break; break;
case 0x02: /* Ctrl-B */ case KEY_LEFT:
case CTRL_B:
/* Cursor left */ /* Cursor left */
if ( string->cursor > 0 ) if ( string->cursor > 0 )
string->cursor--; string->cursor--;
break; break;
case 0x06: /* Ctrl-F */ case KEY_RIGHT:
case CTRL_F:
/* Cursor right */ /* Cursor right */
if ( string->cursor < ( string->len - 1 ) ) if ( string->cursor < len )
string->cursor++; string->cursor++;
break; break;
default: default: