/[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.55 by sysadm, Sat Oct 18 05:02:15 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 stdin_conv[LINE_BUFFER_LEN];
39    static int stdin_buf_len = 0;
40    static int stdin_conv_len = 0;
41    static int stdin_buf_offset = 0;
42    static int stdin_conv_offset = 0;
43    
44  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
45    static char stdout_conv[BUFSIZ];
46  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
47    static int stdout_conv_len = 0;
48  static int stdout_buf_offset = 0;  static int stdout_buf_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  {  {
# 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_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_common("Debug: <--[%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          }          }
405    
406          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
407          {          {
408                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
409    
410                    // Convert \r\n to \r
411                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
412                    {
413                            stdin_conv_offset++;
414                    }
415    
416                    // Convert single \n to \r
417                    if (c == LF)
418                    {
419                            c = CR;
420                    }
421    
422                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
423                  {                  {
# Line 817  int igetch(int timeout) Line 859  int igetch(int timeout)
859                  out = KEY_ESC;                  out = KEY_ESC;
860          }          }
861    
         // Convert LF to CR -- Cterm send LF without CR  
         if (out == LF)  
         {  
                 out = CR;  
         }  
   
862          // for debug          // for debug
863  #ifdef _DEBUG  #ifdef _DEBUG
864          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
# Line 852  void igetch_reset() Line 888  void igetch_reset()
888          int ch;          int ch;
889          do          do
890          {          {
891                  ch = igetch(0);                  ch = igetch(100);
892          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
893  }  }
894    
895    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)
896    {
897            char *in_buf;
898            char *out_buf;
899            size_t in_bytes;
900            size_t out_bytes;
901            int ret;
902    
903            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
904            {
905                    log_error("NULL pointer error\n");
906                    return -1;
907            }
908    
909            in_buf = p_buf + *p_buf_offset;
910            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
911            out_buf = p_conv + *p_conv_len;
912            out_bytes = conv_size - (size_t)(*p_conv_len);
913    
914            while (in_bytes > 0)
915            {
916                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
917                    if (ret == -1)
918                    {
919                            if (errno == EINVAL) // Incomplete
920                            {
921    #ifdef _DEBUG
922                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);
923    #endif
924                                    *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
925                                    *p_buf_offset = 0;
926                                    *p_conv_len = (int)(conv_size - out_bytes);
927                                    memmove(p_buf, in_buf, (size_t)(*p_buf_len));
928    
929                                    break;
930                            }
931                            else if (errno == E2BIG)
932                            {
933                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
934                                    return -1;
935                            }
936                            else if (errno == EILSEQ)
937                            {
938                                    if (in_bytes > out_bytes || out_bytes <= 0)
939                                    {
940                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
941                                            return -2;
942                                    }
943    
944                                    *out_buf = *in_buf;
945                                    in_buf++;
946                                    out_buf++;
947                                    in_bytes--;
948                                    out_bytes--;
949    
950                                    continue;
951                            }
952                    }
953                    else
954                    {
955                            *p_buf_len = 0;
956                            *p_buf_offset = 0;
957                            *p_conv_len = (int)(conv_size - out_bytes);
958    
959                            break;
960                    }
961            }
962    
963            return 0;
964    }
965    
966    int io_conv_init(const char *charset)
967    {
968            if (charset == NULL)
969            {
970                    log_error("NULL pointer error\n");
971                    return -1;
972            }
973    
974            io_conv_cleanup();
975    
976            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
977            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
978    
979            stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset);
980            if (stdin_cd == (iconv_t)(-1))
981            {
982                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno);
983                    return -2;
984            }
985            stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET);
986            if (stdout_cd == (iconv_t)(-1))
987            {
988                    log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno);
989                    iconv_close(stdin_cd);
990                    return -2;
991            }
992    
993            return 0;
994    }
995    
996    int io_conv_cleanup(void)
997    {
998            if (stdin_cd != NULL)
999            {
1000                    iconv_close(stdin_cd);
1001                    stdin_cd = NULL;
1002            }
1003            if (stdout_cd != NULL)
1004            {
1005                    iconv_close(stdout_cd);
1006                    stdout_cd = NULL;
1007            }
1008    
1009            return 0;
1010    }


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

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