/[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.54 by sysadm, Sat Oct 18 01:50:48 2025 UTC Revision 1.66 by sysadm, Mon Nov 17 02:32:42 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                          io.c  -  description  /*
3                                                           -------------------   * io
4          Copyright            : (C) 2004-2025 by Leaflet   *   - basic terminal-based user input / output features
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "common.h"  #include "common.h"
14  #include "io.h"  #include "io.h"
# Line 31  Line 27 
27  #include <libssh/libssh.h>  #include <libssh/libssh.h>
28  #include <libssh/server.h>  #include <libssh/server.h>
29    
30    const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
31    char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
32    
33    // epoll for STDIO
34    static int stdin_epollfd = -1;
35    static int stdout_epollfd = -1;
36    static int stdin_flags;
37    static int stdout_flags;
38    
39    // static input / output buffer
40    static char stdin_buf[LINE_BUFFER_LEN];
41  static char stdout_buf[BUFSIZ];  static char stdout_buf[BUFSIZ];
42    static int stdin_buf_len = 0;
43  static int stdout_buf_len = 0;  static int stdout_buf_len = 0;
44    static int stdin_buf_offset = 0;
45  static int stdout_buf_offset = 0;  static int stdout_buf_offset = 0;
46    
47    static char stdin_conv[LINE_BUFFER_LEN * 2];
48    static char stdout_conv[BUFSIZ * 2];
49    static int stdin_conv_len = 0;
50    static int stdout_conv_len = 0;
51    static int stdin_conv_offset = 0;
52    static int stdout_conv_offset = 0;
53    
54    static iconv_t stdin_cd = NULL;
55    static iconv_t stdout_cd = NULL;
56    
57    int io_init(void)
58    {
59            struct epoll_event ev;
60    
61            if (stdin_epollfd == -1)
62            {
63                    stdin_epollfd = epoll_create1(0);
64                    if (stdin_epollfd == -1)
65                    {
66                            log_error("epoll_create1() error (%d)\n", errno);
67                            return -1;
68                    }
69    
70                    ev.events = EPOLLIN;
71                    ev.data.fd = STDIN_FILENO;
72                    if (epoll_ctl(stdin_epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)
73                    {
74                            log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);
75                            if (close(stdin_epollfd) < 0)
76                            {
77                                    log_error("close(stdin_epollfd) error (%d)\n");
78                            }
79                            stdin_epollfd = -1;
80                            return -1;
81                    }
82    
83                    stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
84                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags | O_NONBLOCK);
85            }
86    
87            if (stdout_epollfd == -1)
88            {
89                    stdout_epollfd = epoll_create1(0);
90                    if (stdout_epollfd == -1)
91                    {
92                            log_error("epoll_create1() error (%d)\n", errno);
93                            return -1;
94                    }
95    
96                    ev.events = EPOLLOUT;
97                    ev.data.fd = STDOUT_FILENO;
98                    if (epoll_ctl(stdout_epollfd, EPOLL_CTL_ADD, STDOUT_FILENO, &ev) == -1)
99                    {
100                            log_error("epoll_ctl(STDOUT_FILENO) error (%d)\n", errno);
101                            if (close(stdout_epollfd) < 0)
102                            {
103                                    log_error("close(stdout_epollfd) error (%d)\n");
104                            }
105                            stdout_epollfd = -1;
106                            return -1;
107                    }
108    
109                    // Set STDOUT as non-blocking
110                    stdout_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
111                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags | O_NONBLOCK);
112            }
113    
114            return 0;
115    }
116    
117    void io_cleanup(void)
118    {
119            if (stdin_epollfd != -1)
120            {
121                    fcntl(STDIN_FILENO, F_SETFL, stdin_flags);
122    
123                    if (close(stdin_epollfd) < 0)
124                    {
125                            log_error("close(stdin_epollfd) error (%d)\n");
126                    }
127                    stdin_epollfd = -1;
128            }
129    
130            if (stdout_epollfd != -1)
131            {
132                    fcntl(STDOUT_FILENO, F_SETFL, stdout_flags);
133    
134                    if (close(stdout_epollfd) < 0)
135                    {
136                            log_error("close(stdout_epollfd) error (%d)\n");
137                    }
138                    stdout_epollfd = -1;
139            }
140    }
141    
142  int prints(const char *format, ...)  int prints(const char *format, ...)
143  {  {
144          char buf[BUFSIZ];          char buf[BUFSIZ];
# Line 93  int outc(char c) Line 197  int outc(char c)
197    
198  int iflush(void)  int iflush(void)
199  {  {
200          int flags;          struct epoll_event events[MAX_EVENTS];
201          struct epoll_event ev, events[MAX_EVENTS];          int nfds;
         int nfds, epollfd;  
202          int retry;          int retry;
203          int ret = 0;          int ret = 0;
204    
         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);  
   
205          // Retry wait / flush for at most 3 times          // Retry wait / flush for at most 3 times
206          retry = 3;          retry = 3;
207          while (retry > 0 && !SYS_server_exit)          while (retry > 0 && !SYS_server_exit)
208          {          {
209                  retry--;                  retry--;
210    
211                  nfds = epoll_wait(epollfd, events, MAX_EVENTS, 100); // 0.1 second                  nfds = epoll_wait(stdout_epollfd, events, MAX_EVENTS, 100); // 0.1 second
212    
213                  if (nfds < 0)                  if (nfds < 0)
214                  {                  {
# Line 148  int iflush(void) Line 228  int iflush(void)
228                  {                  {
229                          if (events[i].data.fd == STDOUT_FILENO)                          if (events[i].data.fd == STDOUT_FILENO)
230                          {                          {
231                                  while (stdout_buf_offset < stdout_buf_len && !SYS_server_exit) // write until complete or error                                  if (stdout_buf_offset < stdout_buf_len)
232                                    {
233                                            ret = io_buf_conv(stdout_cd, stdout_buf, &stdout_buf_len, &stdout_buf_offset, stdout_conv, sizeof(stdout_conv), &stdout_conv_len);
234                                            if (ret < 0)
235                                            {
236                                                    log_error("io_buf_conv(stdout, %d, %d, %d) error\n", stdout_buf_len, stdout_buf_offset, stdout_conv_len);
237                                                    stdout_buf_len = stdout_buf_offset; // Discard invalid sequence
238                                            }
239                                    }
240    
241                                    while (stdout_conv_offset < stdout_conv_len && !SYS_server_exit) // write until complete or error
242                                  {                                  {
243                                          if (SSH_v2)                                          if (SSH_v2)
244                                          {                                          {
245                                                  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));
246                                                  if (ret == SSH_ERROR)                                                  if (ret == SSH_ERROR)
247                                                  {                                                  {
248                                                          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(void) Line 252  int iflush(void)
252                                          }                                          }
253                                          else                                          else
254                                          {                                          {
255                                                  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));
256                                          }                                          }
257                                          if (ret < 0)                                          if (ret < 0)
258                                          {                                          {
# Line 190  int iflush(void) Line 280  int iflush(void)
280                                          }                                          }
281                                          else                                          else
282                                          {                                          {
283                                                  stdout_buf_offset += ret;                                                  stdout_conv_offset += ret;
284                                                  if (stdout_buf_offset >= stdout_buf_len) // flush buffer completely                                                  if (stdout_conv_offset >= stdout_conv_len) // flush buffer completely
285                                                  {                                                  {
286                                                          ret = 0;                                                          ret = 0;
287                                                          stdout_buf_offset = 0;                                                          stdout_conv_offset = 0;
288                                                          stdout_buf_len = 0;                                                          stdout_conv_len = 0;
289                                                          retry = 0;                                                          retry = 0;
290                                                          break;                                                          break;
291                                                  }                                                  }
# Line 206  int iflush(void) Line 296  int iflush(void)
296                  }                  }
297          }          }
298    
         // Restore STDOUT flags  
         fcntl(STDOUT_FILENO, F_SETFL, flags);  
   
         if (close(epollfd) < 0)  
         {  
                 log_error("close(epoll) error (%d)\n");  
         }  
   
299          return ret;          return ret;
300  }  }
301    
302  int igetch(int timeout)  int igetch(int timeout)
303  {  {
304          // static input buffer          static int stdin_read_wait = 0;
         static unsigned char buf[LINE_BUFFER_LEN];  
         static int len = 0;  
         static int pos = 0;  
305    
306          struct epoll_event ev, events[MAX_EVENTS];          struct epoll_event events[MAX_EVENTS];
307          int nfds, epollfd;          int nfds;
308          int ret;          int ret;
309          int loop;          int loop;
310    
# Line 235  int igetch(int timeout) Line 314  int igetch(int timeout)
314          int in_ascii = 0;          int in_ascii = 0;
315          int in_control = 0;          int in_control = 0;
316          int i = 0;          int i = 0;
         int flags;  
317    
318          if (pos >= len)          if (stdin_conv_offset >= stdin_conv_len)
319          {          {
320                  len = 0;                  stdin_conv_len = 0;
321                  pos = 0;                  stdin_conv_offset = 0;
322    
323                  epollfd = epoll_create1(0);                  for (loop = 1; loop && stdin_buf_len < sizeof(stdin_buf) && stdin_conv_offset >= stdin_conv_len && !SYS_server_exit;)
                 if (epollfd < 0)  
                 {  
                         log_error("epoll_create1() error (%d)\n", errno);  
                         return -1;  
                 }  
   
                 ev.events = EPOLLIN;  
                 ev.data.fd = STDIN_FILENO;  
                 if (epoll_ctl(epollfd, EPOLL_CTL_ADD, STDIN_FILENO, &ev) == -1)  
                 {  
                         log_error("epoll_ctl(STDIN_FILENO) error (%d)\n", errno);  
   
                         if (close(epollfd) < 0)  
                         {  
                                 log_error("close(epoll) error (%d)\n");  
                         }  
                         return -1;  
                 }  
   
                 flags = fcntl(STDIN_FILENO, F_GETFL, 0);  
                 fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);  
   
                 for (loop = 1; loop && pos >= len && !SYS_server_exit;)  
324                  {                  {
325                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))                          if (SSH_v2 && ssh_channel_is_closed(SSH_channel))
326                          {                          {
# Line 274  int igetch(int timeout) Line 329  int igetch(int timeout)
329                                  break;                                  break;
330                          }                          }
331    
332                          nfds = epoll_wait(epollfd, events, MAX_EVENTS, timeout);                          if (!stdin_read_wait)
   
                         if (nfds < 0)  
333                          {                          {
334                                  if (errno != EINTR)                                  nfds = epoll_wait(stdin_epollfd, events, MAX_EVENTS, timeout);
335    
336                                    if (nfds < 0)
337                                    {
338                                            if (errno != EINTR)
339                                            {
340                                                    log_error("epoll_wait() error (%d)\n", errno);
341                                                    break;
342                                            }
343                                            continue;
344                                    }
345                                    else if (nfds == 0) // timeout
346                                  {                                  {
347                                          log_error("epoll_wait() error (%d)\n", errno);                                          out = KEY_TIMEOUT;
348                                          break;                                          break;
349                                  }                                  }
350                                  continue;  
351                          }                                  for (int i = 0; i < nfds; i++)
352                          else if (nfds == 0) // timeout                                  {
353                          {                                          if (events[i].data.fd == STDIN_FILENO)
354                                  out = KEY_TIMEOUT;                                          {
355                                  break;                                                  stdin_read_wait = 1;
356                                            }
357                                    }
358                          }                          }
359    
360                          for (int i = 0; i < nfds; i++)                          if (stdin_read_wait)
361                          {                          {
362                                  if (events[i].data.fd == STDIN_FILENO)                                  while (stdin_buf_len < sizeof(stdin_buf) && !SYS_server_exit) // read until complete or error
363                                  {                                  {
364                                          while (len < sizeof(buf) && !SYS_server_exit) // read until complete or error                                          if (SSH_v2)
365                                          {                                          {
366                                                  if (SSH_v2)                                                  ret = ssh_channel_read_nonblocking(SSH_channel, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (uint32_t)stdin_buf_len, 0);
367                                                    if (ret == SSH_ERROR)
368                                                  {                                                  {
369                                                          ret = ssh_channel_read_nonblocking(SSH_channel, buf + len, sizeof(buf) - (uint32_t)len, 0);                                                          log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));
370                                                          if (ret == SSH_ERROR)                                                          loop = 0;
371                                                          {                                                          break;
                                                                 log_error("ssh_channel_read_nonblocking() error: %s\n", ssh_get_error(SSH_session));  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
                                                         else if (ret == SSH_EOF)  
                                                         {  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
                                                         else if (ret == 0)  
                                                         {  
                                                                 out = 0;  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
372                                                  }                                                  }
373                                                  else                                                  else if (ret == SSH_EOF)
374                                                  {                                                  {
375                                                          ret = (int)read(STDIN_FILENO, buf + len, sizeof(buf) - (size_t)len);                                                          stdin_read_wait = 0;
376                                                            loop = 0;
377                                                            break;
378                                                  }                                                  }
379                                                  if (ret < 0)                                                  else if (ret == 0)
380                                                  {                                                  {
381                                                          if (errno == EAGAIN || errno == EWOULDBLOCK)                                                          out = 0;
382                                                          {                                                          stdin_read_wait = 0;
383                                                                  out = 0;                                                          loop = 0;
384                                                                  loop = 0;                                                          break;
                                                                 break;  
                                                         }  
                                                         else if (errno == EINTR)  
                                                         {  
                                                                 continue;  
                                                         }  
                                                         else  
                                                         {  
 #ifdef _DEBUG  
                                                                 log_error("read(STDIN) error (%d)\n", errno);  
 #endif  
                                                                 loop = 0;  
                                                                 break;  
                                                         }  
385                                                  }                                                  }
386                                                  else if (ret == 0) // broken pipe                                          }
387                                            else
388                                            {
389                                                    ret = (int)read(STDIN_FILENO, stdin_buf + stdin_buf_len, sizeof(stdin_buf) - (size_t)stdin_buf_len);
390                                            }
391                                            if (ret < 0)
392                                            {
393                                                    if (errno == EAGAIN || errno == EWOULDBLOCK)
394                                                  {                                                  {
395                                                            out = 0;
396                                                            stdin_read_wait = 0;
397                                                          loop = 0;                                                          loop = 0;
398                                                          break;                                                          break;
399                                                  }                                                  }
400                                                  else                                                  else if (errno == EINTR)
401                                                  {                                                  {
                                                         len += ret;  
402                                                          continue;                                                          continue;
403                                                  }                                                  }
404                                                    else
405                                                    {
406    #ifdef _DEBUG
407                                                            log_error("read(STDIN) error (%d)\n", errno);
408    #endif
409                                                            loop = 0;
410                                                            break;
411                                                    }
412                                            }
413                                            else if (ret == 0) // broken pipe
414                                            {
415                                                    stdin_read_wait = 0;
416                                                    loop = 0;
417                                                    break;
418                                            }
419                                            else
420                                            {
421                                                    stdin_buf_len += ret;
422                                                    continue;
423                                          }                                          }
424                                  }                                  }
425                          }                          }
426    
427                          // For debug                          // For debug
428  #ifdef _DEBUG  #ifdef _DEBUG
429                          for (int j = pos; j < len; j++)                          for (int j = stdin_buf_offset; j < stdin_buf_len; j++)
430                          {                          {
431                                  log_common("Debug: <--[%u]\n", (buf[j] + 256) % 256);                                  log_error("Debug input: <--[%u]\n", (stdin_buf[j] + 256) % 256);
432                          }                          }
433  #endif  #endif
434                  }                  }
435    
436                  fcntl(STDIN_FILENO, F_SETFL, flags);                  if (stdin_buf_offset < stdin_buf_len)
   
                 if (close(epollfd) < 0)  
437                  {                  {
438                          log_error("close(epoll) error (%d)\n");                          ret = io_buf_conv(stdin_cd, stdin_buf, &stdin_buf_len, &stdin_buf_offset, stdin_conv, sizeof(stdin_conv), &stdin_conv_len);
439                            if (ret < 0)
440                            {
441                                    log_error("io_buf_conv(stdin, %d, %d, %d) error\n", stdin_buf_len, stdin_buf_offset, stdin_conv_len);
442                                    stdin_buf_len = stdin_buf_offset; // Discard invalid sequence
443                            }
444    
445                            // For debug
446    #ifdef _DEBUG
447                            for (int j = stdin_conv_offset; j < stdin_conv_len; j++)
448                            {
449                                    log_error("Debug input_conv: <--[%u]\n", (stdin_conv[j] + 256) % 256);
450                            }
451    #endif
452                  }                  }
453          }          }
454    
455          while (pos < len)          while (stdin_conv_offset < stdin_conv_len)
456          {          {
457                  unsigned char c = buf[pos++];                  unsigned char c = (unsigned char)stdin_conv[stdin_conv_offset++];
458    
459                  // Convert \r\n to \r                  // Convert \r\n to \r
460                  if (c == CR && pos < len && buf[pos] == LF)                  if (c == CR && stdin_conv_offset < stdin_conv_len && stdin_conv[stdin_conv_offset] == LF)
461                  {                  {
462                          pos++;                          stdin_conv_offset++;
463                  }                  }
464    
465                  // Convert single \n to \r                  // Convert single \n to \r
# Line 834  int igetch(int timeout) Line 912  int igetch(int timeout)
912  #ifdef _DEBUG  #ifdef _DEBUG
913          if (out != KEY_TIMEOUT && out != KEY_NULL)          if (out != KEY_TIMEOUT && out != KEY_NULL)
914          {          {
915                  log_common("Debug: -->[0x %x]\n", out);                  log_error("Debug: -->[0x %x]\n", out);
916          }          }
917  #endif  #endif
918    
# Line 870  int io_buf_conv(iconv_t cd, char *p_buf, Line 948  int io_buf_conv(iconv_t cd, char *p_buf,
948          size_t in_bytes;          size_t in_bytes;
949          size_t out_bytes;          size_t out_bytes;
950          int ret;          int ret;
951            int in_control = 0;
952            size_t i = 0;
953    
954            if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
955            {
956                    log_error("NULL pointer error\n");
957                    return -1;
958            }
959    
960          in_buf = p_buf + *p_buf_offset;          in_buf = p_buf + *p_buf_offset;
961          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);          in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
# Line 878  int io_buf_conv(iconv_t cd, char *p_buf, Line 964  int io_buf_conv(iconv_t cd, char *p_buf,
964    
965          while (in_bytes > 0)          while (in_bytes > 0)
966          {          {
967                    if ((unsigned char)(*in_buf) == KEY_CONTROL)
968                    {
969                            if (in_control == 0)
970                            {
971                                    in_control = 1;
972                                    i = 0;
973                            }
974                    }
975    
976                    if (in_control)
977                    {
978                            if (out_bytes <= 0)
979                            {
980                                    log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
981                                    return -2;
982                            }
983    
984                            *out_buf = *in_buf;
985                            in_buf++;
986                            out_buf++;
987                            in_bytes--;
988                            out_bytes--;
989    
990                            (*p_buf_offset)++;
991                            *p_conv_len = (int)(conv_size - out_bytes);
992    
993                            i++;
994                            if (i >= 2)
995                            {
996                                    in_control = 0;
997                            }
998                            continue;
999                    }
1000    
1001                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);                  ret = (int)iconv(cd, &in_buf, &in_bytes, &out_buf, &out_bytes);
1002                  if (ret == -1)                  if (ret == -1)
1003                  {                  {
1004                          if (errno == EINVAL) // Incomplete                          if (errno == EINVAL) // Incomplete
1005                          {                          {
1006  #ifdef _DEBUG  #ifdef _DEBUG
1007                                  log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL\n", in_bytes, out_bytes);                                  log_error("iconv(inbytes=%d, outbytes=%d) error: EINVAL, in_buf[0]=%d\n", in_bytes, out_bytes, in_buf[0]);
1008  #endif  #endif
1009                                  *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                  if (p_buf != in_buf)
1010                                  *p_buf_offset = 0;                                  {
1011                                  *p_conv_len = (int)(conv_size - out_bytes);                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);
1012                                  memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          *p_buf_offset = 0;
1013                                            *p_conv_len = (int)(conv_size - out_bytes);
1014                                            memmove(p_buf, in_buf, (size_t)(*p_buf_len));
1015                                    }
1016    
1017                                  break;                                  break;
1018                          }                          }
# Line 906  int io_buf_conv(iconv_t cd, char *p_buf, Line 1029  int io_buf_conv(iconv_t cd, char *p_buf,
1029                                          return -2;                                          return -2;
1030                                  }                                  }
1031    
1032                                    // reset in_bytes when "//IGNORE" is applied
1033                                    if (in_bytes == 0)
1034                                    {
1035                                            in_bytes = (size_t)(*p_buf_len - *p_buf_offset);
1036                                    }
1037    
1038                                  *out_buf = *in_buf;                                  *out_buf = *in_buf;
1039                                  in_buf++;                                  in_buf++;
1040                                  out_buf++;                                  out_buf++;
# Line 926  int io_buf_conv(iconv_t cd, char *p_buf, Line 1055  int io_buf_conv(iconv_t cd, char *p_buf,
1055          }          }
1056    
1057          return 0;          return 0;
1058    }
1059    
1060    int io_conv_init(const char *charset)
1061    {
1062            char tocode[CHARSET_MAX_LEN + 20];
1063    
1064            if (charset == NULL)
1065            {
1066                    log_error("NULL pointer error\n");
1067                    return -1;
1068            }
1069    
1070            io_conv_cleanup();
1071    
1072            strncpy(stdio_charset, charset, sizeof(stdio_charset) - 1);
1073            stdio_charset[sizeof(stdio_charset) - 1] = '\0';
1074    
1075            snprintf(tocode, sizeof(tocode), "%s%s", BBS_default_charset,
1076                             (strcasecmp(stdio_charset, BBS_default_charset) == 0 ? "" : "//IGNORE"));
1077            stdin_cd = iconv_open(tocode, stdio_charset);
1078            if (stdin_cd == (iconv_t)(-1))
1079            {
1080                    log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno);
1081                    return -2;
1082            }
1083    
1084            snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset,
1085                             (strcasecmp(BBS_default_charset, stdio_charset) == 0 ? "" : "//TRANSLIT"));
1086            stdout_cd = iconv_open(tocode, BBS_default_charset);
1087            if (stdout_cd == (iconv_t)(-1))
1088            {
1089                    log_error("iconv_open(%s->%s) error: %d\n", BBS_default_charset, tocode, errno);
1090                    iconv_close(stdin_cd);
1091                    return -2;
1092            }
1093    
1094            return 0;
1095    }
1096    
1097    int io_conv_cleanup(void)
1098    {
1099            if (stdin_cd != NULL)
1100            {
1101                    iconv_close(stdin_cd);
1102                    stdin_cd = NULL;
1103            }
1104            if (stdout_cd != NULL)
1105            {
1106                    iconv_close(stdout_cd);
1107                    stdout_cd = NULL;
1108            }
1109    
1110            return 0;
1111  }  }


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

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