]> git.pld-linux.org Git - projects/rc-scripts.git/blame - src/getkey.c
- removed minilogd and its calls (more pain than gain)
[projects/rc-scripts.git] / src / getkey.c
CommitLineData
00196ec7
AM
1/*\r
2 * Copyright (c) 1999-2003 Red Hat, Inc. All rights reserved.\r
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
27struct termios tp;\r
28\r
29void reset_term(int x) {\r
30 tcsetattr(0,TCSANOW,&tp);\r
31 exit(x);\r
32}\r
33\r
34int main(int argc, char **argv) {\r
35 char foo[2];\r
36 char list[100]; /* should be enough */\r
37 char *waitmessage = NULL;\r
38 char *waitprint, *waitsprint;\r
39 const char *fooptr;\r
40 int waitseconds=0;\r
41 int alarmlen=0;\r
42 int ignore_control=0;\r
43 int tp_if,tp_of,tp_lf;\r
44 int x, r;\r
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
49 /* { "message", 'm', POPT_ARG_STRING, &waitmessage, 0, "Message to print out while waiting for string", "NOTE: argument must have a \"%d\" in it so the number of seconds\nleft until getkey times out can be printed" },*/\r
50 { "message", 'm', POPT_ARG_STRING, &waitmessage, 0, "Message to print out while waiting for string\nNOTE: message must have a \"%d\" in it, to hold the number of seconds left to wait", NULL },\r
51 { "ignore-control-chars", 'i', POPT_ARG_NONE, &ignore_control, 0, "Ignore Control-C and Control-D", NULL },\r
52 POPT_AUTOHELP\r
53 POPT_TABLEEND\r
54 };\r
55\r
56 strcpy(list, "");\r
57 context = poptGetContext("getkey", argc, argv, options, \r
58 POPT_CONTEXT_POSIXMEHARDER);\r
59 poptSetOtherOptionHelp(context, "[keys]");\r
60\r
61 r = poptGetNextOpt(context);\r
62 if (r < -1) {\r
63 fprintf(stderr, "%s: %s\n", \r
64 poptBadOption(context, POPT_BADOPTION_NOALIAS),\r
65 poptStrerror(r));\r
66\r
67 return -1;\r
68 }\r
69 fooptr = poptGetArg(context);\r
70 if (fooptr != NULL) {\r
71 strncpy(list, fooptr, sizeof(list) - 1);\r
72 list[99] = '\0';\r
73 for (x=0;list[x];x++) list[x]=toupper(list[x]);\r
74 }\r
75 if (waitseconds) {\r
76 alarmlen = waitseconds;\r
77 }\r
78 foo[0]=foo[1]='\0';\r
79\r
80 signal(SIGTERM,reset_term);\r
81 alarm(alarmlen);\r
82 signal(SIGALRM,reset_term);\r
83\r
84 tcgetattr(0,&tp);\r
85 tp_if=tp.c_iflag;\r
86 tp_of=tp.c_oflag;\r
87 tp_lf=tp.c_lflag;\r
88 tp.c_iflag=0;\r
89 tp.c_oflag &= ~OPOST;\r
90 tp.c_lflag &= ~(ISIG | ICANON);\r
91 tcsetattr(0,TCSANOW,&tp);\r
92 tp.c_iflag=tp_if;\r
93 tp.c_oflag=tp_of;\r
94 tp.c_lflag=tp_lf;\r
95\r
96 ufds.events = POLLIN;\r
97 ufds.fd = 0;\r
98\r
99 if (waitseconds && waitmessage) {\r
100 waitprint = alloca (strlen(waitmessage)+15); /* long enough */\r
101 waitprint[0] = '\r';\r
102 waitsprint = waitprint + 1;\r
103 }\r
104\r
105 while (1) {\r
106 if (waitseconds && waitmessage) {\r
107 sprintf (waitsprint, waitmessage, waitseconds);\r
108 write (1, waitprint, strlen(waitprint));\r
109 }\r
110 r = poll(&ufds, 1, alarmlen ? 1000 : -1);\r
111 if (r == 0) {\r
112 /* we have waited a whole second with no keystroke... */\r
113 waitseconds--;\r
114 }\r
115 if (r > 0) {\r
116 read(0,foo,1);\r
117 foo[0]=toupper(foo[0]);\r
118 /* Die if we get a control-c or control-d */\r
119 if (ignore_control == 0) {\r
120 if (foo[0]==3 || foo[0]==4) reset_term(1);\r
121 }\r
122 /* Don't let a null character be interpreted as a match\r
123 by strstr */\r
124 if (foo[0] != 0) {\r
125 if (strcmp(list, "") == 0 || strstr(list,foo)) {\r
126 reset_term(0);\r
127 }\r
128 }\r
129 }\r
130 }\r
131}\r
This page took 0.192641 seconds and 4 git commands to generate.