Skip to content

Commit 0a84efe

Browse files
shell: flush keyboard buffer around exec to avoid stale input
shell: flush keyboard buffer around exec to avoid stale input
2 parents 63f165b + f4fa2b3 commit 0a84efe

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

source/includes/keyboard.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ uint8_t getc();
7474
*/
7575
int getc_nonblock();
7676

77+
/**
78+
* @brief Clears pending keyboard input so stale keypresses are discarded.
79+
*/
80+
void keyboard_flush_buffer(void);
81+
7782
/**
7883
* @brief Converts the scancode passed to it to ASCII characters.
7984
*
@@ -82,4 +87,4 @@ int getc_nonblock();
8287
*/
8388
int handle_char_from_scancode(uint8_t data);
8489

85-
#endif
90+
#endif

source/kernel/C/shell/commands/exec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <userland.h>
1515
#include <graphics.h>
1616
#include <tty.h>
17+
#include <keyboard.h>
1718

1819
int cmd_exec(int argc, char** argv)
1920
{
@@ -39,11 +40,14 @@ int cmd_exec(int argc, char** argv)
3940
user_argv[user_argc] = NULL;
4041

4142
tty_flush_input();
43+
keyboard_flush_buffer();
4244

4345
if (userland_exec(path, user_argc, user_argv, NULL) != 0) {
46+
keyboard_flush_buffer();
4447
eprintf("exec: failed to load ELF");
4548
return -1;
4649
}
4750

51+
keyboard_flush_buffer();
4852
return 0;
4953
}

source/kernel/C/user-input/keyboard.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ int getc_nonblock() {
119119
return 0; // no key available
120120
}
121121

122+
void keyboard_flush_buffer(void) {
123+
rb_clear(&kb_rb);
124+
}
122125

123126
static bool extended = false;
124127
int handle_char_from_scancode(uint8_t data)
@@ -221,4 +224,4 @@ int handle_char_from_scancode(uint8_t data)
221224
return 0;
222225

223226
return c;
224-
}
227+
}

0 commit comments

Comments
 (0)