summaryrefslogtreecommitdiffstats
path: root/commands/timeout.c
diff options
context:
space:
mode:
authorMarc Kleine-Budde <mkl@pengutronix.de>2015-04-22 10:20:10 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2015-04-23 08:12:28 +0200
commit1aa3dbfc1a6048fe7f6af4f1af4fba7b83194f1f (patch)
tree093235e097fed31da77a856f91fee739b816dfd5 /commands/timeout.c
parent78caf2d171c97f7d385654af1778ef3e726173ae (diff)
downloadbarebox-1aa3dbfc1a6048fe7f6af4f1af4fba7b83194f1f.tar.gz
timeout: factor out wait-for-key-press loop into separate file
This patch factors out the wait-for-key-press loop from the shell command "timeout" into a sparate file, so that it can be used from C, too. Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands/timeout.c')
-rw-r--r--commands/timeout.c62
1 files changed, 13 insertions, 49 deletions
diff --git a/commands/timeout.c b/commands/timeout.c
index c8e930c..2b99d4f 100644
--- a/commands/timeout.c
+++ b/commands/timeout.c
@@ -16,40 +16,35 @@
* GNU General Public License for more details.
*
*/
-#include <common.h>
+
#include <command.h>
-#include <linux/stat.h>
#include <errno.h>
#include <getopt.h>
-#include <clock.h>
#include <environment.h>
+#include <console_countdown.h>
-#define TIMEOUT_RETURN (1 << 0)
-#define TIMEOUT_CTRLC (1 << 1)
-#define TIMEOUT_ANYKEY (1 << 2)
-#define TIMEOUT_SILENT (1 << 3)
+#include <linux/kernel.h>
static int do_timeout(int argc, char *argv[])
{
- int timeout = 3, ret = 1;
- int flags = 0, opt, countdown;
- int key = 0;
- uint64_t start, second;
+ int timeout, ret, opt;
+ unsigned flags = 0;
+ char str[2] = { };
const char *varname = NULL;
while((opt = getopt(argc, argv, "crsav:")) > 0) {
switch(opt) {
case 'r':
- flags |= TIMEOUT_RETURN;
+ flags |= CONSOLE_COUNTDOWN_RETURN;
break;
case 'c':
- flags |= TIMEOUT_CTRLC;
+ flags |= CONSOLE_COUNTDOWN_CTRLC;
break;
case 'a':
- flags |= TIMEOUT_ANYKEY;
+ flags |= CONSOLE_COUNTDOWN_ANYKEY;
break;
case 's':
- flags |= TIMEOUT_SILENT;
+ flags |= CONSOLE_COUNTDOWN_SILENT;
break;
case 'v':
varname = optarg;
@@ -63,43 +58,12 @@ static int do_timeout(int argc, char *argv[])
return COMMAND_ERROR_USAGE;
timeout = simple_strtoul(argv[optind], NULL, 0);
+ ret = console_countdown(timeout, flags, str);
- start = get_time_ns();
- second = start;
-
- countdown = timeout;
-
- if (!(flags & TIMEOUT_SILENT))
- printf("%2d", countdown--);
-
- do {
- if (tstc()) {
- key = getc();
- if (flags & TIMEOUT_CTRLC && key == 3)
- goto out;
- if (flags & TIMEOUT_ANYKEY)
- goto out;
- if (flags & TIMEOUT_RETURN && key == '\n')
- goto out;
- key = 0;
- }
- if (!(flags & TIMEOUT_SILENT) && is_timeout(second, SECOND)) {
- printf("\b\b%2d", countdown--);
- second += SECOND;
- }
- } while (!is_timeout(start, timeout * SECOND));
-
- ret = 0;
-out:
- if (varname && key) {
- char str[2] = { };
- str[0] = key;
+ if (varname && str[0])
setenv(varname, str);
- }
- if (!(flags & TIMEOUT_SILENT))
- printf("\n");
- return ret;
+ return ret ? 1 : 0;
}
BAREBOX_CMD_HELP_START(timeout)