/[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.77 by sysadm, Thu Dec 18 11:18:29 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 24  Line 20 
20  #include <string.h>  #include <string.h>
21  #include <time.h>  #include <time.h>
22  #include <unistd.h>  #include <unistd.h>
 #include <sys/epoll.h>  
23  #include <sys/ioctl.h>  #include <sys/ioctl.h>
24  #include <sys/select.h>  #include <sys/select.h>
25  #include <libssh/callbacks.h>  #include <libssh/callbacks.h>
26  #include <libssh/libssh.h>  #include <libssh/libssh.h>
27  #include <libssh/server.h>  #include <libssh/server.h>
28    
29  char stdio_charset[20] = BBS_DEFAULT_CHARSET;  #ifdef HAVE_SYS_EPOLL_H
30    #include <sys/epoll.h>
31    #else
32    #include <poll.h>
33    #endif
34    
35    enum _io_constant_t
36    {
37            OUTPUT_BUF_SIZE = 8192,
38    };
39    
40    const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
41    char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
42    
43    #ifdef HAVE_SYS_EPOLL_H
44    // epoll for STDIO
45    static int stdin_epollfd = -1;
46    static int stdout_epollfd = -1;
47    #endif
48    
49    static int stdin_flags = 0;
50    static int stdout_flags = 0;
51    
52  // static input / output buffer  // static input / output buffer
53  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
54  static char stdout_buf[BUFSIZ];  static char stdout_buf[OUTPUT_BUF_SIZE];
55  static int stdin_buf_len = 0;  static int stdin_buf_len = 0;
56  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
57  static int stdin_buf_offset = 0;  static int stdin_buf_offset = 0;
58  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
59    
60  static char stdin_conv[LINE_BUFFER_LEN * 2];  static char stdin_conv[LINE_BUFFER_LEN * 2];
61  static char stdout_conv[BUFSIZ * 2];  static char stdout_conv[OUTPUT_BUF_SIZE * 2];
62  static int stdin_conv_len = 0;  static int stdin_conv_len = 0;
63  static int stdout_conv_len = 0;  static int stdout_conv_len = 0;
64  static int stdin_conv_offset = 0;  static int stdin_conv_offset = 0;
65  static int stdout_conv_offset = 0;  static int stdout_conv_offset = 0;
66    
67  static iconv_t stdin_cd = NULL;  static iconv_t stdin_cd = (iconv_t)(-1);
68  static iconv_t stdout_cd = NULL;  static iconv_t stdout_cd = (iconv_t)(-1);
69    
70    int io_init(void)
71    {
72    #ifdef HAVE_SYS_EPOLL_H
73            struct epoll_event ev;
74    
75            if (stdin_epollfd == -1)
76            {
77                    stdin_epollfd = epoll_create1(0);
78                    if (stdin_epollfd == -1)
79                    {
80                            log_error("epoll_create1() error (%d)\n", errno);
81                            return -1;
82                    }
83    
84                    ev.events = EPOLLIN;
85                    ev.data.fd = STDIN_FILENO;
86                    if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
87                    {
88                            log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
89                            if (close(stdin_epollfd) < 0)
90                            {
91                                    log_error("close(stdin_epollfd) error (%d)\n", errno);
92                            }
93                            stdin_epollfd = -1;
94                            return -1;
95                    }
96    
97                    if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
98                    {
99                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
100                            if (close(stdin_epollfd) < 0)
101                            {
102                                    log_error("close(stdin_epollfd) error (%d)\n", errno);
103                            }
104                            stdin_epollfd = -1;
105                            return -1;
106                    }
107                    if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1)
108                    {
109                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
110                            if (close(stdin_epollfd) < 0)
111                            {
112                                    log_error("close(stdin_epollfd) error (%d)\n", errno);
113                            }
114                            stdin_epollfd = -1;
115                            return -1;
116                    }
117            }
118    
119            if (stdout_epollfd == -1)
120            {
121                    stdout_epollfd = epoll_create1(0);
122                    if (stdout_epollfd == -1)
123                    {
124                            log_error("epoll_create1() error (%d)\n", errno);
125                            return -1;
126                    }
127    
128                    ev.events = EPOLLOUT;
129                    ev.data.fd = STDOUT_FILENO;
130                    if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
131                    {
132                            log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
133                            if (close(stdout_epollfd) < 0)
134                            {
135                                    log_error("close(stdout_epollfd) error (%d)\n");
136                            }
137                            stdout_epollfd = -1;
138                            return -1;
139                    }
140    
141                    if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
142                    {
143                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
144                            if (close(stdout_epollfd) < 0)
145                            {
146                                    log_error("close(stdout_epollfd) error (%d)\n", errno);
147                            }
148                            stdout_epollfd = -1;
149                            return -1;
150                    }
151                    if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1)
152                    {
153                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
154                            if (close(stdout_epollfd) < 0)
155                            {
156                                    log_error("close(stdout_epollfd) error (%d)\n", errno);
157                            }
158                            stdout_epollfd = -1;
159                            return -1;
160                    }
161            }
162    #else
163            if (stdin_flags == 0)
164            {
165                    if ((stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0)) == -1)
166                    {
167                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
168                            return -1;
169                    }
170                    if ((fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK)) == -1)
171                    {
172                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
173                            return -1;
174                    }
175            }
176    
177            if (stdout_flags == 0)
178            {
179                    if ((stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) == -1)
180                    {
181                            log_error("fcntl(F_GETFL) error (%d)\n", errno);
182                            return -1;
183                    }
184                    if ((fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK)) == -1)
185                    {
186                            log_error("fcntl(F_SETFL) error (%d)\n", errno);
187                            return -1;
188                    }
189            }
190    #endif
191    
192            return 0;
193    }
194    
195    void io_cleanup(void)
196    {
197    #ifdef HAVE_SYS_EPOLL_H
198            if (stdin_epollfd != -1)
199            {
200                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
201                    stdin_flags = 0;
202    
203                    if (close(stdin_epollfd) < 0)
204                    {
205                            log_error("close(stdin_epollfd) error (%d)\n", errno);
206                    }
207                    stdin_epollfd = -1;
208            }
209    
210            if (stdout_epollfd != -1)
211            {
212                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
213                    stdout_flags = 0;
214    
215                    if (close(stdout_epollfd) < 0)
216                    {
217                            log_error("close(stdout_epollfd) error (%d)\n");
218                    }
219                    stdout_epollfd = -1;
220            }
221    #else
222            if (stdin_flags != 0)
223            {
224                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
225                    stdin_flags = 0;
226            }
227    
228            if (stdout_flags != 0)
229            {
230                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
231                    stdout_flags = 0;
232            }
233    #endif
234    }
235    
236  int prints(const char *format, ...)  int prints(const char *format, ...)
237  {  {
238          char buf[BUFSIZ];          char buf[OUTPUT_BUF_SIZE];
239          va_list args;          va_list args;
240          int ret;          int ret;
241    
# Line 63  int prints(const char *format, ...) Line 245  int prints(const char *format, ...)
245    
246          if (ret > 0)          if (ret > 0)
247          {          {
248                  if (stdout_buf_len + ret > BUFSIZ)                  int written = (ret >= sizeof(buf) ? (int)sizeof(buf) - 1 : ret);
249    
250                    if (stdout_buf_len + written > OUTPUT_BUF_SIZE)
251                  {                  {
252                          iflush();                          iflush();
253                  }                  }
254    
255                  if (stdout_buf_len + ret <= BUFSIZ)                  if (stdout_buf_len + written <= OUTPUT_BUF_SIZE)
256                  {                  {
257                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret);                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)written);
258                          stdout_buf_len += ret;                          stdout_buf_len += written;
259                  }                  }
260                  else                  else
261                  {                  {
262                          errno = EAGAIN;                          errno = EAGAIN;
263                          ret = (BUFSIZ - stdout_buf_len - ret);                          int need = stdout_buf_len + ret - OUTPUT_BUF_SIZE;
264                          log_error("Output buffer is full, additional %d is required\n", ret);                          log_error("Output buffer is full, additional %d is required\n", need);
265                  }                  }
266          }          }
267    
# Line 86  int prints(const char *format, ...) Line 270  int prints(const char *format, ...)
270    
271  int outc(char c)  int outc(char c)
272  {  {
273          int ret;          int ret = 0;
274    
275          if (stdout_buf_len + 1 > BUFSIZ)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
276          {          {
277                  iflush();                  iflush();
278          }          }
279    
280          if (stdout_buf_len + 1 <= BUFSIZ)          if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE)
281          {          {
282                  stdout_buf[stdout_buf_len] = c;                  stdout_buf[stdout_buf_len] = c;
283                  stdout_buf_len++;                  stdout_buf_len++;
# Line 109  int outc(char c) Line 293  int outc(char c)
293    
294  int iflush(void)  int iflush(void)
295  {  {
296          int flags;  #ifdef HAVE_SYS_EPOLL_H
297          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
298          int nfds, epollfd;  #else
299          int retry;          struct pollfd pfds[1];
300          int ret = 0;  #endif
   
         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;  
         }  
301    
302          // Set STDOUT as non-blocking          int nfds;
303          flags = fcntl(STDOUT_FILENO, F_GETFL, 0);          int ret = 0;
         fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);  
304    
305          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
306          retry = 3;          for (int retry = 3; retry > 0 && !SYS_server_exit; retry--)
         while (retry > 0 && !SYS_server_exit)  
307          {          {
308                  retry--;  #ifdef HAVE_SYS_EPOLL_H
309                    nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
310                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  ret = nfds;
311    #else
312                    pfds[0].fd = STDOUT_FILENO;
313                    pfds[0].events = POLLOUT;
314                    nfds = 1;
315                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
316    #endif
317    
318                  if (nfds < 0)                  if (ret < 0)
319                  {                  {
320                          if (errno != EINTR)                          if (errno != EINTR)
321                          {                          {
322    #ifdef HAVE_SYS_EPOLL_H
323                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
324    #else
325                                    log_error("poll() error (%d)\n", errno);
326    #endif
327                                  break;                                  break;
328                          }                          }
329                          continue;                          continue;
330                  }                  }
331                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
332                  {                  {
333                          continue;                          continue;
334                  }                  }
335    
336                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
337                  {                  {
338                          if (events[i].data.fd == STDOUT_FILENO)  #ifdef HAVE_SYS_EPOLL_H
339                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
340    #else
341                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
342    #endif
343                            {
344    #ifdef HAVE_SYS_EPOLL_H
345                                    log_debug("STDOUT error events (%d)\n", events[i].events);
346    #else
347                                    log_debug("STDOUT error events (%d)\n", pfds[i].revents);
348    #endif
349                                    retry = 0;
350                                    break;
351                            }
352    
353    #ifdef HAVE_SYS_EPOLL_H
354                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
355    #else
356                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
357    #endif
358                          {                          {
359                                  if (stdout_buf_offset < stdout_buf_len)                                  if (stdout_buf_offset < stdout_buf_len)
360                                  {                                  {
# Line 170  int iflush(void) Line 362  int iflush(void)
362                                          if (ret < 0)                                          if (ret < 0)
363                                          {                                          {
364                                                  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);
365                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
366                                          }                                          }
367                                  }                                  }
368    
# Line 180  int iflush(void) Line 373  int iflush(void)
373                                                  ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset));                                                  ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset));
374                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
375                                                  {                                                  {
376                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                          log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
377                                                          retry = 0;                                                          retry = 0;
378                                                          break;                                                          break;
379                                                  }                                                  }
# Line 201  int iflush(void) Line 394  int iflush(void)
394                                                  }                                                  }
395                                                  else                                                  else
396                                                  {                                                  {
397  #ifdef _DEBUG                                                          log_debug("write(STDOUT) error (%d)\n", errno);
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
 #endif  
398                                                          retry = 0;                                                          retry = 0;
399                                                          break;                                                          break;
400                                                  }                                                  }
# Line 231  int iflush(void) Line 422  int iflush(void)
422                  }                  }
423          }          }
424    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
425          return ret;          return ret;
426  }  }
427    
428  int igetch(int timeout)  int igetch(int timeout)
429  {  {
430          struct epoll_event ev, events[MAX_EVENTS];          static int stdin_read_wait = 0;
431          int nfds, epollfd;  
432    #ifdef HAVE_SYS_EPOLL_H
433            struct epoll_event events[MAX_EVENTS];
434    #else
435            struct pollfd pfds[1];
436    #endif
437    
438            int nfds;
439          int ret;          int ret;
440          int loop;          int loop;
441    
# Line 255  int igetch(int timeout) Line 445  int igetch(int timeout)
445          int in_ascii = 0;          int in_ascii = 0;
446          int in_control = 0;          int in_control = 0;
447          int i = 0;          int i = 0;
         int flags;  
448    
449          if (stdin_conv_offset >= stdin_conv_len)          if (stdin_conv_offset >= stdin_conv_len)
450          {          {
451                  stdin_conv_len = 0;                  stdin_conv_len = 0;
452                  stdin_conv_offset = 0;                  stdin_conv_offset = 0;
453    
                 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);  
   
454                  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;)
455                  {                  {
456                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
457                          {                          {
458                                  log_error("SSH channel is closed\n");                                  log_debug("SSH channel is closed\n");
459                                  loop = 0;                                  loop = 0;
460                                  break;                                  break;
461                          }                          }
462    
463                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
464                          {                          {
465                                  if (errno != EINTR)  #ifdef HAVE_SYS_EPOLL_H
466                                    nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
467                                    ret = nfds;
468    #else
469                                    pfds[0].fd = STDIN_FILENO;
470                                    pfds[0].events = POLLIN;
471                                    nfds = 1;
472                                    ret = poll(pfds, (nfds_t)nfds, timeout);
473    #endif
474    
475                                    if (ret < 0)
476                                  {                                  {
477                                          log_error("epoll_wait() error (%d)\n", errno);                                          if (errno != EINTR)
478                                            {
479    #ifdef HAVE_SYS_EPOLL_H
480                                                    log_error("epoll_wait() error (%d)\n", errno);
481    #else
482                                                    log_error("poll() error (%d)\n", errno);
483    #endif
484                                                    break;
485                                            }
486                                            continue;
487                                    }
488                                    else if (ret == 0) // timeout
489                                    {
490                                            out = KEY_TIMEOUT;
491                                          break;                                          break;
492                                  }                                  }
493                                  continue;  
494                          }                                  for (int i = 0; i < nfds; i++)
495                          else if (nfds == 0) // timeout                                  {
496                          {  #ifdef HAVE_SYS_EPOLL_H
497                                  out = KEY_TIMEOUT;                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
498                                  break;  #else
499                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
500    #endif
501                                            {
502    #ifdef HAVE_SYS_EPOLL_H
503                                                    log_debug("STDIN error events (%d)\n", events[i].events);
504    #else
505                                                    log_debug("STDIN error events (%d)\n", pfds[i].revents);
506    #endif
507                                                    loop = 0;
508                                                    break;
509                                            }
510    
511    #ifdef HAVE_SYS_EPOLL_H
512                                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
513    #else
514                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
515    #endif
516                                            {
517                                                    stdin_read_wait = 1;
518                                            }
519                                    }
520                          }                          }
521    
522                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
523                          {                          {
524                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
525                                  {                                  {
526                                          while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
527                                          {                                          {
528                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
529                                                    if (ret == SSH_ERROR)
530                                                  {                                                  {
531                                                          ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);                                                          log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
532                                                          if (ret == SSH_ERROR)                                                          loop = 0;
533                                                          {                                                          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;  
                                                         }  
534                                                  }                                                  }
535                                                  else                                                  else if (ret == SSH_EOF)
536                                                  {                                                  {
537                                                          ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);                                                          stdin_read_wait = 0;
538                                                            loop = 0;
539                                                            break;
540                                                  }                                                  }
541                                                  if (ret < 0)                                                  else if (ret == 0)
542                                                  {                                                  {
543                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
544                                                          {                                                          stdin_read_wait = 0;
545                                                                  out = 0;                                                          loop = 0;
546                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
547                                                  }                                                  }
548                                                  else if (ret == 0) // broken pipe                                          }
549                                            else
550                                            {
551                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
552                                            }
553                                            if (ret < 0)
554                                            {
555                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
556                                                  {                                                  {
557                                                            out = 0;
558                                                            stdin_read_wait = 0;
559                                                          loop = 0;                                                          loop = 0;
560                                                          break;                                                          break;
561                                                  }                                                  }
562                                                  else                                                  else if (errno == EINTR)
563                                                  {                                                  {
                                                         stdin_buf_len += ret;  
564                                                          continue;                                                          continue;
565                                                  }                                                  }
566                                                    else
567                                                    {
568                                                            log_debug("read(STDIN) error (%d)\n", errno);
569                                                            loop = 0;
570                                                            break;
571                                                    }
572                                            }
573                                            else if (ret == 0) // broken pipe
574                                            {
575                                                    stdin_read_wait = 0;
576                                                    loop = 0;
577                                                    break;
578                                            }
579                                            else
580                                            {
581                                                    stdin_buf_len += ret;
582                                                    continue;
583                                          }                                          }
584                                  }                                  }
585                          }                          }
# Line 381  int igetch(int timeout) Line 588  int igetch(int timeout)
588  #ifdef _DEBUG  #ifdef _DEBUG
589                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
590                          {                          {
591                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);                                  log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
592                          }                          }
593  #endif  #endif
594                  }                  }
595    
                 fcntl(STDIN_FILENO, F_SETFL, flags);  
   
                 if (close(epollfd) < 0)  
                 {  
                         log_error("close(epoll) error (%d)\n");  
                 }  
   
