/[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.43 by sysadm, Fri Jun 13 10:51:46 2025 UTC Revision 1.74 by sysadm, Thu Dec 18 02:17:39 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/epoll.h>  #include <sys/select.h>
25    #include <libssh/callbacks.h>
26  #include <libssh/libssh.h>  #include <libssh/libssh.h>
27  #include <libssh/server.h>  #include <libssh/server.h>
 #include <libssh/callbacks.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;  
         }  
247    
248          ev.events = EPOLLOUT;          int nfds;
249          ev.data.fd = STDOUT_FILENO;          int ret = 0;
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)  
         {  
                 log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);  
                 if (close(epollfd) < 0)  
                 {  
                         log_error("close(epoll) error (%d)\n");  
                 }  
                 return -1;  
         }  
   
         // Set STDOUT as non-blocking  
         flags = fcntl(STDOUT_FILENO, F_GETFL, 0);  
         fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);  
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                    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                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  if (ret < 0)
   
                 if (nfds < 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 & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)))
286    #else
287                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR)))
288    #endif
289                            {
290    #ifdef _DEBUG
291    #ifdef HAVE_SYS_EPOLL_H
292                                    log_error("STDOUT error events (%d)\n", events[i].events);
293    #else
294                                    log_error("STDOUT error events (%d)\n", pfds[i].revents);
295    #endif
296    #endif
297                                    retry = 0;
298                                    break;
299                            }
300    
301    #ifdef HAVE_SYS_EPOLL_H
302                            if (events[i].data.fd == STDOUT_FILENO && (events[i].events & EPOLLOUT))
303    #else
304                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
305    #endif
306                          {                          {
307                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
308                                    {
309                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
310                                            if (ret < 0)
311                                            {
312                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
313                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
314                                            }
315                                    }
316    
317                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
318                                  {                                  {
319                                          if (SSH_v2)                                          if (SSH_v2)
320                                          {                                          {
321                                                  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));
322                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
323                                                  {                                                  {
324    #ifdef _DEBUG
325                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));                                                          log_error("ssh_channel_write() error: %s\n", ssh_get_error(SSH_session));
326    #endif
327                                                          retry = 0;                                                          retry = 0;
328                                                          break;                                                          break;
329                                                  }                                                  }
330                                          }                                          }
331                                          else                                          else
332                                          {                                          {
333                                                  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));
334                                          }                                          }
335                                          if (ret < 0)                                          if (ret < 0)
336                                          {                                          {
# Line 176  int iflush(void) Line 344  int iflush(void)
344                                                  }                                                  }
345                                                  else                                                  else
346                                                  {                                                  {
347    #ifdef _DEBUG
348                                                          log_error("write(STDOUT) error (%d)\n", errno);                                                          log_error("write(STDOUT) error (%d)\n", errno);
349    #endif
350                                                          retry = 0;                                                          retry = 0;
351                                                          break;                                                          break;
352                                                  }                                                  }
# Line 188  int iflush(void) Line 358  int iflush(void)
358                                          }                                          }
359                                          else                                          else
360                                          {                                          {
361                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
362                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
363                                                  {                                                  {
364                                                          ret = 0;                                                          ret = 0;
365                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
366                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
367                                                          retry = 0;                                                          retry = 0;
368                                                          break;                                                          break;
369                                                  }                                                  }
# Line 204  int iflush(void) Line 374  int iflush(void)
374                  }                  }
375          }          }
376    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
377          return ret;          return ret;
378  }  }
379    
380  int igetch(int timeout)  int igetch(int timeout)
381  {  {
382          // static input buffer          static int stdin_read_wait = 0;
383          static unsigned char buf[LINE_BUFFER_LEN];  
384          static int len = 0;  #ifdef HAVE_SYS_EPOLL_H
385          static int pos = 0;          struct epoll_event events[MAX_EVENTS];
386    #else
387            struct pollfd pfds[1];
388    #endif
389    
390          struct epoll_event ev, events[MAX_EVENTS];          int nfds;
         int nfds, epollfd;  
391          int ret;          int ret;
392          int loop;          int loop;
393    
# Line 233  int igetch(int timeout) Line 397  int igetch(int timeout)
397          int in_ascii = 0;          int in_ascii = 0;
398          int in_control = 0;          int in_control = 0;
399          int i = 0;          int i = 0;
         int flags;  
   
         epollfd = epoll_create1(0);  
         if (epollfd < 0)  
         {  
                 log_error("epoll_create1() error (%d)\n", errno);  
                 return -1;  
         }  
400    
401          ev.events = EPOLLIN;          if (stdin_conv_offset >= stdin_conv_len)
         ev.data.fd = STDIN_FILENO;  
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)  
402          {          {
403                  log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);                  stdin_conv_len = 0;
404                    stdin_conv_offset = 0;
405    
406                  if (close(epollfd) < 0)                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
407                  {                  {
408                          log_error("close(epoll) error (%d)\n");                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
409                  }                          {
410                  return -1;  #ifdef _DEBUG
411          }                                  log_error("SSH channel is closed\n");
412    #endif
413          flags = fcntl(STDIN_FILENO, F_GETFL, 0);                                  loop = 0;
414          fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);                                  break;
415                            }
         loop = 1;  
416    
417          while (loop && pos >= len && !SYS_server_exit)                          if (!stdin_read_wait)
418          {                          {
419                  len = 0;  #ifdef HAVE_SYS_EPOLL_H
420                  pos = 0;                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
421                                    ret = nfds;
422    #else
423                                    pfds[0].fd = STDIN_FILENO;
424                                    pfds[0].events = POLLIN;
425                                    nfds = 1;
426                                    ret = poll(pfds, (nfds_t)nfds, timeout);
427    #endif
428    
429                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                                  if (ret < 0)
430                  {                                  {
431                          log_error("SSH channel is closed\n");                                          if (errno != EINTR)
432                          loop = 0;                                          {
433                          break;  #ifdef HAVE_SYS_EPOLL_H
434                  }                                                  log_error("epoll_wait() error (%d)\n", errno);
435    #else
436                                                    log_error("poll() error (%d)\n", errno);
437    #endif
438                                                    break;
439                                            }
440                                            continue;
441                                    }
442                                    else if (ret == 0) // timeout
443                                    {
444                                            out = KEY_TIMEOUT;
445                                            break;
446                                    }
447    
448                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                                  for (int i = 0; i < nfds; i++)
449                                    {
450    #ifdef HAVE_SYS_EPOLL_H
451                                            if (events[i].data.fd == STDIN_FILENO && (events[i].events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR)))
452    #else
453                                            if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & (POLLRDHUP | POLLHUP | POLLERR)))
454    #endif
455                                            {
456    #ifdef _DEBUG
457    #ifdef HAVE_SYS_EPOLL_H
458                                                    log_error("STDIN error events (%d)\n", events[i].events);
459    #else
460                                                    log_error("STDIN error events (%d)\n", pfds[i].revents);
461    #endif
462    #endif
463                                                    loop = 0;
464                                                    break;
465                                            }
466    
467                  if (nfds < 0)  #ifdef HAVE_SYS_EPOLL_H
468                  {                                          if (events[i].data.fd == STDIN_FILENO && (events[i].events & EPOLLIN))
469                          if (errno != EINTR)  #else
470                          {                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
471                                  log_error("epoll_wait() error (%d)\n", errno);  #endif
472                                  break;                                          {
473                                                    stdin_read_wait = 1;
474                                            }
475                                    }
476                          }                          }
                         continue;  
                 }  
                 else if (nfds == 0) // timeout  
                 {  
                         out = KEY_TIMEOUT;  
                         break;  
                 }  
