/[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.4 by sysadm, Tue Jun 3 01:26:52 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 66  static int lml_tag_color_filter(const ch Line 66  static int lml_tag_color_filter(const ch
66  #define LML_TAG_QUOTE_LEVEL_LOOP 3  #define LML_TAG_QUOTE_LEVEL_LOOP 3
67    
68  static const char *lml_tag_quote_color[] = {  static const char *lml_tag_quote_color[] = {
69          "\033[33m",          "\033[33m", // yellow
70          "\033[32m",          "\033[32m", // green
71          "\033[35m",          "\033[35m", // magenta
72  };  };
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          {          {
80                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
81                  {                  {
82                          lml_tag_quote_level++;                          lml_tag_quote_level++;
83                  }                  }
84                  return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);                  return snprintf(tag_output_buf, tag_output_buf_len, "%s",
85                                                    lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
86          }          }
87          else if (strcasecmp(tag_name, "/quote") == 0)          else if (strcasecmp(tag_name, "/quote") == 0)
88          {          {
# Line 90  static int lml_tag_quote_filter(const ch Line 90  static int lml_tag_quote_filter(const ch
90                  {                  {
91                          lml_tag_quote_level--;                          lml_tag_quote_level--;
92                  }                  }
93                  if (lml_tag_quote_level > 0)                  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"));
                         return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);  
                 }  
                 else  
                 {  
                         return snprintf(tag_output_buf, tag_output_buf_len, "\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", ""},  {
104          {"/bold", "\033[21m", NULL},          lml_tag_disabled = 1;
105          {"b", "\033[1m", ""},  
106          {"/b", "\033[21m", NULL},          return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "" : "[plain]"));
107          {"italic", "\033[3m", ""},  }
108          {"/italic", "\033[23m", NULL},  
109          {"i", "\033[3m", ""},  const static char *lml_tag_def[][4] = {
110          {"/i", "\033[23m", 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[24m", NULL},          {"left", "[", "", "[left]"},
113          {"u", "\033[4m", ""},          {"right", "]", "", "[right]"},
114          {"/u", "\033[24m", 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 168  int lml_plain(const char *str_in, char * Line 174  int lml_plain(const char *str_in, char *
174          int tag_end_pos = -1;          int tag_end_pos = -1;
175          int tag_param_pos = -1;          int tag_param_pos = -1;
176          int tag_output_len;          int tag_output_len;
177            int new_line = 1;
178            int fb_quote_level = 0;
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 (str_in[i] == '[')                  if (quote_mode && !lml_tag_disabled && new_line)
188                    {
189                            if (fb_quote_level > 0)
190                            {
191                                    lml_tag_quote_level -= fb_quote_level;
192    
193                                    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"));
195                                    if (j + tag_output_len >= buf_len)
196                                    {
197                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
198                                            str_out[j] = '\0';
199                                            return j;
200                                    }
201                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
202                                    j += tag_output_len;
203    
204                                    fb_quote_level = 0;
205                            }
206    
207                            while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
208                            {
209                                    fb_quote_level++;
210                            }
211    
212                            if (fb_quote_level > 0)
213                            {
214                                    lml_tag_quote_level += fb_quote_level;
215    
216                                    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]);
218                                    if (j + tag_output_len >= buf_len)
219                                    {
220                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
221                                            str_out[j] = '\0';
222                                            return j;
223                                    }
224                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
225                                    j += tag_output_len;
226                            }
227    
228                            new_line = 0;
229                    }
230    
231                    if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
232                    {
233                            for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
234                                    ;
235    
236                            if (str_in[k] == 'm') // Valid
237                            {
238                                    if (j + (k - i + 1) >= buf_len)
239                                    {
240                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
241                                            str_out[j] = '\0';
242                                            return j;
243                                    }
244                                    memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
245                                    j += (k - i + 1);
246                                    i = k;
247                                    continue;
248                            }
249                            else // reach end of string
250                            {
251                                    break;
252                            }
253                    }
254    
255                    if (str_in[i] == '\n')
256                    {
257                            tag_start_pos = -1; // jump out of tag at end of line
258                            new_line = 1;
259                    }
260                    else if (str_in[i] == '\r')
261                    {
262                            continue; // ignore '\r'
263                    }
264    
265                    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 193  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 207  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 - 1)                                                          if (j + tag_output_len >= buf_len)
327                                                          {                                                          {
328                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
329                                                                  str_out[j] = '\0';                                                                  str_out[j] = '\0';
330                                                                  return j;                                                                  return j;
331                                                          }                                                          }
# Line 240  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 - 1)                                  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 - 1);                                          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    
369                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
370                          {                          {
371                                  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);
372                                  str_out[j] = '\0';                                  str_out[j] = '\0';
373                                  return j;                                  return j;
374                          }                          }
# Line 272  int lml_plain(const char *str_in, char * Line 380  int lml_plain(const char *str_in, char *
380                  }                  }
381          }          }
382    
383            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");
386                    if (j + tag_output_len >= buf_len)
387                    {
388                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
389                            str_out[j] = '\0';
390                            return j;
391                    }
392                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
393                    j += tag_output_len;
394            }
395    
396          str_out[j] = '\0';          str_out[j] = '\0';
397    
398          return j;          return j;
399  }  }


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

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