/[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.46 by sysadm, Wed Jun 18 04:29:18 2025 UTC Revision 1.56 by sysadm, Sun Oct 19 01:30:38 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>
33  #include <libssh/callbacks.h>  
34    char stdio_charset[32] = BBS_DEFAULT_CHARSET;
35    
36    // static input / output buffer
37    static char stdin_buf[LINE_BUFFER_LEN];
38    static char stdin_conv[LINE_BUFFER_LEN];
39    static int stdin_buf_len = 0;
40    static int stdin_conv_len = 0;
41    static int stdin_buf_offset = 0;
42    static int stdin_conv_offset = 0;
43    
44  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
45    static char stdout_conv[BUFSIZ];
46  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
47    static int stdout_conv_len = 0;
48  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
49    static int stdout_conv_offset = 0;
50    
51    static iconv_t stdin_cd = NULL;
52    static iconv_t stdout_cd = NULL;
53    
54  int prints(const char *format, ...)  int prints(const char *format, ...)
55  {  {
# Line 148  int iflush(void) Line 164  int iflush(void)
164                  {                  {
165                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
166                          {                          {
167                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
168                                    {
169                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
170                                            if (ret < 0)
171                                            {
172                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
173                                            }
174                                    }
175    
176                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
177                                  {                                  {
178                                          if (SSH_v2)                                          if (SSH_v2)
179                                          {                                          {
180                                                  ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset));                                                  ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset));
181                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
182                                                  {                                                  {
183                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
# Line 162  int iflush(void) Line 187  int iflush(void)
187                                          }                                          }
188                                          else                                          else
189                                          {                                          {
190                                                  ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset));                                                  ret = (int)write(STDOUT_FILENO, stdout_conv + stdout_conv_offset, (size_t)(stdout_conv_len - stdout_conv_offset));
191                                          }                                          }
192                                          if (ret < 0)                                          if (ret < 0)
193                                          {                                          {
# Line 190  int iflush(void) Line 215  int iflush(void)
215                                          }                                          }
216                                          else                                          else
217                                          {                                          {
218                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
219                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
220                                                  {                                                  {
221                                                          ret = 0;                                                          ret = 0;
222                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
223                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
224                                                          retry = 0;                                                          retry = 0;
225                                                          break;                                                          break;
226                                                  }                                                  }
# Line 219  int iflush(void) Line 244  int iflush(void)
244    
245  int igetch(int timeout)  int igetch(int timeout)
246  {  {
         // static input buffer  
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
   
247          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
248          int nfds, epollfd;          int nfds, epollfd;
249          int ret;          int ret;
# Line 237  int igetch(int timeout) Line 257  int igetch(int timeout)
257          int i = 0;          int i = 0;
258          int flags;          int flags;
259    
260          epollfd = epoll_create1(0);          if (stdin_conv_offset >= stdin_conv_len)
         if (epollfd < 0)  
         {  
                 log_error("epoll_create1() error (%d)\n", errno);  
                 return -1;  
         }  
   
         ev.events = EPOLLIN;  
         ev.data.fd = STDIN_FILENO;  
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)  
261          {          {
262                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  stdin_conv_len = 0;
263                    stdin_conv_offset = 0;
264    
265                  if (close(epollfd) < 0)                  epollfd = epoll_create1(0);
266                    if (epollfd < 0)
267                  {                  {
268                          log_error("close(epoll) error (%d)\n");                          log_error("epoll_create1() error (%d)\n", errno);
269                            return -1;
270                  }                  }
                 return -1;  
         }  
271    
272          flags = fcntl(STDIN_FILENO, F_GETFL, 0);                  ev.events = EPOLLIN;
273          fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);                  ev.data.fd = STDIN_FILENO;
274                    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
275                    {
276                            log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
277    
278          loop = 1;                          if (close(epollfd) < 0)
279                            {
280                                    log_error("close(epoll) error (%d)\n");
281                            }
282                            return -1;
283                    }
284    
285          while (loop && pos >= len && !SYS_server_exit)                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
286          {                  fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
                 len = 0;  
                 pos = 0;  
287    
288                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  for (loop = 1; loop && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
289                  {                  {
290                          log_error("SSH channel is closed\n");                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
291                          loop = 0;                          {
292                          break;                                  log_error("SSH channel is closed\n");
293                  }                                  loop = 0;
294                                    break;
295                            }
296    
297                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
298    
299                  if (nfds < 0)                          if (nfds < 0)
                 {  
                         if (errno != EINTR)  
300                          {                          {
301                                  log_error("epoll_wait() error (%d)\n", errno);                                  if (errno != EINTR)
302                                    {
303                                            log_error("epoll_wait() error (%d)\n", errno);
304                                            break;
305                                    }
306                                    continue;
307                            }
308                            else if (nfds == 0) // timeout
309                            {
310                                    out = KEY_TIMEOUT;
311                                  break;                                  break;
312                          }                          }
                         continue;  
                 }  
                 else if (nfds == 0) // timeout  
                 {  
                         out = KEY_TIMEOUT;  
                         break;  
                 }  
313    
314                  for (int i = 0; i < nfds; i++)                          for (int i = 0; i < nfds; i++)
                 {  
                         if (events[i].data.fd == STDIN_FILENO)  
315                          {                          {
316                                  while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                  if (events[i].data.fd == STDIN_FILENO)
317                                  {                                  {
318                                          if (SSH_v2)                                          while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
319                                          {                                          {
320                                                  ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);                                                  if (SSH_v2)
                                                 if (ret == SSH_ERROR)  
321                                                  {                                                  {
322                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                          ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
323                                                          loop = 0;                                                          if (ret == SSH_ERROR)
324                                                          break;                                                          {
325                                                                    log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
326                                                                    loop = 0;
327                                                                    break;
328                                                            }
329                                                            else if (ret == SSH_EOF)
330                                                            {
331                                                                    loop = 0;
332                                                                    break;
333                                                            }
334                                                            else if (ret == 0)
335                                                            {
336                                                                    out = 0;
337                                                                    loop = 0;
338                                                                    break;
339                                                            }
340                                                  }                                                  }
341                                                  else if (ret == SSH_EOF)                                                  else
342                                                  {                                                  {
343                                                          loop = 0;                                                          ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
                                                         break;  
344                                                  }                                                  }
345                                                  else if (ret == 0)                                                  if (ret < 0)
346                                                  {                                                  {
347                                                          out = 0;                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)
348                                                          break; // Check whether channel is still open                                                          {
349                                                                    out = 0;
350                                                                    loop = 0;
351                                                                    break;
352                                                            }
353                                                            else if (errno == EINTR)
354                                                            {
355                                                                    continue;
356                                                            }
357                                                            else
358                                                            {
359    #ifdef _DEBUG
360                                                                    log_error("read(STDIN) error (%d)\n", errno);
361    #endif
362                                                                    loop = 0;
363                                                                    break;
364                                                            }
365                                                  }                                                  }
366                                          }                                                  else if (ret == 0) // broken pipe
                                         else  
                                         {  
                                                 ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);  
                                         }  
                                         if (ret < 0)  
                                         {  
                                                 if (errno == EAGAIN || errno == EWOULDBLOCK)  
367                                                  {                                                  {
                                                         out = 0;  
368                                                          loop = 0;                                                          loop = 0;
369                                                          break;                                                          break;
370                                                  }                                                  }
                                                 else if (errno == EINTR)  
                                                 {  
                                                         continue;  
                                                 }  
371                                                  else                                                  else
372                                                  {                                                  {
373  #ifdef _DEBUG                                                          stdin_buf_len += ret;
374                                                          log_error("read(STDIN) error (%d)\n", errno);                                                          continue;
 #endif  
                                                         loop = 0;  
                                                         break;  
375                                                  }                                                  }
376                                          }                                          }
                                         else if (ret == 0) // broken pipe  
                                         {  
                                                 loop = 0;  
                                                 break;  
                                         }  
                                         else  
                                         {  
                                                 len += ret;  
                                                 continue;  
                                         }  
377                                  }                                  }
378                          }                          }
                 }  
379    
380                  // For debug                          // For debug
381  #ifdef _DEBUG  #ifdef _DEBUG
382                  for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
383                            {
384                                    log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
385                            }
386    #endif
387                    }
388    
389                    fcntl(STDIN_FILENO, F_SETFL, flags);
390    
391                    if (close(epollfd) < 0)
392                  {                  {
393                          log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                          log_error("close(epoll) error (%d)\n");
394                  }                  }
395    
396                    if (stdin_buf_offset < stdin_buf_len)
397                    {
398                            ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
399                            if (ret < 0)
400                            {
401                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
402                            }
403    
404                            // For debug
405    #ifdef _DEBUG
406                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
407                            {
408                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
409                            }
410  #endif  #endif
411                    }
412          }          }
413    
414          fcntl(STDIN_FILENO, F_SETFL, flags);          while (stdin_conv_offset < stdin_conv_len)
   
         while (pos < len)  
415          {          {
416                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
417    
418                    // Convert \r\n to \r
419                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
420                    {
421                            stdin_conv_offset++;
422                    }
423    
424                    // Convert single \n to \r
425                    if (c == LF)
426                    {
427                            c = CR;
428                    }
429    
430                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
431                  {                  {
# Line 805  int igetch(int timeout) Line 861  int igetch(int timeout)
861                  break;                  break;
862          }          }
863    
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
864          // For ESC key          // For ESC key
865          if (out == 0 && in_esc)          if (out == 0 && in_esc)
866          {          {
# Line 820  int igetch(int timeout) Line 871  int igetch(int timeout)
871  #ifdef _DEBUG  #ifdef _DEBUG
872          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
873          {          {
874                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
875          }          }
876  #endif  #endif
877    
# Line 845  void igetch_reset() Line 896  void igetch_reset()
896          int ch;          int ch;
897          do          do
898          {          {
899                  ch = igetch(0);                  ch = igetch(100);
900          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
901  }  }
902    
903    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)
904    {
905            char *in_buf;
906            char *out_buf;
907            size_t in_bytes;
908            size_t out_bytes;
909            int ret;
910            int in_control = 0;
911            size_t i = 0;
912    
913            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
914            {
915                    log_error("NULL pointer error\n");
916                    return -1;
917            }
918    
919            in_buf = p_buf + *p_buf_offset;
920            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
921            out_buf = p_conv + *p_conv_len;
922            out_bytes = conv_size - (size_t)(*p_conv_len);
923    
924            while (in_bytes > 0)
925            {
926                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
927                    {
928                            if (in_control == 0)
929                            {
930                                    in_control = 1;
931                                    i = 0;
932                            }
933                    }
934    
935                    if (in_control)
936                    {
937                            if (out_bytes <= 0)
938                            {
939                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
940                                    return -2;
941                            }
942    
943                            *out_buf = *in_buf;
944                            in_buf++;
945                            out_buf++;
946                            in_bytes--;
947                            out_bytes--;
948    
949                            (*p_buf_offset)++;
950                            *p_conv_len = (int)(conv_size - out_bytes);
951    
952                            i++;
953                            if (i >= 2)
954                            {
955                                    in_control = 0;
956                            }
957                            continue;
958                    }
959    
960                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
961                    if (ret == -1)
962                    {
963                            if (errno == EINVAL) // Incomplete
964                            {
965    #ifdef _DEBUG
966                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
967    #endif
968                                    if (p_buf != in_buf)
969                                    {
970                                            *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
971                                            *p_buf_offset = 0;
972                                            *p_conv_len = (int)(conv_size - out_bytes);
973                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
974                                    }
975    
976                                    break;
977                            }
978                            else if (errno == E2BIG)
979                            {
980                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
981                                    return -1;
982                            }
983                            else if (errno == EILSEQ)
984                            {
985                                    if (in_bytes > out_bytes || out_bytes <= 0)
986                                    {
987                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
988                                            return -2;
989                                    }
990    
991                                    *out_buf = *in_buf;
992                                    in_buf++;
993                                    out_buf++;
994                                    in_bytes--;
995                                    out_bytes--;
996    
997                                    continue;
998                            }
999                    }
1000                    else
1001                    {
1002                            *p_buf_len = 0;
1003                            *p_buf_offset = 0;
1004                            *p_conv_len = (int)(conv_size - out_bytes);
1005    
1006                            break;
1007                    }
1008            }
1009    
1010            return 0;
1011    }
1012    
1013    int io_conv_init(const char *charset)
1014    {
1015            if (charset == NULL)
1016            {
1017                    log_error("NULL pointer error\n");
1018                    return -1;
1019            }
1020    
1021            io_conv_cleanup();
1022    
1023            snprintf(stdio_charset, sizeof(stdio_charset), "%s//TRANSLIT", charset);
1024    
1025            stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset);
1026            if (stdin_cd == (iconv_t)(-1))
1027            {
1028                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno);
1029                    return -2;
1030            }
1031            stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET);
1032            if (stdout_cd == (iconv_t)(-1))
1033            {
1034                    log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno);
1035                    iconv_close(stdin_cd);
1036                    return -2;
1037            }
1038    
1039            return 0;
1040    }
1041    
1042    int io_conv_cleanup(void)
1043    {
1044            if (stdin_cd != NULL)
1045            {
1046                    iconv_close(stdin_cd);
1047                    stdin_cd = NULL;
1048            }
1049            if (stdout_cd != NULL)
1050            {
1051                    iconv_close(stdout_cd);
1052                    stdout_cd = NULL;
1053            }
1054    
1055            return 0;
1056    }


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

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