/[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.8 by sysadm, Sat Jun 14 10:03:32 2025 UTC Revision 1.21 by sysadm, Sun Oct 5 08:14:06 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 24  Line 24 
24  #define LML_TAG_PARAM_BUF_LEN 256  #define LML_TAG_PARAM_BUF_LEN 256
25  #define LML_TAG_OUTPUT_BUF_LEN 1024  #define LML_TAG_OUTPUT_BUF_LEN 1024
26    
27  typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len);  typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode);
28    
29  static int lml_tag_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len)  static int lml_tag_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len)
30  {  {
# Line 73  static const char *lml_tag_quote_color[] Line 73  static const char *lml_tag_quote_color[]
73    
74  static int lml_tag_quote_level = 0;  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, int quote_mode)
77  {  {
78          if (strcasecmp(tag_name, "quote") == 0)          if (strcasecmp(tag_name, "quote") == 0)
79          {          {
# Line 81  static int lml_tag_quote_filter(const ch Line 81  static int lml_tag_quote_filter(const ch
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 89  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    
97          return 0;          return 0;
98  }  }
99    
100  const static char *LML_tag_def[][3] = {  static int lml_tag_disabled = 0;
101          {"left", "[", ""},  
102          {"right", "]", NULL},  static int lml_tag_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
103          {"bold", "\033[1m", ""}, // does not work in Fterm  {
104          {"/bold", "\033[22m", NULL},          lml_tag_disabled = 1;
105          {"b", "\033[1m", ""},  
106          {"/b", "\033[22m", NULL},          return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "" : "[plain]"));
107          {"italic", "\033[5m", ""},       // use blink instead  }
108          {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm  
109          {"i", "\033[5m", ""},  const static char *lml_tag_def[][4] = {
110          {"/i", "\033[m", NULL},          // Definition of tuple: {lml_tag, lml_output, default_param | lml_filter_cb, quote_mode_output}
111          {"underline", "\033[4m", ""},          {"plain", NULL, (const char *)lml_tag_disable_filter, NULL},
112          {"/underline", "\033[m", NULL}, // \033[24m does not work in Fterm          {"left", "[", "", "[left]"},
113          {"u", "\033[4m", ""},          {"right", "]", "", "[right]"},
114          {"/u", "\033[m", NULL},          {"bold", "\033[1m", "", ""}, // does not work in Fterm
115          {"color", NULL, (const char *)lml_tag_color_filter},          {"/bold", "\033[22m", NULL, ""},
116          {"/color", "\033[m", NULL},          {"b", "\033[1m", "", ""},
117          {"quote", NULL, (const char *)lml_tag_quote_filter},          {"/b", "\033[22m", NULL, ""},
118          {"/quote", NULL, (const char *)lml_tag_quote_filter},          {"italic", "\033[5m", "", ""},   // use blink instead
119          {"url", "", ""},          {"/italic", "\033[m", NULL, ""}, // \033[25m does not work in Fterm
120          {"/url", "(Á´½Ó: %s)", NULL},          {"i", "\033[5m", "", ""},
121          {"link", "", ""},          {"/i", "\033[m", NULL, ""},
122          {"/link", "(Á´½Ó: %s)", NULL},          {"underline", "\033[4m", "", ""},
123          {"email", "", ""},          {"/underline", "\033[m", NULL, ""}, // \033[24m does not work in Fterm
124          {"/email", "(Email: %s)", NULL},          {"u", "\033[4m", "", ""},
125          {"user", "", ""},          {"/u", "\033[m", NULL, ""},
126          {"/user", "(Óû§: %s)", NULL},          {"color", NULL, (const char *)lml_tag_color_filter, ""},
127          {"article", "", ""},          {"/color", "\033[m", NULL, ""},
128          {"/article", "(ÎÄÕÂ: %s)", NULL},          {"quote", NULL, (const char *)lml_tag_quote_filter, ""},
129          {"image", "(ͼƬ: %s)", ""},          {"/quote", NULL, (const char *)lml_tag_quote_filter, ""},
130          {"flash", "(Flash: %s)", ""},          {"url", "", "", ""},
131          {"bwf", "\033[1;31m****\033[m", ""},          {"/url", "(链接: %s)", NULL, ""},
132            {"link", "", "", ""},
133            {"/link", "(链接: %s)", NULL, ""},
134            {"email", "", "", ""},
135            {"/email", "(Email: %s)", NULL, ""},
136            {"user", "", "", ""},
137            {"/user", "(用户: %s)", NULL, ""},
138            {"article", "", "", ""},
139            {"/article", "(文章: %s)", NULL, ""},
140            {"image", "(图片: %s)", "", "%s"},
141            {"flash", "(Flash: %s)", "", ""},
142            {"bwf", "\033[1;31m****\033[m", "", "****"},
143  };  };
144    
145  #define LML_TAG_COUNT 31  #define LML_TAG_COUNT 32
146    
147  static int LML_tag_name_len[LML_TAG_COUNT];  static int lml_tag_name_len[LML_TAG_COUNT];
148  static int LML_init = 0;  static int lml_ready = 0;
149    
150  inline static void lml_init(void)  inline static void lml_init(void)
151  {  {
152          int i;          int i;
153    
154          if (!LML_init)          if (!lml_ready)
155          {          {
156                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
157                  {                  {
158                          LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]);                          lml_tag_name_len[i] = (int)strlen(lml_tag_def[i][0]);
159                  }                  }
160    
161                  LML_init = 1;                  lml_ready = 1;
162          }          }
163  }  }
164    
165  int lml_plain(const char *str_in, char *str_out, int buf_len)  int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)
166  {  {
167            char c;
168          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
169          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
170          int i;          int i;
# Line 166  int lml_plain(const char *str_in, char * Line 179  int lml_plain(const char *str_in, char *
179    
180          lml_init();          lml_init();
181    
182            lml_tag_disabled = 0;
183          lml_tag_quote_level = 0;          lml_tag_quote_level = 0;
184    
185          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
186          {          {
187                  if (new_line)                  if (quote_mode && !lml_tag_disabled && new_line)
188                  {                  {
189                          if (fb_quote_level > 0)                          if (fb_quote_level > 0)
190                          {                          {
191                                  lml_tag_quote_level -= fb_quote_level;                                  lml_tag_quote_level -= fb_quote_level;
192    
193                                  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",
194                                                                                    (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"));
195                                  if (j + tag_output_len >= buf_len)                                  if (j + tag_output_len >= buf_len)
196                                  {                                  {
# Line 199  int lml_plain(const char *str_in, char * Line 213  int lml_plain(const char *str_in, char *
213                          {                          {
214                                  lml_tag_quote_level += fb_quote_level;                                  lml_tag_quote_level += fb_quote_level;
215    
216                                  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",
217                                                                                    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]);
218                                  if (j + tag_output_len >= buf_len)                                  if (j + tag_output_len >= buf_len)
219                                  {                                  {
# Line 228  int lml_plain(const char *str_in, char * Line 242  int lml_plain(const char *str_in, char *
242                                          return j;                                          return j;
243                                  }                                  }
244                                  memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));                                  memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
245                                    j += (k - i + 1);
246                                  i = k;                                  i = k;
247                                  continue;                                  continue;
248                          }                          }
# Line 247  int lml_plain(const char *str_in, char * Line 262  int lml_plain(const char *str_in, char *
262                          continue; // ignore '\r'                          continue; // ignore '\r'
263                  }                  }
264    
265                  if (str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
266                  {                  {
267                          tag_start_pos = i + 1;                          tag_start_pos = i + 1;
268                  }                  }
269                  else if (str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']')
270                  {                  {
271                          if (tag_start_pos >= 0)                          if (tag_start_pos >= 0)
272                          {                          {
# Line 265  int lml_plain(const char *str_in, char * Line 280  int lml_plain(const char *str_in, char *
280    
281                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (k = 0; k < LML_TAG_COUNT; k++)
282                                  {                                  {
283                                          if (strncasecmp(LML_tag_def[k][0], str_in + tag_start_pos, (size_t)LML_tag_name_len[k]) == 0)                                          if (strncasecmp(lml_tag_def[k][0], str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0)
284                                          {                                          {
285                                                  tag_param_pos = -1;                                                  tag_param_pos = -1;
286                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  switch (str_in[tag_start_pos + lml_tag_name_len[k]])
287                                                  {                                                  {
288                                                  case ' ':                                                  case ' ':
289                                                          tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1;                                                          tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;
290                                                          while (str_in[tag_param_pos] == ' ')                                                          while (str_in[tag_param_pos] == ' ')
291                                                          {                                                          {
292                                                                  tag_param_pos++;                                                                  tag_param_pos++;
# Line 279  int lml_plain(const char *str_in, char * Line 294  int lml_plain(const char *str_in, char *
294                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
295                                                          tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';                                                          tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
296                                                  case ']':                                                  case ']':
297                                                          if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined                                                          if (tag_param_pos == -1 && lml_tag_def[k][1] != NULL && lml_tag_def[k][2] != NULL) // Apply default param if not defined
298                                                          {                                                          {
299                                                                  strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);                                                                  strncpy(tag_param_buf, lml_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);
300                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
301                                                          }                                                          }
302                                                          if (LML_tag_def[k][1] != NULL)                                                          if (quote_mode)
303                                                          {                                                          {
304                                                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);                                                                  if (lml_tag_def[k][1] != NULL)
305                                                                    {
306                                                                            tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k][1], tag_param_buf);
307                                                                    }
308                                                                    else
309                                                                    {
310                                                                            tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])(
311                                                                                    lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode);
312                                                                    }
313                                                          }                                                          }
314                                                          else                                                          else
315                                                          {                                                          {
316                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                  if (lml_tag_def[k][3] != NULL)
317                                                                          LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);                                                                  {
318                                                                            tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k][3], tag_param_buf);
319                                                                    }
320                                                                    else
321                                                                    {
322                                                                            tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])(
323                                                                                    lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode);
324                                                                    }
325                                                          }                                                          }
326                                                          if (j + tag_output_len >= buf_len)                                                          if (j + tag_output_len >= buf_len)
327                                                          {                                                          {
# Line 312  int lml_plain(const char *str_in, char * Line 342  int lml_plain(const char *str_in, char *
342                                  tag_start_pos = -1;                                  tag_start_pos = -1;
343                          }                          }
344                  }                  }
345                  else if (tag_start_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag
346                  {                  {
347                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] & 0b10000000) // head of multi-byte character
348                          {                          {
349                                  if (j + 2 >= buf_len)                                  if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes
350                                  {                                  {
351                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 2, buf_len);                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len);
352                                          str_out[j] = '\0';                                          str_out[j] = '\0';
353                                          return j;                                          return j;
354                                  }                                  }
355                                  str_out[j++] = str_in[i++];  
356                                  if (str_in[i] == '\0')                                  c = (str_in[i] & 0b01110000) << 1;
357                                    while (c & 0b10000000)
358                                  {                                  {
359                                          str_out[j] = '\0';                                          str_out[j++] = str_in[i++];
360                                          return j;                                          if (str_in[i] == '\0')
361                                            {
362                                                    str_out[j] = '\0';
363                                                    return j;
364                                            }
365                                            c = (c & 0b01111111) << 1;
366                                  }                                  }
367                          }                          }
368    
# Line 344  int lml_plain(const char *str_in, char * Line 380  int lml_plain(const char *str_in, char *
380                  }                  }
381          }          }
382    
383          if (lml_tag_quote_level > 0)          if (quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
384          {          {
385                  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");
386                  if (j + tag_output_len >= buf_len)                  if (j + tag_output_len >= buf_len)


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

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