From f26667d117af5a119471eb531f3ded65b9c1fbc8 Mon Sep 17 00:00:00 2001 From: David Walter Seikel Date: Sat, 1 Feb 2014 15:00:00 +1000 Subject: Better method to tell if it's ordinary characters, and deal with partial reads better. --- dumbsh.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to 'dumbsh.c') diff --git a/dumbsh.c b/dumbsh.c index 875614b..5f2c63e 100644 --- a/dumbsh.c +++ b/dumbsh.c @@ -188,25 +188,30 @@ static struct keyCommand simpleEmacsKeys[] = }; // Callback for incoming key sequences from the user. -static int handleKeySequence(long extra, char *sequence) +static int handleKeySequence(long extra, char *sequence, int isTranslated) { - int j; + int j, l = strlen(sequence); // Search for a key sequence bound to a command. for (j = 0; j < (sizeof(simpleEmacsKeys) / sizeof(*simpleEmacsKeys)); j++) { - if (strcmp(simpleEmacsKeys[j].key, sequence) == 0) + if (strncmp(simpleEmacsKeys[j].key, sequence, l) == 0) { - if (simpleEmacsKeys[j].handler) simpleEmacsKeys[j].handler(); - return 1; + // If it's a partial match, keep accumulating them. + if (strlen(simpleEmacsKeys[j].key) != l) + return 0; + else + { + if (simpleEmacsKeys[j].handler) simpleEmacsKeys[j].handler(); + return 1; + } } } // See if it's ordinary keys. // NOTE - with vi style ordinary keys can be commands, // but they would be found by the command check above first. - // So here we just check the first character, and insert it all. - if (isprint(sequence[0])) + if (!isTranslated) { if (TT.x < sizeof(toybuf)) { @@ -219,12 +224,10 @@ static int handleKeySequence(long extra, char *sequence) TT.x += l; updateLine(); } - return 1; } - // Return 0 if we didn't handle it, handle_keys will just keep on - // accumulating sequences and trying again. - return 0; + // Tell handle_keys to drop it, coz we dealt with it, or it's not one of ours. + return 1; } void dumbsh_main(void) -- cgit v1.1