/[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.52 by sysadm, Thu Oct 16 12:11:31 2025 UTC Revision 1.76 by sysadm, Thu Dec 18 03:23:48 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                          io.c  -  description  /*
3                                                           -------------------   * io
4          Copyright            : (C) 2004-2025 by Leaflet   *   - basic terminal-based user input / output features
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
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  static char stdout_buf[BUFSIZ];  #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
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");
92                            }
93                            stdin_epollfd = -1;
94                            return -1;
95                    }
96    
97                    stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
98                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
99            }
100    
101            if (stdout_epollfd == -1)
102            {
103                    stdout_epollfd = epoll_create1(0);
104                    if (stdout_epollfd == -1)
105                    {
106                            log_error("epoll_create1() error (%d)\n", errno);
107                            return -1;
108                    }
109    
110                    ev.events = EPOLLOUT;
111                    ev.data.fd = STDOUT_FILENO;
112                    if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
113                    {
114                            log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
115                            if (close(stdout_epollfd) < 0)
116                            {
117                                    log_error("close(stdout_epollfd) error (%d)\n");
118                            }
119                            stdout_epollfd = -1;
120                            return -1;
121                    }
122    
123                    stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
124                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
125            }
126    #else
127            if (stdin_flags == 0)
128            {
129                    stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
130                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
131            }
132    
133            if (stdout_flags == 0)
134            {
135                    stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
136                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
137            }
138    #endif
139    
140            return 0;
141    }
142    
143    void io_cleanup(void)
144    {
145    #ifdef HAVE_SYS_EPOLL_H
146            if (stdin_epollfd != -1)
147            {
148                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
149                    stdin_flags = 0;
150    
151                    if (close(stdin_epollfd) < 0)
152                    {
153                            log_error("close(stdin_epollfd) error (%d)\n");
154                    }
155                    stdin_epollfd = -1;
156            }
157    
158            if (stdout_epollfd != -1)
159            {
160                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
161                    stdout_flags = 0;
162    
163                    if (close(stdout_epollfd) < 0)
164                    {
165                            log_error("close(stdout_epollfd) error (%d)\n");
166                    }
167                    stdout_epollfd = -1;
168            }
169    #else
170            if (stdin_flags != 0)
171            {
172                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
173                    stdin_flags = 0;
174            }
175    
176            if (stdout_flags != 0)
177            {
178                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
179                    stdout_flags = 0;
180            }
181    #endif
182    }
183    
184  int prints(const char *format, ...)  int prints(const char *format, ...)
185  {  {
186          char buf[BUFSIZ];          char buf[OUTPUT_BUF_SIZE];
187          va_list args;          va_list args;
188          int ret;          int ret;
189    
# Line 47  int prints(const char *format, ...) Line 193  int prints(const char *format, ...)
193    
194          if (ret > 0)          if (ret > 0)
195          {          {
196                  if (stdout_buf_len + ret > BUFSIZ)                  if (stdout_buf_len + ret > OUTPUT_BUF_SIZE)
197                  {                  {
198                          iflush();                          iflush();
199                  }                  }
200    
201                  if (stdout_buf_len + ret <= BUFSIZ)                  if (stdout_buf_len + ret <= OUTPUT_BUF_SIZE)
202                  {                  {
203                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret);                          memcpy(stdout_buf + stdout_buf_len, buf, (size_t)ret);
204                          stdout_buf_len += ret;                          stdout_buf_len += ret;
# Line 60  int prints(const char *format, ...) Line 206  int prints(const char *format, ...)
206                  else                  else
207                  {                  {
208                          errno = EAGAIN;                          errno = EAGAIN;
209                          ret = (BUFSIZ - stdout_buf_len - ret);                          ret = (OUTPUT_BUF_SIZE - stdout_buf_len - ret);
210                          log_error("Output buffer is full, additional %d is required\n", ret);                          log_error("Output buffer is full, additional %d is required\n", ret);
211                  }                  }
212          }          }
# Line 72  int outc(char c) Line 218  int outc(char c)
218  {  {
219          int ret;          int ret;
220    
221          if (stdout_buf_len + 1 > BUFSIZ)          if (stdout_buf_len + 1 > OUTPUT_BUF_SIZE)
222          {          {
223                  iflush();                  iflush();
224          }          }
225    
226          if (stdout_buf_len + 1 <= BUFSIZ)          if (stdout_buf_len + 1 <= OUTPUT_BUF_SIZE)
227          {          {
228                  stdout_buf[stdout_buf_len] = c;                  stdout_buf[stdout_buf_len] = c;
229                  stdout_buf_len++;                  stdout_buf_len++;
# Line 93  int outc(char c) Line 239  int outc(char c)
239    
240  int iflush(void)  int iflush(void)
241  {  {
242          int flags;  #ifdef HAVE_SYS_EPOLL_H
243          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
244          int nfds, epollfd;  #else
245          int retry;          struct pollfd pfds[1];
246          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;  
         }  
247    
248          // Set STDOUT as non-blocking          int nfds;
249          flags = fcntl(STDOUT_FILENO, F_GETFL, 0);          int ret = 0;
         fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);  
250    
251          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
252          retry = 3;          for (int retry = 3; retry > 0 && !SYS_server_exit; retry--)
         while (retry > 0 && !SYS_server_exit)  
253          {          {
254                  retry--;  #ifdef HAVE_SYS_EPOLL_H
255                    nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
256                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  ret = nfds;
257    #else
258                    pfds[0].fd = STDOUT_FILENO;
259                    pfds[0].events = POLLOUT;
260                    nfds = 1;
261                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
262    #endif
263    
264                  if (nfds < 0)                  if (ret < 0)
265                  {                  {
266                          if (errno != EINTR)                          if (errno != EINTR)
267                          {                          {
268    #ifdef HAVE_SYS_EPOLL_H
269                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
270    #else
271                                    log_error("poll() error (%d)\n", errno);
272    #endif
273                                  break;                                  break;
274                          }                          }
275                          continue;                          continue;
276                  }                  }
277                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
278                  {                  {
279                          continue;                          continue;
280                  }                  }
281    
282                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
283                  {                  {
284                          if (events[i].data.fd == STDOUT_FILENO)  #ifdef HAVE_SYS_EPOLL_H
285                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
286    #else
287                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
288    #endif
289                            {
290    #ifdef HAVE_SYS_EPOLL_H
291                                    log_debug("STDOUT error events (%d)\n", events[i].events);
292    #else
293                                    log_debug("STDOUT error events (%d)\n", pfds[i].revents);
294    #endif
295                                    retry = 0;
296                                    break;
297                            }
298    
299    #ifdef HAVE_SYS_EPOLL_H
300                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
301    #else
302                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
303    #endif
304                          {                          {
305                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
306                                    {
307                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
308                                            if (ret < 0)
309                                            {
310                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
311                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
312                                            }
313                                    }
314    
315                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
316                                  {                                  {
317                                          if (SSH_v2)                                          if (SSH_v2)
318                                          {                                          {
319                                                  ret = ssh_channel_write(SSH_channel, stdout_buf + stdout_buf_offset, (uint32_t)(stdout_buf_len - stdout_buf_offset));                                                  ret = ssh_channel_write(SSH_channel, stdout_conv + stdout_conv_offset, (uint32_t)(stdout_conv_len - stdout_conv_offset));
320                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
321                                                  {                                                  {
322                                                          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));
323                                                          retry = 0;                                                          retry = 0;
324                                                          break;                                                          break;
325                                                  }                                                  }
326                                          }                                          }
327                                          else                                          else
328                                          {                                          {
329                                                  ret = (int)write(STDOUT_FILENO, stdout_buf + stdout_buf_offset, (size_t)(stdout_buf_len - stdout_buf_offset));                                                  ret = (int)write(STDOUT_FILENO, stdout_conv + stdout_conv_offset, (size_t)(stdout_conv_len - stdout_conv_offset));
330                                          }                                          }
331                                          if (ret < 0)                                          if (ret < 0)
332                                          {                                          {
# Line 176  int iflush(void) Line 340  int iflush(void)
340                                                  }                                                  }
341                                                  else                                                  else
342                                                  {                                                  {
343  #ifdef _DEBUG                                                          log_debug("write(STDOUT) error (%d)\n", errno);
                                                         log_error("write(STDOUT) error (%d)\n", errno);  
 #endif  
344                                                          retry = 0;                                                          retry = 0;
345                                                          break;                                                          break;
346                                                  }                                                  }
# Line 190  int iflush(void) Line 352  int iflush(void)
352                                          }                                          }
353                                          else                                          else
354                                          {                                          {
355                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
356                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
357                                                  {                                                  {
358                                                          ret = 0;                                                          ret = 0;
359                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
360                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
361                                                          retry = 0;                                                          retry = 0;
362                                                          break;                                                          break;
363                                                  }                                                  }
# Line 206  int iflush(void) Line 368  int iflush(void)
368                  }                  }
369          }          }
370    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
371          return ret;          return ret;
372  }  }
373    
374  int igetch(int timeout)  int igetch(int timeout)
375  {  {
376          // static input buffer          static int stdin_read_wait = 0;
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
377    
378          struct epoll_event ev, events[MAX_EVENTS];  #ifdef HAVE_SYS_EPOLL_H
379          int nfds, epollfd;          struct epoll_event events[MAX_EVENTS];
380    #else
381            struct pollfd pfds[1];
382    #endif
383    
384            int nfds;
385          int ret;          int ret;
386          int loop;          int loop;
387    
# Line 235  int igetch(int timeout) Line 391  int igetch(int timeout)
391          int in_ascii = 0;          int in_ascii = 0;
392          int in_control = 0;          int in_control = 0;
393          int i = 0;          int i = 0;
         int flags;  
394    
395          if (pos >= len)          if (stdin_conv_offset >= stdin_conv_len)
396          {          {
397                  len = 0;                  stdin_conv_len = 0;
398                  pos = 0;                  stdin_conv_offset = 0;
   
                 epollfd = epoll_create1(0);  
                 if (epollfd < 0)  
                 {  
                         log_error("epoll_create1() error (%d)\n", errno);  
                         return -1;  
                 }  
399    
400                  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)  
                         {  
                                 log_error("close(epoll) error (%d)\n");  
                         }  
                         return -1;  
                 }  
   
                 flags = fcntl(STDIN_FILENO, F_GETFL, 0);  
                 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
                 for (loop = 1; loop && pos >= len && !SYS_server_exit;)  
401                  {                  {
402                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
403                          {                          {
404                                  log_error("SSH channel is closed\n");                                  log_debug("SSH channel is closed\n");
405                                  loop = 0;                                  loop = 0;
406                                  break;                                  break;
407                          }                          }
408    
409                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
410                          {                          {
411                                  if (errno != EINTR)  #ifdef HAVE_SYS_EPOLL_H
412                                    nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
413                                    ret = nfds;
414    #else
415                                    pfds[0].fd = STDIN_FILENO;
416                                    pfds[0].events = POLLIN;
417                                    nfds = 1;
418                                    ret = poll(pfds, (nfds_t)nfds, timeout);
419    #endif
420    
421                                    if (ret < 0)
422                                  {                                  {
423                                          log_error("epoll_wait() error (%d)\n", errno);                                          if (errno != EINTR)
424                                            {
425    #ifdef HAVE_SYS_EPOLL_H
426                                                    log_error("epoll_wait() error (%d)\n", errno);
427    #else
428                                                    log_error("poll() error (%d)\n", errno);
429    #endif
430                                                    break;
431                                            }
432                                            continue;
433                                    }
434                                    else if (ret == 0) // timeout
435                                    {
436                                            out = KEY_TIMEOUT;
437                                          break;                                          break;
438                                  }                                  }
439                                  continue;  
440                          }                                  for (int i = 0; i < nfds; i++)
441                          else if (nfds == 0) // timeout                                  {
442                          {  #ifdef HAVE_SYS_EPOLL_H
443                                  out = KEY_TIMEOUT;                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLHUP | EPOLLERR)))
444                                  break;  #else
445                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLHUP | POLLERR)))
446    #endif
447                                            {
448    #ifdef HAVE_SYS_EPOLL_H
449                                                    log_debug("STDIN error events (%d)\n", events[i].events);
450    #else
451                                                    log_debug("STDIN error events (%d)\n", pfds[i].revents);
452    #endif
453                                                    loop = 0;
454                                                    break;
455                                            }
456    
457    #ifdef HAVE_SYS_EPOLL_H
458                                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
459    #else
460                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
461    #endif
462                                            {
463                                                    stdin_read_wait = 1;
464                                            }
465                                    }
466                          }                          }
467    
468                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
469                          {                          {
470                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
471                                  {                                  {
472                                          while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
473                                          {                                          {
474                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
475                                                    if (ret == SSH_ERROR)
476                                                  {                                                  {
477                                                          ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);                                                          log_debug("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
478                                                          if (ret == SSH_ERROR)                                                          loop = 0;
479                                                          {                                                          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;  
                                                         }  
480                                                  }                                                  }
481                                                  else                                                  else if (ret == SSH_EOF)
482                                                  {                                                  {
483                                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                                          stdin_read_wait = 0;
484                                                            loop = 0;
485                                                            break;
486                                                  }                                                  }
487                                                  if (ret < 0)                                                  else if (ret == 0)
488                                                  {                                                  {
489                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
490                                                          {                                                          stdin_read_wait = 0;
491                                                                  out = 0;                                                          loop = 0;
492                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
493                                                  }                                                  }
494                                                  else if (ret == 0) // broken pipe                                          }
495                                            else
496                                            {
497                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
498                                            }
499                                            if (ret < 0)
500                                            {
501                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
502                                                  {                                                  {
503                                                            out = 0;
504                                                            stdin_read_wait = 0;
505                                                          loop = 0;                                                          loop = 0;
506                                                          break;                                                          break;
507                                                  }                                                  }
508                                                  else                                                  else if (errno == EINTR)
509                                                  {                                                  {
                                                         len += ret;  
510                                                          continue;                                                          continue;
511                                                  }                                                  }
512                                                    else
513                                                    {
514                                                            log_debug("read(STDIN) error (%d)\n", errno);
515                                                            loop = 0;
516                                                            break;
517                                                    }
518                                            }
519                                            else if (ret == 0) // broken pipe
520                                            {
521                                                    stdin_read_wait = 0;
522                                                    loop = 0;
523                                                    break;
524                                            }
525                                            else
526                                            {
527                                                    stdin_buf_len += ret;
528                                                    continue;
529                                          }                                          }
530                                  }                                  }
531                          }                          }
532    
533                          // For debug                          // For debug
534  #ifdef _DEBUG  #ifdef _DEBUG
535                          for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
536                          {                          {
537                                  log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                                  log_debug("input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
538                          }                          }
539  #endif  #endif
540                  }                  }
541    
542                  fcntl(STDIN_FILENO, F_SETFL, flags);                  if (stdin_buf_offset < stdin_buf_len)
   
                 if (close(epollfd) < 0)  
543                  {                  {
544                          log_error("close(epoll) error (%d)\n");                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
545                            if (ret < 0)
546                            {
547                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
548                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
549                            }
550    
551                            // For debug
552    #ifdef _DEBUG
553                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
554                            {
555                                    log_debug("input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
556                            }
557    #endif
558                  }                  }
559          }          }
560    
561          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
562          {          {
563                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
564    
565                  // Convert \r\n to \r                  // Convert \r\n to \r
566                  if (c == CR && pos < len && buf[pos] == LF)                  if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
567                  {                  {
568                          pos++;                          stdin_conv_offset++;
569                  }                  }
570    
571                  // Convert single \n to \r                  // Convert single \n to \r
# Line 834  int igetch(int timeout) Line 1018  int igetch(int timeout)
1018  #ifdef _DEBUG  #ifdef _DEBUG
1019          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
1020          {          {
1021                  log_common("Debug: -->[0x %x]\n", out);                  log_debug("output: -->[0x %x]\n", out);
1022          }          }
1023  #endif  #endif
1024    
# Line 859  void igetch_reset() Line 1043  void igetch_reset()
1043          int ch;          int ch;
1044          do          do
1045          {          {
1046                  ch = igetch(0);                  ch = igetch(100);
1047          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
1048  }  }
1049    
1050    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)
1051    {
1052            char *in_buf;
1053            char *out_buf;
1054            size_t in_bytes;
1055            size_t out_bytes;
1056            int ret;
1057            int in_control = 0;
1058            size_t i = 0;
1059            int skip_current = 0;
1060    
1061            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1062            {
1063                    log_error("NULL pointer error\n");
1064                    return -1;
1065            }
1066    
1067            in_buf = p_buf + *p_buf_offset;
1068            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1069            out_buf = p_conv + *p_conv_len;
1070            out_bytes = conv_size - (size_t)(*p_conv_len);
1071    
1072            while (in_bytes > 0)
1073            {
1074                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
1075                    {
1076                            if (in_control == 0)
1077                            {
1078                                    in_control = 1;
1079                                    i = 0;
1080                            }
1081                    }
1082    
1083                    if (in_control || skip_current)
1084                    {
1085                            skip_current = 0;
1086    
1087                            if (out_bytes <= 0)
1088                            {
1089                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
1090                                    return -2;
1091                            }
1092    
1093                            *out_buf = *in_buf;
1094                            in_buf++;
1095                            out_buf++;
1096                            in_bytes--;
1097                            out_bytes--;
1098    
1099                            (*p_buf_offset)++;
1100                            (*p_conv_len)++;
1101    
1102                            i++;
1103                            if (i >= 2)
1104                            {
1105                                    in_control = 0;
1106                            }
1107                            continue;
1108                    }
1109    
1110                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
1111                    if (ret == -1)
1112                    {
1113                            if (errno == EINVAL) // Incomplete
1114                            {
1115                                    log_debug("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
1116                                    if (p_buf != in_buf)
1117                                    {
1118                                            *p_buf_len -= (int)(in_buf - p_buf);
1119                                            *p_buf_offset = 0;
1120                                            *p_conv_len = (int)(conv_size - out_bytes);
1121                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
1122                                    }
1123    
1124                                    break;
1125                            }
1126                            else if (errno == E2BIG)
1127                            {
1128                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
1129                                    return -1;
1130                            }
1131                            else if (errno == EILSEQ)
1132                            {
1133                                    if (in_bytes > out_bytes || out_bytes <= 0)
1134                                    {
1135                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1136                                            return -2;
1137                                    }
1138    
1139                                    // reset in_bytes when "//IGNORE" is applied
1140                                    if (in_bytes == 0)
1141                                    {
1142                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1143                                            log_debug("Reset in_bytes from 0 to %d\n", in_bytes);
1144                                    }
1145    
1146                                    log_debug("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n",
1147                                                      in_bytes, out_bytes, in_buf[0]);
1148                                    skip_current = 1;
1149                            }
1150                            else // something strange
1151                            {
1152                                    log_debug("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",
1153                                                      in_bytes, out_bytes, errno, in_buf[0]);
1154                                    *p_buf_offset = (int)(in_buf - p_buf);
1155                                    *p_conv_len = (int)(conv_size - out_bytes);
1156                                    skip_current = 1;
1157                            }
1158                    }
1159                    else
1160                    {
1161                            *p_buf_offset = (int)(in_buf - p_buf);
1162                            *p_conv_len = (int)(conv_size - out_bytes);
1163                    }
1164            }
1165    
1166            if (*p_buf_offset >= *p_buf_len)
1167            {
1168                    *p_buf_len = 0;
1169                    *p_buf_offset = 0;
1170            }
1171    
1172            return 0;
1173    }
1174    
1175    int io_conv_init(const char *charset)
1176    {
1177            char tocode[CHARSET_MAX_LEN + 20];
1178    
1179            if (charset == NULL)
1180            {
1181                    log_error("NULL pointer error\n");
1182                    return -1;
1183            }
1184    
1185            io_conv_cleanup();
1186    
1187            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1188            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1189    
1190            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1191                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1192            stdin_cd = iconv_open(tocode, stdio_charset);
1193            if (stdin_cd == (iconv_t)(-1))
1194            {
1195                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1196                    return -2;
1197            }
1198    
1199            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1200                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1201            stdout_cd = iconv_open(tocode, BBS_default_charset);
1202            if (stdout_cd == (iconv_t)(-1))
1203            {
1204                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1205                    iconv_close(stdin_cd);
1206                    stdin_cd = (iconv_t)(-1);
1207                    return -2;
1208            }
1209    
1210            return 0;
1211    }
1212    
1213    int io_conv_cleanup(void)
1214    {
1215            if (stdin_cd != (iconv_t)(-1))
1216            {
1217                    iconv_close(stdin_cd);
1218                    stdin_cd = (iconv_t)(-1);
1219            }
1220            if (stdout_cd != (iconv_t)(-1))
1221            {
1222                    iconv_close(stdout_cd);
1223                    stdout_cd = (iconv_t)(-1);
1224            }
1225    
1226            return 0;
1227    }


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

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