/[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.59 by sysadm, Sun Oct 19 13:13:07 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                                            }
174                                    }
175    
176                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
177                                  {                                  {
178                                          if (SSH_v2)                                          if (SSH_v2)
179                                          {                                          {
180                                                  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));
181                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
182                                                  {                                                  {
183                                                          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 187  int iflush(void)
187                                          }                                          }
188                                          else                                          else
189                                          {                                          {
190                                                  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));
191                                          }                                          }
192                                          if (ret < 0)                                          if (ret < 0)
193                                          {                                          {
# Line 190  int iflush(void) Line 215  int iflush(void)
215                                          }                                          }
216                                          else                                          else
217                                          {                                          {
218                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
219                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
220                                                  {                                                  {
221                                                          ret = 0;                                                          ret = 0;
222                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
223                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
224                                                          retry = 0;                                                          retry = 0;
225                                                          break;                                                          break;
226                                                  }                                                  }
# Line 219  int iflush(void) Line 244  int iflush(void)
244    
245  int igetch(int timeout)  int igetch(int timeout)
246  {  {
         // static input buffer  
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
   
247          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
248          int nfds, epollfd;          int nfds, epollfd;
249          int ret;          int ret;
# 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 295  int igetch(int timeout) Line 315  int igetch(int timeout)
315                          {                          {
316                                  if (events[i].data.fd == STDIN_FILENO)                                  if (events[i].data.fd == STDIN_FILENO)
317                                  {                                  {
318                                          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
319                                          {                                          {
320                                                  if (SSH_v2)                                                  if (SSH_v2)
321                                                  {                                                  {
322                                                          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);
323                                                          if (ret == SSH_ERROR)                                                          if (ret == SSH_ERROR)
324                                                          {                                                          {
325                                                                  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 334  int igetch(int timeout)
334                                                          else if (ret == 0)                                                          else if (ret == 0)
335                                                          {                                                          {
336                                                                  out = 0;                                                                  out = 0;
337                                                                  break; // Check whether channel is still open                                                                  loop = 0;
338                                                                    break;
339                                                          }                                                          }
340                                                  }                                                  }
341                                                  else                                                  else
342                                                  {                                                  {
343                                                          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);
344                                                  }                                                  }
345                                                  if (ret < 0)                                                  if (ret < 0)
346                                                  {                                                  {
# Line 349  int igetch(int timeout) Line 370  int igetch(int timeout)
370                                                  }                                                  }
371                                                  else                                                  else
372                                                  {                                                  {
373                                                          len += ret;                                                          stdin_buf_len += ret;
374                                                          continue;                                                          continue;
375                                                  }                                                  }
376                                          }                                          }
# Line 358  int igetch(int timeout) Line 379  int igetch(int timeout)
379    
380                          // For debug                          // For debug
381  #ifdef _DEBUG  #ifdef _DEBUG
382                          for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
383                          {                          {
384                                  log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
385                          }                          }
386  #endif  #endif
387                  }                  }
# Line 371  int igetch(int timeout) Line 392  int igetch(int timeout)
392                  {                  {
393                          log_error("close(epoll) error (%d)\n");                          log_error("close(epoll) error (%d)\n");
394                  }                  }
395    
396                    if (stdin_buf_offset < stdin_buf_len)
397                    {
398                            ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
399                            if (ret < 0)
400                            {
401                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
402                            }
403    
404                            // For debug
405    #ifdef _DEBUG
406                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
407                            {
408                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
409                            }
410    #endif
411                    }
412          }          }
413    
414          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
415          {          {
416                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
417    
418                  // Convert \r\n to \r                  // Convert \r\n to \r
419                  if (c == CR && pos < len && buf[pos] == LF)                  if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
420                  {                  {
421                          pos++;                          stdin_conv_offset++;
422                  }                  }
423    
424                  // Convert single \n to \r                  // Convert single \n to \r
# Line 833  int igetch(int timeout) Line 871  int igetch(int timeout)
871  #ifdef _DEBUG  #ifdef _DEBUG
872          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
873          {          {
874                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
875          }          }
876  #endif  #endif
877    
# Line 858  void igetch_reset() Line 896  void igetch_reset()
896          int ch;          int ch;
897          do          do
898          {          {
899                  ch = igetch(0);                  ch = igetch(100);
900          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
901  }  }
902    
903    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)
904    {
905            char *in_buf;
906            char *out_buf;
907            size_t in_bytes;
908            size_t out_bytes;
909            int ret;
910            int in_control = 0;
911            size_t i = 0;
912    
913            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
914            {
915                    log_error("NULL pointer error\n");
916                    return -1;
917            }
918    
919            in_buf = p_buf + *p_buf_offset;
920            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
921            out_buf = p_conv + *p_conv_len;
922            out_bytes = conv_size - (size_t)(*p_conv_len);
923    
924            while (in_bytes > 0)
925            {
926                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
927                    {
928                            if (in_control == 0)
929                            {
930                                    in_control = 1;
931                                    i = 0;
932                            }
933                    }
934    
935                    if (in_control)
936                    {
937                            if (out_bytes <= 0)
938                            {
939                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
940                                    return -2;
941                            }
942    
943                            *out_buf = *in_buf;
944                            in_buf++;
945                            out_buf++;
946                            in_bytes--;
947                            out_bytes--;
948    
949                            (*p_buf_offset)++;
950                            *p_conv_len = (int)(conv_size - out_bytes);
951    
952                            i++;
953                            if (i >= 2)
954                            {
955                                    in_control = 0;
956                            }
957                            continue;
958                    }
959    
960                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
961                    if (ret == -1)
962                    {
963                            if (errno == EINVAL) // Incomplete
964                            {
965    #ifdef _DEBUG
966                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
967    #endif
968                                    if (p_buf != in_buf)
969                                    {
970                                            *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
971                                            *p_buf_offset = 0;
972                                            *p_conv_len = (int)(conv_size - out_bytes);
973                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
974                                    }
975    
976                                    break;
977                            }
978                            else if (errno == E2BIG)
979                            {
980                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
981                                    return -1;
982                            }
983                            else if (errno == EILSEQ)
984                            {
985                                    if (in_bytes > out_bytes || out_bytes <= 0)
986                                    {
987                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
988                                            return -2;
989                                    }
990    
991                                    // reset in_bytes when "//IGNORE" is applied
992                                    if (in_bytes == 0)
993                                    {
994                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
995                                    }
996    
997                                    *out_buf = *in_buf;
998                                    in_buf++;
999                                    out_buf++;
1000                                    in_bytes--;
1001                                    out_bytes--;
1002    
1003                                    continue;
1004                            }
1005                    }
1006                    else
1007                    {
1008                            *p_buf_len = 0;
1009                            *p_buf_offset = 0;
1010                            *p_conv_len = (int)(conv_size - out_bytes);
1011    
1012                            break;
1013                    }
1014            }
1015    
1016            return 0;
1017    }
1018    
1019    int io_conv_init(const char *charset)
1020    {
1021            char tocode[32];
1022    
1023            if (charset == NULL)
1024            {
1025                    log_error("NULL pointer error\n");
1026                    return -1;
1027            }
1028    
1029            io_conv_cleanup();
1030    
1031            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1032            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1033    
1034            stdin_cd = iconv_open(BBS_DEFAULT_CHARSET "//IGNORE", stdio_charset);
1035            if (stdin_cd == (iconv_t)(-1))
1036            {
1037                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET "//IGNORE", errno);
1038                    return -2;
1039            }
1040    
1041            snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset);
1042            stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);
1043            if (stdout_cd == (iconv_t)(-1))
1044            {
1045                    log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno);
1046                    iconv_close(stdin_cd);
1047                    return -2;
1048            }
1049    
1050            return 0;
1051    }
1052    
1053    int io_conv_cleanup(void)
1054    {
1055            if (stdin_cd != NULL)
1056            {
1057                    iconv_close(stdin_cd);
1058                    stdin_cd = NULL;
1059            }
1060            if (stdout_cd != NULL)
1061            {
1062                    iconv_close(stdout_cd);
1063                    stdout_cd = NULL;
1064            }
1065    
1066            return 0;
1067    }


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

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