/[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.54 by sysadm, Sat Oct 18 01:50:48 2025 UTC Revision 1.58 by sysadm, Sun Oct 19 09:09:18 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 320  int igetch(int timeout) Line 340  int igetch(int timeout)
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 350  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 359  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 372  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 834  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 870  int io_buf_conv(iconv_t cd, char *p_buf, Line 907  int io_buf_conv(iconv_t cd, char *p_buf,
907          size_t in_bytes;          size_t in_bytes;
908          size_t out_bytes;          size_t out_bytes;
909          int ret;          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;          in_buf = p_buf + *p_buf_offset;
920          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
# Line 878  int io_buf_conv(iconv_t cd, char *p_buf, Line 923  int io_buf_conv(iconv_t cd, char *p_buf,
923    
924          while (in_bytes > 0)          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);                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
961                  if (ret == -1)                  if (ret == -1)
962                  {                  {
963                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
964                          {                          {
965  #ifdef _DEBUG  #ifdef _DEBUG
966                                  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]);
967  #endif  #endif
968                                  *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                  if (p_buf != in_buf)
969                                  *p_buf_offset = 0;                                  {
970                                  *p_conv_len = (int)(conv_size - out_bytes);                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
971                                  memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          *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;                                  break;
977                          }                          }
# Line 926  int io_buf_conv(iconv_t cd, char *p_buf, Line 1008  int io_buf_conv(iconv_t cd, char *p_buf,
1008          }          }
1009    
1010          return 0;          return 0;
1011    }
1012    
1013    int io_conv_init(const char *charset)
1014    {
1015            char tocode[32];
1016    
1017            if (charset == NULL)
1018            {
1019                    log_error("NULL pointer error\n");
1020                    return -1;
1021            }
1022    
1023            io_conv_cleanup();
1024    
1025            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1026            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1027    
1028            stdin_cd = iconv_open(BBS_DEFAULT_CHARSET "//TRANSLIT", stdio_charset);
1029            if (stdin_cd == (iconv_t)(-1))
1030            {
1031                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET "//TRANSLIT", errno);
1032                    return -2;
1033            }
1034    
1035            snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset);
1036            stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);
1037            if (stdout_cd == (iconv_t)(-1))
1038            {
1039                    log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno);
1040                    iconv_close(stdin_cd);
1041                    return -2;
1042            }
1043    
1044            return 0;
1045    }
1046    
1047    int io_conv_cleanup(void)
1048    {
1049            if (stdin_cd != NULL)
1050            {
1051                    iconv_close(stdin_cd);
1052                    stdin_cd = NULL;
1053            }
1054            if (stdout_cd != NULL)
1055            {
1056                    iconv_close(stdout_cd);
1057                    stdout_cd = NULL;
1058            }
1059    
1060            return 0;
1061  }  }


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

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