/[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.55 by sysadm, Sat Oct 18 05:02:15 2025 UTC Revision 1.61 by sysadm, Tue Nov 4 05:24:09 2025 UTC
# Line 35  char stdio_charset[20] = BBS_DEFAULT_CHA Line 35  char stdio_charset[20] = BBS_DEFAULT_CHA
35    
36  // static input / output buffer  // static input / output buffer
37  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
38  static char stdin_conv[LINE_BUFFER_LEN];  static char stdout_buf[BUFSIZ];
39  static int stdin_buf_len = 0;  static int stdin_buf_len = 0;
40  static int stdin_conv_len = 0;  static int stdout_buf_len = 0;
41  static int stdin_buf_offset = 0;  static int stdin_buf_offset = 0;
42  static int stdin_conv_offset = 0;  static int stdout_buf_offset = 0;
43    
44  static char stdout_buf[BUFSIZ];  static char stdin_conv[LINE_BUFFER_LEN * 2];
45  static char stdout_conv[BUFSIZ];  static char stdout_conv[BUFSIZ * 2];
46  static int stdout_buf_len = 0;  static int stdin_conv_len = 0;
47  static int stdout_conv_len = 0;  static int stdout_conv_len = 0;
48  static int stdout_buf_offset = 0;  static int stdin_conv_offset = 0;
49  static int stdout_conv_offset = 0;  static int stdout_conv_offset = 0;
50    
51  static iconv_t stdin_cd = NULL;  static iconv_t stdin_cd = NULL;
# Line 170  int iflush(void) Line 170  int iflush(void)
170                                          if (ret < 0)                                          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);                                                  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    
# Line 244  int iflush(void) Line 245  int iflush(void)
245    
246  int igetch(int timeout)  int igetch(int timeout)
247  {  {
248            static int stdin_read_wait = 0;
249    
250          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
251          int nfds, epollfd;          int nfds, epollfd;
252          int ret;          int ret;
# Line 285  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 && stdin_conv_offset >= stdin_conv_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 294  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                                          log_error("epoll_wait() error (%d)\n", errno);                                          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                                            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 (stdin_buf_len < sizeof(stdin_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, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_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, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_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                                                  {                                                  {
                                                         stdin_buf_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                          }                          }
# Line 381  int igetch(int timeout) Line 396  int igetch(int timeout)
396  #ifdef _DEBUG  #ifdef _DEBUG
397                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
398                          {                          {
399                                  log_common("Debug: <--[%u]\n", (stdin_buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
400                          }                          }
401  #endif  #endif
402                  }                  }
# Line 399  int igetch(int timeout) Line 414  int igetch(int timeout)
414                          if (ret < 0)                          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);                                  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    
# Line 863  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 899  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)          if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
930          {          {
# Line 913  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 941  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 965  int io_buf_conv(iconv_t cd, char *p_buf, Line 1034  int io_buf_conv(iconv_t cd, char *p_buf,
1034    
1035  int io_conv_init(const char *charset)  int io_conv_init(const char *charset)
1036  {  {
1037            char tocode[32];
1038    
1039          if (charset == NULL)          if (charset == NULL)
1040          {          {
1041                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
# Line 976  int io_conv_init(const char *charset) Line 1047  int io_conv_init(const char *charset)
1047          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1048          stdio_charset[sizeof(stdio_charset) - 1] = '\0';          stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1049    
1050          stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset);          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))          if (stdin_cd == (iconv_t)(-1))
1054          {          {
1055                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1056                  return -2;                  return -2;
1057          }          }
1058          stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET);  
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))          if (stdout_cd == (iconv_t)(-1))
1063          {          {
1064                  log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno);                  log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno);
1065                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1066                  return -2;                  return -2;
1067          }          }


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

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