/[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.61 by sysadm, Tue Nov 4 05:24:09 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  {  {
248          // static input buffer          static int stdin_read_wait = 0;
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
249    
250          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
251          int nfds, epollfd;          int nfds, epollfd;
# Line 237  int igetch(int timeout) Line 260  int igetch(int timeout)
260          int i = 0;          int i = 0;
261          int flags;          int flags;
262    
263          if (pos >= len)          if (stdin_conv_offset >= stdin_conv_len)
264          {          {
265                  len = 0;                  stdin_conv_len = 0;
266                  pos = 0;                  stdin_conv_offset = 0;
267    
268                  epollfd = epoll_create1(0);                  epollfd = epoll_create1(0);
269                  if (epollfd < 0)                  if (epollfd < 0)
# Line 265  int igetch(int timeout) Line 288  int igetch(int timeout)
288                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
289                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
290    
291                  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;)
292                  {                  {
293                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
294                          {                          {
# Line 274  int igetch(int timeout) Line 297  int igetch(int timeout)
297                                  break;                                  break;
298                          }                          }
299    
300                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
301                          {                          {
302                                  if (errno != EINTR)                                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
303    
304                                    if (nfds < 0)
305                                    {
306                                            if (errno != EINTR)
307                                            {
308                                                    log_error("epoll_wait() error (%d)\n", errno);
309                                                    break;
310                                            }
311                                            continue;
312                                    }
313                                    else if (nfds == 0) // timeout
314                                  {                                  {
315                                          log_error("epoll_wait() error (%d)\n", errno);                                          out = KEY_TIMEOUT;
316                                          break;                                          break;
317                                  }                                  }
318                                  continue;  
319                          }                                  for (int i = 0; i < nfds; i++)
320                          else if (nfds == 0) // timeout                                  {
321                          {                                          if (events[i].data.fd == STDIN_FILENO)
322                                  out = KEY_TIMEOUT;                                          {
323                                  break;                                                  stdin_read_wait = 1;
324                                            }
325                                    }
326                          }                          }
327    
328                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
329                          {                          {
330                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
331                                  {                                  {
332                                          while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
333                                          {                                          {
334                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
335                                                    if (ret == SSH_ERROR)
336                                                  {                                                  {
337                                                          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));
338                                                          if (ret == SSH_ERROR)                                                          loop = 0;
339                                                          {                                                          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;  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
340                                                  }                                                  }
341                                                  else                                                  else if (ret == SSH_EOF)
342                                                  {                                                  {
343                                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                                          stdin_read_wait = 0;
344                                                            loop = 0;
345                                                            break;
346                                                  }                                                  }
347                                                  if (ret < 0)                                                  else if (ret == 0)
348                                                  {                                                  {
349                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
350                                                          {                                                          stdin_read_wait = 0;
351                                                                  out = 0;                                                          loop = 0;
352                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
353                                                  }                                                  }
354                                                  else if (ret == 0) // broken pipe                                          }
355                                            else
356                                            {
357                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
358                                            }
359                                            if (ret < 0)
360                                            {
361                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
362                                                  {                                                  {
363                                                            out = 0;
364                                                            stdin_read_wait = 0;
365                                                          loop = 0;                                                          loop = 0;
366                                                          break;                                                          break;
367                                                  }                                                  }
368                                                  else                                                  else if (errno == EINTR)
369                                                  {                                                  {
                                                         len += ret;  
370                                                          continue;                                                          continue;
371                                                  }                                                  }
372                                                    else
373                                                    {
374    #ifdef _DEBUG
375                                                            log_error("read(STDIN) error (%d)\n", errno);
376    #endif
377                                                            loop = 0;
378                                                            break;
379                                                    }
380                                            }
381                                            else if (ret == 0) // broken pipe
382                                            {
383                                                    stdin_read_wait = 0;
384                                                    loop = 0;
385                                                    break;
386                                            }
387                                            else
388                                            {
389                                                    stdin_buf_len += ret;
390                                                    continue;
391                                          }                                          }
392                                  }                                  }
393                          }                          }
394    
395                          // For debug                          // For debug
396  #ifdef _DEBUG  #ifdef _DEBUG
397                          for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
398                          {                          {
399                                  log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
400                          }                          }
401  #endif  #endif
402                  }                  }
# Line 372  int igetch(int timeout) Line 407  int igetch(int timeout)
407                  {                  {
408                          log_error("close(epoll) error (%d)\n");                          log_error("close(epoll) error (%d)\n");
409                  }                  }
410    
411                    if (stdin_buf_offset < stdin_buf_len)
412                    {
413                            ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
414                            if (ret < 0)
415                            {
416                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
417                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
418                            }
419    
420                            // For debug
421    #ifdef _DEBUG
422                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
423                            {
424                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
425                            }
426    #endif
427                    }
428          }          }
429    
430          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
431          {          {
432                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
433    
434                  // Convert \r\n to \r                  // Convert \r\n to \r
435                  if (c == CR && pos < len && buf[pos] == LF)                  if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
436                  {                  {
437                          pos++;                          stdin_conv_offset++;
438                  }                  }
439    
440                  // Convert single \n to \r                  // Convert single \n to \r
# Line 834  int igetch(int timeout) Line 887  int igetch(int timeout)
887  #ifdef _DEBUG  #ifdef _DEBUG
888          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
889          {          {
890                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
891          }          }
892  #endif  #endif
893    
# Line 870  int io_buf_conv(iconv_t cd, char *p_buf, Line 923  int io_buf_conv(iconv_t cd, char *p_buf,
923          size_t in_bytes;          size_t in_bytes;
924          size_t out_bytes;          size_t out_bytes;
925          int ret;          int ret;
926            int in_control = 0;
927            size_t i = 0;
928    
929            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
930            {
931                    log_error("NULL pointer error\n");
932                    return -1;
933            }
934    
935          in_buf = p_buf + *p_buf_offset;          in_buf = p_buf + *p_buf_offset;
936          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 939  int io_buf_conv(iconv_t cd, char *p_buf,
939    
940          while (in_bytes > 0)          while (in_bytes > 0)
941          {          {
942                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
943                    {
944                            if (in_control == 0)
945                            {
946                                    in_control = 1;
947                                    i = 0;
948                            }
949                    }
950    
951                    if (in_control)
952                    {
953                            if (out_bytes <= 0)
954                            {
955                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
956                                    return -2;
957                            }
958    
959                            *out_buf = *in_buf;
960                            in_buf++;
961                            out_buf++;
962                            in_bytes--;
963                            out_bytes--;
964    
965                            (*p_buf_offset)++;
966                            *p_conv_len = (int)(conv_size - out_bytes);
967    
968                            i++;
969                            if (i >= 2)
970                            {
971                                    in_control = 0;
972                            }
973                            continue;
974                    }
975    
976                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
977                  if (ret == -1)                  if (ret == -1)
978                  {                  {
979                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
980                          {                          {
981  #ifdef _DEBUG  #ifdef _DEBUG
982                                  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]);
983  #endif  #endif
984                                  *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                  if (p_buf != in_buf)
985                                  *p_buf_offset = 0;                                  {
986                                  *p_conv_len = (int)(conv_size - out_bytes);                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
987                                  memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          *p_buf_offset = 0;
988                                            *p_conv_len = (int)(conv_size - out_bytes);
989                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
990                                    }
991    
992                                  break;                                  break;
993                          }                          }
# Line 906  int io_buf_conv(iconv_t cd, char *p_buf, Line 1004  int io_buf_conv(iconv_t cd, char *p_buf,
1004                                          return -2;                                          return -2;
1005                                  }                                  }
1006    
1007                                    // reset in_bytes when "//IGNORE" is applied
1008                                    if (in_bytes == 0)
1009                                    {
1010                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1011                                    }
1012    
1013                                  *out_buf = *in_buf;                                  *out_buf = *in_buf;
1014                                  in_buf++;                                  in_buf++;
1015                                  out_buf++;                                  out_buf++;
# Line 926  int io_buf_conv(iconv_t cd, char *p_buf, Line 1030  int io_buf_conv(iconv_t cd, char *p_buf,
1030          }          }
1031    
1032          return 0;          return 0;
1033    }
1034    
1035    int io_conv_init(const char *charset)
1036    {
1037            char tocode[32];
1038    
1039            if (charset == NULL)
1040            {
1041                    log_error("NULL pointer error\n");
1042                    return -1;
1043            }
1044    
1045            io_conv_cleanup();
1046    
1047            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1048            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1049    
1050            snprintf(tocode, sizeof(tocode), "%s%s", BBS_DEFAULT_CHARSET,
1051                             (strcasecmp(stdio_charset, BBS_DEFAULT_CHARSET) == 0 ? "" : "//IGNORE"));
1052            stdin_cd = iconv_open(tocode, stdio_charset);
1053            if (stdin_cd == (iconv_t)(-1))
1054            {
1055                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1056                    return -2;
1057            }
1058    
1059            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1060                             (strcasecmp(BBS_DEFAULT_CHARSET, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1061            stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);
1062            if (stdout_cd == (iconv_t)(-1))
1063            {
1064                    log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno);
1065                    iconv_close(stdin_cd);
1066                    return -2;
1067            }
1068    
1069            return 0;
1070    }
1071    
1072    int io_conv_cleanup(void)
1073    {
1074            if (stdin_cd != NULL)
1075            {
1076                    iconv_close(stdin_cd);
1077                    stdin_cd = NULL;
1078            }
1079            if (stdout_cd != NULL)
1080            {
1081                    iconv_close(stdout_cd);
1082                    stdout_cd = NULL;
1083            }
1084    
1085            return 0;
1086  }  }


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

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