| 3 |
* io |
* io |
| 4 |
* - basic terminal-based user input / output features |
* - basic terminal-based user input / output features |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "common.h" |
#include "common.h" |
| 14 |
#include "io.h" |
#include "io.h" |
| 15 |
#include "log.h" |
#include "log.h" |
| 20 |
#include <string.h> |
#include <string.h> |
| 21 |
#include <time.h> |
#include <time.h> |
| 22 |
#include <unistd.h> |
#include <unistd.h> |
|
#include <sys/epoll.h> |
|
| 23 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
| 24 |
#include <sys/select.h> |
#include <sys/select.h> |
| 25 |
#include <libssh/callbacks.h> |
#include <libssh/callbacks.h> |
| 26 |
#include <libssh/libssh.h> |
#include <libssh/libssh.h> |
| 27 |
#include <libssh/server.h> |
#include <libssh/server.h> |
| 28 |
|
|
| 29 |
char stdio_charset[20] = BBS_DEFAULT_CHARSET; |
#ifdef HAVE_SYS_EPOLL_H |
| 30 |
|
#include <sys/epoll.h> |
| 31 |
|
#else |
| 32 |
|
#include <poll.h> |
| 33 |
|
#endif |
| 34 |
|
|
| 35 |
|
enum _io_constant_t |
| 36 |
|
{ |
| 37 |
|
OUTPUT_BUF_SIZE = 8192, |
| 38 |
|
}; |
| 39 |
|
|
| 40 |
|
const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 41 |
|
char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8"; |
| 42 |
|
|
| 43 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 44 |
|
// epoll for STDIO |
| 45 |
|
static int stdin_epollfd = -1; |
| 46 |
|
static int stdout_epollfd = -1; |
| 47 |
|
#endif |
| 48 |
|
|
| 49 |
|
static int stdin_flags = 0; |
| 50 |
|
static int stdout_flags = 0; |
| 51 |
|
|
| 52 |
// static input / output buffer |
// static input / output buffer |
| 53 |
static char stdin_buf[LINE_BUFFER_LEN]; |
static char stdin_buf[LINE_BUFFER_LEN]; |
| 54 |
static char stdout_buf[BUFSIZ]; |
static char stdout_buf[OUTPUT_BUF_SIZE]; |
| 55 |
static int stdin_buf_len = 0; |
static int stdin_buf_len = 0; |
| 56 |
static int stdout_buf_len = 0; |
static int stdout_buf_len = 0; |
| 57 |
static int stdin_buf_offset = 0; |
static int stdin_buf_offset = 0; |
| 58 |
static int stdout_buf_offset = 0; |
static int stdout_buf_offset = 0; |
| 59 |
|
|
| 60 |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
static char stdin_conv[LINE_BUFFER_LEN * 2]; |
| 61 |
static char stdout_conv[BUFSIZ * 2]; |
static char stdout_conv[OUTPUT_BUF_SIZE * 2]; |
| 62 |
static int stdin_conv_len = 0; |
static int stdin_conv_len = 0; |
| 63 |
static int stdout_conv_len = 0; |
static int stdout_conv_len = 0; |
| 64 |
static int stdin_conv_offset = 0; |
static int stdin_conv_offset = 0; |
| 65 |
static int stdout_conv_offset = 0; |
static int stdout_conv_offset = 0; |
| 66 |
|
|
| 67 |
static iconv_t stdin_cd = NULL; |
static iconv_t stdin_cd = (iconv_t)(-1); |
| 68 |
static iconv_t stdout_cd = NULL; |
static iconv_t stdout_cd = (iconv_t)(-1); |
| 69 |
|
|
| 70 |
|
int io_init(void) |
| 71 |
|
{ |
| 72 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 73 |
|
struct epoll_event ev; |
| 74 |
|
|
| 75 |
|
if (stdin_epollfd == -1) |
| 76 |
|
{ |
| 77 |
|
stdin_epollfd = epoll_create1(0); |
| 78 |
|
if (stdin_epollfd == -1) |
| 79 |
|
{ |
| 80 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 81 |
|
return -1; |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
ev.events = EPOLLIN; |
| 85 |
|
ev.data.fd = STDIN_FILENO; |
| 86 |
|
if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1) |
| 87 |
|
{ |
| 88 |
|
log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno); |
| 89 |
|
if (close(stdin_epollfd) < 0) |
| 90 |
|
{ |
| 91 |
|
log_error("close(stdin_epollfd) error (%d)\n"); |
| 92 |
|
} |
| 93 |
|
stdin_epollfd = -1; |
| 94 |
|
return -1; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 98 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); |
| 99 |
|
} |
| 100 |
|
|
| 101 |
|
if (stdout_epollfd == -1) |
| 102 |
|
{ |
| 103 |
|
stdout_epollfd = epoll_create1(0); |
| 104 |
|
if (stdout_epollfd == -1) |
| 105 |
|
{ |
| 106 |
|
log_error("epoll_create1() error (%d)\n", errno); |
| 107 |
|
return -1; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
|
ev.events = EPOLLOUT; |
| 111 |
|
ev.data.fd = STDOUT_FILENO; |
| 112 |
|
if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1) |
| 113 |
|
{ |
| 114 |
|
log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno); |
| 115 |
|
if (close(stdout_epollfd) < 0) |
| 116 |
|
{ |
| 117 |
|
log_error("close(stdout_epollfd) error (%d)\n"); |
| 118 |
|
} |
| 119 |
|
stdout_epollfd = -1; |
| 120 |
|
return -1; |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 124 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); |
| 125 |
|
} |
| 126 |
|
#else |
| 127 |
|
if (stdin_flags == 0) |
| 128 |
|
{ |
| 129 |
|
stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
| 130 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK); |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
if (stdout_flags == 0) |
| 134 |
|
{ |
| 135 |
|
stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0); |
| 136 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK); |
| 137 |
|
} |
| 138 |
|
#endif |
| 139 |
|
|
| 140 |
|
return 0; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
void io_cleanup(void) |
| 144 |
|
{ |
| 145 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 146 |
|
if (stdin_epollfd != -1) |
| 147 |
|
{ |
| 148 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags); |
| 149 |
|
stdin_flags = 0; |
| 150 |
|
|
| 151 |
|
if (close(stdin_epollfd) < 0) |
| 152 |
|
{ |
| 153 |
|
log_error("close(stdin_epollfd) error (%d)\n"); |
| 154 |
|
} |
| 155 |
|
stdin_epollfd = -1; |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
if (stdout_epollfd != -1) |
| 159 |
|
{ |
| 160 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); |
| 161 |
|
stdout_flags = 0; |
| 162 |
|
|
| 163 |
|
if (close(stdout_epollfd) < 0) |
| 164 |
|
{ |
| 165 |
|
log_error("close(stdout_epollfd) error (%d)\n"); |
| 166 |
|
} |
| 167 |
|
stdout_epollfd = -1; |
| 168 |
|
} |
| 169 |
|
#else |
| 170 |
|
if (stdin_flags != 0) |
| 171 |
|
{ |
| 172 |
|
fcntl(STDIN_FILENO, F_SETFL, stdin_flags); |
| 173 |
|
stdin_flags = 0; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
if (stdout_flags != 0) |
| 177 |
|
{ |
| 178 |
|
fcntl(STDOUT_FILENO, F_SETFL, stdout_flags); |
| 179 |
|
stdout_flags = 0; |
| 180 |
|
} |
| 181 |
|
#endif |
| 182 |
|
} |
| 183 |
|
|
| 184 |
int prints(const char *format, ...) |
int prints(const char *format, ...) |
| 185 |
{ |
{ |
| 186 |
char buf[BUFSIZ]; |
char buf[OUTPUT_BUF_SIZE]; |
| 187 |
va_list args; |
va_list args; |
| 188 |
int ret; |
int ret; |
| 189 |
|
|
| 193 |
|
|
| 194 |
if (ret > 0) |
if (ret > 0) |
| 195 |
{ |
{ |
| 196 |
if (stdout_buf_len + ret > BUFSIZ) |
if (stdout_buf_len + ret > OUTPUT_BUF_SIZE) |
| 197 |
{ |
{ |
| 198 |
iflush(); |
iflush(); |
| 199 |
} |
} |
| 200 |
|
|
| 201 |
if (stdout_buf_len + ret <= BUFSIZ) |
if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE) |
| 202 |
{ |
{ |
| 203 |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret); |
| 204 |
stdout_buf_len += ret; |
stdout_buf_len += ret; |
| 206 |
else |
else |
| 207 |
{ |
{ |
| 208 |
errno = EAGAIN; |
errno = EAGAIN; |
| 209 |
ret = (BUFSIZ - stdout_buf_len - ret); |
ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret); |
| 210 |
log_error("Output buffer is full, additional %d is required\n", ret); |
log_error("Output buffer is full, additional %d is required\n", ret); |
| 211 |
} |
} |
| 212 |
} |
} |
| 218 |
{ |
{ |
| 219 |
int ret; |
int ret; |
| 220 |
|
|
| 221 |
if (stdout_buf_len + 1 > BUFSIZ) |
if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE) |
| 222 |
{ |
{ |
| 223 |
iflush(); |
iflush(); |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
if (stdout_buf_len + 1 <= BUFSIZ) |
if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE) |
| 227 |
{ |
{ |
| 228 |
stdout_buf[stdout_buf_len] = c; |
stdout_buf[stdout_buf_len] = c; |
| 229 |
stdout_buf_len++; |
stdout_buf_len++; |
| 239 |
|
|
| 240 |
int iflush(void) |
int iflush(void) |
| 241 |
{ |
{ |
| 242 |
int flags; |
#ifdef HAVE_SYS_EPOLL_H |
| 243 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event events[MAX_EVENTS]; |
| 244 |
int nfds, epollfd; |
#else |
| 245 |
|
struct pollfd pfds[1]; |
| 246 |
|
#endif |
| 247 |
|
|
| 248 |
|
int nfds; |
| 249 |
int retry; |
int retry; |
| 250 |
int ret = 0; |
int ret = 0; |
| 251 |
|
|
|
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); |
|
|
|
|
| 252 |
// Retry wait / flush for at most 3 times |
// Retry wait / flush for at most 3 times |
| 253 |
retry = 3; |
retry = 3; |
| 254 |
while (retry > 0 && !SYS_server_exit) |
while (retry > 0 && !SYS_server_exit) |
| 255 |
{ |
{ |
| 256 |
retry--; |
retry--; |
| 257 |
|
|
| 258 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second |
#ifdef HAVE_SYS_EPOLL_H |
| 259 |
|
nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second |
| 260 |
|
ret = nfds; |
| 261 |
|
#else |
| 262 |
|
pfds[0].fd = STDOUT_FILENO; |
| 263 |
|
pfds[0].events = POLLOUT; |
| 264 |
|
nfds = 1; |
| 265 |
|
ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second |
| 266 |
|
#endif |
| 267 |
|
|
| 268 |
if (nfds < 0) |
if (ret < 0) |
| 269 |
{ |
{ |
| 270 |
if (errno != EINTR) |
if (errno != EINTR) |
| 271 |
{ |
{ |
| 272 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 273 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 274 |
|
#else |
| 275 |
|
log_error("poll() error (%d)\n", errno); |
| 276 |
|
#endif |
| 277 |
break; |
break; |
| 278 |
} |
} |
| 279 |
continue; |
continue; |
| 280 |
} |
} |
| 281 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 282 |
{ |
{ |
| 283 |
continue; |
continue; |
| 284 |
} |
} |
| 285 |
|
|
| 286 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 287 |
{ |
{ |
| 288 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 289 |
if (events[i].data.fd == STDOUT_FILENO) |
if (events[i].data.fd == STDOUT_FILENO) |
| 290 |
|
#else |
| 291 |
|
if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT)) |
| 292 |
|
#endif |
| 293 |
{ |
{ |
| 294 |
if (stdout_buf_offset < stdout_buf_len) |
if (stdout_buf_offset < stdout_buf_len) |
| 295 |
{ |
{ |
| 359 |
} |
} |
| 360 |
} |
} |
| 361 |
|
|
|
// Restore STDOUT flags |
|
|
fcntl(STDOUT_FILENO, F_SETFL, flags); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 362 |
return ret; |
return ret; |
| 363 |
} |
} |
| 364 |
|
|
| 366 |
{ |
{ |
| 367 |
static int stdin_read_wait = 0; |
static int stdin_read_wait = 0; |
| 368 |
|
|
| 369 |
struct epoll_event ev, events[MAX_EVENTS]; |
#ifdef HAVE_SYS_EPOLL_H |
| 370 |
int nfds, epollfd; |
struct epoll_event events[MAX_EVENTS]; |
| 371 |
|
#else |
| 372 |
|
struct pollfd pfds[1]; |
| 373 |
|
#endif |
| 374 |
|
|
| 375 |
|
int nfds; |
| 376 |
int ret; |
int ret; |
| 377 |
int loop; |
int loop; |
| 378 |
|
|
| 382 |
int in_ascii = 0; |
int in_ascii = 0; |
| 383 |
int in_control = 0; |
int in_control = 0; |
| 384 |
int i = 0; |
int i = 0; |
|
int flags; |
|
| 385 |
|
|
| 386 |
if (stdin_conv_offset >= stdin_conv_len) |
if (stdin_conv_offset >= stdin_conv_len) |
| 387 |
{ |
{ |
| 388 |
stdin_conv_len = 0; |
stdin_conv_len = 0; |
| 389 |
stdin_conv_offset = 0; |
stdin_conv_offset = 0; |
| 390 |
|
|
|
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); |
|
|
|
|
| 391 |
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;) |
| 392 |
{ |
{ |
| 393 |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
if (SSH_v2 && ssh_channel_is_closed(SSH_channel)) |
| 399 |
|
|
| 400 |
if (!stdin_read_wait) |
if (!stdin_read_wait) |
| 401 |
{ |
{ |
| 402 |
nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout); |
#ifdef HAVE_SYS_EPOLL_H |
| 403 |
|
nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout); |
| 404 |
|
ret = nfds; |
| 405 |
|
#else |
| 406 |
|
pfds[0].fd = STDIN_FILENO; |
| 407 |
|
pfds[0].events = POLLIN; |
| 408 |
|
nfds = 1; |
| 409 |
|
ret = poll(pfds, (nfds_t)nfds, timeout); |
| 410 |
|
#endif |
| 411 |
|
|
| 412 |
if (nfds < 0) |
if (ret < 0) |
| 413 |
{ |
{ |
| 414 |
if (errno != EINTR) |
if (errno != EINTR) |
| 415 |
{ |
{ |
| 416 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 417 |
log_error("epoll_wait() error (%d)\n", errno); |
log_error("epoll_wait() error (%d)\n", errno); |
| 418 |
|
#else |
| 419 |
|
log_error("poll() error (%d)\n", errno); |
| 420 |
|
#endif |
| 421 |
break; |
break; |
| 422 |
} |
} |
| 423 |
continue; |
continue; |
| 424 |
} |
} |
| 425 |
else if (nfds == 0) // timeout |
else if (ret == 0) // timeout |
| 426 |
{ |
{ |
| 427 |
out = KEY_TIMEOUT; |
out = KEY_TIMEOUT; |
| 428 |
break; |
break; |
| 430 |
|
|
| 431 |
for (int i = 0; i < nfds; i++) |
for (int i = 0; i < nfds; i++) |
| 432 |
{ |
{ |
| 433 |
|
#ifdef HAVE_SYS_EPOLL_H |
| 434 |
if (events[i].data.fd == STDIN_FILENO) |
if (events[i].data.fd == STDIN_FILENO) |
| 435 |
|
#else |
| 436 |
|
if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN)) |
| 437 |
|
#endif |
| 438 |
{ |
{ |
| 439 |
stdin_read_wait = 1; |
stdin_read_wait = 1; |
| 440 |
} |
} |
| 517 |
#endif |
#endif |
| 518 |
} |
} |
| 519 |
|
|
|
fcntl(STDIN_FILENO, F_SETFL, flags); |
|
|
|
|
|
if (close(epollfd) < 0) |
|
|
{ |
|
|
log_error("close(epoll) error (%d)\n"); |
|
|
} |
|
|
|
|
| 520 |
if (stdin_buf_offset < stdin_buf_len) |
if (stdin_buf_offset < stdin_buf_len) |
| 521 |
{ |
{ |
| 522 |
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); |
| 1034 |
int ret; |
int ret; |
| 1035 |
int in_control = 0; |
int in_control = 0; |
| 1036 |
size_t i = 0; |
size_t i = 0; |
| 1037 |
|
int skip_current = 0; |
| 1038 |
|
|
| 1039 |
if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL) |
| 1040 |
{ |
{ |
| 1058 |
} |
} |
| 1059 |
} |
} |
| 1060 |
|
|
| 1061 |
if (in_control) |
if (in_control || skip_current) |
| 1062 |
{ |
{ |
| 1063 |
|
skip_current = 0; |
| 1064 |
|
|
| 1065 |
if (out_bytes <= 0) |
if (out_bytes <= 0) |
| 1066 |
{ |
{ |
| 1067 |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size); |
| 1075 |
out_bytes--; |
out_bytes--; |
| 1076 |
|
|
| 1077 |
(*p_buf_offset)++; |
(*p_buf_offset)++; |
| 1078 |
*p_conv_len = (int)(conv_size - out_bytes); |
(*p_conv_len)++; |
| 1079 |
|
|
| 1080 |
i++; |
i++; |
| 1081 |
if (i >= 2) |
if (i >= 2) |
| 1095 |
#endif |
#endif |
| 1096 |
if (p_buf != in_buf) |
if (p_buf != in_buf) |
| 1097 |
{ |
{ |
| 1098 |
*p_buf_len = (int)(p_buf + *p_buf_len - in_buf); |
*p_buf_len -= (int)(in_buf - p_buf); |
| 1099 |
*p_buf_offset = 0; |
*p_buf_offset = 0; |
| 1100 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
| 1101 |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
memmove(p_buf, in_buf, (size_t)(*p_buf_len)); |
| 1120 |
if (in_bytes == 0) |
if (in_bytes == 0) |
| 1121 |
{ |
{ |
| 1122 |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
in_bytes = (size_t)(*p_buf_len - *p_buf_offset); |
| 1123 |
|
#ifdef _DEBUG |
| 1124 |
|
log_error("Reset in_bytes from 0 to %d\n", in_bytes); |
| 1125 |
|
#endif |
| 1126 |
} |
} |
| 1127 |
|
|
| 1128 |
*out_buf = *in_buf; |
#ifdef _DEBUG |
| 1129 |
in_buf++; |
log_error("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n", |
| 1130 |
out_buf++; |
in_bytes, out_bytes, in_buf[0]); |
| 1131 |
in_bytes--; |
#endif |
| 1132 |
out_bytes--; |
skip_current = 1; |
| 1133 |
|
} |
| 1134 |
continue; |
else // something strange |
| 1135 |
|
{ |
| 1136 |
|
#ifdef _DEBUG |
| 1137 |
|
log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n", |
| 1138 |
|
in_bytes, out_bytes, errno, in_buf[0]); |
| 1139 |
|
#endif |
| 1140 |
|
*p_buf_offset = (int)(in_buf - p_buf); |
| 1141 |
|
*p_conv_len = (int)(conv_size - out_bytes); |
| 1142 |
|
skip_current = 1; |
| 1143 |
} |
} |
| 1144 |
} |
} |
| 1145 |
else |
else |
| 1146 |
{ |
{ |
| 1147 |
*p_buf_len = 0; |
*p_buf_offset = (int)(in_buf - p_buf); |
|
*p_buf_offset = 0; |
|
| 1148 |
*p_conv_len = (int)(conv_size - out_bytes); |
*p_conv_len = (int)(conv_size - out_bytes); |
|
|
|
|
break; |
|
| 1149 |
} |
} |
| 1150 |
} |
} |
| 1151 |
|
|
| 1152 |
|
if (*p_buf_offset >= *p_buf_len) |
| 1153 |
|
{ |
| 1154 |
|
*p_buf_len = 0; |
| 1155 |
|
*p_buf_offset = 0; |
| 1156 |
|
} |
| 1157 |
|
|
| 1158 |
return 0; |
return 0; |
| 1159 |
} |
} |
| 1160 |
|
|
| 1161 |
int io_conv_init(const char *charset) |
int io_conv_init(const char *charset) |
| 1162 |
{ |
{ |
| 1163 |
char tocode[32]; |
char tocode[CHARSET_MAX_LEN + 20]; |
| 1164 |
|
|
| 1165 |
if (charset == NULL) |
if (charset == NULL) |
| 1166 |
{ |
{ |
| 1173 |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1); |
| 1174 |
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
stdio_charset[sizeof(stdio_charset) - 1] = '\0'; |
| 1175 |
|
|
| 1176 |
snprintf(tocode, sizeof(tocode), "%s%s", BBS_DEFAULT_CHARSET, |
snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset, |
| 1177 |
(strcasecmp(stdio_charset, BBS_DEFAULT_CHARSET) == 0 ? "" : "//IGNORE")); |
(strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE")); |
| 1178 |
stdin_cd = iconv_open(tocode, stdio_charset); |
stdin_cd = iconv_open(tocode, stdio_charset); |
| 1179 |
if (stdin_cd == (iconv_t)(-1)) |
if (stdin_cd == (iconv_t)(-1)) |
| 1180 |
{ |
{ |
| 1183 |
} |
} |
| 1184 |
|
|
| 1185 |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 1186 |
(strcasecmp(BBS_DEFAULT_CHARSET, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
(strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 1187 |
stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET); |
stdout_cd = iconv_open(tocode, BBS_default_charset); |
| 1188 |
if (stdout_cd == (iconv_t)(-1)) |
if (stdout_cd == (iconv_t)(-1)) |
| 1189 |
{ |
{ |
| 1190 |
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); |
| 1191 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1192 |
|
stdin_cd = (iconv_t)(-1); |
| 1193 |
return -2; |
return -2; |
| 1194 |
} |
} |
| 1195 |
|
|
| 1198 |
|
|
| 1199 |
int io_conv_cleanup(void) |
int io_conv_cleanup(void) |
| 1200 |
{ |
{ |
| 1201 |
if (stdin_cd != NULL) |
if (stdin_cd != (iconv_t)(-1)) |
| 1202 |
{ |
{ |
| 1203 |
iconv_close(stdin_cd); |
iconv_close(stdin_cd); |
| 1204 |
stdin_cd = NULL; |
stdin_cd = (iconv_t)(-1); |
| 1205 |
} |
} |
| 1206 |
if (stdout_cd != NULL) |
if (stdout_cd != (iconv_t)(-1)) |
| 1207 |
{ |
{ |
| 1208 |
iconv_close(stdout_cd); |
iconv_close(stdout_cd); |
| 1209 |
stdout_cd = NULL; |
stdout_cd = (iconv_t)(-1); |
| 1210 |
} |
} |
| 1211 |
|
|
| 1212 |
return 0; |
return 0; |