/[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.39 by sysadm, Thu Jun 5 05:24:56 2025 UTC Revision 1.70 by sysadm, Sun Nov 23 01:47:47 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 = NULL;
68    static iconv_t stdout_cd = NULL;
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 91  int outc(char c) Line 237  int outc(char c)
237          return ret;          return ret;
238  }  }
239    
240  int iflush()  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            struct pollfd pfds[1];
246    #endif
247    
248            int nfds;
249          int retry;          int retry;
250          int ret = 0;          int ret = 0;
251    
         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;  
         }  
   
         // Set STDOUT as non-blocking  
         flags = fcntl(STDOUT_FILENO, F_GETFL, 0);  
         fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
252          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
253          retry = 3;          retry = 3;
254          while (retry > 0 && !SYS_server_exit)          while (retry > 0 && !SYS_server_exit)
255          {          {
256                  retry--;                  retry--;
257    
258                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second  #ifdef HAVE_SYS_EPOLL_H
259                    nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
260                    ret = nfds;
261    #else
262                    pfds[0].fd = STDOUT_FILENO;
263                    pfds[0].events = POLLOUT;
264                    nfds = 1;
265                    ret = poll(pfds, (nfds_t)nfds, 100); // 0.1 second
266    #endif
267    
268                  if (nfds < 0)                  if (ret < 0)
269                  {                  {
270                          if (errno != EINTR)                          if (errno != EINTR)
271                          {                          {
272    #ifdef HAVE_SYS_EPOLL_H
273                                  log_error("epoll_wait() error (%d)\n", errno);                                  log_error("epoll_wait() error (%d)\n", errno);
274    #else
275                                    log_error("poll() error (%d)\n", errno);
276    #endif
277                                  break;                                  break;
278                          }                          }
279                          continue;                          continue;
280                  }                  }
281                  else if (nfds == 0) // timeout                  else if (ret == 0) // timeout
282                  {                  {
283                          continue;                          continue;
284                  }                  }
285    
286                  for (int i = 0; i < nfds; i++)                  for (int i = 0; i < nfds; i++)
287                  {                  {
288    #ifdef HAVE_SYS_EPOLL_H
289                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
290    #else
291                            if (pfds[i].fd == STDOUT_FILENO && (pfds[i].revents & POLLOUT))
292    #endif
293                          {                          {
294                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
295                                    {
296                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
297                                            if (ret < 0)
298                                            {
299                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
300                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
301                                            }
302                                    }
303    
304                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
305                                  {                                  {
306                                          if (SSH_v2)                                          if (SSH_v2)
307                                          {                                          {
308                                                  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));
309                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
310                                                  {                                                  {
311                                                          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));
# Line 162  int iflush() Line 315  int iflush()
315                                          }                                          }
316                                          else                                          else
317                                          {                                          {
318                                                  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));
319                                          }                                          }
320                                          if (ret < 0)                                          if (ret < 0)
321                                          {                                          {
# Line 176  int iflush() Line 329  int iflush()
329                                                  }                                                  }
330                                                  else                                                  else
331                                                  {                                                  {
332    #ifdef _DEBUG
333                                                          log_error("write(STDOUT) error (%d)\n", errno);                                                          log_error("write(STDOUT) error (%d)\n", errno);
334    #endif
335                                                          retry = 0;                                                          retry = 0;
336                                                          break;                                                          break;
337                                                  }                                                  }
# Line 188  int iflush() Line 343  int iflush()
343                                          }                                          }
344                                          else                                          else
345                                          {                                          {
346                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
347                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
348                                                  {                                                  {
349                                                          ret = 0;                                                          ret = 0;
350                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
351                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
352                                                          retry = 0;                                                          retry = 0;
353                                                          break;                                                          break;
354                                                  }                                                  }
# Line 204  int iflush() Line 359  int iflush()
359                  }                  }
360          }          }
361    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
362          return ret;          return ret;
363  }  }
364    
365  int igetch(int timeout)  int igetch(int timeout)
366  {  {
367          // static input buffer          static int stdin_read_wait = 0;
368          static unsigned char buf[LINE_BUFFER_LEN];  
369          static int len = 0;  #ifdef HAVE_SYS_EPOLL_H
370          static int pos = 0;          struct epoll_event events[MAX_EVENTS];
371    #else
372            struct pollfd pfds[1];
373    #endif
374    
375          struct epoll_event ev, events[MAX_EVENTS];          int nfds;
         int nfds, epollfd;  
376          int ret;          int ret;
377          int loop;          int loop;
378    
# Line 233  int igetch(int timeout) Line 382  int igetch(int timeout)
382          int in_ascii = 0;          int in_ascii = 0;
383          int in_control = 0;          int in_control = 0;
384          int i = 0;          int i = 0;
         int flags;  
385    
386          epollfd = epoll_create1(0);          if (stdin_conv_offset >= stdin_conv_len)
         if (epollfd < 0)  
387          {          {
388                  log_error("epoll_create1() error (%d)\n", errno);                  stdin_conv_len = 0;
389                  return -1;                  stdin_conv_offset = 0;
         }  
   
         ev.events = EPOLLIN;  
         ev.data.fd = STDIN_FILENO;  
         if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)  
         {  
                 log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);  
390    
391                  if (close(epollfd) < 0)                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
392                  {                  {
393                          log_error("close(epoll) error (%d)\n");                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
394                  }                          {
395                  return -1;                                  log_error("SSH channel is closed\n");
396          }                                  loop = 0;
397                                    break;
398          flags = fcntl(STDIN_FILENO, F_GETFL, 0);                          }
         fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
         loop = 1;  
   
         while (loop && pos >= len && !SYS_server_exit)  
         {  
                 len = 0;  
                 pos = 0;  
399    
400                  if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (!stdin_read_wait)
401                  {                          {
402                          log_error("SSH channel is closed\n");  #ifdef HAVE_SYS_EPOLL_H
403                          loop = 0;                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
404                          break;                                  ret = nfds;
405                  }  #else
406                                    pfds[0].fd = STDIN_FILENO;
407                                    pfds[0].events = POLLIN;
408                                    nfds = 1;
409                                    ret = poll(pfds, (nfds_t)nfds, timeout);
410    #endif
411    
412                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                                  if (ret < 0)
413                                    {
414                                            if (errno != EINTR)
415                                            {
416    #ifdef HAVE_SYS_EPOLL_H
417                                                    log_error("epoll_wait() error (%d)\n", errno);
418    #else
419                                                    log_error("poll() error (%d)\n", errno);
420    #endif
421                                                    break;
422                                            }
423                                            continue;
424                                    }
425                                    else if (ret == 0) // timeout
426                                    {
427                                            out = KEY_TIMEOUT;
428                                            break;
429                                    }
430    
431                  if (nfds < 0)                                  for (int i = 0; i < nfds; i++)
432                  {                                  {
433                          if (errno != EINTR)  #ifdef HAVE_SYS_EPOLL_H
434                          {                                          if (events[i].data.fd == STDIN_FILENO)
435                                  log_error("epoll_wait() error (%d)\n", errno);  #else
436                                  break;                                          if (pfds[i].fd == STDIN_FILENO && (pfds[i].revents & POLLIN))
437    #endif
438                                            {
439                                                    stdin_read_wait = 1;
440                                            }
441                                    }
442                          }                          }
                         continue;  
                 }  
                 else if (nfds == 0) // timeout  
                 {  
                         out = KEY_TIMEOUT;  
                         break;  
                 }  
443    
444                  for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
                 {  
                         if (events[i].data.fd == STDIN_FILENO)  
445                          {                          {
446                                  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
447                                  {                                  {
448                                          if (SSH_v2)                                          if (SSH_v2)
449                                          {                                          {
450                                                  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);
451                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
452                                                  {                                                  {
453                                                          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));
# Line 306  int igetch(int timeout) Line 456  int igetch(int timeout)
456                                                  }                                                  }
457                                                  else if (ret == SSH_EOF)                                                  else if (ret == SSH_EOF)
458                                                  {                                                  {
459                                                            stdin_read_wait = 0;
460                                                          loop = 0;                                                          loop = 0;
461                                                          break;                                                          break;
462                                                  }                                                  }
463                                                  else if (ret == 0)                                                  else if (ret == 0)
464                                                  {                                                  {
465                                                          break; // Check whether channel is still open                                                          out = 0;
466                                                            stdin_read_wait = 0;
467                                                            loop = 0;
468                                                            break;
469                                                  }                                                  }
470                                          }                                          }
471                                          else                                          else
472                                          {                                          {
473                                                  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);
474                                          }                                          }
475                                          if (ret < 0)                                          if (ret < 0)
476                                          {                                          {
477                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)                                                  if (errno == EAGAIN || errno == EWOULDBLOCK)
478                                                  {                                                  {
479                                                          out = 0;                                                          out = 0;
480                                                            stdin_read_wait = 0;
481                                                          loop = 0;                                                          loop = 0;
482                                                          break;                                                          break;
483                                                  }                                                  }
# Line 332  int igetch(int timeout) Line 487  int igetch(int timeout)
487                                                  }                                                  }
488                                                  else                                                  else
489                                                  {                                                  {
490    #ifdef _DEBUG
491                                                          log_error("read(STDIN) error (%d)\n", errno);                                                          log_error("read(STDIN) error (%d)\n", errno);
492    #endif
493                                                          loop = 0;                                                          loop = 0;
494                                                          break;                                                          break;
495                                                  }                                                  }
496                                          }                                          }
497                                          else if (ret == 0) // broken pipe                                          else if (ret == 0) // broken pipe
498                                          {                                          {
499                                                    stdin_read_wait = 0;
500                                                  loop = 0;                                                  loop = 0;
501                                                  break;                                                  break;
502                                          }                                          }
503                                          else                                          else
504                                          {                                          {
505                                                  len += ret;                                                  stdin_buf_len += ret;
506                                                  continue;                                                  continue;
507                                          }                                          }
508                                  }                                  }
509                          }                          }
510    
511                            // For debug
512    #ifdef _DEBUG
513                            for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
514                            {
515                                    log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
516                            }
517    #endif
518                  }                  }
519    
520                  // For debug                  if (stdin_buf_offset < stdin_buf_len)
521                  // for (int j = pos; j < len; j++)                  {
522                  // {                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
523                  //      log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                          if (ret < 0)
524                  // }                          {
525          }                                  log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
526                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
527                            }
528    
529          fcntl(STDIN_FILENO, F_SETFL, flags);                          // For debug
530    #ifdef _DEBUG
531                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
532                            {
533                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
534                            }
535    #endif
536                    }
537            }
538    
539          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
540          {          {
541                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
542    
543                    // Convert \r\n to \r
544                    if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
545                    {
546                            stdin_conv_offset++;
547                    }
548    
549                    // Convert single \n to \r
550                    if (c == LF)
551                    {
552                            c = CR;
553                    }
554    
555                  if (c == KEY_CONTROL)                  if (c == KEY_CONTROL)
556                  {                  {
# Line 533  int igetch(int timeout) Line 721  int igetch(int timeout)
721                                  case 49:                                  case 49:
722                                          out = KEY_HOME;                                          out = KEY_HOME;
723                                          break;                                          break;
724                                    case 50:
725                                            out = KEY_INS;
726                                            break;
727                                  case 51:                                  case 51:
728                                          out = KEY_DEL;                                          out = KEY_DEL;
729                                          break;                                          break;
# Line 623  int igetch(int timeout) Line 814  int igetch(int timeout)
814                                  in_ascii = 0;                                  in_ascii = 0;
815                                  switch (tmp[4])                                  switch (tmp[4])
816                                  {                                  {
817                                    case 65:
818                                            out = KEY_CTRL_UP;
819                                            break;
820                                    case 66:
821                                            out = KEY_CTRL_DOWN;
822                                            break;
823                                    case 67:
824                                            out = KEY_CTRL_RIGHT;
825                                            break;
826                                    case 68:
827                                            out = KEY_CTRL_LEFT;
828                                            break;
829                                    case 70:
830                                            out = KEY_CTRL_END;
831                                            break;
832                                    case 72:
833                                            out = KEY_CTRL_HOME;
834                                            break;
835                                  case 80:                                  case 80:
836                                          out = KEY_CTRL_F1;                                          out = KEY_CTRL_F1;
837                                          break;                                          break;
# Line 777  int igetch(int timeout) Line 986  int igetch(int timeout)
986                  break;                  break;
987          }          }
988    
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
989          // For ESC key          // For ESC key
990          if (out == 0 && in_esc)          if (out == 0 && in_esc)
991          {          {
# Line 789  int igetch(int timeout) Line 993  int igetch(int timeout)
993          }          }
994    
995          // for debug          // for debug
996          // if (out != KEY_TIMEOUT && out != KEY_NULL)  #ifdef _DEBUG
997          // {          if (out != KEY_TIMEOUT && out != KEY_NULL)
998          //      log_common ("Debug: -->[0x %x]\n", out);          {
999          // }                  log_error("Debug: -->[0x %x]\n", out);
1000            }
1001    #endif
1002    
1003          return out;          return out;
1004  }  }
# Line 800  int igetch(int timeout) Line 1006  int igetch(int timeout)
1006  int igetch_t(int sec)  int igetch_t(int sec)
1007  {  {
1008          int ch;          int ch;
1009          time_t t_begin = time(0);          time_t t_begin = time(NULL);
1010    
1011          do          do
1012          {          {
1013                  ch = igetch(100);                  ch = igetch(100);
1014          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(0) - t_begin < sec));          } while (!SYS_server_exit && ch == KEY_TIMEOUT && (time(NULL) - t_begin < sec));
1015    
1016          return ch;          return ch;
1017  }  }
# Line 815  void igetch_reset() Line 1021  void igetch_reset()
1021          int ch;          int ch;
1022          do          do
1023          {          {
1024                  ch = igetch(0);                  ch = igetch(100);
1025          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);          } while (ch != KEY_NULL && ch != KEY_TIMEOUT);
1026  }  }
1027    
1028    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)
1029    {
1030            char *in_buf;
1031            char *out_buf;
1032            size_t in_bytes;
1033            size_t out_bytes;
1034            int ret;
1035            int in_control = 0;
1036            size_t i = 0;
1037            int skip_current = 0;
1038    
1039            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1040            {
1041                    log_error("NULL pointer error\n");
1042                    return -1;
1043            }
1044    
1045            in_buf = p_buf + *p_buf_offset;
1046            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1047            out_buf = p_conv + *p_conv_len;
1048            out_bytes = conv_size - (size_t)(*p_conv_len);
1049    
1050            while (in_bytes > 0)
1051            {
1052                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
1053                    {
1054                            if (in_control == 0)
1055                            {
1056                                    in_control = 1;
1057                                    i = 0;
1058                            }
1059                    }
1060    
1061                    if (in_control || skip_current)
1062                    {
1063                            skip_current = 0;
1064    
1065                            if (out_bytes <= 0)
1066                            {
1067                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
1068                                    return -2;
1069                            }
1070    
1071                            *out_buf = *in_buf;
1072                            in_buf++;
1073                            out_buf++;
1074                            in_bytes--;
1075                            out_bytes--;
1076    
1077                            (*p_buf_offset)++;
1078                            (*p_conv_len)++;
1079    
1080                            i++;
1081                            if (i >= 2)
1082                            {
1083                                    in_control = 0;
1084                            }
1085                            continue;
1086                    }
1087    
1088                    ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
1089                    if (ret == -1)
1090                    {
1091                            if (errno == EINVAL) // Incomplete
1092                            {
1093    #ifdef _DEBUG
1094                                    log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
1095    #endif
1096                                    if (p_buf != in_buf)
1097                                    {
1098                                            *p_buf_len -= (int)(in_buf - p_buf);
1099                                            *p_buf_offset = 0;
1100                                            *p_conv_len = (int)(conv_size - out_bytes);
1101                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
1102                                    }
1103    
1104                                    break;
1105                            }
1106                            else if (errno == E2BIG)
1107                            {
1108                                    log_error("iconv(inbytes=%d, outbytes=%d) error: E2BIG\n", in_bytes, out_bytes);
1109                                    return -1;
1110                            }
1111                            else if (errno == EILSEQ)
1112                            {
1113                                    if (in_bytes > out_bytes || out_bytes <= 0)
1114                                    {
1115                                            log_error("iconv(inbytes=%d, outbytes=%d) error: EILSEQ and E2BIG\n", in_bytes, out_bytes);
1116                                            return -2;
1117                                    }
1118    
1119                                    // reset in_bytes when "//IGNORE" is applied
1120                                    if (in_bytes == 0)
1121                                    {
1122                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1123                                    }
1124    
1125                                    *out_buf = *in_buf;
1126                                    in_buf++;
1127                                    out_buf++;
1128                                    in_bytes--;
1129                                    out_bytes--;
1130    
1131                                    (*p_buf_offset)++;
1132                                    (*p_conv_len)++;
1133    
1134                                    continue;
1135                            }
1136                            else // something strange
1137                            {
1138    #ifdef _DEBUG
1139                                    log_error("iconv(in_bytes=%d, out_bytes=%d) error: %d, in_buf[0]=%d\n",
1140                                                      in_bytes, out_bytes, errno, in_buf[0]);
1141    #endif
1142    
1143                                    *p_buf_offset = (int)(in_buf - p_buf);
1144                                    *p_conv_len = (int)(conv_size - out_bytes);
1145                                    skip_current = 1;
1146    
1147                                    continue;
1148                            }
1149                    }
1150                    else
1151                    {
1152                            *p_buf_len = 0;
1153                            *p_buf_offset = 0;
1154                            *p_conv_len = (int)(conv_size - out_bytes);
1155    
1156                            break;
1157                    }
1158            }
1159    
1160            if (*p_buf_offset >= *p_buf_len)
1161            {
1162                    *p_buf_len = 0;
1163                    *p_buf_offset = 0;
1164            }
1165    
1166            return 0;
1167    }
1168    
1169    int io_conv_init(const char *charset)
1170    {
1171            char tocode[CHARSET_MAX_LEN + 20];
1172    
1173            if (charset == NULL)
1174            {
1175                    log_error("NULL pointer error\n");
1176                    return -1;
1177            }
1178    
1179            io_conv_cleanup();
1180    
1181            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1182            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1183    
1184            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1185                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1186            stdin_cd = iconv_open(tocode, stdio_charset);
1187            if (stdin_cd == (iconv_t)(-1))
1188            {
1189                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1190                    return -2;
1191            }
1192    
1193            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1194                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1195            stdout_cd = iconv_open(tocode, BBS_default_charset);
1196            if (stdout_cd == (iconv_t)(-1))
1197            {
1198                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1199                    iconv_close(stdin_cd);
1200                    return -2;
1201            }
1202    
1203            return 0;
1204    }
1205    
1206    int io_conv_cleanup(void)
1207    {
1208            if (stdin_cd != NULL)
1209            {
1210                    iconv_close(stdin_cd);
1211                    stdin_cd = NULL;
1212            }
1213            if (stdout_cd != NULL)
1214            {
1215                    iconv_close(stdout_cd);
1216                    stdout_cd = NULL;
1217            }
1218    
1219            return 0;
1220    }


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

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