596                  if (stdin_buf_offset < stdin_buf_len)                  if (stdin_buf_offset < stdin_buf_len)
597                  {                  {
598                          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);
599                          if (ret < 0)                          if (ret < 0)
600                          {                          {
601                                  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);
602                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
603                          }                          }
604    
605                          // For debug                          // For debug
606  #ifdef _DEBUG  #ifdef _DEBUG
607                          for (int j = stdin_conv_offset; j < stdin_conv_len; j++)                          for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
608                          {                          {
609                                  log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);                                  log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
610                          }                          }
611  #endif  #endif
612                  }                  }
# Line 871  int igetch(int timeout) Line 1072  int igetch(int timeout)
1072  #ifdef _DEBUG  #ifdef _DEBUG
1073          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
1074          {          {
1075                  log_error("Debug: -->[0x %x]\n", out);                  log_debug("output: -->[0x %x]\n", out);
1076          }          }
1077  #endif  #endif
1078    
# Line 909  int io_buf_conv(iconv_t cd, char *p_buf, Line 1110  int io_buf_conv(iconv_t cd, char *p_buf,
1110          int ret;          int ret;
1111          int in_control = 0;          int in_control = 0;
1112          size_t i = 0;          size_t i = 0;
1113            int skip_current = 0;
1114    
1115          if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)          if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1116          {          {
1117                  log_error("NULL pointer error\n");                  log_error("NULL pointer error\n");
1118                  return -1;                  return -1;
# Line 932  int io_buf_conv(iconv_t cd, char *p_buf, Line 1134  int io_buf_conv(iconv_t cd, char *p_buf,
1134                          }                          }
1135                  }                  }
1136    
1137                  if (in_control)                  if (in_control || skip_current)
1138                  {                  {
1139                            skip_current = 0;
1140    
1141                          if (out_bytes <= 0)                          if (out_bytes <= 0)
1142                          {                          {
1143                                  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);
1144                                  return -2;                                  return -2;
1145                          }                          }
1146    
# Line 947  int io_buf_conv(iconv_t cd, char *p_buf, Line 1151  int io_buf_conv(iconv_t cd, char *p_buf,
1151                          out_bytes--;                          out_bytes--;
1152    
1153                          (*p_buf_offset)++;                          (*p_buf_offset)++;
1154                          *p_conv_len = (int)(conv_size - out_bytes);                          (*p_conv_len)++;
1155    
1156                          i++;                          i++;
1157                          if (i >= 2)                          if (i >= 2)
# Line 962  int io_buf_conv(iconv_t cd, char *p_buf, Line 1166  int io_buf_conv(iconv_t cd, char *p_buf,
1166                  {                  {
1167                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
1168                          {                          {
1169  #ifdef _DEBUG                                  log_debug("iconv(inbytes=%ld, outbytes=%ld) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
                                 log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);  
 #endif  
1170                                  if (p_buf != in_buf)                                  if (p_buf != in_buf)
1171                                  {                                  {
1172                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                          *p_buf_len -= (int)(in_buf - p_buf);
1173                                          *p_buf_offset = 0;                                          *p_buf_offset = 0;
1174                                          *p_conv_len = (int)(conv_size - out_bytes);                                          *p_conv_len = (int)(conv_size - out_bytes);
1175                                          memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          memmove(p_buf, in_buf, (size_t)(*p_buf_len));
# Line 977  int io_buf_conv(iconv_t cd, char *p_buf, Line 1179  int io_buf_conv(iconv_t cd, char *p_buf,
1179                          }                          }
1180                          else if (errno == E2BIG)                          else if (errno == E2BIG)
1181                          {                          {
1182                                  log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);                                  log_error("iconv(inbytes=%ld, outbytes=%ld) error: E2BIG\n", in_bytes, out_bytes);
1183                                  return -1;                                  return -1;
1184                          }                          }
1185                          else if (errno == EILSEQ)                          else if (errno == EILSEQ)
1186                          {                          {
1187                                  if (in_bytes > out_bytes || out_bytes <= 0)                                  if (in_bytes > out_bytes || out_bytes <= 0)
1188                                  {                                  {
1189                                          log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);                                          log_error("iconv(inbytes=%ld, outbytes=%ld) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1190                                          return -2;                                          return -2;
1191                                  }                                  }
1192    
# Line 992  int io_buf_conv(iconv_t cd, char *p_buf, Line 1194  int io_buf_conv(iconv_t cd, char *p_buf,
1194                                  if (in_bytes == 0)                                  if (in_bytes == 0)
1195                                  {                                  {
1196                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);                                          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1197                                            log_debug("Reset in_bytes from 0 to %ld\n", in_bytes);
1198                                  }                                  }
1199    
1200                                  *out_buf = *in_buf;                                  log_debug("iconv(in_bytes=%ld, out_bytes=%ld) error: EILSEQ, in_buf[0]=%d\n",
1201                                  in_buf++;                                                    in_bytes, out_bytes, in_buf[0]);
1202                                  out_buf++;                                  skip_current = 1;
1203                                  in_bytes--;                          }
1204                                  out_bytes--;                          else // something strange
1205                            {
1206                                  continue;                                  log_debug("iconv(in_bytes=%ld, out_bytes=%ld) error: %d, in_buf[0]=%d\n",
1207                                                      in_bytes, out_bytes, errno, in_buf[0]);
1208                                    *p_buf_offset = (int)(in_buf - p_buf);
1209                                    *p_conv_len = (int)(conv_size - out_bytes);
1210                                    skip_current = 1;
1211                          }                          }
1212                  }                  }
1213                  else                  else
1214                  {                  {
1215                          *p_buf_len = 0;                          *p_buf_offset = (int)(in_buf - p_buf);
                         *p_buf_offset = 0;  
1216                          *p_conv_len = (int)(conv_size - out_bytes);                          *p_conv_len = (int)(conv_size - out_bytes);
   
                         break;  
1217                  }                  }
1218          }          }
1219    
1220            if (*p_buf_offset >= *p_buf_len)
1221            {
1222                    *p_buf_len = 0;
1223                    *p_buf_offset = 0;
1224            }
1225    
1226          return 0;          return 0;
1227  }  }
1228    
1229  int io_conv_init(const char *charset)  int io_conv_init(const char *charset)
1230  {  {
1231          char tocode[32];          char tocode[CHARSET_MAX_LEN + 20];
1232    
1233          if (charset == NULL)          if (charset == NULL)
1234          {          {
# Line 1031  int io_conv_init(const char *charset) Line 1241  int io_conv_init(const char *charset)
1241          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);          strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1242          stdio_charset[sizeof(stdio_charset) - 1] = '\0';          stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1243    
1244          stdin_cd = iconv_open(BBS_DEFAULT_CHARSET "//IGNORE", stdio_charset);          snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1245                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1246            stdin_cd = iconv_open(tocode, stdio_charset);
1247          if (stdin_cd == (iconv_t)(-1))          if (stdin_cd == (iconv_t)(-1))
1248          {          {
1249                  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);
1250                  return -2;                  return -2;
1251          }          }
1252    
1253          snprintf(tocode, sizeof(tocode), "%s//TRANSLIT", stdio_charset);          snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1254          stdout_cd = iconv_open(tocode, BBS_DEFAULT_CHARSET);                           (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1255            stdout_cd = iconv_open(tocode, BBS_default_charset);
1256          if (stdout_cd == (iconv_t)(-1))          if (stdout_cd == (iconv_t)(-1))
1257          {          {
1258                  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);
1259                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1260                    stdin_cd = (iconv_t)(-1);
1261                  return -2;                  return -2;
1262          }          }
1263    
# Line 1052  int io_conv_init(const char *charset) Line 1266  int io_conv_init(const char *charset)
1266    
1267  int io_conv_cleanup(void)  int io_conv_cleanup(void)
1268  {  {
1269          if (stdin_cd != NULL)          if (stdin_cd != (iconv_t)(-1))
1270          {          {
1271                  iconv_close(stdin_cd);                  iconv_close(stdin_cd);
1272                  stdin_cd = NULL;                  stdin_cd = (iconv_t)(-1);
1273          }          }
1274          if (stdout_cd != NULL)          if (stdout_cd != (iconv_t)(-1))
1275          {          {
1276                  iconv_close(stdout_cd);                  iconv_close(stdout_cd);
1277                  stdout_cd = NULL;                  stdout_cd = (iconv_t)(-1);
1278          }          }
1279    
1280          return 0;          return 0;


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

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