/[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.59 by sysadm, Sun Oct 19 13:13:07 2025 UTC Revision 1.65 by sysadm, Tue Nov 11 00:28:05 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[20] = 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  // static input / output buffer  // static input / output buffer
34  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
# Line 170  int iflush(void) Line 167  int iflush(void)
167                                          if (ret < 0)                                          if (ret < 0)
168                                          {                                          {
169                                                  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);
170                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
171                                          }                                          }
172                                  }                                  }
173    
# Line 244  int iflush(void) Line 242  int iflush(void)
242    
243  int igetch(int timeout)  int igetch(int timeout)
244  {  {
245            static int stdin_read_wait = 0;
246    
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 294  int igetch(int timeout) Line 294  int igetch(int timeout)
294                                  break;                                  break;
295                          }                          }
296    
297                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
298                          {                          {
299                                  if (errno != EINTR)                                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);
300    
301                                    if (nfds < 0)
302                                    {
303                                            if (errno != EINTR)
304                                            {
305                                                    log_error("epoll_wait() error (%d)\n", errno);
306                                                    break;
307                                            }
308                                            continue;
309                                    }
310                                    else if (nfds == 0) // timeout
311                                  {                                  {
312                                          log_error("epoll_wait() error (%d)\n", errno);                                          out = KEY_TIMEOUT;
313                                          break;                                          break;
314                                  }                                  }
315                                  continue;  
316                          }                                  for (int i = 0; i < nfds; i++)
317                          else if (nfds == 0) // timeout                                  {
318                          {                                          if (events[i].data.fd == STDIN_FILENO)
319                                  out = KEY_TIMEOUT;                                          {
320                                  break;                                                  stdin_read_wait = 1;
321                                            }
322                                    }
323                          }                          }
324    
325                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
326                          {                          {
327                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
328                                  {                                  {
329                                          while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
330                                          {                                          {
331                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
332                                                    if (ret == SSH_ERROR)
333                                                  {                                                  {
334                                                          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));
335                                                          if (ret == SSH_ERROR)                                                          loop = 0;
336                                                          {                                                          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;  
                                                         }  
337                                                  }                                                  }
338                                                  else                                                  else if (ret == SSH_EOF)
339                                                  {                                                  {
340                                                          ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);                                                          stdin_read_wait = 0;
341                                                            loop = 0;
342                                                            break;
343                                                  }                                                  }
344                                                  if (ret < 0)                                                  else if (ret == 0)
345                                                  {                                                  {
346                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
347                                                          {                                                          stdin_read_wait = 0;
348                                                                  out = 0;                                                          loop = 0;
349                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
350                                                  }                                                  }
351                                                  else if (ret == 0) // broken pipe                                          }
352                                            else
353                                            {
354                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
355                                            }
356                                            if (ret < 0)
357                                            {
358                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
359                                                  {                                                  {
360                                                            out = 0;
361                                                            stdin_read_wait = 0;
362                                                          loop = 0;                                                          loop = 0;
363                                                          break;                                                          break;
364                                                  }                                                  }
365                                                  else                                                  else if (errno == EINTR)
366                                                  {                                                  {
                                                         stdin_buf_len += ret;  
367                                                          continue;                                                          continue;
368                                                  }                                                  }
369                                                    else
370                                                    {
371    #ifdef _DEBUG
372                                                            log_error("read(STDIN) error (%d)\n", errno);
373    #endif
374                                                            loop = 0;
375                                                            break;
376                                                    }
377                                            }
378                                            else if (ret == 0) // broken pipe
379                                            {
380                                                    stdin_read_wait = 0;
381                                                    loop = 0;
382                                                    break;
383                                            }
384                                            else
385                                            {
386                                                    stdin_buf_len += ret;
387                                                    continue;
388                                          }                                          }
389                                  }                                  }
390                          }                          }
# Line 399  int igetch(int timeout) Line 411  int igetch(int timeout)
411                          if (ret < 0)                          if (ret < 0)
412                          {                          {
413                                  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);
414                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
415                          }                          }
416    
417                          // For debug                          // For debug
# Line 936  int io_buf_conv(iconv_t cd, char *p_buf, Line 949  int io_buf_conv(iconv_t cd, char *p_buf,
949                  {                  {
950                          if (out_bytes <= 0)                          if (out_bytes <= 0)
951                          {                          {
952                                  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);
953                                  return -2;                                  return -2;
954                          }                          }
955    
# Line 1018  int io_buf_conv(iconv_t cd, char *p_buf, Line 1031  int io_buf_conv(iconv_t cd, char *p_buf,
1031    
1032  int io_conv_init(const char *charset)  int io_conv_init(const char *charset)
1033  {  {
1034          char tocode[32];          char tocode[CHARSET_MAX_LEN + 20];
1035    
1036          if (charset == NULL)          if (charset == NULL)
1037          {          {
# Line 1031  int io_conv_init(const char *charset) Line 1044  int io_conv_init(const char *charset)
1044          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1045          stdio_charset[sizeof(stdio_charset) - 1] = '\0';          stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1046    
1047          stdin_cd = iconv_open(BBS_DEFAULT_CHARSET "//IGNORE", stdio_charset);          snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1048                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1049            stdin_cd = iconv_open(tocode, stdio_charset);
1050          if (stdin_cd == (iconv_t)(-1))          if (stdin_cd == (iconv_t)(-1))
1051          {          {
1052                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, BBS_DEFAULT_CHARSET "//IGNORE", errno);                  log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1053                  return -2;                  return -2;
1054          }          }
1055    
1056          snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset);          snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1057          stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);                           (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1058            stdout_cd = iconv_open(tocode, BBS_default_charset);
1059          if (stdout_cd == (iconv_t)(-1))          if (stdout_cd == (iconv_t)(-1))
1060          {          {
1061                  log_error("iconv_open(%s->%s) error: %d\n", BBS_DEFAULT_CHARSET, tocode, errno);                  log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1062                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1063                  return -2;                  return -2;
1064          }          }


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

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