/[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.55 by sysadm, Sat Oct 18 05:02:15 2025 UTC Revision 1.64 by sysadm, Wed Nov 5 02:48:48 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     */
 /***************************************************************************  
  *                                                                         *  
  *   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.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "common.h"  #include "common.h"
10  #include "io.h"  #include "io.h"
# Line 31  Line 23 
23  #include <libssh/libssh.h>  #include <libssh/libssh.h>
24  #include <libssh/server.h>  #include <libssh/server.h>
25    
26  char stdio_charset[20] = BBS_DEFAULT_CHARSET;  const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
27    char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
28    
29  // static input / output buffer  // static input / output buffer
30  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
31  static char stdin_conv[LINE_BUFFER_LEN];  static char stdout_buf[BUFSIZ];
32  static int stdin_buf_len = 0;  static int stdin_buf_len = 0;
33  static int stdin_conv_len = 0;  static int stdout_buf_len = 0;
34  static int stdin_buf_offset = 0;  static int stdin_buf_offset = 0;
35  static int stdin_conv_offset = 0;  static int stdout_buf_offset = 0;
36    
37  static char stdout_buf[BUFSIZ];  static char stdin_conv[LINE_BUFFER_LEN * 2];
38  static char stdout_conv[BUFSIZ];  static char stdout_conv[BUFSIZ * 2];
39  static int stdout_buf_len = 0;  static int stdin_conv_len = 0;
40  static int stdout_conv_len = 0;  static int stdout_conv_len = 0;
41  static int stdout_buf_offset = 0;  static int stdin_conv_offset = 0;
42  static int stdout_conv_offset = 0;  static int stdout_conv_offset = 0;
43    
44  static iconv_t stdin_cd = NULL;  static iconv_t stdin_cd = NULL;
# Line 170  int iflush(void) Line 163  int iflush(void)
163                                          if (ret < 0)                                          if (ret < 0)
164                                          {                                          {
165                                                  log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);                                                  log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
166                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
167                                          }                                          }
168                                  }                                  }
169    
# Line 244  int iflush(void) Line 238  int iflush(void)
238    
239  int igetch(int timeout)  int igetch(int timeout)
240  {  {
241            static int stdin_read_wait = 0;
242    
243          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
244          int nfds, epollfd;          int nfds, epollfd;
245          int ret;          int ret;
# Line 285  int igetch(int timeout) Line 281  int igetch(int timeout)
281                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
282                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
283    
284                  for (loop = 1; loop && 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;)
285                  {                  {
286                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
287                          {                          {
# Line 294  int igetch(int timeout) Line 290  int igetch(int timeout)
290                                  break;                                  break;
291                          }                          }
292    
293                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
294                          {                          {
295                                  if (errno != EINTR)                                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
296    
297                                    if (nfds < 0)
298                                  {                                  {
299                                          log_error("epoll_wait() error (%d)\n", errno);                                          if (errno != EINTR)
300                                            {
301                                                    log_error("epoll_wait() error (%d)\n", errno);
302                                                    break;
303                                            }
304                                            continue;
305                                    }
306                                    else if (nfds == 0) // timeout
307                                    {
308                                            out = KEY_TIMEOUT;
309                                          break;                                          break;
310                                  }                                  }
311                                  continue;  
312                          }                                  for (int i = 0; i < nfds; i++)
313                          else if (nfds == 0) // timeout                                  {
314                          {                                          if (events[i].data.fd == STDIN_FILENO)
315                                  out = KEY_TIMEOUT;                                          {
316                                  break;                                                  stdin_read_wait = 1;
317                                            }
318                                    }
319                          }                          }
320    
321                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
322                          {                          {
323                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
324                                  {                                  {
325                                          while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
326                                          {                                          {
327                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
328                                                    if (ret == SSH_ERROR)
329                                                  {                                                  {
330                                                          ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
331                                                          if (ret == SSH_ERROR)                                                          loop = 0;
332                                                          {                                                          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;  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
333                                                  }                                                  }
334                                                  else                                                  else if (ret == SSH_EOF)
335                                                  {                                                  {
336                                                          ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);                                                          stdin_read_wait = 0;
337                                                            loop = 0;
338                                                            break;
339                                                  }                                                  }
340                                                  if (ret < 0)                                                  else if (ret == 0)
341                                                  {                                                  {
342                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
343                                                          {                                                          stdin_read_wait = 0;
344                                                                  out = 0;                                                          loop = 0;
345                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
346                                                  }                                                  }
347                                                  else if (ret == 0) // broken pipe                                          }
348                                            else
349                                            {
350                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
351                                            }
352                                            if (ret < 0)
353                                            {
354                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
355                                                  {                                                  {
356                                                            out = 0;
357                                                            stdin_read_wait = 0;
358                                                          loop = 0;                                                          loop = 0;
359                                                          break;                                                          break;
360                                                  }                                                  }
361                                                  else                                                  else if (errno == EINTR)
362                                                  {                                                  {
                                                         stdin_buf_len += ret;  
363                                                          continue;                                                          continue;
364                                                  }                                                  }
365                                                    else
366                                                    {
367    #ifdef _DEBUG
368                                                            log_error("read(STDIN) error (%d)\n", errno);
369    #endif
370                                                            loop = 0;
371                                                            break;
372                                                    }
373                                            }
374                                            else if (ret == 0) // broken pipe
375                                            {
376                                                    stdin_read_wait = 0;
377                                                    loop = 0;
378                                                    break;
379                                            }
380                                            else
381                                            {
382                                                    stdin_buf_len += ret;
383                                                    continue;
384                                          }                                          }
385                                  }                                  }
386                          }                          }
# Line 381  int igetch(int timeout) Line 389  int igetch(int timeout)
389  #ifdef _DEBUG  #ifdef _DEBUG
390                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
391                          {                          {
392                                  log_common("Debug: <--[%u]\n", (stdin_buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
393                          }                          }
394  #endif  #endif
395                  }                  }
# Line 399  int igetch(int timeout) Line 407  int igetch(int timeout)
407                          if (ret < 0)                          if (ret < 0)
408                          {                          {
409                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
410                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
411                          }                          }
412    
413                            // For debug
414    #ifdef _DEBUG
415                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
416                            {
417                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
418                            }
419    #endif
420                  }                  }
421          }          }
422    
# Line 863  int igetch(int timeout) Line 880  int igetch(int timeout)
880  #ifdef _DEBUG  #ifdef _DEBUG
881          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
882          {          {
883                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
884          }          }
885  #endif  #endif
886    
# Line 899  int io_buf_conv(iconv_t cd, char *p_buf, Line 916  int io_buf_conv(iconv_t cd, char *p_buf,
916          size_t in_bytes;          size_t in_bytes;
917          size_t out_bytes;          size_t out_bytes;
918          int ret;          int ret;
919            int in_control = 0;
920            size_t i = 0;
921    
922          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)
923          {          {
# Line 913  int io_buf_conv(iconv_t cd, char *p_buf, Line 932  int io_buf_conv(iconv_t cd, char *p_buf,
932    
933          while (in_bytes > 0)          while (in_bytes > 0)
934          {          {
935                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
936                    {
937                            if (in_control == 0)
938                            {
939                                    in_control = 1;
940                                    i = 0;
941                            }
942                    }
943    
944                    if (in_control)
945                    {
946                            if (out_bytes <= 0)
947                            {
948                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
949                                    return -2;
950                            }
951    
952                            *out_buf = *in_buf;
953                            in_buf++;
954                            out_buf++;
955                            in_bytes--;
956                            out_bytes--;
957    
958                            (*p_buf_offset)++;
959                            *p_conv_len = (int)(conv_size - out_bytes);
960    
961                            i++;
962                            if (i >= 2)
963                            {
964                                    in_control = 0;
965                            }
966                            continue;
967                    }
968    
969                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
970                  if (ret == -1)                  if (ret == -1)
971                  {                  {
972                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
973                          {                          {
974  #ifdef _DEBUG  #ifdef _DEBUG
975                                  log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);                                  log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
976  #endif  #endif
977                                  *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                  if (p_buf != in_buf)
978                                  *p_buf_offset = 0;                                  {
979                                  *p_conv_len = (int)(conv_size - out_bytes);                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
980                                  memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          *p_buf_offset = 0;
981                                            *p_conv_len = (int)(conv_size - out_bytes);
982                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
983                                    }
984    
985                                  break;                                  break;
986                          }                          }
# Line 941  int io_buf_conv(iconv_t cd, char *p_buf, Line 997  int io_buf_conv(iconv_t cd, char *p_buf,
997                                          return -2;                                          return -2;
998                                  }                                  }
999    
1000                                    // reset in_bytes when "//IGNORE" is applied
1001                                    if (in_bytes == 0)
1002                                    {
1003                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1004                                    }
1005    
1006                                  *out_buf = *in_buf;                                  *out_buf = *in_buf;
1007                                  in_buf++;                                  in_buf++;
1008                                  out_buf++;                                  out_buf++;
# Line 965  int io_buf_conv(iconv_t cd, char *p_buf, Line 1027  int io_buf_conv(iconv_t cd, char *p_buf,
1027    
1028  int io_conv_init(const char *charset)  int io_conv_init(const char *charset)
1029  {  {
1030            char tocode[CHARSET_MAX_LEN + 20];
1031    
1032          if (charset == NULL)          if (charset == NULL)
1033          {          {
1034                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
# Line 976  int io_conv_init(const char *charset) Line 1040  int io_conv_init(const char *charset)
1040          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1041          stdio_charset[sizeof(stdio_charset) - 1] = '\0';          stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1042    
1043          stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset);          snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1044                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1045            stdin_cd = iconv_open(tocode, stdio_charset);
1046          if (stdin_cd == (iconv_t)(-1))          if (stdin_cd == (iconv_t)(-1))
1047          {          {
1048                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1049                  return -2;                  return -2;
1050          }          }
1051          stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET);  
1052            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1053                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1054            stdout_cd = iconv_open(tocode, BBS_default_charset);
1055          if (stdout_cd == (iconv_t)(-1))          if (stdout_cd == (iconv_t)(-1))
1056          {          {
1057                  log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno);                  log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1058                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1059                  return -2;                  return -2;
1060          }          }


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

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