--- lbbs/src/io.c 2025/06/09 15:39:05 1.41 +++ lbbs/src/io.c 2025/11/11 00:28:05 1.65 @@ -1,40 +1,53 @@ -/*************************************************************************** - io.c - description - ------------------- - Copyright : (C) 2004-2025 by Leaflet - Email : leaflet@leafok.com - ***************************************************************************/ - -/*************************************************************************** - * * - * 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. * - * * - ***************************************************************************/ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * io + * - basic terminal-based user input / output features + * + * Copyright (C) 2004-2025 Leaflet + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif +#include "common.h" #include "io.h" #include "log.h" -#include "common.h" #include -#include +#include #include +#include #include #include -#include #include -#include -#include #include +#include +#include +#include #include #include -#include +const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; +char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; + +// static input / output buffer +static char stdin_buf[LINE_BUFFER_LEN]; static char stdout_buf[BUFSIZ]; +static int stdin_buf_len = 0; static int stdout_buf_len = 0; +static int stdin_buf_offset = 0; static int stdout_buf_offset = 0; +static char stdin_conv[LINE_BUFFER_LEN * 2]; +static char stdout_conv[BUFSIZ * 2]; +static int stdin_conv_len = 0; +static int stdout_conv_len = 0; +static int stdin_conv_offset = 0; +static int stdout_conv_offset = 0; + +static iconv_t stdin_cd = NULL; +static iconv_t stdout_cd = NULL; + int prints(const char *format, ...) { char buf[BUFSIZ]; @@ -91,7 +104,7 @@ int outc(char c) return ret; } -int iflush() +int iflush(void) { int flags; struct epoll_event ev, events[MAX_EVENTS]; @@ -148,11 +161,21 @@ int iflush() { if (events[i].data.fd == STDOUT_FILENO) { - while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error + if (stdout_buf_offset < stdout_buf_len) + { + ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len); + if (ret < 0) + { + log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len); + stdout_buf_len = stdout_buf_offset; // Discard invalid sequence + } + } + + while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error { if (SSH_v2) { - ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset)); + ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset)); if (ret == SSH_ERROR) { log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); @@ -162,7 +185,7 @@ int iflush() } else { - ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset)); + ret = (int)write(STDOUT_FILENO, stdout_conv + stdout_conv_offset, (size_t)(stdout_conv_len - stdout_conv_offset)); } if (ret < 0) { @@ -176,7 +199,9 @@ int iflush() } else { +#ifdef _DEBUG log_error("write(STDOUT) error (%d)\n", errno); +#endif retry = 0; break; } @@ -188,12 +213,12 @@ int iflush() } else { - stdout_buf_offset += ret; - if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely + stdout_conv_offset += ret; + if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely { ret = 0; - stdout_buf_offset = 0; - stdout_buf_len = 0; + stdout_conv_offset = 0; + stdout_conv_len = 0; retry = 0; break; } @@ -217,10 +242,7 @@ int iflush() int igetch(int timeout) { - // static input buffer - static unsigned char buf[LINE_BUFFER_LEN]; - static int len = 0; - static int pos = 0; + static int stdin_read_wait = 0; struct epoll_event ev, events[MAX_EVENTS]; int nfds, epollfd; @@ -235,69 +257,78 @@ int igetch(int timeout) int i = 0; int flags; - epollfd = epoll_create1(0); - if (epollfd < 0) + if (stdin_conv_offset >= stdin_conv_len) { - 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); + stdin_conv_len = 0; + stdin_conv_offset = 0; - if (close(epollfd) < 0) + epollfd = epoll_create1(0); + if (epollfd < 0) { - log_error("close(epoll) error (%d)\n"); + log_error("epoll_create1() error (%d)\n", errno); + return -1; } - return -1; - } - - flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - - loop = 1; - - while (loop && pos >= len && !SYS_server_exit) - { - len = 0; - pos = 0; - if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) + ev.events = EPOLLIN; + ev.data.fd = STDIN_FILENO; + if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) { - log_error("SSH channel is closed\n"); - loop = 0; - break; + log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); + + if (close(epollfd) < 0) + { + log_error("close(epoll) error (%d)\n"); + } + return -1; } - nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); + flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - if (nfds < 0) + for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) { - if (errno != EINTR) + if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) { - log_error("epoll_wait() error (%d)\n", errno); + log_error("SSH channel is closed\n"); + loop = 0; break; } - continue; - } - else if (nfds == 0) // timeout - { - out = KEY_TIMEOUT; - break; - } - for (int i = 0; i < nfds; i++) - { - if (events[i].data.fd == STDIN_FILENO) + if (!stdin_read_wait) + { + nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); + + if (nfds < 0) + { + if (errno != EINTR) + { + log_error("epoll_wait() error (%d)\n", errno); + break; + } + continue; + } + else if (nfds == 0) // timeout + { + out = KEY_TIMEOUT; + break; + } + + for (int i = 0; i < nfds; i++) + { + if (events[i].data.fd == STDIN_FILENO) + { + stdin_read_wait = 1; + } + } + } + + if (stdin_read_wait) { - while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error + while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error { if (SSH_v2) { - ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0); + ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0); if (ret == SSH_ERROR) { log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); @@ -306,24 +337,28 @@ int igetch(int timeout) } else if (ret == SSH_EOF) { + stdin_read_wait = 0; loop = 0; break; } else if (ret == 0) { out = 0; - break; // Check whether channel is still open + stdin_read_wait = 0; + loop = 0; + break; } } else { - ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len); + ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len); } if (ret < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { out = 0; + stdin_read_wait = 0; loop = 0; break; } @@ -333,37 +368,77 @@ int igetch(int timeout) } else { +#ifdef _DEBUG log_error("read(STDIN) error (%d)\n", errno); +#endif loop = 0; break; } } else if (ret == 0) // broken pipe { + stdin_read_wait = 0; loop = 0; break; } else { - len += ret; + stdin_buf_len += ret; continue; } } } + + // For debug +#ifdef _DEBUG + for (int j = stdin_buf_offset; j < stdin_buf_len; j++) + { + log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256); + } +#endif } - // For debug - // for (int j = pos; j < len; j++) - // { - // log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); - // } - } + fcntl(STDIN_FILENO, F_SETFL, flags); + + if (close(epollfd) < 0) + { + log_error("close(epoll) error (%d)\n"); + } - fcntl(STDIN_FILENO, F_SETFL, flags); + if (stdin_buf_offset < stdin_buf_len) + { + ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len); + if (ret < 0) + { + log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len); + stdin_buf_len = stdin_buf_offset; // Discard invalid sequence + } - while (pos < len) + // For debug +#ifdef _DEBUG + for (int j = stdin_conv_offset; j < stdin_conv_len; j++) + { + log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); + } +#endif + } + } + + while (stdin_conv_offset < stdin_conv_len) { - unsigned char c = buf[pos++]; + unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++]; + + // Convert \r\n to \r + if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF) + { + stdin_conv_offset++; + } + + // Convert single \n to \r + if (c == LF) + { + c = CR; + } if (c == KEY_CONTROL) { @@ -534,6 +609,9 @@ int igetch(int timeout) case 49: out = KEY_HOME; break; + case 50: + out = KEY_INS; + break; case 51: out = KEY_DEL; break; @@ -796,11 +874,6 @@ int igetch(int timeout) break; } - if (close(epollfd) < 0) - { - log_error("close(epoll) error (%d)\n"); - } - // For ESC key if (out == 0 && in_esc) { @@ -808,10 +881,12 @@ int igetch(int timeout) } // for debug - // if (out != KEY_TIMEOUT && out != KEY_NULL) - // { - // log_common("Debug: -->[0x %x]\n", out); - // } +#ifdef _DEBUG + if (out != KEY_TIMEOUT && out != KEY_NULL) + { + log_error("Debug: -->[0x %x]\n", out); + } +#endif return out; } @@ -819,12 +894,12 @@ int igetch(int timeout) int igetch_t(int sec) { int ch; - time_t t_begin = time(0); + time_t t_begin = time(NULL); do { ch = igetch(100); - } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); + } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec)); return ch; } @@ -834,6 +909,175 @@ void igetch_reset() int ch; do { - ch = igetch(0); + ch = igetch(100); } while (ch != KEY_NULL && ch != KEY_TIMEOUT); } + +int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len) +{ + char *in_buf; + char *out_buf; + size_t in_bytes; + size_t out_bytes; + int ret; + int in_control = 0; + size_t i = 0; + + if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) + { + log_error("NULL pointer error\n"); + return -1; + } + + in_buf = p_buf + *p_buf_offset; + in_bytes = (size_t)(*p_buf_len - *p_buf_offset); + out_buf = p_conv + *p_conv_len; + out_bytes = conv_size - (size_t)(*p_conv_len); + + while (in_bytes > 0) + { + if ((unsigned char)(*in_buf) == KEY_CONTROL) + { + if (in_control == 0) + { + in_control = 1; + i = 0; + } + } + + if (in_control) + { + if (out_bytes <= 0) + { + log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); + return -2; + } + + *out_buf = *in_buf; + in_buf++; + out_buf++; + in_bytes--; + out_bytes--; + + (*p_buf_offset)++; + *p_conv_len = (int)(conv_size - out_bytes); + + i++; + if (i >= 2) + { + in_control = 0; + } + continue; + } + + ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes); + if (ret == -1) + { + if (errno == EINVAL) // Incomplete + { +#ifdef _DEBUG + log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); +#endif + if (p_buf != in_buf) + { + *p_buf_len = (int)(p_buf + *p_buf_len - in_buf); + *p_buf_offset = 0; + *p_conv_len = (int)(conv_size - out_bytes); + memmove(p_buf, in_buf, (size_t)(*p_buf_len)); + } + + break; + } + else if (errno == E2BIG) + { + log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes); + return -1; + } + else if (errno == EILSEQ) + { + if (in_bytes > out_bytes || out_bytes <= 0) + { + log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes); + return -2; + } + + // reset in_bytes when "//IGNORE" is applied + if (in_bytes == 0) + { + in_bytes = (size_t)(*p_buf_len - *p_buf_offset); + } + + *out_buf = *in_buf; + in_buf++; + out_buf++; + in_bytes--; + out_bytes--; + + continue; + } + } + else + { + *p_buf_len = 0; + *p_buf_offset = 0; + *p_conv_len = (int)(conv_size - out_bytes); + + break; + } + } + + return 0; +} + +int io_conv_init(const char *charset) +{ + char tocode[CHARSET_MAX_LEN + 20]; + + if (charset == NULL) + { + log_error("NULL pointer error\n"); + return -1; + } + + io_conv_cleanup(); + + strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); + stdio_charset[sizeof(stdio_charset) - 1] = '\0'; + + snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset, + (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE")); + stdin_cd = iconv_open(tocode, stdio_charset); + if (stdin_cd == (iconv_t)(-1)) + { + log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); + return -2; + } + + snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, + (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); + stdout_cd = iconv_open(tocode, BBS_default_charset); + if (stdout_cd == (iconv_t)(-1)) + { + log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno); + iconv_close(stdin_cd); + return -2; + } + + return 0; +} + +int io_conv_cleanup(void) +{ + if (stdin_cd != NULL) + { + iconv_close(stdin_cd); + stdin_cd = NULL; + } + if (stdout_cd != NULL) + { + iconv_close(stdout_cd); + stdout_cd = NULL; + } + + return 0; +}