2005-09-20 21:26:39 +08:00
|
|
|
/*
|
|
|
|
A small utility to print the resulting key codes from pressing a
|
2012-11-18 18:23:22 +08:00
|
|
|
key. Servers the same function as hitting ^V in bash, but I prefer
|
|
|
|
the way key_reader works.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-18 18:23:22 +08:00
|
|
|
Type ^C to exit the program.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2006-05-03 00:29:50 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2005-09-25 19:32:13 +08:00
|
|
|
#include <locale.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <termcap.h>
|
|
|
|
|
2012-05-09 18:30:09 +08:00
|
|
|
#include "common.h"
|
2006-03-10 21:43:17 +08:00
|
|
|
#include "fallback.h"
|
|
|
|
|
2005-09-23 04:16:52 +08:00
|
|
|
#include "input_common.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
int writestr(char *str)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
write(1, str, strlen(str));
|
|
|
|
return 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
int main(int argc, char **argv)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
set_main_thread();
|
2012-03-08 03:35:22 +08:00
|
|
|
setup_fork_guards();
|
2012-11-19 08:30:30 +08:00
|
|
|
setlocale(LC_ALL, "");
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
if (argc == 2)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
static char term_buffer[2048];
|
|
|
|
char *termtype = getenv("TERM");
|
|
|
|
char *tbuff = new char[9999];
|
|
|
|
char *res;
|
|
|
|
|
|
|
|
tgetent(term_buffer, termtype);
|
|
|
|
res = tgetstr(argv[1], &tbuff);
|
|
|
|
if (res != 0)
|
|
|
|
{
|
|
|
|
while (*res != 0)
|
|
|
|
{
|
|
|
|
printf("%d ", *res);
|
|
|
|
|
|
|
|
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Undefined sequence\n");
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
char scratch[1024];
|
|
|
|
unsigned int c;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
struct termios modes, /* so we can change the modes */
|
|
|
|
savemodes; /* so we can reset the modes when we're done */
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
input_common_init(0);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
tcgetattr(0,&modes); /* get the current terminal modes */
|
|
|
|
savemodes = modes; /* save a copy so we can reset them */
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
modes.c_lflag &= ~ICANON; /* turn off canonical mode */
|
|
|
|
modes.c_lflag &= ~ECHO; /* turn off echo mode */
|
|
|
|
modes.c_cc[VMIN]=1;
|
|
|
|
modes.c_cc[VTIME]=0;
|
|
|
|
tcsetattr(0,TCSANOW,&modes); /* set the new modes */
|
|
|
|
while (1)
|
2012-11-18 18:23:22 +08:00
|
|
|
{
|
2012-11-19 08:30:30 +08:00
|
|
|
if ((c=input_common_readch(0)) == EOF)
|
|
|
|
break;
|
|
|
|
if ((c > 31) && (c != 127))
|
|
|
|
sprintf(scratch, "dec: %d hex: %x char: %c\n", c, c, c);
|
|
|
|
else
|
|
|
|
sprintf(scratch, "dec: %d hex: %x\n", c, c);
|
|
|
|
writestr(scratch);
|
|
|
|
}
|
|
|
|
/* reset the terminal to the saved mode */
|
|
|
|
tcsetattr(0,TCSANOW,&savemodes);
|
|
|
|
|
|
|
|
input_common_destroy();
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|