/[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.14 by sysadm, Wed Jul 16 05:24:08 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 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)
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;
# Line 106  static int lml_tag_quote_filter(const ch Line 100  static int lml_tag_quote_filter(const ch
100  const static char *LML_tag_def[][3] = {  const static char *LML_tag_def[][3] = {
101          {"left", "[", ""},          {"left", "[", ""},
102          {"right", "]", NULL},          {"right", "]", NULL},
103          {"bold", "\033[1m", ""},          {"bold", "\033[1m", ""}, // does not work in Fterm
104          {"/bold", "\033[21m", NULL},          {"/bold", "\033[22m", NULL},
105          {"b", "\033[1m", ""},          {"b", "\033[1m", ""},
106          {"/b", "\033[21m", NULL},          {"/b", "\033[22m", NULL},
107          {"italic", "\033[3m", ""},          {"italic", "\033[5m", ""},       // use blink instead
108          {"/italic", "\033[23m", NULL},          {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm
109          {"i", "\033[3m", ""},          {"i", "\033[5m", ""},
110          {"/i", "\033[23m", NULL},          {"/i", "\033[m", NULL},
111          {"underline", "\033[4m", ""},          {"underline", "\033[4m", ""},
112          {"/underline", "\033[24m", NULL},          {"/underline", "\033[m", NULL}, // \033[24m does not work in Fterm
113          {"u", "\033[4m", ""},          {"u", "\033[4m", ""},
114          {"/u", "\033[24m", NULL},          {"/u", "\033[m", NULL},
115          {"color", NULL, (const char *)lml_tag_color_filter},          {"color", NULL, (const char *)lml_tag_color_filter},
116          {"/color", "\033[m", NULL},          {"/color", "\033[m", NULL},
117          {"quote", NULL, (const char *)lml_tag_quote_filter},          {"quote", NULL, (const char *)lml_tag_quote_filter},
118          {"/quote", NULL, (const char *)lml_tag_quote_filter},          {"/quote", NULL, (const char *)lml_tag_quote_filter},
119          {"url", "", ""},          {"url", "", ""},
120          {"/url", "(链接: %s)", NULL},          {"/url", "(閾炬帴: %s)", NULL},
121          {"link", "", ""},          {"link", "", ""},
122          {"/link", "(链接: %s)", NULL},          {"/link", "(閾炬帴: %s)", NULL},
123          {"email", "", ""},          {"email", "", ""},
124          {"/email", "(Email: %s)", NULL},          {"/email", "(Email: %s)", NULL},
125          {"user", "", ""},          {"user", "", ""},
126          {"/user", "(用户: %s)", NULL},          {"/user", "(鐢ㄦ埛: %s)", NULL},
127          {"article", "", ""},          {"article", "", ""},
128          {"/article", "(文章: %s)", NULL},          {"/article", "(鏂囩珷: %s)", NULL},
129          {"image", "(图片: %s)", ""},          {"image", "(鍥剧墖: %s)", ""},
130          {"flash", "(Flash: %s)", ""},          {"flash", "(Flash: %s)", ""},
131          {"bwf", "\033[1;31m****\033[m", ""},          {"bwf", "\033[1;31m****\033[m", ""},
132  };  };
# Line 159  inline static void lml_init(void) Line 153  inline static void lml_init(void)
153    
154  int lml_plain(const char *str_in, char *str_out, int buf_len)  int lml_plain(const char *str_in, char *str_out, int buf_len)
155  {  {
156            char c;
157          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
158          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
159          int i;          int i;
# Line 168  int lml_plain(const char *str_in, char * Line 163  int lml_plain(const char *str_in, char *
163          int tag_end_pos = -1;          int tag_end_pos = -1;
164          int tag_param_pos = -1;          int tag_param_pos = -1;
165          int tag_output_len;          int tag_output_len;
166            int new_line = 1;
167            int fb_quote_level = 0;
168    
169          lml_init();          lml_init();
170    
# Line 175  int lml_plain(const char *str_in, char * Line 172  int lml_plain(const char *str_in, char *
172    
173          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
174          {          {
175                    if (new_line)
176                    {
177                            if (fb_quote_level > 0)
178                            {
179                                    lml_tag_quote_level -= fb_quote_level;
180    
181                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
182                                                                                      (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
183                                    if (j + tag_output_len >= buf_len)
184                                    {
185                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
186                                            str_out[j] = '\0';
187                                            return j;
188                                    }
189                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
190                                    j += tag_output_len;
191    
192                                    fb_quote_level = 0;
193                            }
194    
195                            while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
196                            {
197                                    fb_quote_level++;
198                            }
199    
200                            if (fb_quote_level > 0)
201                            {
202                                    lml_tag_quote_level += fb_quote_level;
203    
204                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
205                                                                                      lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);
206                                    if (j + tag_output_len >= buf_len)
207                                    {
208                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
209                                            str_out[j] = '\0';
210                                            return j;
211                                    }
212                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
213                                    j += tag_output_len;
214                            }
215    
216                            new_line = 0;
217                    }
218    
219                    if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
220                    {
221                            for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
222                                    ;
223    
224                            if (str_in[k] == 'm') // Valid
225                            {
226                                    if (j + (k - i + 1) >= buf_len)
227                                    {
228                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
229                                            str_out[j] = '\0';
230                                            return j;
231                                    }
232                                    memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
233                                    j += (k - i + 1);
234                                    i = k;
235                                    continue;
236                            }
237                            else // reach end of string
238                            {
239                                    break;
240                            }
241                    }
242    
243                    if (str_in[i] == '\n')
244                    {
245                            tag_start_pos = -1; // jump out of tag at end of line
246                            new_line = 1;
247                    }
248                    else if (str_in[i] == '\r')
249                    {
250                            continue; // ignore '\r'
251                    }
252    
253                  if (str_in[i] == '[')                  if (str_in[i] == '[')
254                  {                  {
255                          tag_start_pos = i + 1;                          tag_start_pos = i + 1;
# Line 221  int lml_plain(const char *str_in, char * Line 296  int lml_plain(const char *str_in, char *
296                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(
297                                                                          LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);                                                                          LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);
298                                                          }                                                          }
299                                                          if (j + tag_output_len >= buf_len - 1)                                                          if (j + tag_output_len >= buf_len)
300                                                          {                                                          {
301                                                                  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);
302                                                                  str_out[j] = '\0';                                                                  str_out[j] = '\0';
303                                                                  return j;                                                                  return j;
304                                                          }                                                          }
# Line 242  int lml_plain(const char *str_in, char * Line 317  int lml_plain(const char *str_in, char *
317                  }                  }
318                  else if (tag_start_pos == -1) // not in LML tag                  else if (tag_start_pos == -1) // not in LML tag
319                  {                  {
320                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] & 0b10000000) // head of multi-byte character
321                          {                          {
322                                  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
323                                  {                                  {
324                                          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);
325                                          str_out[j] = '\0';                                          str_out[j] = '\0';
326                                          return j;                                          return j;
327                                  }                                  }
328                                  str_out[j++] = str_in[i++];  
329                                  if (str_in[i] == '\0')                                  c = (str_in[i] & 0b01110000) << 1;
330                                    while (c & 0b10000000)
331                                  {                                  {
332                                          str_out[j] = '\0';                                          str_out[j++] = str_in[i++];
333                                          return j;                                          if (str_in[i] == '\0')
334                                            {
335                                                    str_out[j] = '\0';
336                                                    return j;
337                                            }
338                                            c = (c & 0b01111111) << 1;
339                                  }                                  }
340                          }                          }
341    
342                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
343                          {                          {
344                                  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);
345                                  str_out[j] = '\0';                                  str_out[j] = '\0';
346                                  return j;                                  return j;
347                          }                          }
# Line 272  int lml_plain(const char *str_in, char * Line 353  int lml_plain(const char *str_in, char *
353                  }                  }
354          }          }
355    
356            if (lml_tag_quote_level > 0)
357            {
358                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
359                    if (j + tag_output_len >= buf_len)
360                    {
361                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
362                            str_out[j] = '\0';
363                            return j;
364                    }
365                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
366                    j += tag_output_len;
367            }
368    
369          str_out[j] = '\0';          str_out[j] = '\0';
370    
371          return j;          return j;
372  }  }


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

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