| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
io.c - description |
io.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 57 |
|
|
| 58 |
int igetch(int clear_buf) |
int igetch(int clear_buf) |
| 59 |
{ |
{ |
| 60 |
static char buf[256]; |
// static input buffer |
| 61 |
unsigned char c, tmp[256]; |
static unsigned char buf[256]; |
| 62 |
int out = KEY_NULL, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i = 0, j, result; |
static ssize_t len = 0; |
| 63 |
static int len = 0, pos = 0; |
static int pos = 0; |
| 64 |
|
|
| 65 |
fd_set testfds; |
fd_set testfds; |
| 66 |
struct timeval timeout; |
struct timeval timeout; |
| 67 |
|
|
| 68 |
|
unsigned char tmp[256]; |
| 69 |
|
int ret; |
| 70 |
|
int out = KEY_NULL; |
| 71 |
|
int in_esc = 0; |
| 72 |
|
int in_ascii = 0; |
| 73 |
|
int in_control = 0; |
| 74 |
|
int i = 0; |
| 75 |
|
|
| 76 |
if (clear_buf) |
if (clear_buf) |
| 77 |
{ |
{ |
| 78 |
pos = 0; |
pos = 0; |
| 81 |
return 0; |
return 0; |
| 82 |
} |
} |
| 83 |
|
|
| 84 |
// Stop on system exit |
while (!SYS_server_exit && pos >= len) |
|
if (SYS_exit) |
|
|
return KEY_NULL; |
|
|
|
|
|
if (pos >= len) |
|
| 85 |
{ |
{ |
|
pos = 0; |
|
|
len = 0; |
|
|
|
|
| 86 |
FD_ZERO(&testfds); |
FD_ZERO(&testfds); |
| 87 |
FD_SET(0, &testfds); |
FD_SET(STDIN_FILENO, &testfds); |
| 88 |
|
|
| 89 |
timeout.tv_sec = 1; |
timeout.tv_sec = 1; |
| 90 |
timeout.tv_usec = 0; |
timeout.tv_usec = 0; |
| 91 |
|
|
| 92 |
result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout); |
|
(fd_set *)NULL, &timeout); |
|
| 93 |
|
|
| 94 |
if (result == 0) |
if (ret < 0) |
| 95 |
{ |
{ |
| 96 |
return KEY_TIMEOUT; |
if (errno != EINTR) |
| 97 |
|
{ |
| 98 |
|
log_error("Select error in igetch: !\n", errno); |
| 99 |
|
return KEY_NULL; |
| 100 |
|
} |
| 101 |
|
continue; |
| 102 |
} |
} |
| 103 |
if (result < 0) |
else if (ret == 0) |
| 104 |
{ |
{ |
| 105 |
log_error("select() error (%d) !\n", result); |
return KEY_TIMEOUT; |
|
return KEY_NULL; |
|
| 106 |
} |
} |
| 107 |
if (result > 0) |
|
| 108 |
|
if (FD_ISSET(STDIN_FILENO, &testfds)) |
| 109 |
{ |
{ |
| 110 |
if (FD_ISSET(0, &testfds)) |
len = read(STDIN_FILENO, buf, 255); |
| 111 |
{ |
pos = 0; |
| 112 |
len = read(0, buf, 255); |
break; |
|
} |
|
| 113 |
} |
} |
| 114 |
|
|
| 115 |
// For debug |
// For debug |
| 119 |
|
|
| 120 |
while (pos < len) |
while (pos < len) |
| 121 |
{ |
{ |
| 122 |
c = buf[pos++]; |
unsigned char c = buf[pos++]; |
| 123 |
|
|
| 124 |
if (c == '\0') |
if (c == '\0') |
| 125 |
{ |
{ |
| 244 |
|
|
| 245 |
return ch; |
return ch; |
| 246 |
} |
} |
|
|
|
|
int ikbhit() |
|
|
{ |
|
|
int len; |
|
|
|
|
|
ioctl(0, FIONREAD, &len); |
|
|
|
|
|
return len; |
|
|
} |
|