| 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 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "common.h" |
#include "common.h" |
| 14 |
#include "io.h" |
#include "io.h" |
| 27 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 28 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 29 |
|
|
| 30 |
char stdio_charset[20] = BBS_DEFAULT_CHARSET; |
const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 31 |
|
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 32 |
|
|
| 33 |
|
// epoll for STDIO |
| 34 |
|
static int stdin_epollfd = -1; |
| 35 |
|
static int stdout_epollfd = -1; |
| 36 |
|
static int stdin_flags; |
| 37 |
|
static int stdout_flags; |
| 38 |
|
|
| 39 |
// static input / output buffer |
// static input / output buffer |
| 40 |
static char stdin_buf[LINE_BUFFER_LEN]; |
static char stdin_buf[LINE_BUFFER_LEN]; |
| 54 |
static iconv_t stdin_cd = NULL; |
static iconv_t stdin_cd = NULL; |
| 55 |
static iconv_t stdout_cd = NULL; |
static iconv_t stdout_cd = NULL; |
| 56 |
|
|
| 57 |
|
int io_init(void) |
| 58 |
|
{ |
| 59 |
|
struct epoll_event ev; |
| 60 |
|
|
| 61 |
|
if (stdin_epollfd == -1) |
| 62 |
|
{ |
| 63 |
|
stdin_epollfd = epoll_create1(0); |
| 64 |
|
if (stdin_epollfd == -1) |
| 65 |
|
{ |
| 66 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 67 |
|
return -1; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
|
ev.events = EPOLLIN; |
| 71 |
|
ev.data.fd = STDIN_FILENO; |
| 72 |
|
if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 73 |
|
{ |
| 74 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 75 |
|
if (close(stdin_epollfd) < 0) |
| 76 |
|
{ |
| 77 |
|
log_error("close(stdin_epollfd) error (%d)\n"); |
| 78 |
|
} |
| 79 |
|
stdin_epollfd = -1; |
| 80 |
|
return -1; |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 84 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
if (stdout_epollfd == -1) |
| 88 |
|
{ |
| 89 |
|
stdout_epollfd = epoll_create1(0); |
| 90 |
|
if (stdout_epollfd == -1) |
| 91 |
|
{ |
| 92 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 93 |
|
return -1; |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
ev.events = EPOLLOUT; |
| 97 |
|
ev.data.fd = STDOUT_FILENO; |
| 98 |
|
if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 99 |
|
{ |
| 100 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 101 |
|
if (close(stdout_epollfd) < 0) |
| 102 |
|
{ |
| 103 |
|
log_error("close(stdout_epollfd) error (%d)\n"); |
| 104 |
|
} |
| 105 |
|
stdout_epollfd = -1; |
| 106 |
|
return -1; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
// Set STDOUT as non-blocking |
| 110 |
|
stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 111 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
return 0; |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
void io_cleanup(void) |
| 118 |
|
{ |
| 119 |
|
if (stdin_epollfd != -1) |
| 120 |
|
{ |
| 121 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags); |
| 122 |
|
|
| 123 |
|
if (close(stdin_epollfd) < 0) |
| 124 |
|
{ |
| 125 |
|
log_error("close(stdin_epollfd) error (%d)\n"); |
| 126 |
|
} |
| 127 |
|
stdin_epollfd = -1; |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
if (stdout_epollfd != -1) |
| 131 |
|
{ |
| 132 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); |
| 133 |
|
|
| 134 |
|
if (close(stdout_epollfd) < 0) |
| 135 |
|
{ |
| 136 |
|
log_error("close(stdout_epollfd) error (%d)\n"); |
| 137 |
|
} |
| 138 |
|
stdout_epollfd = -1; |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
|
| 142 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 143 |
{ |
{ |
| 144 |
char buf[BUFSIZ]; |
char buf[BUFSIZ]; |
| 197 |
|
|
| 198 |
int iflush(void) |
int iflush(void) |
| 199 |
{ |
{ |
| 200 |
int flags; |
struct epoll_event events[MAX_EVENTS]; |
| 201 |
struct epoll_event ev, events[MAX_EVENTS]; |
int nfds; |
|
int nfds, epollfd; |
|
| 202 |
int retry; |
int retry; |
| 203 |
int ret = 0; |
int ret = 0; |
| 204 |
|
|
|
epollfd = epoll_create1(0); |
|
|
if (epollfd < 0) |
|
|
{ |
|
|
log_error("epoll_create1() error (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
ev.events = EPOLLOUT; |
|
|
ev.data.fd = STDOUT_FILENO; |
|
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
|
|
{ |
|
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
return -1; |
|
|
} |
|
|
|
|
|
// Set STDOUT as non-blocking |
|
|
flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
|
|
fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); |
|
|
|
|
| 205 |
// Retry wait / flush for at most 3 times |
// Retry wait / flush for at most 3 times |
| 206 |
retry = 3; |
retry = 3; |
| 207 |
while (retry > 0 && !SYS_server_exit) |
while (retry > 0 && !SYS_server_exit) |
| 208 |
{ |
{ |
| 209 |
retry--; |
retry--; |
| 210 |
|
|
| 211 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 212 |
|
|
| 213 |
if (nfds < 0) |
if (nfds < 0) |
| 214 |
{ |
{ |
| 296 |
} |
} |
| 297 |
} |
} |
| 298 |
|
|
|
// Restore STDOUT flags |
|
|
fcntl(STDOUT_FILENO, F_SETFL, flags); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 299 |
return ret; |
return ret; |
| 300 |
} |
} |
| 301 |
|
|
| 303 |
{ |
{ |
| 304 |
static int stdin_read_wait = 0; |
static int stdin_read_wait = 0; |
| 305 |
|
|
| 306 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event events[MAX_EVENTS]; |
| 307 |
int nfds, epollfd; |
int nfds; |
| 308 |
int ret; |
int ret; |
| 309 |
int loop; |
int loop; |
| 310 |
|
|
| 314 |
int in_ascii = 0; |
int in_ascii = 0; |
| 315 |
int in_control = 0; |
int in_control = 0; |
| 316 |
int i = 0; |
int i = 0; |
|
int flags; |
|
| 317 |
|
|
| 318 |
if (stdin_conv_offset >= stdin_conv_len) |
if (stdin_conv_offset >= stdin_conv_len) |
| 319 |
{ |
{ |
| 320 |
stdin_conv_len = 0; |
stdin_conv_len = 0; |
| 321 |
stdin_conv_offset = 0; |
stdin_conv_offset = 0; |
| 322 |
|
|
|
epollfd = epoll_create1(0); |
|
|
if (epollfd < 0) |
|
|
{ |
|
|
log_error("epoll_create1() error (%d)\n", errno); |
|
|
return -1; |
|
|
} |
|
|
|
|
|
ev.events = EPOLLIN; |
|
|
ev.data.fd = STDIN_FILENO; |
|
|
if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
|
|
{ |
|
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
return -1; |
|
|
} |
|
|
|
|
|
flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
|
|
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); |
|
|
|
|
| 323 |
for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) |
for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) |
| 324 |
{ |
{ |
| 325 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 331 |
|
|
| 332 |
if (!stdin_read_wait) |
if (!stdin_read_wait) |
| 333 |
{ |
{ |
| 334 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout); |
| 335 |
|
|
| 336 |
if (nfds < 0) |
if (nfds < 0) |
| 337 |
{ |
{ |
| 433 |
#endif |
#endif |
| 434 |
} |
} |
| 435 |
|
|
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 436 |
if (stdin_buf_offset < stdin_buf_len) |
if (stdin_buf_offset < stdin_buf_len) |
| 437 |
{ |
{ |
| 438 |
ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); |
ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); |
| 1059 |
|
|
| 1060 |
int io_conv_init(const char *charset) |
int io_conv_init(const char *charset) |
| 1061 |
{ |
{ |
| 1062 |
char tocode[32]; |
char tocode[CHARSET_MAX_LEN + 20]; |
| 1063 |
|
|
| 1064 |
if (charset == NULL) |
if (charset == NULL) |
| 1065 |
{ |
{ |
| 1072 |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
| 1073 |
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
| 1074 |
|
|
| 1075 |
snprintf(tocode, sizeof(tocode), "%s%s", BBS_DEFAULT_CHARSET, |
snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset, |
| 1076 |
(strcasecmp(stdio_charset, BBS_DEFAULT_CHARSET) == 0 ? "" : "//IGNORE")); |
(strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE")); |
| 1077 |
stdin_cd = iconv_open(tocode, stdio_charset); |
stdin_cd = iconv_open(tocode, stdio_charset); |
| 1078 |
if (stdin_cd == (iconv_t)(-1)) |
if (stdin_cd == (iconv_t)(-1)) |
| 1079 |
{ |
{ |
| 1082 |
} |
} |
| 1083 |
|
|
| 1084 |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 1085 |
(strcasecmp(BBS_DEFAULT_CHARSET, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
(strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 1086 |
stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET); |
stdout_cd = iconv_open(tocode, BBS_default_charset); |
| 1087 |
if (stdout_cd == (iconv_t)(-1)) |
if (stdout_cd == (iconv_t)(-1)) |
| 1088 |
{ |
{ |
| 1089 |
log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno); |
log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); |
| 1090 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1091 |
return -2; |
return -2; |
| 1092 |
} |
} |