/[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.41 by sysadm, Mon Jun 9 15:39:05 2025 UTC Revision 1.61 by sysadm, Tue Nov 4 05:24:09 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "common.h"
18  #include "io.h"  #include "io.h"
19  #include "log.h"  #include "log.h"
 #include "common.h"  
20  #include <errno.h>  #include <errno.h>
21  #include <stdio.h>  #include <fcntl.h>
22  #include <stdarg.h>  #include <stdarg.h>
23    #include <stdio.h>
24  #include <string.h>  #include <string.h>
25  #include <time.h>  #include <time.h>
 #include <fcntl.h>  
26  #include <unistd.h>  #include <unistd.h>
 #include <sys/select.h>  
 #include <sys/ioctl.h>  
27  #include <sys/epoll.h>  #include <sys/epoll.h>
28    #include <sys/ioctl.h>
29    #include <sys/select.h>
30    #include <libssh/callbacks.h>
31  #include <libssh/libssh.h>  #include <libssh/libssh.h>
32  #include <libssh/server.h>  #include <libssh/server.h>
 #include <libssh/callbacks.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 91  int outc(char c) Line 107  int outc(char c)
107          return ret;          return ret;
108  }  }
109    
110  int iflush()  int iflush(void)
111  {  {
112          int flags;          int flags;
113          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
# Line 148  int iflush() Line 164  int iflush()
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() Line 188  int iflush()
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 176  int iflush() Line 202  int iflush()
202                                                  }                                                  }
203                                                  else                                                  else
204                                                  {                                                  {
205    #ifdef _DEBUG
206                                                          log_error("write(STDOUT) error (%d)\n", errno);                                                          log_error("write(STDOUT) error (%d)\n", errno);
207    #endif
208                                                          retry = 0;                                                          retry = 0;
209                                                          break;                                                          break;
210                                                  }                                                  }
# Line 188  int iflush() Line 216  int iflush()
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 217  int iflush() Line 245  int iflush()
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 235  int igetch(int timeout) Line 260  int igetch(int timeout)
260          int i = 0;          int i = 0;
261          int flags;          int flags;
262    
263          epollfd = epoll_create1(0);          if (stdin_conv_offset >= stdin_conv_len)
         if (epollfd < 0)  
264          {          {
265                  log_error("epoll_create1() error (%d)\n", errno);                  stdin_conv_len = 0;
266                  return -1;                  stdin_conv_offset = 0;
         }  
   
         ev.events = EPOLLIN;  
         ev.data.fd = STDIN_FILENO;  
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)  
         {  
                 log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);  
