00001
00011 #include <unistd.h>
00012 #include <termios.h>
00013 #include <fcntl.h>
00014
00015 static int initialized = 0;
00016
00017 static struct termios termattr, save_termattr;
00018
00026 static int set_tty_raw( void ) {
00027 int i = tcgetattr( 0, &termattr );
00028 if ( i < 0 ) {
00029 printf( "%m\n" );
00030 fflush( stdout );
00031 return -1;
00032 }
00033 save_termattr = termattr;
00034
00035 termattr.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
00036 termattr.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
00037 termattr.c_cflag &= ~(CSIZE | PARENB);
00038 termattr.c_cflag |= CS8 | OCRNL ;
00039 termattr.c_oflag &= ~(OPOST | ONOCR);
00040
00041 termattr.c_cc[VMIN] = 1;
00042 termattr.c_cc[VTIME] = 0;
00043
00044 i = tcsetattr( 0, TCSANOW, &termattr );
00045 if ( i < 0 ) {
00046 printf( "%m\n" );
00047 fflush( stdout );
00048 }
00049
00050 return 0;
00051 }
00052
00056 static void bye_bye( void ) {
00057 if ( initialized )
00058 tcsetattr( 0, TCSAFLUSH, &save_termattr );
00059 }
00060
00068 static int peekch( void ) {
00069 if ( !initialized ) {
00070 initialized = 1;
00071 set_tty_raw( );
00072 atexit( bye_bye );
00073 }
00074 termattr.c_cc[VMIN] = 0;
00075 int i = tcsetattr( 0, TCSANOW, &termattr );
00076 unsigned char ch;
00077 ssize_t size = read( 0, &ch, 1 );
00078 termattr.c_cc[VMIN] = 1;
00079 i = tcsetattr( 0, TCSANOW, &termattr );
00080 if (size == 0)
00081 return -1;
00082 return ch;
00083 }
00084
00090 static inline void delay( int msec ) {
00091 usleep( msec*1000 );
00092 }
00093
00097 static void help( int port ) {
00098 printf( "Point your browser to: http://localhost:%d\n", port );
00099 printf( "x : eXit aplication\n" );
00100 printf( "i : dump Information\n" );
00101 printf( "v : toggle VERBOSE mode\n" );
00102 printf( "d : toggle DEBUG mode\n" );
00103 }
00104
00113 static int peek_key( mihl_ctx_t *ctx ) {
00114 unsigned vlog = mihl_get_log_level( ctx );
00115 delay ( 1 );
00116 int key = peekch( );
00117 switch ( key ) {
00118 case 'x' :
00119 case 'X' :
00120 return 1;
00121 break;
00122 case 'i' :
00123 case 'I' :
00124 mihl_dump_info( ctx );
00125 mihl_dump_info_handlers( ctx );
00126 break;
00127 case 'v' :
00128 case 'V' :
00129 vlog ^= MIHL_LOG_INFO_VERBOSE;
00130 printf( "verbose:%s\015\012", (vlog & MIHL_LOG_INFO_VERBOSE) ? "ON" : "OFF" );
00131 mihl_set_log_level( ctx, vlog );
00132 break;
00133 case 'd' :
00134 case 'D' :
00135 vlog ^= MIHL_LOG_DEBUG;
00136 printf( "debug:%s\015\012", (vlog & MIHL_LOG_DEBUG) ? "ON" : "OFF" );
00137 mihl_set_log_level( ctx, vlog );
00138 break;
00139 }
00140 return 0;
00141 }