/[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.17 by sysadm, Fri Oct 3 11:43:16 2025 UTC Revision 1.25 by sysadm, Fri Oct 24 03:57:45 2025 UTC
# 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, int quote_mode)
30  {  {
31          if (strcasecmp(tag_name, "color") == 0)          if (strcasecmp(tag_name, "color") == 0)
32          {          {
# 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 97  static int lml_tag_quote_filter(const ch Line 97  static int lml_tag_quote_filter(const ch
97          return 0;          return 0;
98  }  }
99    
100  const static char *LML_tag_def[][4] = {  static int lml_tag_disabled = 0;
101          // Definition of tuple: {lml_tag, lml_output, default_param | lml_filter_cb, no_lml_output}  
102          {"left", "[", "", "[left]"},  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          {"right", "]", "", "[right]"},  {
104          {"bold", "\033[1m", "", ""}, // does not work in Fterm          lml_tag_disabled = 1;
105          {"/bold", "\033[22m", NULL, ""},  
106          {"b", "\033[1m", "", ""},          return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));
107          {"/b", "\033[22m", NULL, ""},  }
108          {"italic", "\033[5m", "", ""},   // use blink instead  
109          {"/italic", "\033[m", NULL, ""}, // \033[25m does not work in Fterm  static int lml_tag_user_disabled = 0;
110          {"i", "\033[5m", "", ""},  
111          {"/i", "\033[m", NULL, ""},  static int lml_tag_user_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
112          {"underline", "\033[4m", "", ""},  {
113          {"/underline", "\033[m", NULL, ""}, // \033[24m does not work in Fterm          lml_tag_user_disabled = 1;
114          {"u", "\033[4m", "", ""},          tag_output_buf[0] = '\0';
115          {"/u", "\033[m", NULL, ""},          return 0;
116          {"color", NULL, (const char *)lml_tag_color_filter, ""},  }
117          {"/color", "\033[m", NULL, ""},  
118          {"quote", NULL, (const char *)lml_tag_quote_filter, ""},  static int lml_tag_user_enable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
119          {"/quote", NULL, (const char *)lml_tag_quote_filter, ""},  {
120          {"url", "", "", ""},          lml_tag_user_disabled = 0;
121          {"/url", "(链接: %s)", NULL, ""},          tag_output_buf[0] = '\0';
122          {"link", "", ""},          return 0;
123          {"/link", "(链接: %s)", NULL, ""},  }
124          {"email", "", ""},  
125          {"/email", "(Email: %s)", NULL, ""},  typedef struct lml_tag_def_t
126          {"user", "", ""},  {
127          {"/user", "(用户: %s)", NULL, ""},          const char *tag_name; // tag name
128          {"article", "", ""},          const char *tag_output; // output string
129          {"/article", "(文章: %s)", NULL, ""},          const char *default_param; // default param string
130          {"image", "(图片: %s)", "", "%s"},          const char *quote_mode_output; // output string in quote mode
131          {"flash", "(Flash: %s)", "", ""},          lml_tag_filter_cb tag_filter_cb; // tag filter callback
132          {"bwf", "\033[1;31m****\033[m", "", "****"},  } LML_TAG_DEF;
133    
134    const LML_TAG_DEF lml_tag_def[] = {
135            // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
136            {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
137            {"nolml", NULL, NULL, NULL, lml_tag_user_disable_filter},
138            {"lml", NULL, NULL, NULL, lml_tag_user_enable_filter},
139            {"left", "[", "", "[left]", NULL},
140            {"right", "]", "", "[right]", NULL},
141            {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
142            {"/bold", "\033[22m", NULL, "", NULL},
143            {"b", "\033[1m", "", "", NULL},
144            {"/b", "\033[22m", NULL, "", NULL},
145            {"italic", "\033[5m", "", "", NULL}, // use blink instead
146            {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
147            {"i", "\033[5m", "", "", NULL},
148            {"/i", "\033[m", NULL, "", NULL},
149            {"underline", "\033[4m", "", "", NULL},
150            {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm
151            {"u", "\033[4m", "", "", NULL},
152            {"/u", "\033[m", NULL, "", NULL},
153            {"color", NULL, NULL, "", lml_tag_color_filter},
154            {"/color", "\033[m", NULL, "", NULL},
155            {"quote", NULL, NULL, "", lml_tag_quote_filter},
156            {"/quote", NULL, NULL, "", lml_tag_quote_filter},
157            {"url", "", "", "", NULL},
158            {"/url", "(链接: %s)", NULL, "", NULL},
159            {"link", "", "", "", NULL},
160            {"/link", "(链接: %s)", NULL, "", NULL},
161            {"email", "", "", "", NULL},
162            {"/email", "(Email: %s)", NULL, "", NULL},
163            {"user", "", "", "", NULL},
164            {"/user", "(用户: %s)", NULL, "", NULL},
165            {"article", "", "", "", NULL},
166            {"/article", "(文章: %s)", NULL, "", NULL},
167            {"image", "(图片: %s)", "", "%s", NULL},
168            {"flash", "(Flash: %s)", "", "", NULL},
169            {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
170  };  };
171    
172  #define LML_TAG_COUNT 31  #define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF))
173    
174  static int LML_tag_name_len[LML_TAG_COUNT];  static int lml_tag_name_len[LML_TAG_COUNT];
175  static int LML_init = 0;  static int lml_ready = 0;
176    
177  inline static void lml_init(void)  inline static void lml_init(void)
178  {  {
179          int i;          int i;
180    
181          if (!LML_init)          if (!lml_ready)
182          {          {
183                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
184                  {                  {
185                          LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]);                          lml_tag_name_len[i] = (int)strlen(lml_tag_def[i].tag_name);
186                  }                  }
187    
188                  LML_init = 1;                  lml_ready = 1;
189          }          }
190  }  }
191    
192  int lml_plain(const char *str_in, char *str_out, int buf_len, int lml_tag)  int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)
193  {  {
194          char c;          char c;
195          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
# Line 169  int lml_plain(const char *str_in, char * Line 206  int lml_plain(const char *str_in, char *
206    
207          lml_init();          lml_init();
208    
209            lml_tag_disabled = 0;
210            lml_tag_user_disabled = 0;
211          lml_tag_quote_level = 0;          lml_tag_quote_level = 0;
212    
213          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
214          {          {
215                  if (lml_tag && new_line)                  if (!quote_mode && !lml_tag_disabled && new_line)
216                  {                  {
217                          if (fb_quote_level > 0)                          if (fb_quote_level > 0)
218                          {                          {
# Line 251  int lml_plain(const char *str_in, char * Line 290  int lml_plain(const char *str_in, char *
290                          continue; // ignore '\r'                          continue; // ignore '\r'
291                  }                  }
292    
293                  if (str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
294                  {                  {
295                          tag_start_pos = i + 1;                          tag_start_pos = i + 1;
296                  }                  }
297                  else if (str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']')
298                  {                  {
299                          if (tag_start_pos >= 0)                          if (tag_start_pos >= 0)
300                          {                          {
# Line 269  int lml_plain(const char *str_in, char * Line 308  int lml_plain(const char *str_in, char *
308    
309                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (k = 0; k < LML_TAG_COUNT; k++)
310                                  {                                  {
311                                          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].tag_name, str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0)
312                                          {                                          {
313                                                  tag_param_pos = -1;                                                  tag_param_pos = -1;
314                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  switch (str_in[tag_start_pos + lml_tag_name_len[k]])
315                                                  {                                                  {
316                                                  case ' ':                                                  case ' ':
317                                                          tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1;                                                          tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;
318                                                          while (str_in[tag_param_pos] == ' ')                                                          while (str_in[tag_param_pos] == ' ')
319                                                          {                                                          {
320                                                                  tag_param_pos++;                                                                  tag_param_pos++;
# Line 283  int lml_plain(const char *str_in, char * Line 322  int lml_plain(const char *str_in, char *
322                                                          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));
323                                                          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';
324                                                  case ']':                                                  case ']':
325                                                          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].tag_output != NULL && lml_tag_def[k].default_param != NULL) // Apply default param if not defined
326                                                          {                                                          {
327                                                                  strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);                                                                  strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
328                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
329                                                          }                                                          }
330                                                          if (lml_tag)                                                          if (!quote_mode && !lml_tag_user_disabled)
331                                                          {                                                          {
332                                                                  if (LML_tag_def[k][1] != NULL)                                                                  if (lml_tag_def[k].tag_output != NULL)
333                                                                  {                                                                  {
334                                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);                                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
335                                                                    }
336                                                                    else if (lml_tag_def[k].tag_filter_cb != NULL)
337                                                                    {
338                                                                            tag_output_len = lml_tag_def[k].tag_filter_cb(
339                                                                                    lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
340                                                                  }                                                                  }
341                                                                  else                                                                  else
342                                                                  {                                                                  {
343                                                                          tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                          tag_output_len = 0;
                                                                                 LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);  
344                                                                  }                                                                  }
345                                                          }                                                          }
346                                                          else                                                          else // if (quote_mode || lml_tag_user_disabled)
347                                                          {                                                          {
348                                                                  if (LML_tag_def[k][3] != NULL)                                                                  if (lml_tag_def[k].quote_mode_output != NULL)
349                                                                    {
350                                                                            tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
351                                                                    }
352                                                                    else if (lml_tag_def[k].tag_filter_cb != NULL)
353                                                                  {                                                                  {
354                                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][3], tag_param_buf);                                                                          tag_output_len = lml_tag_def[k].tag_filter_cb(
355                                                                                    lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
356                                                                  }                                                                  }
357                                                                  else                                                                  else
358                                                                  {                                                                  {
# Line 330  int lml_plain(const char *str_in, char * Line 378  int lml_plain(const char *str_in, char *
378                                  tag_start_pos = -1;                                  tag_start_pos = -1;
379                          }                          }
380                  }                  }
381                  else if (tag_start_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag
382                  {                  {
383                          if (str_in[i] & 0b10000000) // head of multi-byte character                          if (str_in[i] & 0x80) // head of multi-byte character
384                          {                          {
385                                  if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes                                  if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes
386                                  {                                  {
# Line 341  int lml_plain(const char *str_in, char * Line 389  int lml_plain(const char *str_in, char *
389                                          return j;                                          return j;
390                                  }                                  }
391    
392                                  c = (str_in[i] & 0b01110000) << 1;                                  c = (str_in[i] & 0x70) << 1;
393                                  while (c & 0b10000000)                                  while (c & 0x80)
394                                  {                                  {
395                                          str_out[j++] = str_in[i++];                                          str_out[j++] = str_in[i++];
396                                          if (str_in[i] == '\0')                                          if (str_in[i] == '\0')
# Line 350  int lml_plain(const char *str_in, char * Line 398  int lml_plain(const char *str_in, char *
398                                                  str_out[j] = '\0';                                                  str_out[j] = '\0';
399                                                  return j;                                                  return j;
400                                          }                                          }
401                                          c = (c & 0b01111111) << 1;                                          c = (c & 0x7f) << 1;
402                                  }                                  }
403                          }                          }
404    
# Line 368  int lml_plain(const char *str_in, char * Line 416  int lml_plain(const char *str_in, char *
416                  }                  }
417          }          }
418    
419          if (lml_tag && lml_tag_quote_level > 0)          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
420          {          {
421                  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");
422                  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