/[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.16 by sysadm, Fri Oct 3 09:46:28 2025 UTC Revision 1.26 by sysadm, Fri Oct 24 07:20:34 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  typedef struct lml_tag_def_t
110          {"i", "\033[5m", "", ""},  {
111          {"/i", "\033[m", NULL, ""},          const char *tag_name;                    // tag name
112          {"underline", "\033[4m", "", ""},          const char *tag_output;                  // output string
113          {"/underline", "\033[m", NULL, ""}, // \033[24m does not work in Fterm          const char *default_param;               // default param string
114          {"u", "\033[4m", "", ""},          const char *quote_mode_output;   // output string in quote mode
115          {"/u", "\033[m", NULL, ""},          lml_tag_filter_cb tag_filter_cb; // tag filter callback
116          {"color", NULL, (const char *)lml_tag_color_filter, ""},  } LML_TAG_DEF;
117          {"/color", "\033[m", NULL, ""},  
118          {"quote", NULL, (const char *)lml_tag_quote_filter, ""},  const LML_TAG_DEF lml_tag_def[] = {
119          {"/quote", NULL, (const char *)lml_tag_quote_filter, ""},          // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
120          {"url", "", "", ""},          {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
121          {"/url", "(链接: %s)", NULL, ""},          {"nolml", "", NULL, "", NULL},
122          {"link", "", ""},          {"lml", "", NULL, "", NULL},
123          {"/link", "(链接: %s)", NULL, ""},          {"left", "[", "", "[left]", NULL},
124          {"email", "", ""},          {"right", "]", "", "[right]", NULL},
125          {"/email", "(Email: %s)", NULL, ""},          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
126          {"user", "", ""},          {"/bold", "\033[22m", NULL, "", NULL},
127          {"/user", "(用户: %s)", NULL, ""},          {"b", "\033[1m", "", "", NULL},
128          {"article", "", ""},          {"/b", "\033[22m", NULL, "", NULL},
129          {"/article", "(文章: %s)", NULL, ""},          {"italic", "\033[5m", "", "", NULL},   // use blink instead
130          {"image", "(图片: %s)", "", "%s"},          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
131          {"flash", "(Flash: %s)", "", ""},          {"i", "\033[5m", "", "", NULL},
132          {"bwf", "\033[1;31m****\033[m", "", "****"},          {"/i", "\033[m", NULL, "", NULL},
133            {"underline", "\033[4m", "", "", NULL},
134            {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm
135            {"u", "\033[4m", "", "", NULL},
136            {"/u", "\033[m", NULL, "", NULL},
137            {"color", NULL, NULL, "", lml_tag_color_filter},
138            {"/color", "\033[m", NULL, "", NULL},
139            {"quote", NULL, NULL, "", lml_tag_quote_filter},
140            {"/quote", NULL, NULL, "", lml_tag_quote_filter},
141            {"url", "", "", "", NULL},
142            {"/url", "(链接: %s)", NULL, "", NULL},
143            {"link", "", "", "", NULL},
144            {"/link", "(链接: %s)", NULL, "", NULL},
145            {"email", "", "", "", NULL},
146            {"/email", "(Email: %s)", NULL, "", NULL},
147            {"user", "", "", "", NULL},
148            {"/user", "(用户: %s)", NULL, "", NULL},
149            {"article", "", "", "", NULL},
150            {"/article", "(文章: %s)", NULL, "", NULL},
151            {"image", "(图片: %s)", "", "%s", NULL},
152            {"flash", "(Flash: %s)", "", "", NULL},
153            {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
154  };  };
155    
156  #define LML_TAG_COUNT 31  #define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF))
157    
158  static int LML_tag_name_len[LML_TAG_COUNT];  static int lml_tag_name_len[LML_TAG_COUNT];
159  static int LML_init = 0;  static int lml_ready = 0;
160    
161  inline static void lml_init(void)  inline static void lml_init(void)
162  {  {
163          int i;          int i;
164    
165          if (!LML_init)          if (!lml_ready)
166          {          {
167                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
168                  {                  {
169                          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);
170                  }                  }
171    
172                  LML_init = 1;                  lml_ready = 1;
173          }          }
174  }  }
175    
176  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)
177  {  {
178          char c;          char c;
179          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
# Line 161  int lml_plain(const char *str_in, char * Line 182  int lml_plain(const char *str_in, char *
182          int j = 0;          int j = 0;
183          int k;          int k;
184          int tag_start_pos = -1;          int tag_start_pos = -1;
185            int tag_name_pos = -1;
186          int tag_end_pos = -1;          int tag_end_pos = -1;
187          int tag_param_pos = -1;          int tag_param_pos = -1;
188          int tag_output_len;          int tag_output_len;
189          int new_line = 1;          int new_line = 1;
190          int fb_quote_level = 0;          int fb_quote_level = 0;
191            int tag_name_found;
192    
193          lml_init();          lml_init();
194    
195            lml_tag_disabled = 0;
196          lml_tag_quote_level = 0;          lml_tag_quote_level = 0;
197    
198          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
199          {          {
200                  if (lml_tag && new_line)                  if (!quote_mode && !lml_tag_disabled && new_line)
201                  {                  {
202                          if (fb_quote_level > 0)                          if (fb_quote_level > 0)
203                          {                          {
# Line 243  int lml_plain(const char *str_in, char * Line 267  int lml_plain(const char *str_in, char *
267    
268                  if (str_in[i] == '\n')                  if (str_in[i] == '\n')
269                  {                  {
270                          tag_start_pos = -1; // jump out of tag at end of line                          tag_name_pos = -1; // jump out of tag at end of line
271                          new_line = 1;                          new_line = 1;
272                  }                  }
273                  else if (str_in[i] == '\r')                  else if (str_in[i] == '\r')
# Line 251  int lml_plain(const char *str_in, char * Line 275  int lml_plain(const char *str_in, char *
275                          continue; // ignore '\r'                          continue; // ignore '\r'
276                  }                  }
277    
278                  if (str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
279                  {                  {
280                          tag_start_pos = i + 1;                          tag_start_pos = i;
281                            tag_name_pos = i + 1;
282                  }                  }
283                  else if (str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']')
284                  {                  {
285                          if (tag_start_pos >= 0)                          if (tag_name_pos >= 0)
286                          {                          {
287                                  tag_end_pos = i;                                  tag_end_pos = i;
288    
289                                  // Skip space characters                                  // Skip space characters
290                                  while (str_in[tag_start_pos] == ' ')                                  while (str_in[tag_name_pos] == ' ')
291                                  {                                  {
292                                          tag_start_pos++;                                          tag_name_pos++;
293                                  }                                  }
294    
295                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)
296                                  {                                  {
297                                          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_name_pos, (size_t)lml_tag_name_len[k]) == 0)
298                                          {                                          {
299                                                  tag_param_pos = -1;                                                  tag_param_pos = -1;
300                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  switch (str_in[tag_name_pos + lml_tag_name_len[k]])
301                                                  {                                                  {
302                                                  case ' ':                                                  case ' ':
303                                                          tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1;                                                          tag_name_found = 1;
304                                                            tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
305                                                          while (str_in[tag_param_pos] == ' ')                                                          while (str_in[tag_param_pos] == ' ')
306                                                          {                                                          {
307                                                                  tag_param_pos++;                                                                  tag_param_pos++;
# Line 283  int lml_plain(const char *str_in, char * Line 309  int lml_plain(const char *str_in, char *
309                                                          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));
310                                                          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';
311                                                  case ']':                                                  case ']':
312                                                          if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined                                                          tag_name_found = 1;
313                                                            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
314                                                          {                                                          {
315                                                                  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);
316                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
317                                                          }                                                          }
318                                                          if (lml_tag)                                                          if (!quote_mode)
319                                                          {                                                          {
320                                                                  if (LML_tag_def[k][1] != NULL)                                                                  if (lml_tag_def[k].tag_output != NULL)
321                                                                  {                                                                  {
322                                                                          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);
323                                                                    }
324                                                                    else if (lml_tag_def[k].tag_filter_cb != NULL)
325                                                                    {
326                                                                            tag_output_len = lml_tag_def[k].tag_filter_cb(
327                                                                                    lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
328                                                                  }                                                                  }
329                                                                  else                                                                  else
330                                                                  {                                                                  {
331                                                                          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);  
332                                                                  }                                                                  }
333                                                          }                                                          }
334                                                          else                                                          else // if (quote_mode)
335                                                          {                                                          {
336                                                                  if (LML_tag_def[k][3] != NULL)                                                                  if (lml_tag_def[k].quote_mode_output != NULL)
337                                                                    {
338                                                                            tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
339                                                                    }
340                                                                    else if (lml_tag_def[k].tag_filter_cb != NULL)
341                                                                  {                                                                  {
342                                                                          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(
343                                                                                    lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
344                                                                  }                                                                  }
345                                                                  else                                                                  else
346                                                                  {                                                                  {
# Line 327  int lml_plain(const char *str_in, char * Line 363  int lml_plain(const char *str_in, char *
363                                          }                                          }
364                                  }                                  }
365    
366                                  tag_start_pos = -1;                                  if (!tag_name_found)
367                                    {
368                                            tag_output_len = tag_end_pos - tag_start_pos + 1;
369                                            if (j + tag_output_len >= buf_len)
370                                            {
371                                                    log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
372                                                    str_out[j] = '\0';
373                                                    return j;
374                                            }
375    
376                                            memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
377                                            j += tag_output_len;
378                                    }
379    
380                                    tag_name_pos = -1;
381                          }                          }
382                  }                  }
383                  else if (tag_start_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag
384                  {                  {
385                          if (str_in[i] & 0b10000000) // head of multi-byte character                          if (str_in[i] & 0x80) // head of multi-byte character
386                          {                          {
387                                  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
388                                  {                                  {
# Line 341  int lml_plain(const char *str_in, char * Line 391  int lml_plain(const char *str_in, char *
391                                          return j;                                          return j;
392                                  }                                  }
393    
394                                  c = (str_in[i] & 0b01110000) << 1;                                  c = (str_in[i] & 0x70) << 1;
395                                  while (c & 0b10000000)                                  while (c & 0x80)
396                                  {                                  {
397                                          str_out[j++] = str_in[i++];                                          str_out[j++] = str_in[i++];
398                                          if (str_in[i] == '\0')                                          if (str_in[i] == '\0')
# Line 350  int lml_plain(const char *str_in, char * Line 400  int lml_plain(const char *str_in, char *
400                                                  str_out[j] = '\0';                                                  str_out[j] = '\0';
401                                                  return j;                                                  return j;
402                                          }                                          }
403                                          c = (c & 0b01111111) << 1;                                          c = (c & 0x7f) << 1;
404                                  }                                  }
405                          }                          }
406    
# Line 368  int lml_plain(const char *str_in, char * Line 418  int lml_plain(const char *str_in, char *
418                  }                  }
419          }          }
420    
421          if (lml_tag && lml_tag_quote_level > 0)          if (tag_start_pos != -1) // tag is not closed
422            {
423                    tag_end_pos = i - 1;
424                    tag_output_len = tag_end_pos - tag_start_pos + 1;
425                    if (j + tag_output_len >= buf_len)
426                    {
427                            log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
428                            str_out[j] = '\0';
429                            return j;
430                    }
431    
432                    memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
433                    j += tag_output_len;
434            }
435    
436            if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
437          {          {
438                  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");
439                  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