/[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.35 by sysadm, Wed May 28 05:47:37 2025 UTC Revision 1.78 by sysadm, Thu Dec 18 11:37:08 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"
14  #include "io.h"  #include "io.h"
15  #include "log.h"  #include "log.h"
 #include "common.h"  
16  #include <errno.h>  #include <errno.h>
17  #include <stdio.h>  #include <fcntl.h>
18  #include <stdarg.h>  #include <stdarg.h>
19    #include <stdio.h>
20  #include <string.h>  #include <string.h>
21  #include <time.h>  #include <time.h>
 #include <fcntl.h>  
22  #include <unistd.h>  #include <unistd.h>
 #include <sys/select.h>  
23  #include <sys/ioctl.h>  #include <sys/ioctl.h>
24    #include <sys/select.h>
25    #include <libssh/callbacks.h>
26    #include <libssh/libssh.h>
27    #include <libssh/server.h>
28    
29    #ifdef HAVE_SYS_EPOLL_H
30  #include <sys/epoll.h>  #include <sys/epoll.h>
31    #else
32    #include <poll.h>
33    #endif
34    
35  static char stdout_buf[BUFSIZ];  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
53    static char stdin_buf[LINE_BUFFER_LEN];
54    static char stdout_buf[OUTPUT_BUF_SIZE];
55    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;
58  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
59    
60    static char stdin_conv[LINE_BUFFER_LEN * 2];
61    static char stdout_conv[OUTPUT_BUF_SIZE * 2];
62    static int stdin_conv_len = 0;
63    static int stdout_conv_len = 0;
64    static int stdin_conv_offset = 0;
65    static int stdout_conv_offset = 0;
66    
67    static iconv_t stdin_cd = (iconv_t)(-1);
68    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", errno);
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", errno);
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 44  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                            ret = -1;
266                  }                  }
267          }          }
268    
# Line 67  int prints(const char *format, ...) Line 271  int prints(const char *format, ...)
271    
272  int outc(char c)  int outc(char c)
273  {  {
274          int ret;          int ret = 0;
275    
276          if (stdout_buf_len + 1 > BUFSIZ)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
277          {          {
278                  iflush();                  iflush();
279          }          }
280    
281          if (stdout_buf_len + 1 <= BUFSIZ)          if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE)
282          {          {
283                  stdout_buf[stdout_buf_len] = c;                  stdout_buf[stdout_buf_len] = c;
284                  stdout_buf_len++;                  stdout_buf_len++;
# Line 88  int outc(char c) Line 292  int outc(char c)
292          return ret;          return ret;
293  }  }
294    
295  int iflush()  int iflush(void)
296  {  {
297          int flags;  #ifdef HAVE_SYS_EPOLL_H
298          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
299          int nfds, epollfd;  #else
300          int retry;          struct pollfd pfds[1];
301          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;  
         }  
302    
303          // Set STDOUT as non-blocking          int nfds;
304          flags = fcntl(STDOUT_FILENO, F_GETFL, 0);          int ret = 0;
         fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);  
305    
306          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
307          retry = 3;          for (int retry = 3; retry > 0 && !SYS_server_exit; retry--)
         while (retry > 0 && !SYS_server_exit)  
308          {          {
309                  retry--;  #ifdef HAVE_SYS_EPOLL_H
310                    nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
311                    ret = nfds;
312    #else
313                    pfds[0].fd = STDOUT_FILENO;
314                    pfds[0].events = POLLOUT;
315                    nfds = 1;
316                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
317    #endif
318    
319                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  if (ret < 0)
   
                 if (nfds < 0)  
320                  {                  {
321                          if (errno != EINTR)                          if (errno != EINTR)
322                          {                          {
323    #ifdef HAVE_SYS_EPOLL_H
324                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
325    #else
326                                    log_error("poll() error (%d)\n", errno);
327    #endif
328                                  break;                                  break;
329                          }                          }
330                          continue;                          continue;
331                  }                  }
332                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
333                  {                  {
334                          continue;                          continue;
335                  }                  }
336    
337                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
338                  {                  {
339                          if (events[i].data.fd == STDOUT_FILENO)  #ifdef HAVE_SYS_EPOLL_H
340                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
341    #else
342                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
343    #endif
344                            {
345    #ifdef HAVE_SYS_EPOLL_H
346                                    log_debug("STDOUT error events (%d)\n", events[i].events);
347    #else
348                                    log_debug("STDOUT error events (%d)\n", pfds[i].revents);
349    #endif
350                                    retry = 0;
351                                    break;
352                            }
353    
354    #ifdef HAVE_SYS_EPOLL_H
355                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
356    #else
357                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
358    #endif
359                          {                          {
360                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
361                                  {                                  {
362                                          ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset));                                          ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
363                                            if (ret < 0)
364                                            {
365                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
366                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
367                                            }
368                                    }
369    
370                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
371                                    {
372                                            if (SSH_v2)
373                                            {
374                                                    ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset));
375                                                    if (ret == SSH_ERROR)
376                                                    {
377                                                            log_debug("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
378                                                            retry = 0;
379                                                            break;
380                                                    }
381                                            }
382                                            else
383                                            {
384                                                    ret = (int)write(STDOUT_FILENO, stdout_conv + stdout_conv_offset, (size_t)(stdout_conv_len - stdout_conv_offset));
385                                            }
386                                          if (ret < 0)                                          if (ret < 0)
387                                          {                                          {
388                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
# Line 160  int iflush() Line 395  int iflush()
395                                                  }                                                  }
396                                                  else                                                  else
397                                                  {                                                  {
398                                                          log_error("write(STDOUT) error (%d)\n", errno);                                                          log_debug("write(STDOUT) error (%d)\n", errno);
399                                                          retry = 0;                                                          retry = 0;
400                                                          break;                                                          break;
401                                                  }                                                  }
# Line 172  int iflush() Line 407  int iflush()
407                                          }                                          }
408                                          else                                          else
409                                          {                                          {
410                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
411                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
412                                                  {                                                  {
413                                                          ret = 0;                                                          ret = 0;
414                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
415                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
416                                                          retry = 0;                                                          retry = 0;
417                                                          break;                                                          break;
418                                                  }                                                  }
# Line 188  int iflush() Line 423  int iflush()
423                  }                  }
424          }          }
425    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
426          return ret;          return ret;
427  }  }
428    
429  int igetch(int timeout)  int igetch(int timeout)
430  {  {
431          // static input buffer          static int stdin_read_wait = 0;
432          static unsigned char buf[LINE_BUFFER_LEN];  
433          static int len = 0;  #ifdef HAVE_SYS_EPOLL_H
434          static int pos = 0;          struct epoll_event events[MAX_EVENTS];
435    #else
436            struct pollfd pfds[1];
437    #endif
438    
439          struct epoll_event ev, events[MAX_EVENTS];          int nfds;
         int nfds, epollfd;  
440          int ret;          int ret;
441          int loop;          int loop;
442    
# Line 217  int igetch(int timeout) Line 446  int igetch(int timeout)
446          int in_ascii = 0;          int in_ascii = 0;
447          int in_control = 0;          int in_control = 0;
448          int i = 0;          int i = 0;
         int flags;  
449    
450          epollfd = epoll_create1(0);          if (stdin_conv_offset >= stdin_conv_len)
         if (epollfd < 0)  
451          {          {
452                  log_error("epoll_create1() error (%d)\n", errno);                  stdin_conv_len = 0;
453                  return -1;                  stdin_conv_offset = 0;
         }  
454    
455          ev.events = EPOLLIN;                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
         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)  
456                  {                  {
457                          log_error("close(epoll) error (%d)\n");                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
458                  }                          {
459                  return -1;                                  log_debug("SSH channel is closed\n");
460          }                                  loop = 0;
461                                    break;
462          flags = fcntl(STDIN_FILENO, F_GETFL, 0);                          }
         fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
463    
464          loop = 1;                          if (!stdin_read_wait)
465                            {
466    #ifdef HAVE_SYS_EPOLL_H
467                                    nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
468                                    ret = nfds;
469    #else
470                                    pfds[0].fd = STDIN_FILENO;
471                                    pfds[0].events = POLLIN;
472                                    nfds = 1;
473                                    ret = poll(pfds, (nfds_t)nfds, timeout);
474    #endif
475    
476          while (loop && pos >= len && !SYS_server_exit)                                  if (ret < 0)
477          {                                  {
478                  len = 0;                                          if (errno != EINTR)
479                  pos = 0;                                          {
480    #ifdef HAVE_SYS_EPOLL_H
481                                                    log_error("epoll_wait() error (%d)\n", errno);
482    #else
483                                                    log_error("poll() error (%d)\n", errno);
484    #endif
485                                                    break;
486                                            }
487                                            continue;
488                                    }
489                                    else if (ret == 0) // timeout
490                                    {
491                                            out = KEY_TIMEOUT;
492                                            break;
493                                    }
494    
495                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                                  for (int i = 0; i < nfds; i++)
496                                    {
497    #ifdef HAVE_SYS_EPOLL_H
498                                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
499    #else
500                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
501    #endif
502                                            {
503    #ifdef HAVE_SYS_EPOLL_H
504                                                    log_debug("STDIN error events (%d)\n", events[i].events);
505    #else
506                                                    log_debug("STDIN error events (%d)\n", pfds[i].revents);
507    #endif
508                                                    loop = 0;
509                                                    break;
510                                            }
511    
512                  if (nfds < 0)  #ifdef HAVE_SYS_EPOLL_H
513                  {                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
514                          if (errno != EINTR)  #else
515                          {                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
516                                  log_error("epoll_wait() error (%d)\n", errno);  #endif
517                                  break;                                          {
518                                                    stdin_read_wait = 1;
519                                            }
520                                    }
521                          }                          }
                         continue;  
                 }  
                 else if (nfds == 0) // timeout  
                 {  
                         out = KEY_TIMEOUT;  
                         break;  
                 }  
522    
523                  for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
                 {  
                         if (events[i].data.fd == STDIN_FILENO)  
524                          {                          {
525                                  while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
526                                  {                                  {
527                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                          if (SSH_v2)
528                                            {
529                                                    ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
530                                                    if (ret == SSH_ERROR)
531                                                    {
532                                                            log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
533                                                            loop = 0;
534                                                            break;
535                                                    }
536                                                    else if (ret == SSH_EOF)
537                                                    {
538                                                            stdin_read_wait = 0;
539                                                            loop = 0;
540                                                            break;
541                                                    }
542                                                    else if (ret == 0)
543                                                    {
544                                                            out = 0;
545                                                            stdin_read_wait = 0;
546                                                            loop = 0;
547                                                            break;
548                                                    }
549                                            }
550                                            else
551                                            {
552                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
553                                            }
554                                          if (ret < 0)                                          if (ret < 0)
555                                          {                                          {
556                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
557                                                  {                                                  {
558                                                          out = 0;                                                          out = 0;
559                                                            stdin_read_wait = 0;
560                                                          loop = 0;                                                          loop = 0;
561                                                          break;                                                          break;
562                                                  }                                                  }
# Line 287  int igetch(int timeout) Line 566  int igetch(int timeout)
566                                                  }                                                  }
567                                                  else                                                  else
568                                                  {                                                  {
569                                                          log_error("read(STDIN) error (%d)\n", errno);                                                          log_debug("read(STDIN) error (%d)\n", errno);
570                                                          loop = 0;                                                          loop = 0;
571                                                          break;                                                          break;
572                                                  }                                                  }
573                                          }                                          }
574                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
575                                          {                                          {
576                                                    stdin_read_wait = 0;
577                                                  loop = 0;                                                  loop = 0;
578                                                  break;                                                  break;
579                                          }                                          }
580                                          else                                          else
581                                          {                                          {
582                                                  len += ret;                                                  stdin_buf_len += ret;
583                                                  continue;                                                  continue;
584                                          }                                          }
585                                  }                                  }
586                          }                          }
587    
588                            // For debug
589    #ifdef _DEBUG
590                            for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
591                            {
592                                    log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
593                            }
594    #endif
595                  }                  }
596    
597                  // For debug                  if (stdin_buf_offset < stdin_buf_len)
598                  // for (int j = pos; j < len; j++)                  {
599                  // {                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
600                  //      log_std("Debug: <--[%u]\n", (buf[j] + 256) % 256);                          if (ret < 0)
601                  // }                          {
602          }                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
603                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
604                            }
605    
606          fcntl(STDIN_FILENO, F_SETFL, flags);                          // For debug
607    #ifdef _DEBUG
608                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
609                            {
610                                    log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
611                            }
612    #endif
613                    }
614            }
615    
616          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
617          {          {
618                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
619    
620                    // Convert \r\n to \r
621                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
622                    {
623                            stdin_conv_offset++;
624                    }
625    
626                    // Convert single \n to \r
627                    if (c == LF)
628                    {
629                            c = CR;
630                    }
631    
632                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
633                  {                  {
# Line 341  int igetch(int timeout) Line 651  int igetch(int timeout)
651                          continue;                          continue;
652                  }                  }
653    
654                  if (c == ESC_KEY)                  if (c == KEY_ESC)
655                  {                  {
656                          if (in_esc == 0)                          if (in_esc == 0)
657                          {                          {
# Line 352  int igetch(int timeout) Line 662  int igetch(int timeout)
662                          }                          }
663                          else                          else
664                          {                          {
665                                  out = ESC_KEY;                                  out = KEY_CSI;
666                                  in_esc = 0;                                  in_esc = 0;
667                                  break;                                  break;
668                          }                          }
# Line 488  int igetch(int timeout) Line 798  int igetch(int timeout)
798                                  case 49:                                  case 49:
799                                          out = KEY_HOME;                                          out = KEY_HOME;
800                                          break;                                          break;
801                                    case 50:
802                                            out = KEY_INS;
803                                            break;
804                                  case 51:                                  case 51:
805                                          out = KEY_DEL;                                          out = KEY_DEL;
806                                          break;                                          break;
# Line 508  int igetch(int timeout) Line 821  int igetch(int timeout)
821                                          break;                                          break;
822                                  }                                  }
823                          }                          }
824                          if (i == 4 && tmp[0] == 91 && tmp[1] == 49 && tmp[3] == 126)  // Fterm                          if (i == 4 && tmp[0] == 91 && tmp[1] == 49 && tmp[3] == 126) // Fterm
825                          {                          {
826                                  in_ascii = 0;                                  in_ascii = 0;
827                                  switch (tmp[2])                                  switch (tmp[2])
# Line 578  int igetch(int timeout) Line 891  int igetch(int timeout)
891                                  in_ascii = 0;                                  in_ascii = 0;
892                                  switch (tmp[4])                                  switch (tmp[4])
893                                  {                                  {
894                                    case 65:
895                                            out = KEY_CTRL_UP;
896                                            break;
897                                    case 66:
898                                            out = KEY_CTRL_DOWN;
899                                            break;
900                                    case 67:
901                                            out = KEY_CTRL_RIGHT;
902                                            break;
903                                    case 68:
904                                            out = KEY_CTRL_LEFT;
905                                            break;
906                                    case 70:
907                                            out = KEY_CTRL_END;
908                                            break;
909                                    case 72:
910                                            out = KEY_CTRL_HOME;
911                                            break;
912                                  case 80:                                  case 80:
913                                          out = KEY_CTRL_F1;                                          out = KEY_CTRL_F1;
914                                          break;                                          break;
# Line 732  int igetch(int timeout) Line 1063  int igetch(int timeout)
1063                  break;                  break;
1064          }          }
1065    
1066          if (close(epollfd) < 0)          // For ESC key
1067            if (out == 0 && in_esc)
1068          {          {
1069                  log_error("close(epoll) error (%d)\n");                  out = KEY_ESC;
1070          }          }
1071    
1072          // for debug          // for debug
1073          // if (out != KEY_TIMEOUT && out != KEY_NULL)  #ifdef _DEBUG
1074          // {          if (out != KEY_TIMEOUT && out != KEY_NULL)
1075          //      log_std ("Debug: -->[0x %x]\n", out);          {
1076          // }                  log_debug("output: -->[0x %x]\n", out);
1077            }
1078    #endif
1079    
1080          return out;          return out;
1081  }  }
# Line 749  int igetch(int timeout) Line 1083  int igetch(int timeout)
1083  int igetch_t(int sec)  int igetch_t(int sec)
1084  {  {
1085          int ch;          int ch;
1086          time_t t_begin = time(0);          time_t t_begin = time(NULL);
1087    
1088          do          do
1089          {          {
1090                  ch = igetch(100);                  ch = igetch(100);
1091          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec));
1092    
1093          return ch;          return ch;
1094  }  }
# Line 764  void igetch_reset() Line 1098  void igetch_reset()
1098          int ch;          int ch;
1099          do          do
1100          {          {
1101                  ch = igetch(0);                  ch = igetch(100);
1102          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
1103  }  }
1104    
1105    int io_buf_conv(iconv_t cd, char *p_buf, int *p_buf_len, int *p_buf_offset, char *p_conv, size_t conv_size, int *p_conv_len)
1106    {
1107            char *in_buf;
1108            char *out_buf;
1109            size_t in_bytes;
1110            size_t out_bytes;
1111            int ret;
1112            int in_control = 0;
1113            size_t i = 0;
1114            int skip_current = 0;
1115    
1116            if (cd == (iconv_t)(-1) || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1117            {
1118                    log_error("NULL pointer error\n");
1119                    return -1;
1120            }
1121    
1122            in_buf = p_buf + *p_buf_offset;
1123            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1124            out_buf = p_conv + *p_conv_len;
1125            out_bytes = conv_size - (size_t)(*p_conv_len);
1126    
1127            while (in_bytes > 0)
1128            {
1129                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
1130                    {
1131                            if (in_control == 0)
1132                            {
1133                                    in_control = 1;
1134                                    i = 0;
1135                            }
1136                    }
1137    
1138                    if (in_control || skip_current)
1139                    {
1140                            skip_current = 0;
1141    
1142                            if (out_bytes <= 0)
1143                            {
1144                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
1145                                    return -2;
1146                            }
1147    
1148                            *out_buf = *in_buf;
1149                            in_buf++;
1150                            out_buf++;
1151                            in_bytes--;
1152                            out_bytes--;
1153    
1154                            (*p_buf_offset)++;
1155                            (*p_conv_len)++;
1156    
1157                            i++;
1158                            if (i >= 2)
1159                            {
1160                                    in_control = 0;
1161                            }
1162                            continue;
1163                    }
1164    
1165                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
1166                    if (ret == -1)
1167                    {
1168                            if (errno == EINVAL) // Incomplete
1169                            {
1170                                    log_debug("iconv(inbytes=%zu, outbytes=%zu) error: EINVAL, in_buf[0]=%d\n",
1171                                                      in_bytes, out_bytes, (unsigned char)in_buf[0]);
1172                                    if (p_buf != in_buf)
1173                                    {
1174                                            *p_buf_len -= (int)(in_buf - p_buf);
1175                                            *p_buf_offset = 0;
1176                                            *p_conv_len = (int)(conv_size - out_bytes);
1177                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
1178                                    }
1179    
1180                                    break;
1181                            }
1182                            else if (errno == E2BIG)
1183                            {
1184                                    log_error("iconv(inbytes=%zu, outbytes=%zu) error: E2BIG\n", in_bytes, out_bytes);
1185                                    return -1;
1186                            }
1187                            else if (errno == EILSEQ)
1188                            {
1189                                    if (in_bytes > out_bytes || out_bytes <= 0)
1190                                    {
1191                                            log_error("iconv(inbytes=%zu, outbytes=%zu) error: EILSEQ\n", in_bytes, out_bytes);
1192                                            return -2;
1193                                    }
1194    
1195                                    // reset in_bytes when "//IGNORE" is applied
1196                                    if (in_bytes == 0)
1197                                    {
1198                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1199                                            log_debug("Reset in_bytes from 0 to %zu\n", in_bytes);
1200                                    }
1201    
1202                                    log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: EILSEQ, in_buf[0]=%d\n",
1203                                                      in_bytes, out_bytes, (unsigned char)in_buf[0]);
1204                                    skip_current = 1;
1205                            }
1206                            else // something strange
1207                            {
1208                                    log_debug("iconv(in_bytes=%zu, out_bytes=%zu) error: %d, in_buf[0]=%d\n",
1209                                                      in_bytes, out_bytes, errno, (unsigned char)in_buf[0]);
1210                                    *p_buf_offset = (int)(in_buf - p_buf);
1211                                    *p_conv_len = (int)(conv_size - out_bytes);
1212                                    skip_current = 1;
1213                            }
1214                    }
1215                    else
1216                    {
1217                            *p_buf_offset = (int)(in_buf - p_buf);
1218                            *p_conv_len = (int)(conv_size - out_bytes);
1219                    }
1220            }
1221    
1222            if (*p_buf_offset >= *p_buf_len)
1223            {
1224                    *p_buf_len = 0;
1225                    *p_buf_offset = 0;
1226            }
1227    
1228            return 0;
1229    }
1230    
1231    int io_conv_init(const char *charset)
1232    {
1233            char tocode[CHARSET_MAX_LEN + 20];
1234    
1235            if (charset == NULL)
1236            {
1237                    log_error("NULL pointer error\n");
1238                    return -1;
1239            }
1240    
1241            io_conv_cleanup();
1242    
1243            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1244            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1245    
1246            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1247                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1248            stdin_cd = iconv_open(tocode, stdio_charset);
1249            if (stdin_cd == (iconv_t)(-1))
1250            {
1251                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1252                    return -2;
1253            }
1254    
1255            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1256                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1257            stdout_cd = iconv_open(tocode, BBS_default_charset);
1258            if (stdout_cd == (iconv_t)(-1))
1259            {
1260                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1261                    iconv_close(stdin_cd);
1262                    stdin_cd = (iconv_t)(-1);
1263                    return -2;
1264            }
1265    
1266            return 0;
1267    }
1268    
1269    int io_conv_cleanup(void)
1270    {
1271            if (stdin_cd != (iconv_t)(-1))
1272            {
1273                    iconv_close(stdin_cd);
1274                    stdin_cd = (iconv_t)(-1);
1275            }
1276            if (stdout_cd != (iconv_t)(-1))
1277            {
1278                    iconv_close(stdout_cd);
1279                    stdout_cd = (iconv_t)(-1);
1280            }
1281    
1282            return 0;
1283    }


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

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