From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH] timeout: allow to set a variable with the key pressed
Date: Sun, 13 May 2012 14:07:06 +0200 [thread overview]
Message-ID: <1336910826-27453-1-git-send-email-s.hauer@pengutronix.de> (raw)
Useful if we want to know which key caused the timeout.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
commands/timeout.c | 16 ++++++++++++++--
1 file changed, 14 insertions(+), 2 deletions(-)
diff --git a/commands/timeout.c b/commands/timeout.c
index 01ece52..d4e90cd 100644
--- a/commands/timeout.c
+++ b/commands/timeout.c
@@ -25,6 +25,7 @@
#include <errno.h>
#include <getopt.h>
#include <clock.h>
+#include <environment.h>
#define TIMEOUT_RETURN (1 << 0)
#define TIMEOUT_CTRLC (1 << 1)
@@ -35,9 +36,11 @@ 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;
+ const char *varname = NULL;
- while((opt = getopt(argc, argv, "t:crsa")) > 0) {
+ while((opt = getopt(argc, argv, "t:crsav:")) > 0) {
switch(opt) {
case 'r':
flags |= TIMEOUT_RETURN;
@@ -51,6 +54,9 @@ static int do_timeout(int argc, char *argv[])
case 's':
flags |= TIMEOUT_SILENT;
break;
+ case 'v':
+ varname = optarg;
+ break;
default:
return 1;
}
@@ -71,13 +77,14 @@ static int do_timeout(int argc, char *argv[])
do {
if (tstc()) {
- int key = getc();
+ 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--);
@@ -87,6 +94,11 @@ static int do_timeout(int argc, char *argv[])
ret = 0;
out:
+ if (varname && key) {
+ char str[2] = { };
+ str[0] = key;
+ setenv(varname, str);
+ }
if (!(flags & TIMEOUT_SILENT))
printf("\n");
--
1.7.10
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
reply other threads:[~2012-05-13 12:07 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1336910826-27453-1-git-send-email-s.hauer@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox