/[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.67 by sysadm, Mon Nov 17 06:41:18 2025 UTC Revision 1.69 by sysadm, Sat Nov 22 15:51:42 2025 UTC
# Line 32  Line 32 
32  #include <poll.h>  #include <poll.h>
33  #endif  #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";  const char BBS_default_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
41  char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";  char stdio_charset[CHARSET_MAX_LEN + 1] = "UTF-8";
42    
# Line 46  static int stdout_flags = 0; Line 51  static int stdout_flags = 0;
51    
52  // static input / output buffer  // static input / output buffer
53  static char stdin_buf[LINE_BUFFER_LEN];  static char stdin_buf[LINE_BUFFER_LEN];
54  static char stdout_buf[BUFSIZ];  static char stdout_buf[OUTPUT_BUF_SIZE];
55  static int stdin_buf_len = 0;  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;  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];  static char stdin_conv[LINE_BUFFER_LEN * 2];
61  static char stdout_conv[BUFSIZ * 2];  static char stdout_conv[OUTPUT_BUF_SIZE * 2];
62  static int stdin_conv_len = 0;  static int stdin_conv_len = 0;
63  static int stdout_conv_len = 0;  static int stdout_conv_len = 0;
64  static int stdin_conv_offset = 0;  static int stdin_conv_offset = 0;
# Line 178  void io_cleanup(void) Line 183  void io_cleanup(void)
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 188  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 201  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 213  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 1029  int io_buf_conv(iconv_t cd, char *p_buf, Line 1034  int io_buf_conv(iconv_t cd, char *p_buf,
1034          int ret;          int ret;
1035          int in_control = 0;          int in_control = 0;
1036          size_t i = 0;          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)          if (cd == NULL || p_buf == NULL || p_buf_len == NULL || p_buf_offset == NULL || p_conv == NULL || p_conv_len == NULL)
1040          {          {
# Line 1052  int io_buf_conv(iconv_t cd, char *p_buf, Line 1058  int io_buf_conv(iconv_t cd, char *p_buf,
1058                          }                          }
1059                  }                  }
1060    
1061                  if (in_control)                  if (in_control || skip_current)
1062                  {                  {
1063                            skip_current = 0;
1064    
1065                          if (out_bytes <= 0)                          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);                                  log_error("No enough free space in p_conv, conv_len=%d, conv_size=%d\n", *p_conv_len, conv_size);
# Line 1067  int io_buf_conv(iconv_t cd, char *p_buf, Line 1075  int io_buf_conv(iconv_t cd, char *p_buf,
1075                          out_bytes--;                          out_bytes--;
1076    
1077                          (*p_buf_offset)++;                          (*p_buf_offset)++;
1078                          *p_conv_len = (int)(conv_size - out_bytes);                          (*p_conv_len)++;
1079    
1080                          i++;                          i++;
1081                          if (i >= 2)                          if (i >= 2)
# Line 1087  int io_buf_conv(iconv_t cd, char *p_buf, Line 1095  int io_buf_conv(iconv_t cd, char *p_buf,
1095  #endif  #endif
1096                                  if (p_buf != in_buf)                                  if (p_buf != in_buf)
1097                                  {                                  {
1098                                          *p_buf_len = (int)(p_buf + *p_buf_len - in_buf);                                          *p_buf_len -= (int)(in_buf - p_buf);
1099                                          *p_buf_offset = 0;                                          *p_buf_offset = 0;
1100                                          *p_conv_len = (int)(conv_size - out_bytes);                                          *p_conv_len = (int)(conv_size - out_bytes);
1101                                          memmove(p_buf, in_buf, (size_t)(*p_buf_len));                                          memmove(p_buf, in_buf, (size_t)(*p_buf_len));
# Line 1120  int io_buf_conv(iconv_t cd, char *p_buf, Line 1128  int io_buf_conv(iconv_t cd, char *p_buf,
1128                                  in_bytes--;                                  in_bytes--;
1129                                  out_bytes--;                                  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("*p_buf_offset += %d, *p_conv_len = %d, in_bytes=%d, out_bytes=%d, in_buf[0]=%d\n",
1140                                                      (int)(in_buf - p_buf), (int)(conv_size - out_bytes), in_bytes, out_bytes, 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;                                  continue;
1148                          }                          }
1149                  }                  }
# Line 1133  int io_buf_conv(iconv_t cd, char *p_buf, Line 1157  int io_buf_conv(iconv_t cd, char *p_buf,
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;          return 0;
1167  }  }
1168    


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

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