1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

parse_keystroke: Never write back to the input string.

This fixes a bug: in the previous version, l_bind_key() modified the
buffer whose address lua_tostring() returned, even though that is not
allowed according to Lua documentation <http://www.lua.org/pil/24.2.2.html>.

The change affects the user interface: previously, if the user typed
"ctrl+cokebottle" in the "Add keybinding" dialog box, ELinks would
change the text in the widget to "Ctrl-cokebottle" before complaining
that the keystroke is invalid.  Now, it leaves the widget unchanged.

This commit does not yet add const to parameters of parse_keystroke()
and related functions.
This commit is contained in:
Kalle Olavi Niemitalo 2006-08-13 14:24:28 +03:00 committed by Kalle Olavi Niemitalo
parent 93ef5e02f5
commit aba0bc77a8

View File

@ -372,23 +372,20 @@ read_key(unsigned char *key_str)
int
parse_keystroke(unsigned char *s, struct term_event_keyboard *kbd)
{
unsigned char ctrlbuf[2];
if (!strncasecmp(s, "Shift", 5) && (s[5] == '-' || s[5] == '+')) {
/* Shift+a == shiFt-a == Shift-a */
memcpy(s, "Shift-", 6);
kbd->modifier = KBD_MOD_SHIFT;
s += 6;
} else if (!strncasecmp(s, "Ctrl", 4) && (s[4] == '-' || s[4] == '+')) {
/* Ctrl+a == ctRl-a == Ctrl-a */
memcpy(s, "Ctrl-", 5);
kbd->modifier = KBD_MOD_CTRL;
s += 5;
/* Ctrl-a == Ctrl-A */
if (s[0] && !s[1]) s[0] = toupper(s[0]);
} else if (!strncasecmp(s, "Alt", 3) && (s[3] == '-' || s[3] == '+')) {
/* Alt+a == aLt-a == Alt-a */
memcpy(s, "Alt-", 4);
kbd->modifier = KBD_MOD_ALT;
s += 4;
@ -397,6 +394,13 @@ parse_keystroke(unsigned char *s, struct term_event_keyboard *kbd)
kbd->modifier = KBD_MOD_NONE;
}
if ((kbd->modifier & KBD_MOD_CTRL) != 0 && s[0] && !s[1]) {
/* Ctrl-a == Ctrl-A */
ctrlbuf[0] = toupper(s[0]);
ctrlbuf[1] = '\0';
s = ctrlbuf;
}
kbd->key = read_key(s);
return (kbd->key == KBD_UNDEF) ? -1 : 0;
}