--- lbbs/src/io.c 2025/11/17 06:41:18 1.67 +++ lbbs/src/io.c 2025/12/18 03:23:48 1.76 @@ -32,6 +32,11 @@ #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"; @@ -46,21 +51,21 @@ static int stdout_flags = 0; // static input / output buffer static char stdin_buf[LINE_BUFFER_LEN]; -static char stdout_buf[BUFSIZ]; +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[BUFSIZ * 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 = NULL; -static iconv_t stdout_cd = NULL; +static iconv_t stdin_cd = (iconv_t)(-1); +static iconv_t stdout_cd = (iconv_t)(-1); int io_init(void) { @@ -178,7 +183,7 @@ void io_cleanup(void) int prints(const char *format, ...) { - char buf[BUFSIZ]; + char buf[OUTPUT_BUF_SIZE]; va_list args; int ret; @@ -188,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; @@ -201,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); } } @@ -213,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++; @@ -241,15 +246,11 @@ int iflush(void) #endif int nfds; - int retry; 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--; - #ifdef HAVE_SYS_EPOLL_H nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second ret = nfds; @@ -281,7 +282,22 @@ int iflush(void) for (int i = 0; i < nfds; i++) { #ifdef HAVE_SYS_EPOLL_H - if (events[i].data.fd == STDOUT_FILENO) + 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 + { +#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 @@ -303,7 +319,7 @@ int iflush(void) 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; } @@ -324,9 +340,7 @@ int iflush(void) } else { -#ifdef _DEBUG - log_error("write(STDOUT) error (%d)\n", errno); -#endif + log_debug("write(STDOUT) error (%d)\n", errno); retry = 0; break; } @@ -387,7 +401,7 @@ int igetch(int timeout) { if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) { - log_error("SSH channel is closed\n"); + log_debug("SSH channel is closed\n"); loop = 0; break; } @@ -426,7 +440,22 @@ int igetch(int timeout) for (int i = 0; i < nfds; i++) { #ifdef HAVE_SYS_EPOLL_H - if (events[i].data.fd == STDIN_FILENO) + 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; + } + +#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 @@ -445,7 +474,7 @@ int igetch(int timeout) 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; } @@ -482,9 +511,7 @@ int igetch(int timeout) } else { -#ifdef _DEBUG - log_error("read(STDIN) error (%d)\n", errno); -#endif + log_debug("read(STDIN) error (%d)\n", errno); loop = 0; break; } @@ -507,7 +534,7 @@ int igetch(int timeout) #ifdef _DEBUG for (int j = stdin_buf_offset; j < stdin_buf_len; j++) { - log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256); + log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256); } #endif } @@ -525,7 +552,7 @@ int igetch(int timeout) #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); + log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256); } #endif } @@ -991,7 +1018,7 @@ int igetch(int timeout) #ifdef _DEBUG if (out != KEY_TIMEOUT && out != KEY_NULL) { - log_error("Debug: -->[0x %x]\n", out); + log_debug("output: -->[0x %x]\n", out); } #endif @@ -1029,6 +1056,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, 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) { @@ -1052,8 +1080,10 @@ int io_buf_conv(iconv_t cd, char *p_buf, } } - if (in_control) + 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); @@ -1067,7 +1097,7 @@ int io_buf_conv(iconv_t cd, char *p_buf, out_bytes--; (*p_buf_offset)++; - *p_conv_len = (int)(conv_size - out_bytes); + (*p_conv_len)++; i++; if (i >= 2) @@ -1082,12 +1112,10 @@ int io_buf_conv(iconv_t cd, char *p_buf, { 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 + 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)(p_buf + *p_buf_len - 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)); @@ -1112,27 +1140,35 @@ int io_buf_conv(iconv_t cd, char *p_buf, 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); } - *out_buf = *in_buf; - in_buf++; - out_buf++; - in_bytes--; - out_bytes--; - - continue; + 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_len = 0; - *p_buf_offset = 0; + *p_buf_offset = (int)(in_buf - p_buf); *p_conv_len = (int)(conv_size - out_bytes); - - break; } } + if (*p_buf_offset >= *p_buf_len) + { + *p_buf_len = 0; + *p_buf_offset = 0; + } + return 0; } @@ -1167,6 +1203,7 @@ int io_conv_init(const char *charset) { 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; } @@ -1175,15 +1212,15 @@ int io_conv_init(const char *charset) int io_conv_cleanup(void) { - if (stdin_cd != NULL) + if (stdin_cd != (iconv_t)(-1)) { iconv_close(stdin_cd); - stdin_cd = NULL; + stdin_cd = (iconv_t)(-1); } - if (stdout_cd != NULL) + if (stdout_cd != (iconv_t)(-1)) { iconv_close(stdout_cd); - stdout_cd = NULL; + stdout_cd = (iconv_t)(-1); } return 0;