]>
Commit | Line | Data |
---|---|---|
00196ec7 | 1 | /*\r |
c4931d65 | 2 | * Copyright (c) 1999-2003, 2006 Red Hat, Inc. All rights reserved.\r |
00196ec7 AM |
3 | *\r |
4 | * This software may be freely redistributed under the terms of the GNU\r | |
5 | * public license.\r | |
6 | *\r | |
7 | * You should have received a copy of the GNU General Public License\r | |
8 | * along with this program; if not, write to the Free Software\r | |
9 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r | |
10 | *\r | |
11 | * getkey\r | |
12 | *\r | |
13 | * A very simple keygrabber.\r | |
14 | *\r | |
15 | */\r | |
16 | #include <ctype.h>\r | |
17 | #include <errno.h>\r | |
18 | #include <signal.h>\r | |
19 | #include <stdlib.h>\r | |
20 | #include <stdio.h>\r | |
21 | #include <string.h>\r | |
22 | #include <termios.h>\r | |
23 | #include <unistd.h>\r | |
24 | #include <sys/poll.h>\r | |
25 | #include "popt.h"\r | |
26 | \r | |
c4931d65 | 27 | static struct termios orig_tp;\r |
00196ec7 | 28 | \r |
c4931d65 AM |
29 | static void reset_term(int x) {\r |
30 | tcsetattr(0,TCSANOW,&orig_tp);\r | |
31 | _exit(x);\r | |
00196ec7 AM |
32 | }\r |
33 | \r | |
34 | int main(int argc, char **argv) {\r | |
c4931d65 AM |
35 | static const char default_list[] = "";\r |
36 | \r | |
37 | const char *list;\r | |
00196ec7 AM |
38 | char *waitmessage = NULL;\r |
39 | char *waitprint, *waitsprint;\r | |
00196ec7 AM |
40 | int waitseconds=0;\r |
41 | int alarmlen=0;\r | |
42 | int ignore_control=0;\r | |
c4931d65 AM |
43 | struct termios tp;\r |
44 | int r;\r | |
00196ec7 AM |
45 | struct pollfd ufds; /* only one, no need for an array... */\r |
46 | poptContext context;\r | |
47 | struct poptOption options[] = {\r | |
48 | { "wait", 'c', POPT_ARG_INT, &waitseconds, 0, "Number of seconds to wait for keypress", NULL },\r | |
c4931d65 | 49 | { "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 |
00196ec7 AM |
50 | { "ignore-control-chars", 'i', POPT_ARG_NONE, &ignore_control, 0, "Ignore Control-C and Control-D", NULL },\r |
51 | POPT_AUTOHELP\r | |
52 | POPT_TABLEEND\r | |
53 | };\r | |
54 | \r | |
c4931d65 | 55 | context = poptGetContext("getkey", argc, (const char **)argv, options,\r |
00196ec7 AM |
56 | POPT_CONTEXT_POSIXMEHARDER);\r |
57 | poptSetOtherOptionHelp(context, "[keys]");\r | |
58 | \r | |
59 | r = poptGetNextOpt(context);\r | |
60 | if (r < -1) {\r | |
61 | fprintf(stderr, "%s: %s\n", \r | |
62 | poptBadOption(context, POPT_BADOPTION_NOALIAS),\r | |
63 | poptStrerror(r));\r | |
64 | \r | |
65 | return -1;\r | |
66 | }\r | |
c4931d65 AM |
67 | list = poptGetArg(context);\r |
68 | if (list != NULL) {\r | |
69 | char *p;\r | |
70 | \r | |
71 | p = strdup(list);\r | |
72 | list = p;\r | |
73 | while (*p != 0) {\r | |
74 | *p = toupper(*p);\r | |
75 | p++;\r | |
76 | }\r | |
77 | } else\r | |
78 | list = default_list;\r | |
00196ec7 | 79 | if (waitseconds) {\r |
c4931d65 AM |
80 | if (waitseconds < 0) {\r |
81 | fprintf(stderr, "--wait: Invalid time %d seconds\n",\r | |
82 | waitseconds);\r | |
83 | return -1;\r | |
84 | }\r | |
00196ec7 AM |
85 | alarmlen = waitseconds;\r |
86 | }\r | |
00196ec7 | 87 | \r |
c4931d65 AM |
88 | tcgetattr(0,&tp);\r |
89 | orig_tp = tp;\r | |
00196ec7 | 90 | signal(SIGTERM,reset_term);\r |
c4931d65 AM |
91 | if (alarmlen != 0) {\r |
92 | signal(SIGALRM,reset_term);\r | |
93 | alarm(alarmlen);\r | |
94 | }\r | |
00196ec7 | 95 | \r |
00196ec7 AM |
96 | tp.c_iflag=0;\r |
97 | tp.c_oflag &= ~OPOST;\r | |
98 | tp.c_lflag &= ~(ISIG | ICANON);\r | |
99 | tcsetattr(0,TCSANOW,&tp);\r | |
00196ec7 AM |
100 | \r |
101 | ufds.events = POLLIN;\r | |
102 | ufds.fd = 0;\r | |
103 | \r | |
c4931d65 | 104 | if (waitmessage) {\r |
00196ec7 AM |
105 | waitprint = alloca (strlen(waitmessage)+15); /* long enough */\r |
106 | waitprint[0] = '\r';\r | |
107 | waitsprint = waitprint + 1;\r | |
108 | }\r | |
109 | \r | |
110 | while (1) {\r | |
c4931d65 | 111 | if (waitmessage) {\r |
00196ec7 AM |
112 | sprintf (waitsprint, waitmessage, waitseconds);\r |
113 | write (1, waitprint, strlen(waitprint));\r | |
114 | }\r | |
115 | r = poll(&ufds, 1, alarmlen ? 1000 : -1);\r | |
116 | if (r == 0) {\r | |
117 | /* we have waited a whole second with no keystroke... */\r | |
118 | waitseconds--;\r | |
119 | }\r | |
120 | if (r > 0) {\r | |
c4931d65 AM |
121 | char ch;\r |
122 | \r | |
123 | read(0, &ch, sizeof(ch));\r | |
124 | ch = toupper(ch);\r | |
00196ec7 | 125 | /* Die if we get a control-c or control-d */\r |
c4931d65 AM |
126 | if (ignore_control == 0 && (ch == 3 || ch == 4))\r |
127 | reset_term(1);\r | |
00196ec7 | 128 | /* Don't let a null character be interpreted as a match\r |
c4931d65 AM |
129 | by strchr */\r |
130 | if (ch != 0\r | |
131 | && (strcmp(list, "") == 0 || strchr(list, ch) != NULL))\r | |
132 | reset_term(0);\r | |
00196ec7 AM |
133 | }\r |
134 | }\r | |
135 | }\r |