267    
268                  if (close(epollfd) < 0)                  epollfd = epoll_create1(0);
269                    if (epollfd < 0)
270                  {                  {
271                          log_error("close(epoll) error (%d)\n");                          log_error("epoll_create1() error (%d)\n", errno);
272                            return -1;
273                  }                  }
                 return -1;  
         }  
   
         flags = fcntl(STDIN_FILENO, F_GETFL, 0);  
         fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
         loop = 1;  
   
         while (loop && pos >= len && !SYS_server_exit)  
         {  
                 len = 0;  
                 pos = 0;  
274    
275                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  ev.events = EPOLLIN;
276                    ev.data.fd = STDIN_FILENO;
277                    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
278                  {                  {
279                          log_error("SSH channel is closed\n");                          log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
280                          loop = 0;  
281                          break;                          if (close(epollfd) < 0)
282                            {
283                                    log_error("close(epoll) error (%d)\n");
284                            }
285                            return -1;
286                  }                  }
287    
288                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
289                    fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
290    
291                  if (nfds < 0)                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
292                  {                  {
293                          if (errno != EINTR)                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
294                          {                          {
295                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("SSH channel is closed\n");
296                                    loop = 0;
297                                  break;                                  break;
298                          }                          }
                         continue;  
                 }  
                 else if (nfds == 0) // timeout  
                 {  
                         out = KEY_TIMEOUT;  
                         break;  
                 }  
299    
300                  for (int i = 0; i < nfds; i++)                          if (!stdin_read_wait)
301                  {                          {
302                          if (events[i].data.fd == STDIN_FILENO)                                  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                                            out = KEY_TIMEOUT;
316                                            break;
317                                    }
318    
319                                    for (int i = 0; i < nfds; i++)
320                                    {
321                                            if (events[i].data.fd == STDIN_FILENO)
322                                            {
323                                                    stdin_read_wait = 1;
324                                            }
325                                    }
326                            }
327    
328                            if (stdin_read_wait)
329                          {                          {
330                                  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
331                                  {                                  {
332                                          if (SSH_v2)                                          if (SSH_v2)
333                                          {                                          {
334                                                  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);
335                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
336                                                  {                                                  {
337                                                          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 306  int igetch(int timeout) Line 340  int igetch(int timeout)
340                                                  }                                                  }
341                                                  else if (ret == SSH_EOF)                                                  else if (ret == SSH_EOF)
342                                                  {                                                  {
343                                                            stdin_read_wait = 0;
344                                                          loop = 0;                                                          loop = 0;
345                                                          break;                                                          break;
346                                                  }                                                  }
347                                                  else if (ret == 0)                                                  else if (ret == 0)
348                                                  {                                                  {
349                                                          out = 0;                                                          out = 0;
350                                                          break; // Check whether channel is still open                                                          stdin_read_wait = 0;
351                                                            loop = 0;
352                                                            break;
353                                                  }                                                  }
354                                          }                                          }
355                                          else                                          else
356                                          {                                          {
357                                                  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);
358                                          }                                          }
359                                          if (ret < 0)                                          if (ret < 0)
360                                          {                                          {
361                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
362                                                  {                                                  {
363                                                          out = 0;                                                          out = 0;
364                                                            stdin_read_wait = 0;
365                                                          loop = 0;                                                          loop = 0;
366                                                          break;                                                          break;
367                                                  }                                                  }
# Line 333  int igetch(int timeout) Line 371  int igetch(int timeout)
371                                                  }                                                  }
372                                                  else                                                  else
373                                                  {                                                  {
374    #ifdef _DEBUG
375                                                          log_error("read(STDIN) error (%d)\n", errno);                                                          log_error("read(STDIN) error (%d)\n", errno);
376    #endif
377                                                          loop = 0;                                                          loop = 0;
378                                                          break;                                                          break;
379                                                  }                                                  }
380                                          }                                          }
381                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
382                                          {                                          {
383                                                    stdin_read_wait = 0;
384                                                  loop = 0;                                                  loop = 0;
385                                                  break;                                                  break;
386                                          }                                          }
387                                          else                                          else
388                                          {                                          {
389                                                  len += ret;                                                  stdin_buf_len += ret;
390                                                  continue;                                                  continue;
391                                          }                                          }
392                                  }                                  }
393                          }                          }
394    
395                            // For debug
396    #ifdef _DEBUG
397                            for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
398                            {
399                                    log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
400                            }
401    #endif
402                  }                  }
403    
404                  // For debug                  fcntl(STDIN_FILENO, F_SETFL, flags);
405                  // for (int j = pos; j < len; j++)  
406                  // {                  if (close(epollfd) < 0)
407                  //      log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                  {
408                  // }                          log_error("close(epoll) error (%d)\n");
409          }                  }
410    
411          fcntl(STDIN_FILENO, F_SETFL, flags);                  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          while (pos < len)                          // 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 (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
435                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
436                    {
437                            stdin_conv_offset++;
438                    }
439    
440                    // Convert single \n to \r
441                    if (c == LF)
442                    {
443                            c = CR;
444                    }
445    
446                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
447                  {                  {
# Line 534  int igetch(int timeout) Line 612  int igetch(int timeout)
612                                  case 49:                                  case 49:
613                                          out = KEY_HOME;                                          out = KEY_HOME;
614                                          break;                                          break;
615                                    case 50:
616                                            out = KEY_INS;
617                                            break;
618                                  case 51:                                  case 51:
619                                          out = KEY_DEL;                                          out = KEY_DEL;
620                                          break;                                          break;
# Line 796  int igetch(int timeout) Line 877  int igetch(int timeout)
877                  break;                  break;
878          }          }
879    
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
880          // For ESC key          // For ESC key
881          if (out == 0 && in_esc)          if (out == 0 && in_esc)
882          {          {
# Line 808  int igetch(int timeout) Line 884  int igetch(int timeout)
884          }          }
885    
886          // for debug          // for debug
887          // if (out != KEY_TIMEOUT && out != KEY_NULL)  #ifdef _DEBUG
888          // {          if (out != KEY_TIMEOUT && out != KEY_NULL)
889          //      log_common("Debug: -->[0x %x]\n", out);          {
890          // }                  log_error("Debug: -->[0x %x]\n", out);
891            }
892    #endif
893    
894          return out;          return out;
895  }  }
# Line 819  int igetch(int timeout) Line 897  int igetch(int timeout)
897  int igetch_t(int sec)  int igetch_t(int sec)
898  {  {
899          int ch;          int ch;
900          time_t t_begin = time(0);          time_t t_begin = time(NULL);
901    
902          do          do
903          {          {
904                  ch = igetch(100);                  ch = igetch(100);
905          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec));
906    
907          return ch;          return ch;
908  }  }
# Line 834  void igetch_reset() Line 912  void igetch_reset()
912          int ch;          int ch;
913          do          do
914          {          {
915                  ch = igetch(0);                  ch = igetch(100);
916          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
917  }  }
918    
919    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)
920    {
921            char *in_buf;
922            char *out_buf;
923            size_t in_bytes;
924            size_t out_bytes;
925            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;
936            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
937            out_buf = p_conv + *p_conv_len;
938            out_bytes = conv_size - (size_t)(*p_conv_len);
939    
940            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);
977                    if (ret == -1)
978                    {
979                            if (errno == EINVAL) // Incomplete
980                            {
981    #ifdef _DEBUG
982                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
983    #endif
984                                    if (p_buf != in_buf)
985                                    {
986                                            *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
987                                            *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;
993                            }
994                            else if (errno == E2BIG)
995                            {
996                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
997                                    return -1;
998                            }
999                            else if (errno == EILSEQ)
1000                            {
1001                                    if (in_bytes > out_bytes || out_bytes <= 0)
1002                                    {
1003                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1004                                            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;
1014                                    in_buf++;
1015                                    out_buf++;
1016                                    in_bytes--;
1017                                    out_bytes--;
1018    
1019                                    continue;
1020                            }
1021                    }
1022                    else
1023                    {
1024                            *p_buf_len = 0;
1025                            *p_buf_offset = 0;
1026                            *p_conv_len = (int)(conv_size - out_bytes);
1027    
1028                            break;
1029                    }
1030            }
1031    
1032            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