477    
478                  for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
                 {  
                         if (events[i].data.fd == STDIN_FILENO)  
479                          {                          {
480                                  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
481                                  {                                  {
482                                          if (SSH_v2)                                          if (SSH_v2)
483                                          {                                          {
484                                                  ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
485                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
486                                                  {                                                  {
487    #ifdef _DEBUG
488                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
489    #endif
490                                                          loop = 0;                                                          loop = 0;
491                                                          break;                                                          break;
492                                                  }                                                  }
493                                                  else if (ret == SSH_EOF)                                                  else if (ret == SSH_EOF)
494                                                  {                                                  {
495                                                            stdin_read_wait = 0;
496                                                          loop = 0;                                                          loop = 0;
497                                                          break;                                                          break;
498                                                  }                                                  }
499                                                  else if (ret == 0)                                                  else if (ret == 0)
500                                                  {                                                  {
501                                                          out = 0;                                                          out = 0;
502                                                          break; // Check whether channel is still open                                                          stdin_read_wait = 0;
503                                                            loop = 0;
504                                                            break;
505                                                  }                                                  }
506                                          }                                          }
507                                          else                                          else
508                                          {                                          {
509                                                  ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                                  ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
510                                          }                                          }
511                                          if (ret < 0)                                          if (ret < 0)
512                                          {                                          {
513                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
514                                                  {                                                  {
515                                                          out = 0;                                                          out = 0;
516                                                            stdin_read_wait = 0;
517                                                          loop = 0;                                                          loop = 0;
518                                                          break;                                                          break;
519                                                  }                                                  }
# Line 333  int igetch(int timeout) Line 523  int igetch(int timeout)
523                                                  }                                                  }
524                                                  else                                                  else
525                                                  {                                                  {
526    #ifdef _DEBUG
527                                                          log_error("read(STDIN) error (%d)\n", errno);                                                          log_error("read(STDIN) error (%d)\n", errno);
528    #endif
529                                                          loop = 0;                                                          loop = 0;
530                                                          break;                                                          break;
531                                                  }                                                  }
532                                          }                                          }
533                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
534                                          {                                          {
535                                                    stdin_read_wait = 0;
536                                                  loop = 0;                                                  loop = 0;
537                                                  break;                                                  break;
538                                          }                                          }
539                                          else                                          else
540                                          {                                          {
541                                                  len += ret;                                                  stdin_buf_len += ret;
542                                                  continue;                                                  continue;
543                                          }                                          }
544                                  }                                  }
545                          }                          }
546    
547                            // For debug
548    #ifdef _DEBUG
549                            for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
550                            {
551                                    log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
552                            }
553    #endif
554                  }                  }
555    
556                  // For debug                  if (stdin_buf_offset < stdin_buf_len)
557                  // for (int j = pos; j < len; j++)                  {
558                  // {                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
559                  //      log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                          if (ret < 0)
560                  // }                          {
561          }                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
562                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
563                            }
564    
565          fcntl(STDIN_FILENO, F_SETFL, flags);                          // For debug
566    #ifdef _DEBUG
567                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
568                            {
569                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
570                            }
571    #endif
572                    }
573            }
574    
575          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
576          {          {
577                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
578    
579                    // Convert \r\n to \r
580                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
581                    {
582                            stdin_conv_offset++;
583                    }
584    
585                    // Convert single \n to \r
586                    if (c == LF)
587                    {
588                            c = CR;
589                    }
590    
591                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
592                  {                  {
# Line 799  int igetch(int timeout) Line 1022  int igetch(int timeout)
1022                  break;                  break;
1023          }          }
1024    
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
1025          // For ESC key          // For ESC key
1026          if (out == 0 && in_esc)          if (out == 0 && in_esc)
1027          {          {
# Line 811  int igetch(int timeout) Line 1029  int igetch(int timeout)
1029          }          }
1030    
1031          // for debug          // for debug
1032          // if (out != KEY_TIMEOUT && out != KEY_NULL)  #ifdef _DEBUG
1033          // {          if (out != KEY_TIMEOUT && out != KEY_NULL)
1034          //      log_common("Debug: -->[0x %x]\n", out);          {
1035          // }                  log_error("Debug: -->[0x %x]\n", out);
1036            }
1037    #endif
1038    
1039          return out;          return out;
1040  }  }
# Line 822  int igetch(int timeout) Line 1042  int igetch(int timeout)
1042  int igetch_t(int sec)  int igetch_t(int sec)
1043  {  {
1044          int ch;          int ch;
1045          time_t t_begin = time(0);          time_t t_begin = time(NULL);
1046    
1047          do          do
1048          {          {
1049                  ch = igetch(100);                  ch = igetch(100);
1050          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec));
1051    
1052          return ch;          return ch;
1053  }  }
# Line 837  void igetch_reset() Line 1057  void igetch_reset()
1057          int ch;          int ch;
1058          do          do
1059          {          {
1060                  ch = igetch(0);                  ch = igetch(100);
1061          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
1062  }  }
1063    
1064    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)
1065    {
1066            char *in_buf;
1067            char *out_buf;
1068            size_t in_bytes;
1069            size_t out_bytes;
1070            int ret;
1071            int in_control = 0;
1072            size_t i = 0;
1073            int skip_current = 0;
1074    
1075            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1076            {
1077                    log_error("NULL pointer error\n");
1078                    return -1;
1079            }
1080    
1081            in_buf = p_buf + *p_buf_offset;
1082            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1083            out_buf = p_conv + *p_conv_len;
1084            out_bytes = conv_size - (size_t)(*p_conv_len);
1085    
1086            while (in_bytes > 0)
1087            {
1088                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
1089                    {
1090                            if (in_control == 0)
1091                            {
1092                                    in_control = 1;
1093                                    i = 0;
1094                            }
1095                    }
1096    
1097                    if (in_control || skip_current)
1098                    {
1099                            skip_current = 0;
1100    
1101                            if (out_bytes <= 0)
1102                            {
1103                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
1104                                    return -2;
1105                            }
1106    
1107                            *out_buf = *in_buf;
1108                            in_buf++;
1109                            out_buf++;
1110                            in_bytes--;
1111                            out_bytes--;
1112    
1113                            (*p_buf_offset)++;
1114                            (*p_conv_len)++;
1115    
1116                            i++;
1117                            if (i >= 2)
1118                            {
1119                                    in_control = 0;
1120                            }
1121                            continue;
1122                    }
1123    
1124                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
1125                    if (ret == -1)
1126                    {
1127                            if (errno == EINVAL) // Incomplete
1128                            {
1129    #ifdef _DEBUG
1130                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
1131    #endif
1132                                    if (p_buf != in_buf)
1133                                    {
1134                                            *p_buf_len -= (int)(in_buf - p_buf);
1135                                            *p_buf_offset = 0;
1136                                            *p_conv_len = (int)(conv_size - out_bytes);
1137                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
1138                                    }
1139    
1140                                    break;
1141                            }
1142                            else if (errno == E2BIG)
1143                            {
1144                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
1145                                    return -1;
1146                            }
1147                            else if (errno == EILSEQ)
1148                            {
1149                                    if (in_bytes > out_bytes || out_bytes <= 0)
1150                                    {
1151                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1152                                            return -2;
1153                                    }
1154    
1155                                    // reset in_bytes when "//IGNORE" is applied
1156                                    if (in_bytes == 0)
1157                                    {
1158                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1159    #ifdef _DEBUG
1160                                            log_error("Reset in_bytes from 0 to %d\n", in_bytes);
1161    #endif
1162                                    }
1163    
1164    #ifdef _DEBUG
1165                                    log_error("iconv(in_bytes=%d, out_bytes=%d) error: EILSEQ, in_buf[0]=%d\n",
1166                                                      in_bytes, out_bytes, in_buf[0]);
1167    #endif
1168                                    skip_current = 1;
1169                            }
1170                            else // something strange
1171                            {
1172    #ifdef _DEBUG
1173                                    log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",
1174                                                      in_bytes, out_bytes, errno, in_buf[0]);
1175    #endif
1176                                    *p_buf_offset = (int)(in_buf - p_buf);
1177                                    *p_conv_len = (int)(conv_size - out_bytes);
1178                                    skip_current = 1;
1179                            }
1180                    }
1181                    else
1182                    {
1183                            *p_buf_offset = (int)(in_buf - p_buf);
1184                            *p_conv_len = (int)(conv_size - out_bytes);
1185                    }
1186            }
1187    
1188            if (*p_buf_offset >= *p_buf_len)
1189            {
1190                    *p_buf_len = 0;
1191                    *p_buf_offset = 0;
1192            }
1193    
1194            return 0;
1195    }
1196    
1197    int io_conv_init(const char *charset)
1198    {
1199            char tocode[CHARSET_MAX_LEN + 20];
1200    
1201            if (charset == NULL)
1202            {
1203                    log_error("NULL pointer error\n");
1204                    return -1;
1205            }
1206    
1207            io_conv_cleanup();
1208    
1209            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1210            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1211    
1212            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1213                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1214            stdin_cd = iconv_open(tocode, stdio_charset);
1215            if (stdin_cd == (iconv_t)(-1))
1216            {
1217                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1218                    return -2;
1219            }
1220    
1221            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1222                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1223            stdout_cd = iconv_open(tocode, BBS_default_charset);
1224            if (stdout_cd == (iconv_t)(-1))
1225            {
1226                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1227                    iconv_close(stdin_cd);
1228                    stdin_cd = (iconv_t)(-1);
1229                    return -2;
1230            }
1231    
1232            return 0;
1233    }
1234    
1235    int io_conv_cleanup(void)
1236    {
1237            if (stdin_cd != (iconv_t)(-1))
1238            {
1239                    iconv_close(stdin_cd);
1240                    stdin_cd = (iconv_t)(-1);
1241            }
1242            if (stdout_cd != (iconv_t)(-1))
1243            {
1244                    iconv_close(stdout_cd);
1245                    stdout_cd = (iconv_t)(-1);
1246            }
1247    
1248            return 0;
1249    }


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

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