| 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 <string.h> |
| 24 |
#include <time.h> |
#include <time.h> |
| 25 |
#include <fcntl.h> |
#include <fcntl.h> |
| 26 |
#include <unistd.h> |
#include <unistd.h> |
| 27 |
|
#include <sys/select.h> |
| 28 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 29 |
|
#include <sys/epoll.h> |
| 30 |
|
|
| 31 |
int outc(char c) |
static char stdout_buf[BUFSIZ]; |
| 32 |
{ |
static int stdout_buf_len = 0; |
| 33 |
int retval; |
static int stdout_buf_offset = 0; |
|
|
|
|
retval = fprintf(stdout, "%c", c); |
|
|
|
|
|
return retval; |
|
|
} |
|
| 34 |
|
|
| 35 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 36 |
{ |
{ |
| 37 |
|
char buf[BUFSIZ]; |
| 38 |
va_list args; |
va_list args; |
| 39 |
int retval; |
int ret; |
| 40 |
|
|
| 41 |
va_start(args, format); |
va_start(args, format); |
| 42 |
retval = vfprintf(stdout, format, args); |
ret = vsnprintf(buf, sizeof(buf), format, args); |
| 43 |
va_end(args); |
va_end(args); |
| 44 |
|
|
| 45 |
return retval; |
if (ret > 0) |
| 46 |
|
{ |
| 47 |
|
if (stdout_buf_len + ret > BUFSIZ) |
| 48 |
|
{ |
| 49 |
|
iflush(); |
| 50 |
|
} |
| 51 |
|
|
| 52 |
|
if (stdout_buf_len + ret <= BUFSIZ) |
| 53 |
|
{ |
| 54 |
|
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
| 55 |
|
stdout_buf_len += ret; |
| 56 |
|
} |
| 57 |
|
else |
| 58 |
|
{ |
| 59 |
|
errno = EAGAIN; |
| 60 |
|
ret = (BUFSIZ - stdout_buf_len - ret); |
| 61 |
|
} |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
return ret; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
int outc(char c) |
| 68 |
|
{ |
| 69 |
|
int ret; |
| 70 |
|
|
| 71 |
|
if (stdout_buf_len + 1 > BUFSIZ) |
| 72 |
|
{ |
| 73 |
|
iflush(); |
| 74 |
|
} |
| 75 |
|
|
| 76 |
|
if (stdout_buf_len + 1 <= BUFSIZ) |
| 77 |
|
{ |
| 78 |
|
stdout_buf[stdout_buf_len] = c; |
| 79 |
|
stdout_buf_len++; |
| 80 |
|
} |
| 81 |
|
else |
| 82 |
|
{ |
| 83 |
|
errno = EAGAIN; |
| 84 |
|
ret = -1; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
return ret; |
| 88 |
} |
} |
| 89 |
|
|
| 90 |
int iflush() |
int iflush() |
| 91 |
{ |
{ |
| 92 |
int retval; |
int flags; |
| 93 |
|
struct epoll_event ev, events[MAX_EVENTS]; |
| 94 |
|
int nfds, epollfd; |
| 95 |
|
int retry; |
| 96 |
|
int ret = 0; |
| 97 |
|
|
| 98 |
|
epollfd = epoll_create1(0); |
| 99 |
|
if (epollfd < 0) |
| 100 |
|
{ |
| 101 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 102 |
|
return -1; |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
ev.events = EPOLLOUT; |
| 106 |
|
ev.data.fd = STDOUT_FILENO; |
| 107 |
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 108 |
|
{ |
| 109 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 110 |
|
if (close(epollfd) < 0) |
| 111 |
|
{ |
| 112 |
|
log_error("close(epoll) error (%d)\n"); |
| 113 |
|
} |
| 114 |
|
return -1; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
// Set STDOUT as non-blocking |
| 118 |
|
flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 119 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 120 |
|
|
| 121 |
|
// Retry wait / flush for at most 3 times |
| 122 |
|
retry = 3; |
| 123 |
|
while (retry > 0 && !SYS_server_exit) |
| 124 |
|
{ |
| 125 |
|
retry--; |
| 126 |
|
|
| 127 |
|
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 128 |
|
|
| 129 |
retval = fflush(stdout); |
if (nfds < 0) |
| 130 |
|
{ |
| 131 |
|
if (errno != EINTR) |
| 132 |
|
{ |
| 133 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 134 |
|
break; |
| 135 |
|
} |
| 136 |
|
continue; |
| 137 |
|
} |
| 138 |
|
else if (nfds == 0) // timeout |
| 139 |
|
{ |
| 140 |
|
continue; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
return retval; |
for (int i = 0; i < nfds; i++) |
| 144 |
|
{ |
| 145 |
|
if (events[i].data.fd == STDOUT_FILENO) |
| 146 |
|
{ |
| 147 |
|
while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error |
| 148 |
|
{ |
| 149 |
|
ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); |
| 150 |
|
if (ret < 0) |
| 151 |
|
{ |
| 152 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 153 |
|
{ |
| 154 |
|
break; |
| 155 |
|
} |
| 156 |
|
else if (errno == EINTR) |
| 157 |
|
{ |
| 158 |
|
continue; |
| 159 |
|
} |
| 160 |
|
else |
| 161 |
|
{ |
| 162 |
|
log_error("write(STDOUT) error (%d)\n", errno); |
| 163 |
|
retry = 0; |
| 164 |
|
break; |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
|
else if (ret == 0) // broken pipe |
| 168 |
|
{ |
| 169 |
|
retry = 0; |
| 170 |
|
break; |
| 171 |
|
} |
| 172 |
|
else |
| 173 |
|
{ |
| 174 |
|
stdout_buf_offset += ret; |
| 175 |
|
if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely |
| 176 |
|
{ |
| 177 |
|
ret = 0; |
| 178 |
|
stdout_buf_offset = 0; |
| 179 |
|
stdout_buf_len = 0; |
| 180 |
|
retry = 0; |
| 181 |
|
break; |
| 182 |
|
} |
| 183 |
|
continue; |
| 184 |
|
} |
| 185 |
|
} |
| 186 |
|
} |
| 187 |
|
} |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
// Restore STDOUT flags |
| 191 |
|
fcntl(STDOUT_FILENO, F_SETFL, flags); |
| 192 |
|
|
| 193 |
|
if (close(epollfd) < 0) |
| 194 |
|
{ |
| 195 |
|
log_error("close(epoll) error (%d)\n"); |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
return ret; |
| 199 |
} |
} |
| 200 |
|
|
| 201 |
int igetch(int clear_buf) |
int igetch(int timeout) |
| 202 |
{ |
{ |
| 203 |
// static input buffer |
// static input buffer |
| 204 |
static char buf[256]; |
static unsigned char buf[LINE_BUFFER_LEN]; |
| 205 |
static int len = 0; |
static int len = 0; |
| 206 |
static int pos = 0; |
static int pos = 0; |
| 207 |
|
|
| 208 |
unsigned char tmp[256]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 209 |
|
int nfds, epollfd; |
| 210 |
|
int ret; |
| 211 |
|
int loop; |
| 212 |
|
|
| 213 |
|
unsigned char tmp[LINE_BUFFER_LEN]; |
| 214 |
int out = KEY_NULL; |
int out = KEY_NULL; |
| 215 |
int in_esc = 0; |
int in_esc = 0; |
| 216 |
int in_ascii = 0; |
int in_ascii = 0; |
| 217 |
int in_control = 0; |
int in_control = 0; |
| 218 |
int i = 0; |
int i = 0; |
| 219 |
|
int flags; |
| 220 |
|
|
| 221 |
if (clear_buf) |
epollfd = epoll_create1(0); |
| 222 |
|
if (epollfd < 0) |
| 223 |
{ |
{ |
| 224 |
pos = 0; |
log_error("epoll_create1() error (%d)\n", errno); |
| 225 |
len = 0; |
return -1; |
|
|
|
|
return 0; |
|
| 226 |
} |
} |
| 227 |
|
|
| 228 |
// Stop on system exit |
ev.events = EPOLLIN; |
| 229 |
if (SYS_exit) |
ev.data.fd = STDIN_FILENO; |
| 230 |
return KEY_NULL; |
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
|
|
|
|
if (pos >= len) |
|
| 231 |
{ |
{ |
| 232 |
fd_set testfds; |
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
|
struct timeval timeout; |
|
| 233 |
|
|
| 234 |
pos = 0; |
if (close(epollfd) < 0) |
| 235 |
len = 0; |
{ |
| 236 |
|
log_error("close(epoll) error (%d)\n"); |
| 237 |
|
} |
| 238 |
|
return -1; |
| 239 |
|
} |
| 240 |
|
|
| 241 |
FD_ZERO(&testfds); |
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 242 |
FD_SET(0, &testfds); |
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
| 243 |
|
|
| 244 |
timeout.tv_sec = 1; |
loop = 1; |
| 245 |
timeout.tv_usec = 0; |
|
| 246 |
|
while (loop && pos >= len && !SYS_server_exit) |
| 247 |
|
{ |
| 248 |
|
len = 0; |
| 249 |
|
pos = 0; |
| 250 |
|
|
| 251 |
int result = SignalSafeSelect(FD_SETSIZE, &testfds, (fd_set *)NULL, |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
|
(fd_set *)NULL, &timeout); |
|
| 252 |
|
|
| 253 |
if (result == 0) |
if (nfds < 0) |
| 254 |
{ |
{ |
| 255 |
return KEY_TIMEOUT; |
if (errno != EINTR) |
| 256 |
|
{ |
| 257 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 258 |
|
break; |
| 259 |
|
} |
| 260 |
|
continue; |
| 261 |
} |
} |
| 262 |
if (result < 0) |
else if (nfds == 0) // timeout |
| 263 |
{ |
{ |
| 264 |
log_error("select() error (%d) !\n", result); |
out = KEY_TIMEOUT; |
| 265 |
return KEY_NULL; |
break; |
| 266 |
} |
} |
| 267 |
if (result > 0) |
|
| 268 |
|
for (int i = 0; i < nfds; i++) |
| 269 |
{ |
{ |
| 270 |
if (FD_ISSET(0, &testfds)) |
if (events[i].data.fd == STDIN_FILENO) |
| 271 |
{ |
{ |
| 272 |
len = read(0, buf, 255); |
while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error |
| 273 |
|
{ |
| 274 |
|
ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); |
| 275 |
|
if (ret < 0) |
| 276 |
|
{ |
| 277 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 278 |
|
{ |
| 279 |
|
out = 0; |
| 280 |
|
loop = 0; |
| 281 |
|
break; |
| 282 |
|
} |
| 283 |
|
else if (errno == EINTR) |
| 284 |
|
{ |
| 285 |
|
continue; |
| 286 |
|
} |
| 287 |
|
else |
| 288 |
|
{ |
| 289 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 290 |
|
loop = 0; |
| 291 |
|
break; |
| 292 |
|
} |
| 293 |
|
} |
| 294 |
|
else if (ret == 0) // broken pipe |
| 295 |
|
{ |
| 296 |
|
loop = 0; |
| 297 |
|
break; |
| 298 |
|
} |
| 299 |
|
else |
| 300 |
|
{ |
| 301 |
|
len += ret; |
| 302 |
|
continue; |
| 303 |
|
} |
| 304 |
|
} |
| 305 |
} |
} |
| 306 |
} |
} |
| 307 |
|
|
| 310 |
// log_std ("<--[%u]\n", (buf[j] + 256) % 256); |
// log_std ("<--[%u]\n", (buf[j] + 256) % 256); |
| 311 |
} |
} |
| 312 |
|
|
| 313 |
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
| 314 |
|
|
| 315 |
while (pos < len) |
while (pos < len) |
| 316 |
{ |
{ |
| 317 |
unsigned char c = buf[pos++]; |
unsigned char c = buf[pos++]; |
| 318 |
|
|
|
if (c == '\0') |
|
|
{ |
|
|
out = c; |
|
|
break; |
|
|
} |
|
|
|
|
| 319 |
if (c == KEY_CONTROL) |
if (c == KEY_CONTROL) |
| 320 |
{ |
{ |
| 321 |
if (in_control == 0) |
if (in_control == 0) |
| 418 |
// for debug |
// for debug |
| 419 |
// log_std ("-->[%u]\n", out); |
// log_std ("-->[%u]\n", out); |
| 420 |
|
|
| 421 |
|
if (close(epollfd) < 0) |
| 422 |
|
{ |
| 423 |
|
log_error("close(epoll) error (%d)\n"); |
| 424 |
|
} |
| 425 |
|
|
| 426 |
return out; |
return out; |
| 427 |
} |
} |
| 428 |
|
|
| 429 |
int igetch_t(long int sec) |
int igetch_t(int sec) |
| 430 |
{ |
{ |
| 431 |
int ch; |
int ch; |
| 432 |
time_t t_begin = time(0); |
time_t t_begin = time(0); |
| 433 |
|
|
| 434 |
do |
do |
| 435 |
{ |
{ |
| 436 |
ch = igetch(0); |
ch = igetch(100); |
| 437 |
} while ((ch == KEY_TIMEOUT || ch == 0xa) && (time(0) - t_begin < sec)); |
} while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); |
| 438 |
|
|
| 439 |
return ch; |
return ch; |
| 440 |
} |
} |
| 441 |
|
|
| 442 |
int ikbhit() |
void igetch_reset() |
| 443 |
{ |
{ |
| 444 |
int len; |
int ch; |
| 445 |
|
do |
| 446 |
ioctl(0, FIONREAD, &len); |
{ |
| 447 |
|
ch = igetch(0); |
| 448 |
return len; |
} while (ch != KEY_NULL && ch != KEY_TIMEOUT); |
| 449 |
} |
} |