diff options
author | Michael K. Johnson <johnsonm@redhat.com> | 2001-08-29 01:47:08 +0000 |
---|---|---|
committer | Michael K. Johnson <johnsonm@redhat.com> | 2001-08-29 01:47:08 +0000 |
commit | 94086d968b6910eea654cb5ed674ae36e87ecce7 (patch) | |
tree | 7696c1d9d0c4adadb55fad75c2e5038c1b30df75 /src/getkey.c | |
parent | ad64d6ea0c1c7b016a029986364edb129b49c196 (diff) | |
download | initscripts-94086d968b6910eea654cb5ed674ae36e87ecce7.tar initscripts-94086d968b6910eea654cb5ed674ae36e87ecce7.tar.gz initscripts-94086d968b6910eea654cb5ed674ae36e87ecce7.tar.bz2 initscripts-94086d968b6910eea654cb5ed674ae36e87ecce7.tar.xz initscripts-94086d968b6910eea654cb5ed674ae36e87ecce7.zip |
count down in C, not shellr6-30
Diffstat (limited to 'src/getkey.c')
-rw-r--r-- | src/getkey.c | 54 |
1 files changed, 47 insertions, 7 deletions
diff --git a/src/getkey.c b/src/getkey.c index 1305fff9..0aa60921 100644 --- a/src/getkey.c +++ b/src/getkey.c @@ -1,10 +1,13 @@ #include <ctype.h> +#include <errno.h> #include <signal.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> #include <termios.h> #include <unistd.h> +#include <sys/poll.h> /* A very simple keygrabber. */ @@ -18,23 +21,39 @@ void reset_term(int x) { int main(int argc, char **argv) { char foo[2]; char *list = NULL; + char *waitmessage = NULL; + char *waitprint, *waitsprint; + int waitseconds=0; int alarmlen=0; int tp_if,tp_of,tp_lf; - int x; + int x, r; + struct pollfd ufds; /* only one, no need for an array... */ - if (argc>1) { - if (argv[1][0]=='-' && isdigit(argv[1][1])) { - alarmlen = atoi(argv[1]+1); - list = argv[2]; - } else { - list = argv[1]; + for (++argv, --argc; argc; argc--, argv++) { + if (argv[0][0]=='-') { + if (argv[0][1]=='c') { + argc--; argv++; + waitseconds = atoi(argv[0]); + } else if (argv[0][1]=='m') { + argc--; argv++; + waitmessage=argv[0]; + } else if (isdigit(argv[0][1])) { + waitseconds = atoi(argv[0]); } + } else { + list = argv[0]; for (x=0;list[x];x++) list[x]=toupper(list[x]); + } + } + if (waitseconds) { + alarmlen = waitseconds; } foo[0]=foo[1]='\0'; + signal(SIGTERM,reset_term); alarm(alarmlen); signal(SIGALRM,reset_term); + tcgetattr(0,&tp); tp_if=tp.c_iflag; tp_of=tp.c_oflag; @@ -46,7 +65,27 @@ int main(int argc, char **argv) { tp.c_iflag=tp_if; tp.c_oflag=tp_of; tp.c_lflag=tp_lf; + + ufds.events = POLLIN; + ufds.fd = 0; + + if (waitseconds && waitmessage) { + waitprint = alloca (strlen(waitmessage)+15); /* long enough */ + waitprint[0] = '\r'; + waitsprint = waitprint + 1; + } + while (1) { + if (waitseconds) { + sprintf (waitsprint, waitmessage, waitseconds); + write (1, waitprint, strlen(waitprint)); + } + r = poll(&ufds, 1, alarmlen ? 1000 : -1); + if (r == 0) { + /* we have waited a whole second with no keystroke... */ + waitseconds--; + } + if (r > 0) { read(0,foo,1); foo[0]=toupper(foo[0]); /* Die if we get a control-c or control-d */ @@ -54,5 +93,6 @@ int main(int argc, char **argv) { if ((!list) || strstr(list,foo)) { reset_term(0); } + } } } |