| 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 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
#include "io.h" |
#include "io.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
#include "tcplib.h" |
#include <errno.h> |
| 21 |
#include <stdio.h> |
#include <stdio.h> |
| 22 |
#include <stdarg.h> |
#include <stdarg.h> |
| 23 |
#include <time.h> |
#include <time.h> |
| 24 |
#include <fcntl.h> |
#include <fcntl.h> |
| 25 |
#include <unistd.h> |
#include <unistd.h> |
| 26 |
|
#include <sys/select.h> |
| 27 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 28 |
|
|
| 29 |
int outc(char c) |
int outc(char c) |
| 59 |
int igetch(int clear_buf) |
int igetch(int clear_buf) |
| 60 |
{ |
{ |
| 61 |
// static input buffer |
// static input buffer |
| 62 |
static char buf[256]; |
static unsigned char buf[LINE_BUFFER_LEN]; |
| 63 |
static int len = 0; |
static ssize_t len = 0; |
| 64 |
static int pos = 0; |
static int pos = 0; |
| 65 |
|
|
| 66 |
unsigned char tmp[256]; |
fd_set testfds; |
| 67 |
int out = KEY_NULL; |
struct timeval timeout; |
| 68 |
|
|
| 69 |
|
unsigned char tmp[LINE_BUFFER_LEN]; |
| 70 |
|
int ret; |
| 71 |
|
int out = '\0'; |
| 72 |
int in_esc = 0; |
int in_esc = 0; |
| 73 |
int in_ascii = 0; |
int in_ascii = 0; |
| 74 |
int in_control = 0; |
int in_control = 0; |
| 75 |
int i = 0; |
int i = 0; |
| 76 |
|
int flags; |
| 77 |
|
|
| 78 |
if (clear_buf) |
if (clear_buf) |
| 79 |
{ |
{ |
| 80 |
pos = 0; |
pos = 0; |
| 81 |
len = 0; |
len = 0; |
| 82 |
|
|
| 83 |
return 0; |
return '\0'; |
| 84 |
} |
} |
| 85 |
|
|
| 86 |
// Stop on system exit |
while (!SYS_server_exit && pos >= len) |
|
if (SYS_exit) |
|
|
return KEY_NULL; |
|
|
|
|
|
if (pos >= len) |
|
| 87 |
{ |
{ |
|
fd_set testfds; |
|
|
struct timeval timeout; |
|
|
|
|
|
pos = 0; |
|
|
len = 0; |
|
|
|
|
| 88 |
FD_ZERO(&testfds); |
FD_ZERO(&testfds); |
| 89 |
FD_SET(0, &testfds); |
FD_SET(STDIN_FILENO, &testfds); |
| 90 |
|
|
| 91 |
timeout.tv_sec = 1; |
timeout.tv_sec = 0; |
| 92 |
timeout.tv_usec = 0; |
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 93 |
|
|
| 94 |
int result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
ret = select(STDIN_FILENO + 1, &testfds, NULL, NULL, &timeout); |
|
(fd_set *)NULL, &timeout); |
|
| 95 |
|
|
| 96 |
if (result == 0) |
if (ret < 0) |
| 97 |
{ |
{ |
| 98 |
return KEY_TIMEOUT; |
if (errno != EINTR) |
| 99 |
|
{ |
| 100 |
|
log_error("select() error (%d) !\n", errno); |
| 101 |
|
return KEY_NULL; |
| 102 |
|
} |
| 103 |
|
continue; |
| 104 |
} |
} |
| 105 |
if (result < 0) |
else if (ret == 0) |
| 106 |
{ |
{ |
| 107 |
log_error("select() error (%d) !\n", result); |
return KEY_TIMEOUT; |
|
return KEY_NULL; |
|
| 108 |
} |
} |
| 109 |
if (result > 0) |
|
| 110 |
|
if (FD_ISSET(STDIN_FILENO, &testfds)) |
| 111 |
{ |
{ |
| 112 |
if (FD_ISSET(0, &testfds)) |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 113 |
|
fcntl(socket_server, F_SETFL, flags | O_NONBLOCK); |
| 114 |
|
|
| 115 |
|
len = read(STDIN_FILENO, buf, sizeof(buf)); |
| 116 |
|
if (len < 0) |
| 117 |
|
{ |
| 118 |
|
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) |
| 119 |
|
{ |
| 120 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 121 |
|
} |
| 122 |
|
} |
| 123 |
|
else if (len == 0) |
| 124 |
{ |
{ |
| 125 |
len = read(0, buf, 255); |
out = KEY_NULL; // broken pipe |
| 126 |
} |
} |
| 127 |
|
|
| 128 |
|
pos = 0; |
| 129 |
|
|
| 130 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 131 |
|
|
| 132 |
|
break; |
| 133 |
} |
} |
| 134 |
|
|
| 135 |
// For debug |
// For debug |
| 141 |
{ |
{ |
| 142 |
unsigned char c = buf[pos++]; |
unsigned char c = buf[pos++]; |
| 143 |
|
|
|
if (c == '\0') |
|
|
{ |
|
|
out = c; |
|
|
break; |
|
|
} |
|
|
|
|
| 144 |
if (c == KEY_CONTROL) |
if (c == KEY_CONTROL) |
| 145 |
{ |
{ |
| 146 |
if (in_control == 0) |
if (in_control == 0) |
| 254 |
do |
do |
| 255 |
{ |
{ |
| 256 |
ch = igetch(0); |
ch = igetch(0); |
| 257 |
} while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec)); |
} while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); |
| 258 |
|
|
| 259 |
return ch; |
return ch; |
| 260 |
} |
} |
|
|
|
|
int ikbhit() |
|
|
{ |
|
|
int len; |
|
|
|
|
|
ioctl(0, FIONREAD, &len); |
|
|
|
|
|
return len; |
|
|
} |
|