diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2019-02-25 09:03:30 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2019-04-24 09:37:12 +0200 |
commit | c5f991d2ad8ea2d872fad64dee2fd660bb0a1b20 (patch) | |
tree | 8480b1d96613251b08e40702a2b576a3d392577b /common/console.c | |
parent | ea569a0afd6a2e9a84418cd18ddeaeefaa5eae39 (diff) | |
download | barebox-c5f991d2ad8ea2d872fad64dee2fd660bb0a1b20.tar.gz barebox-c5f991d2ad8ea2d872fad64dee2fd660bb0a1b20.tar.xz |
Shell: Handle aborting loops better
It's easy to get stuck in an infinite loop in the hush shell:
while true; do sleep 1; done
The 'sleep' command will check for ctrl-c with the ctrlc() function. This
will abort the sleep command. Hush then checks for ctrl-c again in the
loop. The ctrl-c in the buffer has already been eaten by the sleep
command, so the loop will continue.
With this patch we remember the presence of a ctrl-c character in a
variable instead of checking for a new character each time. The
variable must be resetted explicitly by calling ctrlc_handled() which
will be called by the shell in the outer loop.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'common/console.c')
-rw-r--r-- | common/console.c | 28 |
1 files changed, 23 insertions, 5 deletions
diff --git a/common/console.c b/common/console.c index 47ccf2e54d..b6685ecf6a 100644 --- a/common/console.c +++ b/common/console.c @@ -574,18 +574,36 @@ void console_flush(void) } EXPORT_SYMBOL(console_flush); -#ifndef ARCH_HAS_CTRLC +static int ctrlc_abort; + +void ctrlc_handled(void) +{ + ctrlc_abort = 0; +} + /* test if ctrl-c was pressed */ -int ctrlc (void) +int ctrlc(void) { + int ret = 0; + + if (ctrlc_abort) + return 1; + poller_call(); +#ifdef ARCH_HAS_CTRLC + ret = arch_ctrlc(); +#else if (tstc() && getchar() == 3) - return 1; - return 0; + ret = 1; +#endif + + if (ret) + ctrlc_abort = 1; + + return ret; } EXPORT_SYMBOL(ctrlc); -#endif /* ARCH_HAS_CTRC */ BAREBOX_MAGICVAR_NAMED(global_linux_bootargs_console, global.linux.bootargs.console, "console= argument for Linux from the stdout-path property in /chosen node"); |