/[LeafOK_CVS]/lbbs/src/lml.c
ViewVC logotype

Diff of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.6 by sysadm, Tue Jun 3 03:41:19 2025 UTC Revision 1.14 by sysadm, Wed Jul 16 05:24:08 2025 UTC
# Line 14  Line 14 
14   *                                                                         *   *                                                                         *
15   ***************************************************************************/   ***************************************************************************/
16    
17    #include "common.h"
18  #include "lml.h"  #include "lml.h"
19  #include "log.h"  #include "log.h"
 #include "common.h"  
20  #include <stdio.h>  #include <stdio.h>
21  #include <string.h>  #include <string.h>
22  #include <sys/param.h>  #include <sys/param.h>
# Line 75  static int lml_tag_quote_level = 0; Line 75  static int lml_tag_quote_level = 0;
75    
76  static int lml_tag_quote_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len)  static int lml_tag_quote_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len)
77  {  {
   
78          if (strcasecmp(tag_name, "quote") == 0)          if (strcasecmp(tag_name, "quote") == 0)
79          {          {
80                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
81                  {                  {
82                          lml_tag_quote_level++;                          lml_tag_quote_level++;
83                  }                  }
84                  return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
85                                                    lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
86          }          }
87          else if (strcasecmp(tag_name, "/quote") == 0)          else if (strcasecmp(tag_name, "/quote") == 0)
88          {          {
# Line 90  static int lml_tag_quote_filter(const ch Line 90  static int lml_tag_quote_filter(const ch
90                  {                  {
91                          lml_tag_quote_level--;                          lml_tag_quote_level--;
92                  }                  }
93                  return snprintf(tag_output_buf, tag_output_buf_len,                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
94                                                  (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));                                                  (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
95          }          }
96    
# Line 104  const static char *LML_tag_def[][3] = { Line 104  const static char *LML_tag_def[][3] = {
104          {"/bold", "\033[22m", NULL},          {"/bold", "\033[22m", NULL},
105          {"b", "\033[1m", ""},          {"b", "\033[1m", ""},
106          {"/b", "\033[22m", NULL},          {"/b", "\033[22m", NULL},
107          {"italic", "\033[5m", ""}, // use blink instead          {"italic", "\033[5m", ""},       // use blink instead
108          {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm          {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm
109          {"i", "\033[5m", ""},          {"i", "\033[5m", ""},
110          {"/i", "\033[m", NULL},          {"/i", "\033[m", NULL},
# Line 117  const static char *LML_tag_def[][3] = { Line 117  const static char *LML_tag_def[][3] = {
117          {"quote", NULL, (const char *)lml_tag_quote_filter},          {"quote", NULL, (const char *)lml_tag_quote_filter},
118          {"/quote", NULL, (const char *)lml_tag_quote_filter},          {"/quote", NULL, (const char *)lml_tag_quote_filter},
119          {"url", "", ""},          {"url", "", ""},
120          {"/url", "(链接: %s)", NULL},          {"/url", "(閾炬帴: %s)", NULL},
121          {"link", "", ""},          {"link", "", ""},
122          {"/link", "(链接: %s)", NULL},          {"/link", "(閾炬帴: %s)", NULL},
123          {"email", "", ""},          {"email", "", ""},
124          {"/email", "(Email: %s)", NULL},          {"/email", "(Email: %s)", NULL},
125          {"user", "", ""},          {"user", "", ""},
126          {"/user", "(用户: %s)", NULL},          {"/user", "(鐢ㄦ埛: %s)", NULL},
127          {"article", "", ""},          {"article", "", ""},
128          {"/article", "(文章: %s)", NULL},          {"/article", "(鏂囩珷: %s)", NULL},
129          {"image", "(图片: %s)", ""},          {"image", "(鍥剧墖: %s)", ""},
130          {"flash", "(Flash: %s)", ""},          {"flash", "(Flash: %s)", ""},
131          {"bwf", "\033[1;31m****\033[m", ""},          {"bwf", "\033[1;31m****\033[m", ""},
132  };  };
# Line 153  inline static void lml_init(void) Line 153  inline static void lml_init(void)
153    
154  int lml_plain(const char *str_in, char *str_out, int buf_len)  int lml_plain(const char *str_in, char *str_out, int buf_len)
155  {  {
156            char c;
157          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
158          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
159          int i;          int i;
# Line 177  int lml_plain(const char *str_in, char * Line 178  int lml_plain(const char *str_in, char *
178                          {                          {
179                                  lml_tag_quote_level -= fb_quote_level;                                  lml_tag_quote_level -= fb_quote_level;
180    
181                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
182                                                                                    (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));                                                                                    (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
183                                  if (j + tag_output_len >= buf_len - 1)                                  if (j + tag_output_len >= buf_len)
184                                  {                                  {
185                                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
186                                          str_out[j] = '\0';                                          str_out[j] = '\0';
187                                          return j;                                          return j;
188                                  }                                  }
# Line 200  int lml_plain(const char *str_in, char * Line 201  int lml_plain(const char *str_in, char *
201                          {                          {
202                                  lml_tag_quote_level += fb_quote_level;                                  lml_tag_quote_level += fb_quote_level;
203    
204                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
205                                                                                    lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);                                                                                    lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);
206                                  if (j + tag_output_len >= buf_len - 1)                                  if (j + tag_output_len >= buf_len)
207                                  {                                  {
208                                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
209                                          str_out[j] = '\0';                                          str_out[j] = '\0';
210                                          return j;                                          return j;
211                                  }                                  }
# Line 215  int lml_plain(const char *str_in, char * Line 216  int lml_plain(const char *str_in, char *
216                          new_line = 0;                          new_line = 0;
217                  }                  }
218    
219                    if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
220                    {
221                            for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
222                                    ;
223    
224                            if (str_in[k] == 'm') // Valid
225                            {
226                                    if (j + (k - i + 1) >= buf_len)
227                                    {
228                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
229                                            str_out[j] = '\0';
230                                            return j;
231                                    }
232                                    memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
233                                    j += (k - i + 1);
234                                    i = k;
235                                    continue;
236                            }
237                            else // reach end of string
238                            {
239                                    break;
240                            }
241                    }
242    
243                  if (str_in[i] == '\n')                  if (str_in[i] == '\n')
244                  {                  {
245                          tag_start_pos = -1; // jump out of tag at end of line                          tag_start_pos = -1; // jump out of tag at end of line
# Line 271  int lml_plain(const char *str_in, char * Line 296  int lml_plain(const char *str_in, char *
296                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(
297                                                                          LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);                                                                          LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);
298                                                          }                                                          }
299                                                          if (j + tag_output_len >= buf_len - 1)                                                          if (j + tag_output_len >= buf_len)
300                                                          {                                                          {
301                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
302                                                                  str_out[j] = '\0';                                                                  str_out[j] = '\0';
303                                                                  return j;                                                                  return j;
304                                                          }                                                          }
# Line 292  int lml_plain(const char *str_in, char * Line 317  int lml_plain(const char *str_in, char *
317                  }                  }
318                  else if (tag_start_pos == -1) // not in LML tag                  else if (tag_start_pos == -1) // not in LML tag
319                  {                  {
320                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] & 0b10000000) // head of multi-byte character
321                          {                          {
322                                  if (j + 2 >= buf_len - 1)                                  if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes
323                                  {                                  {
324                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 2, buf_len - 1);                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len);
325                                          str_out[j] = '\0';                                          str_out[j] = '\0';
326                                          return j;                                          return j;
327                                  }                                  }
328                                  str_out[j++] = str_in[i++];  
329                                  if (str_in[i] == '\0')                                  c = (str_in[i] & 0b01110000) << 1;
330                                    while (c & 0b10000000)
331                                  {                                  {
332                                          str_out[j] = '\0';                                          str_out[j++] = str_in[i++];
333                                          return j;                                          if (str_in[i] == '\0')
334                                            {
335                                                    str_out[j] = '\0';
336                                                    return j;
337                                            }
338                                            c = (c & 0b01111111) << 1;
339                                  }                                  }
340                          }                          }
341    
342                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
343                          {                          {
344                                  log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len - 1);                                  log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len);
345                                  str_out[j] = '\0';                                  str_out[j] = '\0';
346                                  return j;                                  return j;
347                          }                          }
# Line 325  int lml_plain(const char *str_in, char * Line 356  int lml_plain(const char *str_in, char *
356          if (lml_tag_quote_level > 0)          if (lml_tag_quote_level > 0)
357          {          {
358                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
359                  if (j + tag_output_len >= buf_len - 1)                  if (j + tag_output_len >= buf_len)
360                  {                  {
361                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                          log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
362                          str_out[j] = '\0';                          str_out[j] = '\0';
363                          return j;                          return j;
364                  }                  }


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

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