Parcourir la source

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

tags/v0.9.3
Michael Brown il y a 18 ans
Parent
révision
4b2b8b02ab
1 fichiers modifiés avec 17 ajouts et 9 suppressions
  1. 17
    9
      src/hci/editstring.c

+ 17
- 9
src/hci/editstring.c Voir le fichier

@@ -18,6 +18,7 @@
18 18
 
19 19
 #include <assert.h>
20 20
 #include <string.h>
21
+#include <gpxe/keys.h>
21 22
 #include <gpxe/editstring.h>
22 23
 
23 24
 /** @file
@@ -124,6 +125,7 @@ static void kill_eol ( struct edit_string *string ) {
124 125
  */
125 126
 int edit_string ( struct edit_string *string, int key ) {
126 127
 	int retval = 0;
128
+	size_t len = strlen ( string->buf );
127 129
 
128 130
 	/* Prepare edit history */
129 131
 	string->last_cursor = string->cursor;
@@ -135,33 +137,39 @@ int edit_string ( struct edit_string *string, int key ) {
135 137
 		/* Printable character; insert at current position */
136 138
 		insert_character ( string, key );
137 139
 	} else switch ( key ) {
138
-	case 0x08: /* Ctrl-H */
140
+	case KEY_BACKSPACE:
139 141
 		/* Backspace */
140 142
 		backspace ( string );
141 143
 		break;
142
-	case 0x04: /* Ctrl-D */
144
+	case KEY_DC:
145
+	case CTRL_D:
143 146
 		/* Delete character */
144 147
 		delete_character ( string );
145 148
 		break;
146
-	case 0x0b: /* Ctrl-K */
149
+	case CTRL_K:
150
+		/* Delete to end of line */
147 151
 		kill_eol ( string );
148 152
 		break;
149
-	case 0x01: /* Ctrl-A */
153
+	case KEY_HOME:
154
+	case CTRL_A:
150 155
 		/* Start of line */
151 156
 		string->cursor = 0;
152 157
 		break;
153
-	case 0x05: /* Ctrl-E */
158
+	case KEY_END:
159
+	case CTRL_E:
154 160
 		/* End of line */
155
-		string->cursor = strlen ( string->buf );
161
+		string->cursor = len;
156 162
 		break;
157
-	case 0x02: /* Ctrl-B */
163
+	case KEY_LEFT:
164
+	case CTRL_B:
158 165
 		/* Cursor left */
159 166
 		if ( string->cursor > 0 )
160 167
 			string->cursor--;
161 168
 		break;
162
-	case 0x06: /* Ctrl-F */
169
+	case KEY_RIGHT:
170
+	case CTRL_F:
163 171
 		/* Cursor right */
164
-		if ( string->cursor < ( string->len - 1 ) )
172
+		if ( string->cursor < len )
165 173
 			string->cursor++;
166 174
 		break;
167 175
 	default:

Chargement…
Annuler
Enregistrer