* by Eric S. Raymond <esr@snark.thyrsus.com>, 1 Nov 88
* - fix for little-endian machines (version 1.1), 21 Oct 1996.
* - cleanup and modern packaging (version 1.2), 1 Aug 2002.
+ * - changed to use termios (version 1.3), 26 Aug 2002.
*/
#include <stdio.h>
-#include <termio.h>
+#include <termios.h>
#include <signal.h>
#define META 0x80
static int signalled;
-static void catcher(sig)
-int sig;
+static void catcher(const int sig)
{
signalled = sig;
}
main()
{
- struct termio cooked, raw;
+ struct termios cooked, raw;
unsigned char c;
unsigned int i, timeouts;
for (i = SIGHUP; i <= SIGPOLL; i++)
(void) signal(c, catcher);
- (void) ioctl(0, TCGETA, &cooked);
-
- (void) memcpy(&raw, &cooked, sizeof(struct termio));
+ // Get the state of the tty
+ (void) tcgetattr(0, &cooked);
+ // Make a copy we can mess with
+ (void) memcpy(&raw, &cooked, sizeof(struct termios));
+ // Turn off echoing, linebuffering, and special-character processing,
+ // but not the SIGINT or SIGQUIT keys.
raw.c_lflag &=~ (ICANON | ECHO);
- raw.c_cc[VEOL] = 1;
- raw.c_cc[VEOF] = 2;
- (void) ioctl(0, TCSETA, &raw);
+ // Ship the raw control blts
+ (void) tcsetattr(0, TCSANOW, &raw);
- (void) printf("Type any key to see the sequence it sends, DEL to quit.\n");
+ (void) printf("Type any key to see the sequence it sends, ^C to quit.\n");
signalled = 0;
- timeouts = 1;
while (!signalled)
{
int meta;
- if (read(0, &c, 1) != 0)
- timeouts = 0;
- else
- /*
- * This is supposed to cause the program to emit a newline .2 sec
- * after reading a key sequence, but it doesn't work, I don't know
- * why.
- */
- {
- if (timeouts++ == 0)
- (void) putchar('\n');
- continue;
- }
-
+ read(0, &c, 1);
if (meta = (c & META))
c &=~ META;
}
(void) printf("\nBye...\n");
- (void) ioctl(0, TCSETA, &cooked);
+ // Restore the cooked state
+ (void) tcsetattr(0, TCSANOW, &cooked);
}
/* showkey.c ends here */