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