/[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.58 by sysadm, Sun Oct 19 09:09:18 2025 UTC Revision 1.61 by sysadm, Tue Nov 4 05:24:09 2025 UTC
# 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 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                                            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 (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 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                          // For debug
# Line 936  int io_buf_conv(iconv_t cd, char *p_buf, Line 952  int io_buf_conv(iconv_t cd, char *p_buf,
952                  {                  {
953                          if (out_bytes <= 0)                          if (out_bytes <= 0)
954                          {                          {
955                                  log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);                                  log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
956                                  return -2;                                  return -2;
957                          }                          }
958    
# Line 988  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 1025  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 "//TRANSLIT", 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 "//TRANSLIT", errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1056                  return -2;                  return -2;
1057          }          }
1058    
1059          snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset);          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);          stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);
1062          if (stdout_cd == (iconv_t)(-1))          if (stdout_cd == (iconv_t)(-1))
1063          {          {


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

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