/* getch - show what the curses routine getch() returns for keystrokes
**
** Just a little test program to see what comes out of the
** keyboard -> OS -> terminal -> curses pipeline.
**
** Type ^C to exit.
*/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <sysexits.h>

#define _XOPEN_SOURCE_EXTENDED 1
#include <curses.h>


int main( int argc, char** argv )
    {
    char* name[KEY_MAX+1];
    int i;
    int c;

    /* curses.h has defines for some keypad keys. */
    for ( i = 0; i < KEY_MAX; ++i )
	name[i] = (char*) 0;
    name[KEY_BREAK] = "KEY_BREAK";
    name[KEY_SRESET] = "KEY_SRESET";
    name[KEY_RESET] = "KEY_RESET";
    name[KEY_DOWN] = "KEY_DOWN";
    name[KEY_UP] = "KEY_UP";
    name[KEY_LEFT] = "KEY_LEFT";
    name[KEY_RIGHT] = "KEY_RIGHT";
    name[KEY_HOME] = "KEY_HOME";
    name[KEY_BACKSPACE] = "KEY_BACKSPACE";
    name[KEY_F(0)] = "KEY_F(0)";
    name[KEY_F(1)] = "KEY_F(1)";
    name[KEY_F(2)] = "KEY_F(2)";
    name[KEY_F(3)] = "KEY_F(3)";
    name[KEY_F(4)] = "KEY_F(4)";
    name[KEY_F(5)] = "KEY_F(5)";
    name[KEY_F(6)] = "KEY_F(6)";
    name[KEY_F(7)] = "KEY_F(7)";
    name[KEY_F(8)] = "KEY_F(8)";
    name[KEY_F(9)] = "KEY_F(9)";
    name[KEY_F(10)] = "KEY_F(10)";
    name[KEY_F(11)] = "KEY_F(11)";
    name[KEY_F(12)] = "KEY_F(12)";
    name[KEY_DL] = "KEY_DL";
    name[KEY_IL] = "KEY_IL";
    name[KEY_DC] = "KEY_DC";
    name[KEY_IC] = "KEY_IC";
    name[KEY_EIC] = "KEY_EIC";
    name[KEY_CLEAR] = "KEY_CLEAR";
    name[KEY_EOS] = "KEY_EOS";
    name[KEY_EOL] = "KEY_EOL";
    name[KEY_SF] = "KEY_SF";
    name[KEY_SR] = "KEY_SR";
    name[KEY_NPAGE] = "KEY_NPAGE";
    name[KEY_PPAGE] = "KEY_PPAGE";
    name[KEY_STAB] = "KEY_STAB";
    name[KEY_CTAB] = "KEY_CTAB";
    name[KEY_CATAB] = "KEY_CATAB";
    name[KEY_ENTER] = "KEY_ENTER";
    name[KEY_PRINT] = "KEY_PRINT";
    name[KEY_LL] = "KEY_LL";
    name[KEY_A1] = "KEY_A1";
    name[KEY_A3] = "KEY_A3";
    name[KEY_B2] = "KEY_B2";
    name[KEY_C1] = "KEY_C1";
    name[KEY_C3] = "KEY_C3";
    name[KEY_BTAB] = "KEY_BTAB";
    name[KEY_BEG] = "KEY_BEG";
    name[KEY_CANCEL] = "KEY_CANCEL";
    name[KEY_CLOSE] = "KEY_CLOSE";
    name[KEY_COMMAND] = "KEY_COMMAND";
    name[KEY_COPY] = "KEY_COPY";
    name[KEY_CREATE] = "KEY_CREATE";
    name[KEY_END] = "KEY_END";
    name[KEY_EXIT] = "KEY_EXIT";
    name[KEY_FIND] = "KEY_FIND";
    name[KEY_HELP] = "KEY_HELP";
    name[KEY_MARK] = "KEY_MARK";
    name[KEY_MESSAGE] = "KEY_MESSAGE";
    name[KEY_MOVE] = "KEY_MOVE";
    name[KEY_NEXT] = "KEY_NEXT";
    name[KEY_OPEN] = "KEY_OPEN";
    name[KEY_OPTIONS] = "KEY_OPTIONS";
    name[KEY_PREVIOUS] = "KEY_PREVIOUS";
    name[KEY_REDO] = "KEY_REDO";
    name[KEY_REFERENCE] = "KEY_REFERENCE";
    name[KEY_REFRESH] = "KEY_REFRESH";
    name[KEY_REPLACE] = "KEY_REPLACE";
    name[KEY_RESTART] = "KEY_RESTART";
    name[KEY_RESUME] = "KEY_RESUME";
    name[KEY_SAVE] = "KEY_SAVE";
    name[KEY_SBEG] = "KEY_SBEG";
    name[KEY_SCANCEL] = "KEY_SCANCEL";
    name[KEY_SCOMMAND] = "KEY_SCOMMAND";
    name[KEY_SCOPY] = "KEY_SCOPY";
    name[KEY_SCREATE] = "KEY_SCREATE";
    name[KEY_SDC] = "KEY_SDC";
    name[KEY_SDL] = "KEY_SDL";
    name[KEY_SELECT] = "KEY_SELECT";
    name[KEY_SEND] = "KEY_SEND";
    name[KEY_SEOL] = "KEY_SEOL";
    name[KEY_SEXIT] = "KEY_SEXIT";
    name[KEY_SFIND] = "KEY_SFIND";
    name[KEY_SHELP] = "KEY_SHELP";
    name[KEY_SHOME] = "KEY_SHOME";
    name[KEY_SIC] = "KEY_SIC";
    name[KEY_SLEFT] = "KEY_SLEFT";
    name[KEY_SMESSAGE] = "KEY_SMESSAGE";
    name[KEY_SMOVE] = "KEY_SMOVE";
    name[KEY_SNEXT] = "KEY_SNEXT";
    name[KEY_SOPTIONS] = "KEY_SOPTIONS";
    name[KEY_SPREVIOUS] = "KEY_SPREVIOUS";
    name[KEY_SPRINT] = "KEY_SPRINT";
    name[KEY_SREDO] = "KEY_SREDO";
    name[KEY_SREPLACE] = "KEY_SREPLACE";
    name[KEY_SRIGHT] = "KEY_SRIGHT";
    name[KEY_SRSUME] = "KEY_SRSUME";
    name[KEY_SSAVE] = "KEY_SSAVE";
    name[KEY_SSUSPEND] = "KEY_SSUSPEND";
    name[KEY_SUNDO] = "KEY_SUNDO";
    name[KEY_SUSPEND] = "KEY_SUSPEND";
    name[KEY_UNDO] = "KEY_UNDO";
    name[KEY_MOUSE] = "KEY_MOUSE";
    name[KEY_RESIZE] = "KEY_RESIZE";
    name[KEY_EVENT] = "KEY_EVENT";

    initscr();
    nonl();
    intrflush( stdscr, FALSE );
    raw();
    noecho();
#ifdef notdef
    keypad( stdscr, TRUE );
#endif /* notdef */

    for (;;)
	{
	c = getch();

	if ( name[c] != (char*) 0 )
	    printf( "%d %s\r\n", c, name[c] );
	else if ( c == '' )
	    printf( "%d '^?'\r\n", c );
	else if ( iscntrl( c ) )
	    printf( "%d '^%c'\r\n", c, c + '@' );
	else
	    printf( "%d '%c'\r\n", c, c );
	fflush( stdout );

	if ( c == '' )
	    break;
	}

    noraw();
    endwin();

    exit( EX_OK );
    }
