summaryrefslogtreecommitdiffstats
path: root/common
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 /common
parent78caf2d171c97f7d385654af1778ef3e726173ae (diff)
downloadbarebox-1aa3dbfc1a6048fe7f6af4f1af4fba7b83194f1f.tar.gz
barebox-1aa3dbfc1a6048fe7f6af4f1af4fba7b83194f1f.tar.xz
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 'common')
-rw-r--r--common/Makefile1
-rw-r--r--common/console_countdown.c67
2 files changed, 68 insertions, 0 deletions
diff --git a/common/Makefile b/common/Makefile
index eca1e3533c..2738238c67 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -20,6 +20,7 @@ obj-$(CONFIG_CMD_MEMTEST) += memtest.o
obj-$(CONFIG_COMMAND_SUPPORT) += command.o
obj-$(CONFIG_CONSOLE_FULL) += console.o
obj-$(CONFIG_CONSOLE_SIMPLE) += console_simple.o
+obj-y += console_countdown.o
obj-$(CONFIG_DDR_SPD) += ddr_spd.o
obj-$(CONFIG_ENV_HANDLING) += environment.o
obj-$(CONFIG_ENVIRONMENT_VARIABLES) += env.o
diff --git a/common/console_countdown.c b/common/console_countdown.c
new file mode 100644
index 0000000000..ffbdb4fa2d
--- /dev/null
+++ b/common/console_countdown.c
@@ -0,0 +1,67 @@
+/*
+ * console_countdown - contdown on the console - interruptible by a keypress
+ *
+ * Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <clock.h>
+#include <command.h>
+#include <errno.h>
+#include <console_countdown.h>
+#include <stdio.h>
+
+int console_countdown(int timeout_s, unsigned flags, char *out_key)
+{
+ uint64_t start, second;
+ int countdown, ret = -EINTR;
+ int key = 0;
+
+ start = get_time_ns();
+ second = start;
+
+ countdown = timeout_s;
+
+ if (!(flags & CONSOLE_COUNTDOWN_SILENT))
+ printf("%2d", countdown--);
+
+ do {
+ if (tstc()) {
+ key = getc();
+ if (flags & CONSOLE_COUNTDOWN_ANYKEY)
+ goto out;
+ if (flags & CONSOLE_COUNTDOWN_RETURN && key == '\n')
+ goto out;
+ if (flags & CONSOLE_COUNTDOWN_CTRLC && key == 3)
+ goto out;
+ key = 0;
+ }
+ if (!(flags & CONSOLE_COUNTDOWN_SILENT) &&
+ is_timeout(second, SECOND)) {
+ printf("\b\b%2d", countdown--);
+ second += SECOND;
+ }
+ } while (!is_timeout(start, timeout_s * SECOND));
+
+ ret = 0;
+
+ out:
+ if (!(flags & CONSOLE_COUNTDOWN_SILENT))
+ printf("\n");
+ if (key && out_key)
+ *out_key = key;
+
+ return ret;
+}