/[LeafOK_CVS]/lbbs/src/io.c
ViewVC logotype

Diff of /lbbs/src/io.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.51 by sysadm, Wed Jul 2 09:07:35 2025 UTC Revision 1.65 by sysadm, Tue Nov 11 00:28:05 2025 UTC
# Line 1  Line 1 
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"
# Line 31  Line 27 
27  #include <libssh/libssh.h>  #include <libssh/libssh.h>
28  #include <libssh/server.h>  #include <libssh/server.h>
29    
30    const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
31    char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
32    
33    // static input / output buffer
34    static char stdin_buf[LINE_BUFFER_LEN];
35  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
36    static int stdin_buf_len = 0;
37  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
38    static int stdin_buf_offset = 0;
39  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
40    
41    static char stdin_conv[LINE_BUFFER_LEN * 2];
42    static char stdout_conv[BUFSIZ * 2];
43    static int stdin_conv_len = 0;
44    static int stdout_conv_len = 0;
45    static int stdin_conv_offset = 0;
46    static int stdout_conv_offset = 0;
47    
48    static iconv_t stdin_cd = NULL;
49    static iconv_t stdout_cd = NULL;
50    
51  int prints(const char *format, ...)  int prints(const char *format, ...)
52  {  {
53          char buf[BUFSIZ];          char buf[BUFSIZ];
# Line 148  int iflush(void) Line 161  int iflush(void)
161                  {                  {
162                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
163                          {                          {
164                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
165                                    {
166                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
167                                            if (ret < 0)
168                                            {
169                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
170                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
171                                            }
172                                    }
173    
174                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
175                                  {                                  {
176                                          if (SSH_v2)                                          if (SSH_v2)
177                                          {                                          {
178                                                  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));
179                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
180                                                  {                                                  {
181                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
# Line 162  int iflush(void) Line 185  int iflush(void)
185                                          }                                          }
186                                          else                                          else
187                                          {                                          {
188                                                  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));
189                                          }                                          }
190                                          if (ret < 0)                                          if (ret < 0)
191                                          {                                          {
# Line 190  int iflush(void) Line 213  int iflush(void)
213                                          }                                          }
214                                          else                                          else
215                                          {                                          {
216                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
217                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
218                                                  {                                                  {
219                                                          ret = 0;                                                          ret = 0;
220                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
221                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
222                                                          retry = 0;                                                          retry = 0;
223                                                          break;                                                          break;
224                                                  }                                                  }
# Line 219  int iflush(void) Line 242  int iflush(void)
242    
243  int igetch(int timeout)  int igetch(int timeout)
244  {  {
245          // static input buffer          static int stdin_read_wait = 0;
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
246    
247          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
248          int nfds, epollfd;          int nfds, epollfd;
# Line 237  int igetch(int timeout) Line 257  int igetch(int timeout)
257          int i = 0;          int i = 0;
258          int flags;          int flags;
259    
260          if (pos >= len)          if (stdin_conv_offset >= stdin_conv_len)
261          {          {
262                  len = 0;                  stdin_conv_len = 0;
263                  pos = 0;                  stdin_conv_offset = 0;
264    
265                  epollfd = epoll_create1(0);                  epollfd = epoll_create1(0);
266                  if (epollfd < 0)                  if (epollfd < 0)
# Line 265  int igetch(int timeout) Line 285  int igetch(int timeout)
285                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
286                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
287    
288                  for (loop = 1; loop && pos >= len && !SYS_server_exit;)                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
289                  {                  {
290                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
291                          {                          {
# Line 274  int igetch(int timeout) Line 294  int igetch(int timeout)
294                                  break;                                  break;
295                          }                          }
296    
297                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
298                          {                          {
299                                  if (errno != EINTR)                                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
300    
301                                    if (nfds < 0)
302                                    {
303                                            if (errno != EINTR)
304                                            {
305                                                    log_error("epoll_wait() error (%d)\n", errno);
306                                                    break;
307                                            }
308                                            continue;
309                                    }
310                                    else if (nfds == 0) // timeout
311                                  {                                  {
312                                          log_error("epoll_wait() error (%d)\n", errno);                                          out = KEY_TIMEOUT;
313                                          break;                                          break;
314                                  }                                  }
315                                  continue;  
316                          }                                  for (int i = 0; i < nfds; i++)
317                          else if (nfds == 0) // timeout                                  {
318                          {                                          if (events[i].data.fd == STDIN_FILENO)
319                                  out = KEY_TIMEOUT;                                          {
320                                  break;                                                  stdin_read_wait = 1;
321                                            }
322                                    }
323                          }                          }
324    
325                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
326                          {                          {
327                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
328                                  {                                  {
329                                          while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
330                                          {                                          {
331                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
332                                                    if (ret == SSH_ERROR)
333                                                  {                                                  {
334                                                          ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
335                                                          if (ret == SSH_ERROR)                                                          loop = 0;
336                                                          {                                                          break;
                                                                 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
                                                         else if (ret == SSH_EOF)  
                                                         {  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
                                                         else if (ret == 0)  
                                                         {  
                                                                 out = 0;  
                                                                 break; // Check whether channel is still open  
                                                         }  
337                                                  }                                                  }
338                                                  else                                                  else if (ret == SSH_EOF)
339                                                  {                                                  {
340                                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                                          stdin_read_wait = 0;
341                                                            loop = 0;
342                                                            break;
343                                                  }                                                  }
344                                                  if (ret < 0)                                                  else if (ret == 0)
345                                                  {                                                  {
346                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
347                                                          {                                                          stdin_read_wait = 0;
348                                                                  out = 0;                                                          loop = 0;
349                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
350                                                  }                                                  }
351                                                  else if (ret == 0) // broken pipe                                          }
352                                            else
353                                            {
354                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
355                                            }
356                                            if (ret < 0)
357                                            {
358                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
359                                                  {                                                  {
360                                                            out = 0;
361                                                            stdin_read_wait = 0;
362                                                          loop = 0;                                                          loop = 0;
363                                                          break;                                                          break;
364                                                  }                                                  }
365                                                  else                                                  else if (errno == EINTR)
366                                                  {                                                  {
                                                         len += ret;  
367                                                          continue;                                                          continue;
368                                                  }                                                  }
369                                                    else
370                                                    {
371    #ifdef _DEBUG
372                                                            log_error("read(STDIN) error (%d)\n", errno);
373    #endif
374                                                            loop = 0;
375                                                            break;
376                                                    }
377                                            }
378                                            else if (ret == 0) // broken pipe
379                                            {
380                                                    stdin_read_wait = 0;
381                                                    loop = 0;
382                                                    break;
383                                            }
384                                            else
385                                            {
386                                                    stdin_buf_len += ret;
387                                                    continue;
388                                          }                                          }
389                                  }                                  }
390                          }                          }
391    
392                          // For debug                          // For debug
393  #ifdef _DEBUG  #ifdef _DEBUG
394                          for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
395                          {                          {
396                                  log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
397                          }                          }
398  #endif  #endif
399                  }                  }
# Line 371  int igetch(int timeout) Line 404  int igetch(int timeout)
404                  {                  {
405                          log_error("close(epoll) error (%d)\n");                          log_error("close(epoll) error (%d)\n");
406                  }                  }
407    
408                    if (stdin_buf_offset < stdin_buf_len)
409                    {
410                            ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
411                            if (ret < 0)
412                            {
413                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
414                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
415                            }
416    
417                            // For debug
418    #ifdef _DEBUG
419                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
420                            {
421                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
422                            }
423    #endif
424                    }
425          }          }
426    
427          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
428          {          {
429                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
430    
431                  // Convert \r\n to \r                  // Convert \r\n to \r
432                  if (c == CR && pos < len && buf[pos] == LF)                  if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
433                  {                  {
434                          pos++;                          stdin_conv_offset++;
435                  }                  }
436    
437                  // Convert single \n to \r                  // Convert single \n to \r
# Line 833  int igetch(int timeout) Line 884  int igetch(int timeout)
884  #ifdef _DEBUG  #ifdef _DEBUG
885          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
886          {          {
887                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
888          }          }
889  #endif  #endif
890    
# Line 858  void igetch_reset() Line 909  void igetch_reset()
909          int ch;          int ch;
910          do          do
911          {          {
912                  ch = igetch(0);                  ch = igetch(100);
913          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
914  }  }
915    
916    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)
917    {
918            char *in_buf;
919            char *out_buf;
920            size_t in_bytes;
921            size_t out_bytes;
922            int ret;
923            int in_control = 0;
924            size_t i = 0;
925    
926            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
927            {
928                    log_error("NULL pointer error\n");
929                    return -1;
930            }
931    
932            in_buf = p_buf + *p_buf_offset;
933            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
934            out_buf = p_conv + *p_conv_len;
935            out_bytes = conv_size - (size_t)(*p_conv_len);
936    
937            while (in_bytes > 0)
938            {
939                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
940                    {
941                            if (in_control == 0)
942                            {
943                                    in_control = 1;
944                                    i = 0;
945                            }
946                    }
947    
948                    if (in_control)
949                    {
950                            if (out_bytes <= 0)
951                            {
952                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
953                                    return -2;
954                            }
955    
956                            *out_buf = *in_buf;
957                            in_buf++;
958                            out_buf++;
959                            in_bytes--;
960                            out_bytes--;
961    
962                            (*p_buf_offset)++;
963                            *p_conv_len = (int)(conv_size - out_bytes);
964    
965                            i++;
966                            if (i >= 2)
967                            {
968                                    in_control = 0;
969                            }
970                            continue;
971                    }
972    
973                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
974                    if (ret == -1)
975                    {
976                            if (errno == EINVAL) // Incomplete
977                            {
978    #ifdef _DEBUG
979                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
980    #endif
981                                    if (p_buf != in_buf)
982                                    {
983                                            *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
984                                            *p_buf_offset = 0;
985                                            *p_conv_len = (int)(conv_size - out_bytes);
986                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
987                                    }
988    
989                                    break;
990                            }
991                            else if (errno == E2BIG)
992                            {
993                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
994                                    return -1;
995                            }
996                            else if (errno == EILSEQ)
997                            {
998                                    if (in_bytes > out_bytes || out_bytes <= 0)
999                                    {
1000                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1001                                            return -2;
1002                                    }
1003    
1004                                    // reset in_bytes when "//IGNORE" is applied
1005                                    if (in_bytes == 0)
1006                                    {
1007                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1008                                    }
1009    
1010                                    *out_buf = *in_buf;
1011                                    in_buf++;
1012                                    out_buf++;
1013                                    in_bytes--;
1014                                    out_bytes--;
1015    
1016                                    continue;
1017                            }
1018                    }
1019                    else
1020                    {
1021                            *p_buf_len = 0;
1022                            *p_buf_offset = 0;
1023                            *p_conv_len = (int)(conv_size - out_bytes);
1024    
1025                            break;
1026                    }
1027            }
1028    
1029            return 0;
1030    }
1031    
1032    int io_conv_init(const char *charset)
1033    {
1034            char tocode[CHARSET_MAX_LEN + 20];
1035    
1036            if (charset == NULL)
1037            {
1038                    log_error("NULL pointer error\n");
1039                    return -1;
1040            }
1041    
1042            io_conv_cleanup();
1043    
1044            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1045            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1046    
1047            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1048                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1049            stdin_cd = iconv_open(tocode, stdio_charset);
1050            if (stdin_cd == (iconv_t)(-1))
1051            {
1052                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1053                    return -2;
1054            }
1055    
1056            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1057                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1058            stdout_cd = iconv_open(tocode, BBS_default_charset);
1059            if (stdout_cd == (iconv_t)(-1))
1060            {
1061                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1062                    iconv_close(stdin_cd);
1063                    return -2;
1064            }
1065    
1066            return 0;
1067    }
1068    
1069    int io_conv_cleanup(void)
1070    {
1071            if (stdin_cd != NULL)
1072            {
1073                    iconv_close(stdin_cd);
1074                    stdin_cd = NULL;
1075            }
1076            if (stdout_cd != NULL)
1077            {
1078                    iconv_close(stdout_cd);
1079                    stdout_cd = NULL;
1080            }
1081    
1082            return 0;
1083    }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1