/[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.50 by sysadm, Sat Jun 28 11:31:17 2025 UTC Revision 1.60 by sysadm, Mon Oct 20 01:55:50 2025 UTC
# Line 31  Line 31 
31  #include <libssh/libssh.h>  #include <libssh/libssh.h>
32  #include <libssh/server.h>  #include <libssh/server.h>
33    
34    char stdio_charset[20] = BBS_DEFAULT_CHARSET;
35    
36    // static input / output buffer
37    static char stdin_buf[LINE_BUFFER_LEN];
38  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
39    static int stdin_buf_len = 0;
40  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
41    static int stdin_buf_offset = 0;
42  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
43    
44    static char stdin_conv[LINE_BUFFER_LEN * 2];
45    static char stdout_conv[BUFSIZ * 2];
46    static int stdin_conv_len = 0;
47    static int stdout_conv_len = 0;
48    static int stdin_conv_offset = 0;
49    static int stdout_conv_offset = 0;
50    
51    static iconv_t stdin_cd = NULL;
52    static iconv_t stdout_cd = NULL;
53    
54  int prints(const char *format, ...)  int prints(const char *format, ...)
55  {  {
56          char buf[BUFSIZ];          char buf[BUFSIZ];
# Line 148  int iflush(void) Line 164  int iflush(void)
164                  {                  {
165                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
166                          {                          {
167                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
168                                    {
169                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
170                                            if (ret < 0)
171                                            {
172                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
173                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
174                                            }
175                                    }
176    
177                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
178                                  {                                  {
179                                          if (SSH_v2)                                          if (SSH_v2)
180                                          {                                          {
181                                                  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));
182                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
183                                                  {                                                  {
184                                                          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 188  int iflush(void)
188                                          }                                          }
189                                          else                                          else
190                                          {                                          {
191                                                  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));
192                                          }                                          }
193                                          if (ret < 0)                                          if (ret < 0)
194                                          {                                          {
# Line 190  int iflush(void) Line 216  int iflush(void)
216                                          }                                          }
217                                          else                                          else
218                                          {                                          {
219                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
220                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
221                                                  {                                                  {
222                                                          ret = 0;                                                          ret = 0;
223                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
224                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
225                                                          retry = 0;                                                          retry = 0;
226                                                          break;                                                          break;
227                                                  }                                                  }
# Line 219  int iflush(void) Line 245  int iflush(void)
245    
246  int igetch(int timeout)  int igetch(int timeout)
247  {  {
         // static input buffer  
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
   
248          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
249          int nfds, epollfd;          int nfds, epollfd;
250          int ret;          int ret;
# Line 237  int igetch(int timeout) Line 258  int igetch(int timeout)
258          int i = 0;          int i = 0;
259          int flags;          int flags;
260    
261          if (pos >= len)          if (stdin_conv_offset >= stdin_conv_len)
262          {          {
263                  len = 0;                  stdin_conv_len = 0;
264                  pos = 0;                  stdin_conv_offset = 0;
265    
266                  epollfd = epoll_create1(0);                  epollfd = epoll_create1(0);
267                  if (epollfd < 0)                  if (epollfd < 0)
# Line 265  int igetch(int timeout) Line 286  int igetch(int timeout)
286                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
287                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
288    
289                  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;)
290                  {                  {
291                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
292                          {                          {
# Line 295  int igetch(int timeout) Line 316  int igetch(int timeout)
316                          {                          {
317                                  if (events[i].data.fd == STDIN_FILENO)                                  if (events[i].data.fd == STDIN_FILENO)
318                                  {                                  {
319                                          while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                          while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
320                                          {                                          {
321                                                  if (SSH_v2)                                                  if (SSH_v2)
322                                                  {                                                  {
323                                                          ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);                                                          ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
324                                                          if (ret == SSH_ERROR)                                                          if (ret == SSH_ERROR)
325                                                          {                                                          {
326                                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                                  log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
# Line 314  int igetch(int timeout) Line 335  int igetch(int timeout)
335                                                          else if (ret == 0)                                                          else if (ret == 0)
336                                                          {                                                          {
337                                                                  out = 0;                                                                  out = 0;
338                                                                  break; // Check whether channel is still open                                                                  loop = 0;
339                                                                    break;
340                                                          }                                                          }
341                                                  }                                                  }
342                                                  else                                                  else
343                                                  {                                                  {
344                                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                                          ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
345                                                  }                                                  }
346                                                  if (ret < 0)                                                  if (ret < 0)
347                                                  {                                                  {
# Line 349  int igetch(int timeout) Line 371  int igetch(int timeout)
371                                                  }                                                  }
372                                                  else                                                  else
373                                                  {                                                  {
374                                                          len += ret;                                                          stdin_buf_len += ret;
375                                                          continue;                                                          continue;
376                                                  }                                                  }
377                                          }                                          }
# Line 358  int igetch(int timeout) Line 380  int igetch(int timeout)
380    
381                          // For debug                          // For debug
382  #ifdef _DEBUG  #ifdef _DEBUG
383                          for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
384                          {                          {
385                                  log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
386                          }                          }
387  #endif  #endif
388                  }                  }
# Line 371  int igetch(int timeout) Line 393  int igetch(int timeout)
393                  {                  {
394                          log_error("close(epoll) error (%d)\n");                          log_error("close(epoll) error (%d)\n");
395                  }                  }
396    
397                    if (stdin_buf_offset < stdin_buf_len)
398                    {
399                            ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
400                            if (ret < 0)
401                            {
402                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
403                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
404                            }
405    
406                            // For debug
407    #ifdef _DEBUG
408                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
409                            {
410                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
411                            }
412    #endif
413                    }
414          }          }
415    
416          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
417          {          {
418                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
419    
420                    // Convert \r\n to \r
421                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
422                    {
423                            stdin_conv_offset++;
424                    }
425    
426                    // Convert single \n to \r
427                    if (c == LF)
428                    {
429                            c = CR;
430                    }
431    
432                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
433                  {                  {
# Line 817  int igetch(int timeout) Line 869  int igetch(int timeout)
869                  out = KEY_ESC;                  out = KEY_ESC;
870          }          }
871    
         // Convert LF to CR -- Cterm send LF without CR  
         if (out == LF)  
         {  
                 out = CR;  
         }  
   
872          // for debug          // for debug
873  #ifdef _DEBUG  #ifdef _DEBUG
874          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
875          {          {
876                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
877          }          }
878  #endif  #endif
879    
# Line 852  void igetch_reset() Line 898  void igetch_reset()
898          int ch;          int ch;
899          do          do
900          {          {
901                  ch = igetch(0);                  ch = igetch(100);
902          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
903  }  }
904    
905    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)
906    {
907            char *in_buf;
908            char *out_buf;
909            size_t in_bytes;
910            size_t out_bytes;
911            int ret;
912            int in_control = 0;
913            size_t i = 0;
914    
915            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
916            {
917                    log_error("NULL pointer error\n");
918                    return -1;
919            }
920    
921            in_buf = p_buf + *p_buf_offset;
922            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
923            out_buf = p_conv + *p_conv_len;
924            out_bytes = conv_size - (size_t)(*p_conv_len);
925    
926            while (in_bytes > 0)
927            {
928                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
929                    {
930                            if (in_control == 0)
931                            {
932                                    in_control = 1;
933                                    i = 0;
934                            }
935                    }
936    
937                    if (in_control)
938                    {
939                            if (out_bytes <= 0)
940                            {
941                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
942                                    return -2;
943                            }
944    
945                            *out_buf = *in_buf;
946                            in_buf++;
947                            out_buf++;
948                            in_bytes--;
949                            out_bytes--;
950    
951                            (*p_buf_offset)++;
952                            *p_conv_len = (int)(conv_size - out_bytes);
953    
954                            i++;
955                            if (i >= 2)
956                            {
957                                    in_control = 0;
958                            }
959                            continue;
960                    }
961    
962                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
963                    if (ret == -1)
964                    {
965                            if (errno == EINVAL) // Incomplete
966                            {
967    #ifdef _DEBUG
968                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
969    #endif
970                                    if (p_buf != in_buf)
971                                    {
972                                            *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
973                                            *p_buf_offset = 0;
974                                            *p_conv_len = (int)(conv_size - out_bytes);
975                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
976                                    }
977    
978                                    break;
979                            }
980                            else if (errno == E2BIG)
981                            {
982                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
983                                    return -1;
984                            }
985                            else if (errno == EILSEQ)
986                            {
987                                    if (in_bytes > out_bytes || out_bytes <= 0)
988                                    {
989                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
990                                            return -2;
991                                    }
992    
993                                    // reset in_bytes when "//IGNORE" is applied
994                                    if (in_bytes == 0)
995                                    {
996                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
997                                    }
998    
999                                    *out_buf = *in_buf;
1000                                    in_buf++;
1001                                    out_buf++;
1002                                    in_bytes--;
1003                                    out_bytes--;
1004    
1005                                    continue;
1006                            }
1007                    }
1008                    else
1009                    {
1010                            *p_buf_len = 0;
1011                            *p_buf_offset = 0;
1012                            *p_conv_len = (int)(conv_size - out_bytes);
1013    
1014                            break;
1015                    }
1016            }
1017    
1018            return 0;
1019    }
1020    
1021    int io_conv_init(const char *charset)
1022    {
1023            char tocode[32];
1024    
1025            if (charset == NULL)
1026            {
1027                    log_error("NULL pointer error\n");
1028                    return -1;
1029            }
1030    
1031            io_conv_cleanup();
1032    
1033            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1034            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1035    
1036            snprintf(tocode, sizeof(tocode), "%s%s", BBS_DEFAULT_CHARSET,
1037                             (strcasecmp(stdio_charset, BBS_DEFAULT_CHARSET) == 0 ? "" : "//IGNORE"));
1038            stdin_cd = iconv_open(tocode, stdio_charset);
1039            if (stdin_cd == (iconv_t)(-1))
1040            {
1041                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1042                    return -2;
1043            }
1044    
1045            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1046                             (strcasecmp(BBS_DEFAULT_CHARSET, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1047            stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);
1048            if (stdout_cd == (iconv_t)(-1))
1049            {
1050                    log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno);
1051                    iconv_close(stdin_cd);
1052                    return -2;
1053            }
1054    
1055            return 0;
1056    }
1057    
1058    int io_conv_cleanup(void)
1059    {
1060            if (stdin_cd != NULL)
1061            {
1062                    iconv_close(stdin_cd);
1063                    stdin_cd = NULL;
1064            }
1065            if (stdout_cd != NULL)
1066            {
1067                    iconv_close(stdout_cd);
1068                    stdout_cd = NULL;
1069            }
1070    
1071            return 0;
1072    }


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

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