2 * Copyright (c) 1999-2003, 2006 Red Hat, Inc. All rights reserved.
\r
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
\r
19 * A very simple keygrabber.
\r
28 #include <termios.h>
\r
30 #include <sys/poll.h>
\r
33 static struct termios orig_tp;
\r
35 static void reset_term(int x) {
\r
36 tcsetattr(0,TCSANOW,&orig_tp);
\r
40 int main(int argc, char **argv) {
\r
41 static const char default_list[] = "";
\r
44 char *waitmessage = NULL;
\r
45 char *waitprint, *waitsprint;
\r
48 int ignore_control=0;
\r
51 struct pollfd ufds; /* only one, no need for an array... */
\r
52 poptContext context;
\r
53 struct poptOption options[] = {
\r
54 { "wait", 'c', POPT_ARG_INT, &waitseconds, 0, "Number of seconds to wait for keypress", NULL },
\r
55 { "message", 'm', POPT_ARG_STRING, &waitmessage, 0, "Message to print out while waiting for string\nNOTE: The message may have a \"%d\" in it, to hold the number of seconds left to wait.", NULL },
\r
56 { "ignore-control-chars", 'i', POPT_ARG_NONE, &ignore_control, 0, "Ignore Control-C and Control-D", NULL },
\r
61 context = poptGetContext("getkey", argc, (const char **)argv, options,
\r
62 POPT_CONTEXT_POSIXMEHARDER);
\r
63 poptSetOtherOptionHelp(context, "[keys]");
\r
65 r = poptGetNextOpt(context);
\r
67 fprintf(stderr, "%s: %s\n",
\r
68 poptBadOption(context, POPT_BADOPTION_NOALIAS),
\r
73 list = poptGetArg(context);
\r
84 list = default_list;
\r
86 if (waitseconds < 0) {
\r
87 fprintf(stderr, "--wait: Invalid time %d seconds\n",
\r
91 alarmlen = waitseconds;
\r
96 signal(SIGTERM,reset_term);
\r
97 if (alarmlen != 0) {
\r
98 signal(SIGALRM,reset_term);
\r
103 tp.c_oflag &= ~OPOST;
\r
104 tp.c_lflag &= ~(ISIG | ICANON);
\r
105 tcsetattr(0,TCSANOW,&tp);
\r
107 ufds.events = POLLIN;
\r
111 waitprint = alloca (strlen(waitmessage)+15); /* long enough */
\r
112 waitprint[0] = '\r';
\r
113 waitsprint = waitprint + 1;
\r
118 sprintf (waitsprint, waitmessage, waitseconds);
\r
119 write (1, waitprint, strlen(waitprint));
\r
121 r = poll(&ufds, 1, alarmlen ? 1000 : -1);
\r
123 /* we have waited a whole second with no keystroke... */
\r
129 read(0, &ch, sizeof(ch));
\r
131 /* Die if we get a control-c or control-d */
\r
132 if (ignore_control == 0 && (ch == 3 || ch == 4))
\r
134 /* Don't let a null character be interpreted as a match
\r
137 && (strcmp(list, "") == 0 || strchr(list, ch) != NULL))
\r