/[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.10 by sysadm, Mon Jun 16 14:30:44 2025 UTC
# Line 19  Line 19 
19  #include "common.h"  #include "common.h"
20  #include <stdio.h>  #include <stdio.h>
21  #include <string.h>  #include <string.h>
22    #include <strings.h>
23  #include <sys/param.h>  #include <sys/param.h>
24    
25  #define LML_TAG_PARAM_BUF_LEN 256  #define LML_TAG_PARAM_BUF_LEN 256
# Line 66  static int lml_tag_color_filter(const ch Line 67  static int lml_tag_color_filter(const ch
67  #define LML_TAG_QUOTE_LEVEL_LOOP 3  #define LML_TAG_QUOTE_LEVEL_LOOP 3
68    
69  static const char *lml_tag_quote_color[] = {  static const char *lml_tag_quote_color[] = {
70          "\033[33m",          "\033[33m", // yellow
71          "\033[32m",          "\033[32m", // green
72          "\033[35m",          "\033[35m", // magenta
73  };  };
74    
75  static int lml_tag_quote_level = 0;  static int lml_tag_quote_level = 0;
76    
77  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)
78  {  {
   
79          if (strcasecmp(tag_name, "quote") == 0)          if (strcasecmp(tag_name, "quote") == 0)
80          {          {
81                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)                  if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
# 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,
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},
# Line 168  int lml_plain(const char *str_in, char * Line 162  int lml_plain(const char *str_in, char *
162          int tag_end_pos = -1;          int tag_end_pos = -1;
163          int tag_param_pos = -1;          int tag_param_pos = -1;
164          int tag_output_len;          int tag_output_len;
165            int new_line = 1;
166            int fb_quote_level = 0;
167    
168          lml_init();          lml_init();
169    
# Line 175  int lml_plain(const char *str_in, char * Line 171  int lml_plain(const char *str_in, char *
171    
172          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
173          {          {
174                    if (new_line)
175                    {
176                            if (fb_quote_level > 0)
177                            {
178                                    lml_tag_quote_level -= fb_quote_level;
179    
180                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,
181                                                                                      (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
182                                    if (j + tag_output_len >= buf_len)
183                                    {
184                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
185                                            str_out[j] = '\0';
186                                            return j;
187                                    }
188                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
189                                    j += tag_output_len;
190    
191                                    fb_quote_level = 0;
192                            }
193    
194                            while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
195                            {
196                                    fb_quote_level++;
197                            }
198    
199                            if (fb_quote_level > 0)
200                            {
201                                    lml_tag_quote_level += fb_quote_level;
202    
203                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,
204                                                                                      lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);
205                                    if (j + tag_output_len >= buf_len)
206                                    {
207                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
208                                            str_out[j] = '\0';
209                                            return j;
210                                    }
211                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
212                                    j += tag_output_len;
213                            }
214    
215                            new_line = 0;
216                    }
217    
218                    if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
219                    {
220                            for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
221                                    ;
222    
223                            if (str_in[k] == 'm') // Valid
224                            {
225                                    if (j + (k - i + 1) >= buf_len)
226                                    {
227                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
228                                            str_out[j] = '\0';
229                                            return j;
230                                    }
231                                    memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
232                                    j += (k - i + 1);
233                                    i = k;
234                                    continue;
235                            }
236                            else // reach end of string
237                            {
238                                    break;
239                            }
240                    }
241    
242                    if (str_in[i] == '\n')
243                    {
244                            tag_start_pos = -1; // jump out of tag at end of line
245                            new_line = 1;
246                    }
247                    else if (str_in[i] == '\r')
248                    {
249                            continue; // ignore '\r'
250                    }
251    
252                  if (str_in[i] == '[')                  if (str_in[i] == '[')
253                  {                  {
254                          tag_start_pos = i + 1;                          tag_start_pos = i + 1;
# Line 221  int lml_plain(const char *str_in, char * Line 295  int lml_plain(const char *str_in, char *
295                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(
296                                                                          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);
297                                                          }                                                          }
298                                                          if (j + tag_output_len >= buf_len - 1)                                                          if (j + tag_output_len >= buf_len)
299                                                          {                                                          {
300                                                                  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);
301                                                                  str_out[j] = '\0';                                                                  str_out[j] = '\0';
302                                                                  return j;                                                                  return j;
303                                                          }                                                          }
# Line 244  int lml_plain(const char *str_in, char * Line 318  int lml_plain(const char *str_in, char *
318                  {                  {
319                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character
320                          {                          {
321                                  if (j + 2 >= buf_len - 1)                                  if (j + 2 >= buf_len)
322                                  {                                  {
323                                          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 + 2, buf_len);
324                                          str_out[j] = '\0';                                          str_out[j] = '\0';
325                                          return j;                                          return j;
326                                  }                                  }
# Line 258  int lml_plain(const char *str_in, char * Line 332  int lml_plain(const char *str_in, char *
332                                  }                                  }
333                          }                          }
334    
335                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
336                          {                          {
337                                  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);
338                                  str_out[j] = '\0';                                  str_out[j] = '\0';
339                                  return j;                                  return j;
340                          }                          }
# Line 272  int lml_plain(const char *str_in, char * Line 346  int lml_plain(const char *str_in, char *
346                  }                  }
347          }          }
348    
349            if (lml_tag_quote_level > 0)
350            {
351                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
352                    if (j + tag_output_len >= buf_len)
353                    {
354                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
355                            str_out[j] = '\0';
356                            return j;
357                    }
358                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
359                    j += tag_output_len;
360            }
361    
362          str_out[j] = '\0';          str_out[j] = '\0';
363    
364          return j;          return j;
365  }  }


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

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