--- lbbs/src/io.c 2025/06/17 13:25:49 1.45 +++ lbbs/src/io.c 2025/12/18 03:23:48 1.76 @@ -1,43 +1,189 @@ -/*************************************************************************** - 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 -static char stdout_buf[BUFSIZ]; +#ifdef HAVE_SYS_EPOLL_H +#include +#else +#include +#endif + +enum _io_constant_t +{ + OUTPUT_BUF_SIZE = 8192, +}; + +const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; +char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; + +#ifdef HAVE_SYS_EPOLL_H +// epoll for STDIO +static int stdin_epollfd = -1; +static int stdout_epollfd = -1; +#endif + +static int stdin_flags = 0; +static int stdout_flags = 0; + +// static input / output buffer +static char stdin_buf[LINE_BUFFER_LEN]; +static char stdout_buf[OUTPUT_BUF_SIZE]; +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[OUTPUT_BUF_SIZE * 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 = (iconv_t)(-1); +static iconv_t stdout_cd = (iconv_t)(-1); + +int io_init(void) +{ +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event ev; + + if (stdin_epollfd == -1) + { + stdin_epollfd = epoll_create1(0); + if (stdin_epollfd == -1) + { + log_error("epoll_create1() error (%d)\n", errno); + return -1; + } + + ev.events = EPOLLIN; + ev.data.fd = STDIN_FILENO; + if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) + { + log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n"); + } + stdin_epollfd = -1; + return -1; + } + + stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + } + + if (stdout_epollfd == -1) + { + stdout_epollfd = epoll_create1(0); + if (stdout_epollfd == -1) + { + log_error("epoll_create1() error (%d)\n", errno); + return -1; + } + + ev.events = EPOLLOUT; + ev.data.fd = STDOUT_FILENO; + if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) + { + log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n"); + } + stdout_epollfd = -1; + return -1; + } + + stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + } +#else + if (stdin_flags == 0) + { + stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); + } + + if (stdout_flags == 0) + { + stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); + } +#endif + + return 0; +} + +void io_cleanup(void) +{ +#ifdef HAVE_SYS_EPOLL_H + if (stdin_epollfd != -1) + { + fcntl(STDIN_FILENO, F_SETFL, stdin_flags); + stdin_flags = 0; + + if (close(stdin_epollfd) < 0) + { + log_error("close(stdin_epollfd) error (%d)\n"); + } + stdin_epollfd = -1; + } + + if (stdout_epollfd != -1) + { + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); + stdout_flags = 0; + + if (close(stdout_epollfd) < 0) + { + log_error("close(stdout_epollfd) error (%d)\n"); + } + stdout_epollfd = -1; + } +#else + if (stdin_flags != 0) + { + fcntl(STDIN_FILENO, F_SETFL, stdin_flags); + stdin_flags = 0; + } + + if (stdout_flags != 0) + { + fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); + stdout_flags = 0; + } +#endif +} + int prints(const char *format, ...) { - char buf[BUFSIZ]; + char buf[OUTPUT_BUF_SIZE]; va_list args; int ret; @@ -47,12 +193,12 @@ int prints(const char *format, ...) if (ret > 0) { - if (stdout_buf_len + ret > BUFSIZ) + if (stdout_buf_len + ret > OUTPUT_BUF_SIZE) { iflush(); } - if (stdout_buf_len + ret <= BUFSIZ) + if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE) { memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); stdout_buf_len += ret; @@ -60,7 +206,7 @@ int prints(const char *format, ...) else { errno = EAGAIN; - ret = (BUFSIZ - stdout_buf_len - ret); + ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret); log_error("Output buffer is full, additional %d is required\n", ret); } } @@ -72,12 +218,12 @@ int outc(char c) { int ret; - if (stdout_buf_len + 1 > BUFSIZ) + if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) { iflush(); } - if (stdout_buf_len + 1 <= BUFSIZ) + if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE) { stdout_buf[stdout_buf_len] = c; stdout_buf_len++; @@ -93,76 +239,94 @@ int outc(char c) int iflush(void) { - int flags; - struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; - int retry; - int ret = 0; - - 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; - } +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event events[MAX_EVENTS]; +#else + struct pollfd pfds[1]; +#endif - // Set STDOUT as non-blocking - flags = fcntl(STDOUT_FILENO, F_GETFL, 0); - fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK); + int nfds; + int ret = 0; // Retry wait / flush for at most 3 times - retry = 3; - while (retry > 0 && !SYS_server_exit) + for (int retry = 3; retry > 0 && !SYS_server_exit; retry--) { - retry--; - - nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second +#ifdef HAVE_SYS_EPOLL_H + nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second + ret = nfds; +#else + pfds[0].fd = STDOUT_FILENO; + pfds[0].events = POLLOUT; + nfds = 1; + ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second +#endif - if (nfds < 0) + if (ret < 0) { if (errno != EINTR) { +#ifdef HAVE_SYS_EPOLL_H log_error("epoll_wait() error (%d)\n", errno); +#else + log_error("poll() error (%d)\n", errno); +#endif break; } continue; } - else if (nfds == 0) // timeout + else if (ret == 0) // timeout { continue; } for (int i = 0; i < nfds; i++) { - if (events[i].data.fd == STDOUT_FILENO) +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) +#else + if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) +#endif { - while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error +#ifdef HAVE_SYS_EPOLL_H + log_debug("STDOUT error events (%d)\n", events[i].events); +#else + log_debug("STDOUT error events (%d)\n", pfds[i].revents); +#endif + retry = 0; + break; + } + +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT)) +#else + if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) +#endif + { + 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)); + log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session)); retry = 0; break; } } 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 +340,7 @@ int iflush(void) } else { - log_error("write(STDOUT) error (%d)\n", errno); + log_debug("write(STDOUT) error (%d)\n", errno); retry = 0; break; } @@ -188,12 +352,12 @@ int iflush(void) } 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; } @@ -204,26 +368,20 @@ int iflush(void) } } - // Restore STDOUT flags - fcntl(STDOUT_FILENO, F_SETFL, flags); - - if (close(epollfd) < 0) - { - log_error("close(epoll) error (%d)\n"); - } - return ret; } 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; + +#ifdef HAVE_SYS_EPOLL_H + struct epoll_event events[MAX_EVENTS]; +#else + struct pollfd pfds[1]; +#endif - struct epoll_event ev, events[MAX_EVENTS]; - int nfds, epollfd; + int nfds; int ret; int loop; @@ -233,97 +391,117 @@ int igetch(int timeout) int in_ascii = 0; int in_control = 0; 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) + for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;) { - log_error("close(epoll) error (%d)\n"); - } - return -1; - } - - flags = fcntl(STDIN_FILENO, F_GETFL, 0); - fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - - loop = 1; + if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) + { + log_debug("SSH channel is closed\n"); + loop = 0; + break; + } - while (loop && pos >= len && !SYS_server_exit) - { - len = 0; - pos = 0; + if (!stdin_read_wait) + { +#ifdef HAVE_SYS_EPOLL_H + nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout); + ret = nfds; +#else + pfds[0].fd = STDIN_FILENO; + pfds[0].events = POLLIN; + nfds = 1; + ret = poll(pfds, (nfds_t)nfds, timeout); +#endif - if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) - { - log_error("SSH channel is closed\n"); - loop = 0; - break; - } + if (ret < 0) + { + if (errno != EINTR) + { +#ifdef HAVE_SYS_EPOLL_H + log_error("epoll_wait() error (%d)\n", errno); +#else + log_error("poll() error (%d)\n", errno); +#endif + break; + } + continue; + } + else if (ret == 0) // timeout + { + out = KEY_TIMEOUT; + break; + } - nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); + for (int i = 0; i < nfds; i++) + { +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR))) +#else + if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR))) +#endif + { +#ifdef HAVE_SYS_EPOLL_H + log_debug("STDIN error events (%d)\n", events[i].events); +#else + log_debug("STDIN error events (%d)\n", pfds[i].revents); +#endif + loop = 0; + break; + } - if (nfds < 0) - { - if (errno != EINTR) - { - log_error("epoll_wait() error (%d)\n", errno); - break; +#ifdef HAVE_SYS_EPOLL_H + if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN)) +#else + if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) +#endif + { + stdin_read_wait = 1; + } + } } - 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) { - 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)); + log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session)); loop = 0; break; } 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,39 +511,68 @@ int igetch(int timeout) } else { - log_error("read(STDIN) error (%d)\n", errno); + log_debug("read(STDIN) error (%d)\n", errno); 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 + // For debug #ifdef _DEBUG - for (int j = pos; j < len; j++) - { - log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256); + for (int j = stdin_buf_offset; j < stdin_buf_len; j++) + { + log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256); + } +#endif } + + 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 + } + + // For debug +#ifdef _DEBUG + for (int j = stdin_conv_offset; j < stdin_conv_len; j++) + { + log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); + } #endif + } } - fcntl(STDIN_FILENO, F_SETFL, flags); - - while (pos < len) + 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) { @@ -801,11 +1008,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) { @@ -816,7 +1018,7 @@ int igetch(int timeout) #ifdef _DEBUG if (out != KEY_TIMEOUT && out != KEY_NULL) { - log_common("Debug: -->[0x %x]\n", out); + log_debug("output: -->[0x %x]\n", out); } #endif @@ -841,6 +1043,185 @@ 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; + int skip_current = 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 || skip_current) + { + skip_current = 0; + + 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)++; + + 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 + { + log_debug("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]); + if (p_buf != in_buf) + { + *p_buf_len -= (int)(in_buf - p_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); + log_debug("Reset in_bytes from 0 to %d\n", in_bytes); + } + + log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", + in_bytes, out_bytes, in_buf[0]); + skip_current = 1; + } + else // something strange + { + log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", + in_bytes, out_bytes, errno, in_buf[0]); + *p_buf_offset = (int)(in_buf - p_buf); + *p_conv_len = (int)(conv_size - out_bytes); + skip_current = 1; + } + } + else + { + *p_buf_offset = (int)(in_buf - p_buf); + *p_conv_len = (int)(conv_size - out_bytes); + } + } + + if (*p_buf_offset >= *p_buf_len) + { + *p_buf_len = 0; + *p_buf_offset = 0; + } + + 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); + stdin_cd = (iconv_t)(-1); + return -2; + } + + return 0; +} + +int io_conv_cleanup(void) +{ + if (stdin_cd != (iconv_t)(-1)) + { + iconv_close(stdin_cd); + stdin_cd = (iconv_t)(-1); + } + if (stdout_cd != (iconv_t)(-1)) + { + iconv_close(stdout_cd); + stdout_cd = (iconv_t)(-1); + } + + return 0; +}