/[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.57 by sysadm, Sun Oct 19 02:42:19 2025 UTC Revision 1.66 by sysadm, Mon Nov 17 02:32:42 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     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "common.h"  #include "common.h"
14  #include "io.h"  #include "io.h"
# Line 31  Line 27 
27  #include <libssh/libssh.h>  #include <libssh/libssh.h>
28  #include <libssh/server.h>  #include <libssh/server.h>
29    
30  char stdio_charset[32] = BBS_DEFAULT_CHARSET;  const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
31    char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
32    
33    // epoll for STDIO
34    static int stdin_epollfd = -1;
35    static int stdout_epollfd = -1;
36    static int stdin_flags;
37    static int stdout_flags;
38    
39  // static input / output buffer  // static input / output buffer
40  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
# Line 51  static int stdout_conv_offset = 0; Line 54  static int stdout_conv_offset = 0;
54  static iconv_t stdin_cd = NULL;  static iconv_t stdin_cd = NULL;
55  static iconv_t stdout_cd = NULL;  static iconv_t stdout_cd = NULL;
56    
57    int io_init(void)
58    {
59            struct epoll_event ev;
60    
61            if (stdin_epollfd == -1)
62            {
63                    stdin_epollfd = epoll_create1(0);
64                    if (stdin_epollfd == -1)
65                    {
66                            log_error("epoll_create1() error (%d)\n", errno);
67                            return -1;
68                    }
69    
70                    ev.events = EPOLLIN;
71                    ev.data.fd = STDIN_FILENO;
72                    if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
73                    {
74                            log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
75                            if (close(stdin_epollfd) < 0)
76                            {
77                                    log_error("close(stdin_epollfd) error (%d)\n");
78                            }
79                            stdin_epollfd = -1;
80                            return -1;
81                    }
82    
83                    stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
84                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
85            }
86    
87            if (stdout_epollfd == -1)
88            {
89                    stdout_epollfd = epoll_create1(0);
90                    if (stdout_epollfd == -1)
91                    {
92                            log_error("epoll_create1() error (%d)\n", errno);
93                            return -1;
94                    }
95    
96                    ev.events = EPOLLOUT;
97                    ev.data.fd = STDOUT_FILENO;
98                    if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
99                    {
100                            log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
101                            if (close(stdout_epollfd) < 0)
102                            {
103                                    log_error("close(stdout_epollfd) error (%d)\n");
104                            }
105                            stdout_epollfd = -1;
106                            return -1;
107                    }
108    
109                    // Set STDOUT as non-blocking
110                    stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
111                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
112            }
113    
114            return 0;
115    }
116    
117    void io_cleanup(void)
118    {
119            if (stdin_epollfd != -1)
120            {
121                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
122    
123                    if (close(stdin_epollfd) < 0)
124                    {
125                            log_error("close(stdin_epollfd) error (%d)\n");
126                    }
127                    stdin_epollfd = -1;
128            }
129    
130            if (stdout_epollfd != -1)
131            {
132                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
133    
134                    if (close(stdout_epollfd) < 0)
135                    {
136                            log_error("close(stdout_epollfd) error (%d)\n");
137                    }
138                    stdout_epollfd = -1;
139            }
140    }
141    
142  int prints(const char *format, ...)  int prints(const char *format, ...)
143  {  {
144          char buf[BUFSIZ];          char buf[BUFSIZ];
# Line 109  int outc(char c) Line 197  int outc(char c)
197    
198  int iflush(void)  int iflush(void)
199  {  {
200          int flags;          struct epoll_event events[MAX_EVENTS];
201          struct epoll_event ev, events[MAX_EVENTS];          int nfds;
         int nfds, epollfd;  
202          int retry;          int retry;
203          int ret = 0;          int ret = 0;
204    
         epollfd = epoll_create1(0);  
         if (epollfd < 0)  
         {  
                 log_error("epoll_create1() error (%d)\n", errno);  
                 return -1;  
         }  
   
         ev.events = EPOLLOUT;  
         ev.data.fd = STDOUT_FILENO;  
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)  
         {  
                 log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);  
                 if (close(epollfd) < 0)  
                 {  
                         log_error("close(epoll) error (%d)\n");  
                 }  
                 return -1;  
         }  
   
         // Set STDOUT as non-blocking  
         flags = fcntl(STDOUT_FILENO, F_GETFL, 0);  
         fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
205          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
206          retry = 3;          retry = 3;
207          while (retry > 0 && !SYS_server_exit)          while (retry > 0 && !SYS_server_exit)
208          {          {
209                  retry--;                  retry--;
210    
211                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
212    
213                  if (nfds < 0)                  if (nfds < 0)
214                  {                  {
# Line 170  int iflush(void) Line 234  int iflush(void)
234                                          if (ret < 0)                                          if (ret < 0)
235                                          {                                          {
236                                                  log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);                                                  log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
237                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
238                                          }                                          }
239                                  }                                  }
240    
# Line 231  int iflush(void) Line 296  int iflush(void)
296                  }                  }
297          }          }
298    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
299          return ret;          return ret;
300  }  }
301    
302  int igetch(int timeout)  int igetch(int timeout)
303  {  {
304          struct epoll_event ev, events[MAX_EVENTS];          static int stdin_read_wait = 0;
305          int nfds, epollfd;  
306            struct epoll_event events[MAX_EVENTS];
307            int nfds;
308          int ret;          int ret;
309          int loop;          int loop;
310    
# Line 255  int igetch(int timeout) Line 314  int igetch(int timeout)
314          int in_ascii = 0;          int in_ascii = 0;
315          int in_control = 0;          int in_control = 0;
316          int i = 0;          int i = 0;
         int flags;  
317    
318          if (stdin_conv_offset >= stdin_conv_len)          if (stdin_conv_offset >= stdin_conv_len)
319          {          {
320                  stdin_conv_len = 0;                  stdin_conv_len = 0;
321                  stdin_conv_offset = 0;                  stdin_conv_offset = 0;
322    
                 epollfd = epoll_create1(0);  
                 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)  
                 {  
                         log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);  
   
                         if (close(epollfd) < 0)  
                         {  
                                 log_error("close(epoll) error (%d)\n");  
                         }  
                         return -1;  
                 }  
   
                 flags = fcntl(STDIN_FILENO, F_GETFL, 0);  
                 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
323                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
324                  {                  {
325                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
# Line 294  int igetch(int timeout) Line 329  int igetch(int timeout)
329                                  break;                                  break;
330                          }                          }
331    
332                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
333                          {                          {
334                                  if (errno != EINTR)                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
335    
336                                    if (nfds < 0)
337                                    {
338                                            if (errno != EINTR)
339                                            {
340                                                    log_error("epoll_wait() error (%d)\n", errno);
341                                                    break;
342                                            }
343                                            continue;
344                                    }
345                                    else if (nfds == 0) // timeout
346                                  {                                  {
347                                          log_error("epoll_wait() error (%d)\n", errno);                                          out = KEY_TIMEOUT;
348                                          break;                                          break;
349                                  }                                  }
350                                  continue;  
351                          }                                  for (int i = 0; i < nfds; i++)
352                          else if (nfds == 0) // timeout                                  {
353                          {                                          if (events[i].data.fd == STDIN_FILENO)
354                                  out = KEY_TIMEOUT;                                          {
355                                  break;                                                  stdin_read_wait = 1;
356                                            }
357                                    }
358                          }                          }
359    
360                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
361                          {                          {
362                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
363                                  {                                  {
364                                          while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
365                                          {                                          {
366                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
367                                                    if (ret == SSH_ERROR)
368                                                  {                                                  {
369                                                          ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
370                                                          if (ret == SSH_ERROR)                                                          loop = 0;
371                                                          {                                                          break;
                                                                 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
                                                         else if (ret == SSH_EOF)  
                                                         {  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
                                                         else if (ret == 0)  
                                                         {  
                                                                 out = 0;  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
372                                                  }                                                  }
373                                                  else                                                  else if (ret == SSH_EOF)
374                                                  {                                                  {
375                                                          ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);                                                          stdin_read_wait = 0;
376                                                            loop = 0;
377                                                            break;
378                                                  }                                                  }
379                                                  if (ret < 0)                                                  else if (ret == 0)
380                                                  {                                                  {
381                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
382                                                          {                                                          stdin_read_wait = 0;
383                                                                  out = 0;                                                          loop = 0;
384                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
385                                                  }                                                  }
386                                                  else if (ret == 0) // broken pipe                                          }
387                                            else
388                                            {
389                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
390                                            }
391                                            if (ret < 0)
392                                            {
393                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
394                                                  {                                                  {
395                                                            out = 0;
396                                                            stdin_read_wait = 0;
397                                                          loop = 0;                                                          loop = 0;
398                                                          break;                                                          break;
399                                                  }                                                  }
400                                                  else                                                  else if (errno == EINTR)
401                                                  {                                                  {
                                                         stdin_buf_len += ret;  
402                                                          continue;                                                          continue;
403                                                  }                                                  }
404                                                    else
405                                                    {
406    #ifdef _DEBUG
407                                                            log_error("read(STDIN) error (%d)\n", errno);
408    #endif
409                                                            loop = 0;
410                                                            break;
411                                                    }
412                                            }
413                                            else if (ret == 0) // broken pipe
414                                            {
415                                                    stdin_read_wait = 0;
416                                                    loop = 0;
417                                                    break;
418                                            }
419                                            else
420                                            {
421                                                    stdin_buf_len += ret;
422                                                    continue;
423                                          }                                          }
424                                  }                                  }
425                          }                          }
# Line 386  int igetch(int timeout) Line 433  int igetch(int timeout)
433  #endif  #endif
434                  }                  }
435    
                 fcntl(STDIN_FILENO, F_SETFL, flags);  
   
                 if (close(epollfd) < 0)  
                 {  
                         log_error("close(epoll) error (%d)\n");  
                 }  
   
436                  if (stdin_buf_offset < stdin_buf_len)                  if (stdin_buf_offset < stdin_buf_len)
437                  {                  {
438                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
439                          if (ret < 0)                          if (ret < 0)
440                          {                          {
441                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
442                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
443                          }                          }
444    
445                          // For debug                          // For debug
# Line 936  int io_buf_conv(iconv_t cd, char *p_buf, Line 977  int io_buf_conv(iconv_t cd, char *p_buf,
977                  {                  {
978                          if (out_bytes <= 0)                          if (out_bytes <= 0)
979                          {                          {
980                                  log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);                                  log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
981                                  return -2;                                  return -2;
982                          }                          }
983    
# Line 988  int io_buf_conv(iconv_t cd, char *p_buf, Line 1029  int io_buf_conv(iconv_t cd, char *p_buf,
1029                                          return -2;                                          return -2;
1030                                  }                                  }
1031    
1032                                    // reset in_bytes when "//IGNORE" is applied
1033                                    if (in_bytes == 0)
1034                                    {
1035                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1036                                    }
1037    
1038                                  *out_buf = *in_buf;                                  *out_buf = *in_buf;
1039                                  in_buf++;                                  in_buf++;
1040                                  out_buf++;                                  out_buf++;
# Line 1012  int io_buf_conv(iconv_t cd, char *p_buf, Line 1059  int io_buf_conv(iconv_t cd, char *p_buf,
1059    
1060  int io_conv_init(const char *charset)  int io_conv_init(const char *charset)
1061  {  {
1062            char tocode[CHARSET_MAX_LEN + 20];
1063    
1064          if (charset == NULL)          if (charset == NULL)
1065          {          {
1066                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
# Line 1020  int io_conv_init(const char *charset) Line 1069  int io_conv_init(const char *charset)
1069    
1070          io_conv_cleanup();          io_conv_cleanup();
1071    
1072          snprintf(stdio_charset, sizeof(stdio_charset), "%s//TRANSLIT", charset);          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1073            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1074    
1075          stdin_cd = iconv_open(BBS_DEFAULT_CHARSET, stdio_charset);          snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1076                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1077            stdin_cd = iconv_open(tocode, stdio_charset);
1078          if (stdin_cd == (iconv_t)(-1))          if (stdin_cd == (iconv_t)(-1))
1079          {          {
1080                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET, errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1081                  return -2;                  return -2;
1082          }          }
1083          stdout_cd = iconv_open(stdio_charset, BBS_DEFAULT_CHARSET);  
1084            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1085                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1086            stdout_cd = iconv_open(tocode, BBS_default_charset);
1087          if (stdout_cd == (iconv_t)(-1))          if (stdout_cd == (iconv_t)(-1))
1088          {          {
1089                  log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, stdio_charset, errno);                  log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1090                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1091                  return -2;                  return -2;
1092          }          }


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

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