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


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

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