/[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.2 by sysadm, Mon Jun 2 23:49:47 2025 UTC Revision 1.23 by sysadm, Sat Oct 18 13:22:52 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  #define LML_TAG_COUNT 19  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, int quote_mode)
30    {
31            if (strcasecmp(tag_name, "color") == 0)
32            {
33                    if (strcasecmp(tag_param_buf, "red") == 0)
34                    {
35                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;31m");
36                    }
37                    else if (strcasecmp(tag_param_buf, "green") == 0)
38                    {
39                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;32m");
40                    }
41                    else if (strcasecmp(tag_param_buf, "yellow") == 0)
42                    {
43                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;33m");
44                    }
45                    else if (strcasecmp(tag_param_buf, "blue") == 0)
46                    {
47                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;34m");
48                    }
49                    else if (strcasecmp(tag_param_buf, "magenta") == 0)
50                    {
51                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;35m");
52                    }
53                    else if (strcasecmp(tag_param_buf, "cyan") == 0)
54                    {
55                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;36m");
56                    }
57                    else if (strcasecmp(tag_param_buf, "black") == 0)
58                    {
59                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;30;47m");
60                    }
61            }
62            return 0;
63    }
64    
65    #define LML_TAG_QUOTE_MAX_LEVEL 10
66    #define LML_TAG_QUOTE_LEVEL_LOOP 3
67    
68    static const char *lml_tag_quote_color[] = {
69            "\033[33m", // yellow
70            "\033[32m", // green
71            "\033[35m", // magenta
72    };
73    
74    static int lml_tag_quote_level = 0;
75    
76  const static char *LML_tag_def[][3] = {  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          {"left", "[", ""},  {
78          {"right", "]", NULL},          if (strcasecmp(tag_name, "quote") == 0)
79          {"underline", "\033[4m", ""},          {
80          {"/underline", "\033[24m", NULL},                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
81          {"u", "\033[4m", ""},                  {
82          {"/u", "\033[24m", NULL},                          lml_tag_quote_level++;
83          {"url", "", ""},                  }
84          {"/url", "(Á´½Ó: %s)", NULL},                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
85          {"link", "", ""},                                                  lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
86          {"/link", "(Á´½Ó: %s)", NULL},          }
87          {"email", "", ""},          else if (strcasecmp(tag_name, "/quote") == 0)
88          {"/email", "(Email: %s)", NULL},          {
89          {"user", "", ""},                  if (lml_tag_quote_level > 0)
90          {"/user", "(Óû§: %s)", NULL},                  {
91          {"article", "", ""},                          lml_tag_quote_level--;
92          {"/article", "(ÎÄÕÂ: %s)", NULL},                  }
93          {"image", "(ͼƬ: %s)", ""},                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
94          {"flash", "(Flash: %s)", ""},                                                  (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
95          {"bwf", "\033[31m****\033[m", ""},          }
96    
97            return 0;
98    }
99    
100    static int lml_tag_disabled = 0;
101    
102    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    {
104            lml_tag_disabled = 1;
105    
106            return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));
107    }
108    
109    typedef struct lml_tag_def_t
110    {
111            const char *tag_name; // tag name
112            const char *tag_output; // output string
113            const char *default_param; // default param string
114            const char *quote_mode_output; // output string in quote mode
115            lml_tag_filter_cb tag_filter_cb; // tag filter callback
116    } LML_TAG_DEF;
117    
118    const LML_TAG_DEF lml_tag_def[] = {
119            // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
120            {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
121            {"left", "[", "", "[left]", NULL},
122            {"right", "]", "", "[right]", NULL},
123            {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
124            {"/bold", "\033[22m", NULL, "", NULL},
125            {"b", "\033[1m", "", "", NULL},
126            {"/b", "\033[22m", NULL, "", NULL},
127            {"italic", "\033[5m", "", "", NULL}, // use blink instead
128            {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
129            {"i", "\033[5m", "", "", NULL},
130            {"/i", "\033[m", NULL, "", NULL},
131            {"underline", "\033[4m", "", "", NULL},
132            {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm
133            {"u", "\033[4m", "", "", NULL},
134            {"/u", "\033[m", NULL, "", NULL},
135            {"color", NULL, NULL, "", lml_tag_color_filter},
136            {"/color", "\033[m", NULL, "", NULL},
137            {"quote", NULL, NULL, "", lml_tag_quote_filter},
138            {"/quote", NULL, NULL, "", lml_tag_quote_filter},
139            {"url", "", "", "", NULL},
140            {"/url", "(链接: %s)", NULL, "", NULL},
141            {"link", "", "", "", NULL},
142            {"/link", "(链接: %s)", NULL, "", NULL},
143            {"email", "", "", "", NULL},
144            {"/email", "(Email: %s)", NULL, "", NULL},
145            {"user", "", "", "", NULL},
146            {"/user", "(用户: %s)", NULL, "", NULL},
147            {"article", "", "", "", NULL},
148            {"/article", "(文章: %s)", NULL, "", NULL},
149            {"image", "(图片: %s)", "", "%s", NULL},
150            {"flash", "(Flash: %s)", "", "", NULL},
151            {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
152  };  };
153    
154  static int LML_tag_name_len[LML_TAG_COUNT];  #define LML_TAG_COUNT 32
155  static int LML_init = 0;  
156    static int lml_tag_name_len[LML_TAG_COUNT];
157    static int lml_ready = 0;
158    
159  inline static void lml_init(void)  inline static void lml_init(void)
160  {  {
161          int i;          int i;
162    
163          if (!LML_init)          if (!lml_ready)
164          {          {
165                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
166                  {                  {
167                          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);
168                  }                  }
169    
170                  LML_init = 1;                  lml_ready = 1;
171          }          }
172  }  }
173    
174  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)
175  {  {
176            char c;
177          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
178          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
179          int i;          int i;
# Line 77  int lml_plain(const char *str_in, char * Line 183  int lml_plain(const char *str_in, char *
183          int tag_end_pos = -1;          int tag_end_pos = -1;
184          int tag_param_pos = -1;          int tag_param_pos = -1;
185          int tag_output_len;          int tag_output_len;
186            int new_line = 1;
187            int fb_quote_level = 0;
188    
189          lml_init();          lml_init();
190    
191            lml_tag_disabled = 0;
192            lml_tag_quote_level = 0;
193    
194          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
195          {          {
196                  if (str_in[i] == '[')                  if (!quote_mode && !lml_tag_disabled && new_line)
197                    {
198                            if (fb_quote_level > 0)
199                            {
200                                    lml_tag_quote_level -= fb_quote_level;
201    
202                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
203                                                                                      (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
204                                    if (j + tag_output_len >= buf_len)
205                                    {
206                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
207                                            str_out[j] = '\0';
208                                            return j;
209                                    }
210                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
211                                    j += tag_output_len;
212    
213                                    fb_quote_level = 0;
214                            }
215    
216                            while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
217                            {
218                                    fb_quote_level++;
219                            }
220    
221                            if (fb_quote_level > 0)
222                            {
223                                    lml_tag_quote_level += fb_quote_level;
224    
225                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
226                                                                                      lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);
227                                    if (j + tag_output_len >= buf_len)
228                                    {
229                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
230                                            str_out[j] = '\0';
231                                            return j;
232                                    }
233                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
234                                    j += tag_output_len;
235                            }
236    
237                            new_line = 0;
238                    }
239    
240                    if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
241                    {
242                            for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
243                                    ;
244    
245                            if (str_in[k] == 'm') // Valid
246                            {
247                                    if (j + (k - i + 1) >= buf_len)
248                                    {
249                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
250                                            str_out[j] = '\0';
251                                            return j;
252                                    }
253                                    memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
254                                    j += (k - i + 1);
255                                    i = k;
256                                    continue;
257                            }
258                            else // reach end of string
259                            {
260                                    break;
261                            }
262                    }
263    
264                    if (str_in[i] == '\n')
265                    {
266                            tag_start_pos = -1; // jump out of tag at end of line
267                            new_line = 1;
268                    }
269                    else if (str_in[i] == '\r')
270                    {
271                            continue; // ignore '\r'
272                    }
273    
274                    if (!lml_tag_disabled && str_in[i] == '[')
275                  {                  {
276                          tag_start_pos = i + 1;                          tag_start_pos = i + 1;
277                  }                  }
278                  else if (str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']')
279                  {                  {
280                          if (tag_start_pos >= 0)                          if (tag_start_pos >= 0)
281                          {                          {
# Line 100  int lml_plain(const char *str_in, char * Line 289  int lml_plain(const char *str_in, char *
289    
290                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (k = 0; k < LML_TAG_COUNT; k++)
291                                  {                                  {
292                                          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)
293                                          {                                          {
294                                                  tag_param_pos = -1;                                                  tag_param_pos = -1;
295                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  switch (str_in[tag_start_pos + lml_tag_name_len[k]])
296                                                  {                                                  {
297                                                  case ' ':                                                  case ' ':
298                                                          tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1;                                                          tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;
299                                                          while (str_in[tag_param_pos] == ' ')                                                          while (str_in[tag_param_pos] == ' ')
300                                                          {                                                          {
301                                                                  tag_param_pos++;                                                                  tag_param_pos++;
# Line 114  int lml_plain(const char *str_in, char * Line 303  int lml_plain(const char *str_in, char *
303                                                          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));
304                                                          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';
305                                                  case ']':                                                  case ']':
306                                                          if (tag_param_pos == -1 && 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
307                                                          {                                                          {
308                                                                  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);
309                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
310                                                          }                                                          }
311                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);                                                          if (!quote_mode)
                                                         if (j + tag_output_len >= buf_len - 1)  
312                                                          {                                                          {
313                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                                                  if (lml_tag_def[k].tag_output != NULL)
314                                                                    {
315                                                                            tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
316                                                                    }
317                                                                    else if (lml_tag_def[k].tag_filter_cb != NULL)
318                                                                    {
319                                                                            tag_output_len = lml_tag_def[k].tag_filter_cb(
320                                                                                    lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
321                                                                    }
322                                                                    else
323                                                                    {
324                                                                            tag_output_len = 0;
325                                                                    }
326                                                            }
327                                                            else // quote mode
328                                                            {
329                                                                    if (lml_tag_def[k].quote_mode_output != NULL)
330                                                                    {
331                                                                            tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
332                                                                    }
333                                                                    else if (lml_tag_def[k].tag_filter_cb != NULL)
334                                                                    {
335                                                                            tag_output_len = lml_tag_def[k].tag_filter_cb(
336                                                                                    lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
337                                                                    }
338                                                                    else
339                                                                    {
340                                                                            tag_output_len = 0;
341                                                                    }
342                                                            }
343                                                            if (j + tag_output_len >= buf_len)
344                                                            {
345                                                                    log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
346                                                                  str_out[j] = '\0';                                                                  str_out[j] = '\0';
347                                                                  return j;                                                                  return j;
348                                                          }                                                          }
# Line 139  int lml_plain(const char *str_in, char * Line 359  int lml_plain(const char *str_in, char *
359                                  tag_start_pos = -1;                                  tag_start_pos = -1;
360                          }                          }
361                  }                  }
362                  else if (tag_start_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag
363                  {                  {
364                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] & 0x80) // head of multi-byte character
365                          {                          {
366                                  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
367                                  {                                  {
368                                          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);
369                                          str_out[j] = '\0';                                          str_out[j] = '\0';
370                                          return j;                                          return j;
371                                  }                                  }
372                                  str_out[j++] = str_in[i++];  
373                                  if (str_in[i] == '\0')                                  c = (str_in[i] & 0x70) << 1;
374                                    while (c & 0x80)
375                                  {                                  {
376                                          str_out[j] = '\0';                                          str_out[j++] = str_in[i++];
377                                          return j;                                          if (str_in[i] == '\0')
378                                            {
379                                                    str_out[j] = '\0';
380                                                    return j;
381                                            }
382                                            c = (c & 0x7f) << 1;
383                                  }                                  }
384                          }                          }
385    
386                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
387                          {                          {
388                                  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);
389                                  str_out[j] = '\0';                                  str_out[j] = '\0';
390                                  return j;                                  return j;
391                          }                          }
# Line 171  int lml_plain(const char *str_in, char * Line 397  int lml_plain(const char *str_in, char *
397                  }                  }
398          }          }
399    
400            if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
401            {
402                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
403                    if (j + tag_output_len >= buf_len)
404                    {
405                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
406                            str_out[j] = '\0';
407                            return j;
408                    }
409                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
410                    j += tag_output_len;
411            }
412    
413          str_out[j] = '\0';          str_out[j] = '\0';
414    
415          return j;          return j;
416  }  }


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

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