| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
io.c - description |
/* |
| 3 |
------------------- |
* io |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - basic terminal-based user input / output features |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "common.h" |
#include "common.h" |
| 10 |
#include "io.h" |
#include "io.h" |
| 162 |
if (ret < 0) |
if (ret < 0) |
| 163 |
{ |
{ |
| 164 |
log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); |
| 165 |
|
stdout_buf_len = stdout_buf_offset; // Discard invalid sequence |
| 166 |
} |
} |
| 167 |
} |
} |
| 168 |
|
|
| 237 |
|
|
| 238 |
int igetch(int timeout) |
int igetch(int timeout) |
| 239 |
{ |
{ |
| 240 |
|
static int stdin_read_wait = 0; |
| 241 |
|
|
| 242 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 243 |
int nfds, epollfd; |
int nfds, epollfd; |
| 244 |
int ret; |
int ret; |
| 289 |
break; |
break; |
| 290 |
} |
} |
| 291 |
|
|
| 292 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
if (!stdin_read_wait) |
|
|
|
|
if (nfds < 0) |
|
| 293 |
{ |
{ |
| 294 |
if (errno != EINTR) |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
| 295 |
|
|
| 296 |
|
if (nfds < 0) |
| 297 |
{ |
{ |
| 298 |
log_error("epoll_wait() error (%d)\n", errno); |
if (errno != EINTR) |
| 299 |
|
{ |
| 300 |
|
log_error("epoll_wait() error (%d)\n", errno); |
| 301 |
|
break; |
| 302 |
|
} |
| 303 |
|
continue; |
| 304 |
|
} |
| 305 |
|
else if (nfds == 0) // timeout |
| 306 |
|
{ |
| 307 |
|
out = KEY_TIMEOUT; |
| 308 |
break; |
break; |
| 309 |
} |
} |
| 310 |
continue; |
|
| 311 |
} |
for (int i = 0; i < nfds; i++) |
| 312 |
else if (nfds == 0) // timeout |
{ |
| 313 |
{ |
if (events[i].data.fd == STDIN_FILENO) |
| 314 |
out = KEY_TIMEOUT; |
{ |
| 315 |
break; |
stdin_read_wait = 1; |
| 316 |
|
} |
| 317 |
|
} |
| 318 |
} |
} |
| 319 |
|
|
| 320 |
for (int i = 0; i < nfds; i++) |
if (stdin_read_wait) |
| 321 |
{ |
{ |
| 322 |
if (events[i].data.fd == STDIN_FILENO) |
while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error |
| 323 |
{ |
{ |
| 324 |
while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error |
if (SSH_v2) |
| 325 |
{ |
{ |
| 326 |
if (SSH_v2) |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
| 327 |
|
if (ret == SSH_ERROR) |
| 328 |
{ |
{ |
| 329 |
ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); |
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
| 330 |
if (ret == SSH_ERROR) |
loop = 0; |
| 331 |
{ |
break; |
|
log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == SSH_EOF) |
|
|
{ |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
|
else if (ret == 0) |
|
|
{ |
|
|
out = 0; |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 332 |
} |
} |
| 333 |
else |
else if (ret == SSH_EOF) |
| 334 |
{ |
{ |
| 335 |
ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); |
stdin_read_wait = 0; |
| 336 |
|
loop = 0; |
| 337 |
|
break; |
| 338 |
} |
} |
| 339 |
if (ret < 0) |
else if (ret == 0) |
| 340 |
{ |
{ |
| 341 |
if (errno == EAGAIN || errno == EWOULDBLOCK) |
out = 0; |
| 342 |
{ |
stdin_read_wait = 0; |
| 343 |
out = 0; |
loop = 0; |
| 344 |
loop = 0; |
break; |
|
break; |
|
|
} |
|
|
else if (errno == EINTR) |
|
|
{ |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
#ifdef _DEBUG |
|
|
log_error("read(STDIN) error (%d)\n", errno); |
|
|
#endif |
|
|
loop = 0; |
|
|
break; |
|
|
} |
|
| 345 |
} |
} |
| 346 |
else if (ret == 0) // broken pipe |
} |
| 347 |
|
else |
| 348 |
|
{ |
| 349 |
|
ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); |
| 350 |
|
} |
| 351 |
|
if (ret < 0) |
| 352 |
|
{ |
| 353 |
|
if (errno == EAGAIN || errno == EWOULDBLOCK) |
| 354 |
{ |
{ |
| 355 |
|
out = 0; |
| 356 |
|
stdin_read_wait = 0; |
| 357 |
loop = 0; |
loop = 0; |
| 358 |
break; |
break; |
| 359 |
} |
} |
| 360 |
else |
else if (errno == EINTR) |
| 361 |
{ |
{ |
|
stdin_buf_len += ret; |
|
| 362 |
continue; |
continue; |
| 363 |
} |
} |
| 364 |
|
else |
| 365 |
|
{ |
| 366 |
|
#ifdef _DEBUG |
| 367 |
|
log_error("read(STDIN) error (%d)\n", errno); |
| 368 |
|
#endif |
| 369 |
|
loop = 0; |
| 370 |
|
break; |
| 371 |
|
} |
| 372 |
|
} |
| 373 |
|
else if (ret == 0) // broken pipe |
| 374 |
|
{ |
| 375 |
|
stdin_read_wait = 0; |
| 376 |
|
loop = 0; |
| 377 |
|
break; |
| 378 |
|
} |
| 379 |
|
else |
| 380 |
|
{ |
| 381 |
|
stdin_buf_len += ret; |
| 382 |
|
continue; |
| 383 |
} |
} |
| 384 |
} |
} |
| 385 |
} |
} |
| 406 |
if (ret < 0) |
if (ret < 0) |
| 407 |
{ |
{ |
| 408 |
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); |
| 409 |
|
stdin_buf_len = stdin_buf_offset; // Discard invalid sequence |
| 410 |
} |
} |
| 411 |
|
|
| 412 |
// For debug |
// For debug |
| 944 |
{ |
{ |
| 945 |
if (out_bytes <= 0) |
if (out_bytes <= 0) |
| 946 |
{ |
{ |
| 947 |
log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
| 948 |
return -2; |
return -2; |
| 949 |
} |
} |
| 950 |
|
|
| 1039 |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
| 1040 |
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
| 1041 |
|
|
| 1042 |
stdin_cd = iconv_open(BBS_DEFAULT_CHARSET "//IGNORE", stdio_charset); |
snprintf(tocode, sizeof(tocode), "%s%s", BBS_DEFAULT_CHARSET, |
| 1043 |
|
(strcasecmp(stdio_charset, BBS_DEFAULT_CHARSET) == 0 ? "" : "//IGNORE")); |
| 1044 |
|
stdin_cd = iconv_open(tocode, stdio_charset); |
| 1045 |
if (stdin_cd == (iconv_t)(-1)) |
if (stdin_cd == (iconv_t)(-1)) |
| 1046 |
{ |
{ |
| 1047 |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET "//IGNORE", errno); |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 1048 |
return -2; |
return -2; |
| 1049 |
} |
} |
| 1050 |
|
|
| 1051 |
snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset); |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 1052 |
|
(strcasecmp(BBS_DEFAULT_CHARSET, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 1053 |
stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET); |
stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET); |
| 1054 |
if (stdout_cd == (iconv_t)(-1)) |
if (stdout_cd == (iconv_t)(-1)) |
| 1055 |
{ |
{ |