/[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.64 by sysadm, Wed Nov 5 02:48:48 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                          io.c  -  description  /*
3                                                           -------------------   * io
4          Copyright            : (C) 2004-2025 by Leaflet   *   - basic terminal-based user input / output features
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9    #include "common.h"
10  #include "io.h"  #include "io.h"
11  #include "log.h"  #include "log.h"
 #include "common.h"  
12  #include <errno.h>  #include <errno.h>
13  #include <stdio.h>  #include <fcntl.h>
14  #include <stdarg.h>  #include <stdarg.h>
15    #include <stdio.h>
16  #include <string.h>  #include <string.h>
17  #include <time.h>  #include <time.h>
 #include <fcntl.h>  
18  #include <unistd.h>  #include <unistd.h>
 #include <sys/select.h>  
 #include <sys/ioctl.h>  
19  #include <sys/epoll.h>  #include <sys/epoll.h>
20    #include <sys/ioctl.h>
21    #include <sys/select.h>
22    #include <libssh/callbacks.h>
23  #include <libssh/libssh.h>  #include <libssh/libssh.h>
24  #include <libssh/server.h>  #include <libssh/server.h>
 #include <libssh/callbacks.h>  
25    
26    const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
27    char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
28    
29    // static input / output buffer
30    static char stdin_buf[LINE_BUFFER_LEN];
31  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
32    static int stdin_buf_len = 0;
33  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
34    static int stdin_buf_offset = 0;
35  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
36    
37    static char stdin_conv[LINE_BUFFER_LEN * 2];
38    static char stdout_conv[BUFSIZ * 2];
39    static int stdin_conv_len = 0;
40    static int stdout_conv_len = 0;
41    static int stdin_conv_offset = 0;
42    static int stdout_conv_offset = 0;
43    
44    static iconv_t stdin_cd = NULL;
45    static iconv_t stdout_cd = NULL;
46    
47  int prints(const char *format, ...)  int prints(const char *format, ...)
48  {  {
49          char buf[BUFSIZ];          char buf[BUFSIZ];
# Line 148  int iflush(void) Line 157  int iflush(void)
157                  {                  {
158                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
159                          {                          {
160                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
161                                    {
162                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
163                                            if (ret < 0)
164                                            {
165                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
166                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
167                                            }
168                                    }
169    
170                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
171                                  {                                  {
172                                          if (SSH_v2)                                          if (SSH_v2)
173                                          {                                          {
174                                                  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));
175                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
176                                                  {                                                  {
177                                                          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 181  int iflush(void)
181                                          }                                          }
182                                          else                                          else
183                                          {                                          {
184                                                  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));
185                                          }                                          }
186                                          if (ret < 0)                                          if (ret < 0)
187                                          {                                          {
# Line 190  int iflush(void) Line 209  int iflush(void)
209                                          }                                          }
210                                          else                                          else
211                                          {                                          {
212                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
213                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
214                                                  {                                                  {
215                                                          ret = 0;                                                          ret = 0;
216                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
217                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
218                                                          retry = 0;                                                          retry = 0;
219                                                          break;                                                          break;
220                                                  }                                                  }
# Line 219  int iflush(void) Line 238  int iflush(void)
238    
239  int igetch(int timeout)  int igetch(int timeout)
240  {  {
241          // static input buffer          static int stdin_read_wait = 0;
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
242    
243          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event ev, events[MAX_EVENTS];
244          int nfds, epollfd;          int nfds, epollfd;
# Line 237  int igetch(int timeout) Line 253  int igetch(int timeout)
253          int i = 0;          int i = 0;
254          int flags;          int flags;
255    
256          epollfd = epoll_create1(0);          if (stdin_conv_offset >= stdin_conv_len)
         if (epollfd < 0)  
257          {          {
258                  log_error("epoll_create1() error (%d)\n", errno);                  stdin_conv_len = 0;
259                  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);  
260    
261                  if (close(epollfd) < 0)                  epollfd = epoll_create1(0);
262                    if (epollfd < 0)
263                  {                  {
264                          log_error("close(epoll) error (%d)\n");                          log_error("epoll_create1() error (%d)\n", errno);
265                            return -1;
266                  }                  }
                 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;  
267    
268                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                  ev.events = EPOLLIN;
269                    ev.data.fd = STDIN_FILENO;
270                    if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
271                  {                  {
272                          log_error("SSH channel is closed\n");                          log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
273                          loop = 0;  
274                          break;                          if (close(epollfd) < 0)
275                            {
276                                    log_error("close(epoll) error (%d)\n");
277                            }
278                            return -1;
279                  }                  }
280    
281                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                  flags = fcntl(STDIN_FILENO, F_GETFL, 0);
282                    fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
283    
284                  if (nfds < 0)                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
285                  {                  {
286                          if (errno != EINTR)                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
287                          {                          {
288                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("SSH channel is closed\n");
289                                    loop = 0;
290                                  break;                                  break;
291                          }                          }
                         continue;  
                 }  
                 else if (nfds == 0) // timeout  
                 {  
                         out = KEY_TIMEOUT;  
                         break;  
                 }  
292    
293                  for (int i = 0; i < nfds; i++)                          if (!stdin_read_wait)
294                  {                          {
295                          if (events[i].data.fd == STDIN_FILENO)                                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
296    
297                                    if (nfds < 0)
298                                    {
299                                            if (errno != EINTR)
300                                            {
301                                                    log_error("epoll_wait() error (%d)\n", errno);
302                                                    break;
303                                            }
304                                            continue;
305                                    }
306                                    else if (nfds == 0) // timeout
307                                    {
308                                            out = KEY_TIMEOUT;
309                                            break;
310                                    }
311    
312                                    for (int i = 0; i < nfds; i++)
313                                    {
314                                            if (events[i].data.fd == STDIN_FILENO)
315                                            {
316                                                    stdin_read_wait = 1;
317                                            }
318                                    }
319                            }
320    
321                            if (stdin_read_wait)
322                          {                          {
323                                  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
324                                  {                                  {
325                                          if (SSH_v2)                                          if (SSH_v2)
326                                          {                                          {
327                                                  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);
328                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
329                                                  {                                                  {
330                                                          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 308  int igetch(int timeout) Line 333  int igetch(int timeout)
333                                                  }                                                  }
334                                                  else if (ret == SSH_EOF)                                                  else if (ret == SSH_EOF)
335                                                  {                                                  {
336                                                            stdin_read_wait = 0;
337                                                          loop = 0;                                                          loop = 0;
338                                                          break;                                                          break;
339                                                  }                                                  }
340                                                  else if (ret == 0)                                                  else if (ret == 0)
341                                                  {                                                  {
342                                                          out = 0;                                                          out = 0;
343                                                          break; // Check whether channel is still open                                                          stdin_read_wait = 0;
344                                                            loop = 0;
345                                                            break;
346                                                  }                                                  }
347                                          }                                          }
348                                          else                                          else
349                                          {                                          {
350                                                  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);
351                                          }                                          }
352                                          if (ret < 0)                                          if (ret < 0)
353                                          {                                          {
354                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
355                                                  {                                                  {
356                                                          out = 0;                                                          out = 0;
357                                                            stdin_read_wait = 0;
358                                                          loop = 0;                                                          loop = 0;
359                                                          break;                                                          break;
360                                                  }                                                  }
# Line 344  int igetch(int timeout) Line 373  int igetch(int timeout)
373                                          }                                          }
374                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
375                                          {                                          {
376                                                    stdin_read_wait = 0;
377                                                  loop = 0;                                                  loop = 0;
378                                                  break;                                                  break;
379                                          }                                          }
380                                          else                                          else
381                                          {                                          {
382                                                  len += ret;                                                  stdin_buf_len += ret;
383                                                  continue;                                                  continue;
384                                          }                                          }
385                                  }                                  }
386                          }                          }
                 }  
387    
388                  // For debug                          // For debug
389  #ifdef _DEBUG  #ifdef _DEBUG
390                  for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
391                            {
392                                    log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
393                            }
394    #endif
395                    }
396    
397                    fcntl(STDIN_FILENO, F_SETFL, flags);
398    
399                    if (close(epollfd) < 0)
400                  {                  {
401                          log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                          log_error("close(epoll) error (%d)\n");
402                  }                  }
403    
404                    if (stdin_buf_offset < stdin_buf_len)
405                    {
406                            ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
407                            if (ret < 0)
408                            {
409                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
410                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
411                            }
412    
413                            // For debug
414    #ifdef _DEBUG
415                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
416                            {
417                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
418                            }
419  #endif  #endif
420                    }
421          }          }
422    
423          fcntl(STDIN_FILENO, F_SETFL, flags);          while (stdin_conv_offset < stdin_conv_len)
   
         while (pos < len)  
424          {          {
425                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
426    
427                    // Convert \r\n to \r
428                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
429                    {
430                            stdin_conv_offset++;
431                    }
432    
433                    // Convert single \n to \r
434                    if (c == LF)
435                    {
436                            c = CR;
437                    }
438    
439                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
440                  {                  {
# Line 805  int igetch(int timeout) Line 870  int igetch(int timeout)
870                  break;                  break;
871          }          }
872    
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
873          // For ESC key          // For ESC key
874          if (out == 0 && in_esc)          if (out == 0 && in_esc)
875          {          {
# Line 820  int igetch(int timeout) Line 880  int igetch(int timeout)
880  #ifdef _DEBUG  #ifdef _DEBUG
881          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
882          {          {
883                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
884          }          }
885  #endif  #endif
886    
# Line 845  void igetch_reset() Line 905  void igetch_reset()
905          int ch;          int ch;
906          do          do
907          {          {
908                  ch = igetch(0);                  ch = igetch(100);
909          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
910  }  }
911    
912    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)
913    {
914            char *in_buf;
915            char *out_buf;
916            size_t in_bytes;
917            size_t out_bytes;
918            int ret;
919            int in_control = 0;
920            size_t i = 0;
921    
922            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
923            {
924                    log_error("NULL pointer error\n");
925                    return -1;
926            }
927    
928            in_buf = p_buf + *p_buf_offset;
929            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
930            out_buf = p_conv + *p_conv_len;
931            out_bytes = conv_size - (size_t)(*p_conv_len);
932    
933            while (in_bytes > 0)
934            {
935                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
936                    {
937                            if (in_control == 0)
938                            {
939                                    in_control = 1;
940                                    i = 0;
941                            }
942                    }
943    
944                    if (in_control)
945                    {
946                            if (out_bytes <= 0)
947                            {
948                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
949                                    return -2;
950                            }
951    
952                            *out_buf = *in_buf;
953                            in_buf++;
954                            out_buf++;
955                            in_bytes--;
956                            out_bytes--;
957    
958                            (*p_buf_offset)++;
959                            *p_conv_len = (int)(conv_size - out_bytes);
960    
961                            i++;
962                            if (i >= 2)
963                            {
964                                    in_control = 0;
965                            }
966                            continue;
967                    }
968    
969                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
970                    if (ret == -1)
971                    {
972                            if (errno == EINVAL) // Incomplete
973                            {
974    #ifdef _DEBUG
975                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
976    #endif
977                                    if (p_buf != in_buf)
978                                    {
979                                            *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
980                                            *p_buf_offset = 0;
981                                            *p_conv_len = (int)(conv_size - out_bytes);
982                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
983                                    }
984    
985                                    break;
986                            }
987                            else if (errno == E2BIG)
988                            {
989                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
990                                    return -1;
991                            }
992                            else if (errno == EILSEQ)
993                            {
994                                    if (in_bytes > out_bytes || out_bytes <= 0)
995                                    {
996                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
997                                            return -2;
998                                    }
999    
1000                                    // reset in_bytes when "//IGNORE" is applied
1001                                    if (in_bytes == 0)
1002                                    {
1003                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1004                                    }
1005    
1006                                    *out_buf = *in_buf;
1007                                    in_buf++;
1008                                    out_buf++;
1009                                    in_bytes--;
1010                                    out_bytes--;
1011    
1012                                    continue;
1013                            }
1014                    }
1015                    else
1016                    {
1017                            *p_buf_len = 0;
1018                            *p_buf_offset = 0;
1019                            *p_conv_len = (int)(conv_size - out_bytes);
1020    
1021                            break;
1022                    }
1023            }
1024    
1025            return 0;
1026    }
1027    
1028    int io_conv_init(const char *charset)
1029    {
1030            char tocode[CHARSET_MAX_LEN + 20];
1031    
1032            if (charset == NULL)
1033            {
1034                    log_error("NULL pointer error\n");
1035                    return -1;
1036            }
1037    
1038            io_conv_cleanup();
1039    
1040            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1041            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1042    
1043            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1044                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1045            stdin_cd = iconv_open(tocode, stdio_charset);
1046            if (stdin_cd == (iconv_t)(-1))
1047            {
1048                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1049                    return -2;
1050            }
1051    
1052            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1053                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1054            stdout_cd = iconv_open(tocode, BBS_default_charset);
1055            if (stdout_cd == (iconv_t)(-1))
1056            {
1057                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1058                    iconv_close(stdin_cd);
1059                    return -2;
1060            }
1061    
1062            return 0;
1063    }
1064    
1065    int io_conv_cleanup(void)
1066    {
1067            if (stdin_cd != NULL)
1068            {
1069                    iconv_close(stdin_cd);
1070                    stdin_cd = NULL;
1071            }
1072            if (stdout_cd != NULL)
1073            {
1074                    iconv_close(stdout_cd);
1075                    stdout_cd = NULL;
1076            }
1077    
1078            return 0;
1079    }


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

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