2005-10-24 23:26:25 +08:00
|
|
|
/** \file input_common.c
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
Implementation file for the low level input library
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include "config.h"
|
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2006-01-19 20:23:22 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2006-08-11 03:02:46 +08:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2006-01-19 20:22:07 +08:00
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
#include "fallback.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "wutil.h"
|
|
|
|
#include "input_common.h"
|
|
|
|
#include "env_universal.h"
|
2011-12-27 13:05:25 +08:00
|
|
|
#include "iothread.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Time in milliseconds to wait for another byte to be available for
|
2008-01-14 00:47:47 +08:00
|
|
|
reading after \\x1b is read before assuming that escape key was
|
2005-09-20 21:26:39 +08:00
|
|
|
pressed, and not an escape sequence.
|
|
|
|
*/
|
|
|
|
#define WAIT_ON_ESCAPE 10
|
|
|
|
|
|
|
|
/**
|
|
|
|
Characters that have been read and returned by the sequence matching code
|
|
|
|
*/
|
2006-08-13 09:38:03 +08:00
|
|
|
static wint_t lookahead_arr[1024];
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Number of entries in lookahead_arr
|
|
|
|
*/
|
|
|
|
static int lookahead_count = 0;
|
|
|
|
|
2012-03-25 18:00:38 +08:00
|
|
|
/** Callback function for handling interrupts on reading */
|
2005-09-20 21:26:39 +08:00
|
|
|
static int (*interrupt_handler)();
|
|
|
|
|
2012-03-25 18:00:38 +08:00
|
|
|
/** Callback function to be invoked before reading each byte */
|
|
|
|
static void (*poll_handler)();
|
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
void input_common_init( int (*ih)() )
|
|
|
|
{
|
|
|
|
interrupt_handler = ih;
|
|
|
|
}
|
|
|
|
|
2012-03-25 18:00:38 +08:00
|
|
|
void input_common_set_poll_callback(void (*handler)(void))
|
|
|
|
{
|
|
|
|
poll_handler = handler;
|
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
void input_common_destroy()
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Internal function used by input_common_readch to read one byte from fd 1. This function should only be called by
|
|
|
|
input_common_readch().
|
|
|
|
*/
|
|
|
|
static wint_t readb()
|
|
|
|
{
|
2012-03-26 16:21:10 +08:00
|
|
|
/* do_loop must be set on every path through the loop; leaving it uninitialized allows the static analyzer to assist in catching mistakes. */
|
2005-09-26 06:39:48 +08:00
|
|
|
unsigned char arr[1];
|
2012-03-26 16:21:10 +08:00
|
|
|
bool do_loop;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
do
|
|
|
|
{
|
2012-03-25 18:00:38 +08:00
|
|
|
/* Invoke any poll handler */
|
|
|
|
if (poll_handler)
|
|
|
|
poll_handler();
|
|
|
|
|
2011-12-27 13:05:25 +08:00
|
|
|
fd_set fdset;
|
|
|
|
int fd_max=0;
|
|
|
|
int ioport = iothread_port();
|
2005-09-20 21:26:39 +08:00
|
|
|
int res;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2011-12-27 13:05:25 +08:00
|
|
|
FD_ZERO( &fdset );
|
|
|
|
FD_SET( 0, &fdset );
|
2005-09-20 21:26:39 +08:00
|
|
|
if( env_universal_server.fd > 0 )
|
|
|
|
{
|
2011-12-27 13:05:25 +08:00
|
|
|
FD_SET( env_universal_server.fd, &fdset );
|
|
|
|
if (fd_max < env_universal_server.fd) fd_max = env_universal_server.fd;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 13:05:25 +08:00
|
|
|
if (ioport > 0) {
|
|
|
|
FD_SET( ioport, &fdset );
|
|
|
|
if (fd_max < ioport) fd_max = ioport;
|
|
|
|
}
|
2012-03-26 16:21:10 +08:00
|
|
|
|
2011-12-27 13:05:25 +08:00
|
|
|
res = select( fd_max + 1, &fdset, 0, 0, 0 );
|
2005-09-20 21:26:39 +08:00
|
|
|
if( res==-1 )
|
|
|
|
{
|
|
|
|
switch( errno )
|
|
|
|
{
|
|
|
|
case EINTR:
|
|
|
|
case EAGAIN:
|
|
|
|
{
|
|
|
|
if( interrupt_handler )
|
|
|
|
{
|
|
|
|
int res = interrupt_handler();
|
|
|
|
if( res )
|
2006-05-14 18:16:23 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
return res;
|
2006-05-14 18:16:23 +08:00
|
|
|
}
|
2006-11-19 05:15:31 +08:00
|
|
|
if( lookahead_count )
|
|
|
|
{
|
|
|
|
return lookahead_arr[--lookahead_count];
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
|
2012-03-25 18:00:38 +08:00
|
|
|
do_loop = true;
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2006-05-14 18:16:23 +08:00
|
|
|
/*
|
2006-11-19 05:15:31 +08:00
|
|
|
The terminal has been closed. Save and exit.
|
2006-05-14 18:16:23 +08:00
|
|
|
*/
|
|
|
|
return R_EOF;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-26 16:21:10 +08:00
|
|
|
/* Assume we loop unless we see a character in stdin */
|
|
|
|
do_loop = true;
|
|
|
|
|
|
|
|
if( env_universal_server.fd > 0 && FD_ISSET( env_universal_server.fd, &fdset ) )
|
|
|
|
{
|
|
|
|
debug( 3, L"Wake up on universal variable event" );
|
|
|
|
env_universal_read_all();
|
|
|
|
if( lookahead_count )
|
|
|
|
{
|
|
|
|
return lookahead_arr[--lookahead_count];
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 13:05:25 +08:00
|
|
|
|
2012-03-26 16:21:10 +08:00
|
|
|
if ( ioport > 0 && FD_ISSET(ioport, &fdset))
|
2011-12-27 13:05:25 +08:00
|
|
|
{
|
2012-03-26 16:21:10 +08:00
|
|
|
iothread_service_completion();
|
|
|
|
if( lookahead_count )
|
|
|
|
{
|
|
|
|
return lookahead_arr[--lookahead_count];
|
|
|
|
}
|
2011-12-27 13:05:25 +08:00
|
|
|
}
|
|
|
|
|
2012-03-26 16:21:10 +08:00
|
|
|
if( FD_ISSET( STDIN_FILENO, &fdset ) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-05-14 18:16:23 +08:00
|
|
|
if( read_blocked( 0, arr, 1 ) != 1 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-03-26 16:21:10 +08:00
|
|
|
/* The teminal has been closed. Save and exit. */
|
2006-05-14 18:16:23 +08:00
|
|
|
return R_EOF;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2012-03-26 16:21:10 +08:00
|
|
|
|
|
|
|
/* We read from stdin, so don't loop */
|
2012-03-25 18:00:38 +08:00
|
|
|
do_loop = false;
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while( do_loop );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
return arr[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t input_common_readch( int timed )
|
|
|
|
{
|
|
|
|
if( lookahead_count == 0 )
|
|
|
|
{
|
|
|
|
if( timed )
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
fd_set fds;
|
|
|
|
struct timeval tm=
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
1000 * WAIT_ON_ESCAPE
|
|
|
|
}
|
|
|
|
;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
FD_ZERO( &fds );
|
|
|
|
FD_SET( 0, &fds );
|
|
|
|
count = select(1, &fds, 0, 0, &tm);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
switch( count )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return WEOF;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case -1:
|
|
|
|
return WEOF;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
wchar_t res;
|
|
|
|
static mbstate_t state;
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
wint_t b = readb();
|
2005-09-25 03:55:58 +08:00
|
|
|
char bb;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
int sz;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-10-05 07:33:12 +08:00
|
|
|
if( (b >= R_NULL) && (b < R_NULL + 1000) )
|
2006-05-14 18:16:23 +08:00
|
|
|
return b;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2005-09-25 03:55:58 +08:00
|
|
|
bb=b;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-25 03:55:58 +08:00
|
|
|
sz = mbrtowc( &res, &bb, 1, &state );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
switch( sz )
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
memset (&state, '\0', sizeof (state));
|
2011-12-27 11:18:46 +08:00
|
|
|
debug( 2, L"Illegal input" );
|
|
|
|
return R_NULL;
|
2005-09-20 21:26:39 +08:00
|
|
|
case -2:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
return 0;
|
|
|
|
default:
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
else
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
if( !timed )
|
|
|
|
{
|
|
|
|
while( (lookahead_count >= 0) && (lookahead_arr[lookahead_count-1] == WEOF) )
|
|
|
|
lookahead_count--;
|
|
|
|
if( lookahead_count == 0 )
|
|
|
|
return input_common_readch(0);
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
return lookahead_arr[--lookahead_count];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void input_common_unreadch( wint_t ch )
|
|
|
|
{
|
|
|
|
lookahead_arr[lookahead_count++] = ch;
|
|
|
|
}
|
|
|
|
|