/[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.8 by sysadm, Sat Jun 14 10:03:32 2025 UTC
# 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)
# Line 90  static int lml_tag_quote_filter(const ch Line 89  static int lml_tag_quote_filter(const ch
89                  {                  {
90                          lml_tag_quote_level--;                          lml_tag_quote_level--;
91                  }                  }
92                  if (lml_tag_quote_level > 0)                  return snprintf(tag_output_buf, tag_output_buf_len,
93                  {                                                  (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");  
                 }  
94          }          }
95    
96          return 0;          return 0;
# Line 106  static int lml_tag_quote_filter(const ch Line 99  static int lml_tag_quote_filter(const ch
99  const static char *LML_tag_def[][3] = {  const static char *LML_tag_def[][3] = {
100          {"left", "[", ""},          {"left", "[", ""},
101          {"right", "]", NULL},          {"right", "]", NULL},
102          {"bold", "\033[1m", ""},          {"bold", "\033[1m", ""}, // does not work in Fterm
103          {"/bold", "\033[21m", NULL},          {"/bold", "\033[22m", NULL},
104          {"b", "\033[1m", ""},          {"b", "\033[1m", ""},
105          {"/b", "\033[21m", NULL},          {"/b", "\033[22m", NULL},
106          {"italic", "\033[3m", ""},          {"italic", "\033[5m", ""},       // use blink instead
107          {"/italic", "\033[23m", NULL},          {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm
108          {"i", "\033[3m", ""},          {"i", "\033[5m", ""},
109          {"/i", "\033[23m", NULL},          {"/i", "\033[m", NULL},
110          {"underline", "\033[4m", ""},          {"underline", "\033[4m", ""},
111          {"/underline", "\033[24m", NULL},          {"/underline", "\033[m", NULL}, // \033[24m does not work in Fterm
112          {"u", "\033[4m", ""},          {"u", "\033[4m", ""},
113          {"/u", "\033[24m", NULL},          {"/u", "\033[m", NULL},
114          {"color", NULL, (const char *)lml_tag_color_filter},          {"color", NULL, (const char *)lml_tag_color_filter},
115          {"/color", "\033[m", NULL},          {"/color", "\033[m", NULL},
116          {"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 161  int lml_plain(const char *str_in, char *
161          int tag_end_pos = -1;          int tag_end_pos = -1;
162          int tag_param_pos = -1;          int tag_param_pos = -1;
163          int tag_output_len;          int tag_output_len;
164            int new_line = 1;
165            int fb_quote_level = 0;
166    
167          lml_init();          lml_init();
168    
# Line 175  int lml_plain(const char *str_in, char * Line 170  int lml_plain(const char *str_in, char *
170    
171          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
172          {          {
173                    if (new_line)
174                    {
175                            if (fb_quote_level > 0)
176                            {
177                                    lml_tag_quote_level -= fb_quote_level;
178    
179                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN,
180                                                                                      (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
181                                    if (j + tag_output_len >= buf_len)
182                                    {
183                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
184                                            str_out[j] = '\0';
185                                            return j;
186                                    }
187                                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
188                                    j += tag_output_len;
189    
190                                    fb_quote_level = 0;
191                            }
192    
193                            while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
194                            {
195                                    fb_quote_level++;
196                            }
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,
203                                                                                      lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);
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    
214                            new_line = 0;
215                    }
216    
217                    if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
218                    {
219                            for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
220                                    ;
221    
222                            if (str_in[k] == 'm') // Valid
223                            {
224                                    if (j + (k - i + 1) >= buf_len)
225                                    {
226                                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
227                                            str_out[j] = '\0';
228                                            return j;
229                                    }
230                                    memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
231                                    i = k;
232                                    continue;
233                            }
234                            else // reach end of string
235                            {
236                                    break;
237                            }
238                    }
239    
240                    if (str_in[i] == '\n')
241                    {
242                            tag_start_pos = -1; // jump out of tag at end of line
243                            new_line = 1;
244                    }
245                    else if (str_in[i] == '\r')
246                    {
247                            continue; // ignore '\r'
248                    }
249    
250                  if (str_in[i] == '[')                  if (str_in[i] == '[')
251                  {                  {
252                          tag_start_pos = i + 1;                          tag_start_pos = i + 1;
# Line 221  int lml_plain(const char *str_in, char * Line 293  int lml_plain(const char *str_in, char *
293                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(                                                                  tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(
294                                                                          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);
295                                                          }                                                          }
296                                                          if (j + tag_output_len >= buf_len - 1)                                                          if (j + tag_output_len >= buf_len)
297                                                          {                                                          {
298                                                                  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);
299                                                                  str_out[j] = '\0';                                                                  str_out[j] = '\0';
300                                                                  return j;                                                                  return j;
301                                                          }                                                          }
# Line 244  int lml_plain(const char *str_in, char * Line 316  int lml_plain(const char *str_in, char *
316                  {                  {
317                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character                          if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character
318                          {                          {
319                                  if (j + 2 >= buf_len - 1)                                  if (j + 2 >= buf_len)
320                                  {                                  {
321                                          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);
322                                          str_out[j] = '\0';                                          str_out[j] = '\0';
323                                          return j;                                          return j;
324                                  }                                  }
# Line 258  int lml_plain(const char *str_in, char * Line 330  int lml_plain(const char *str_in, char *
330                                  }                                  }
331                          }                          }
332    
333                          if (j + 1 >= buf_len - 1)                          if (j + 1 >= buf_len)
334                          {                          {
335                                  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);
336                                  str_out[j] = '\0';                                  str_out[j] = '\0';
337                                  return j;                                  return j;
338                          }                          }
# Line 272  int lml_plain(const char *str_in, char * Line 344  int lml_plain(const char *str_in, char *
344                  }                  }
345          }          }
346    
347            if (lml_tag_quote_level > 0)
348            {
349                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
350                    if (j + tag_output_len >= buf_len)
351                    {
352                            log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
353                            str_out[j] = '\0';
354                            return j;
355                    }
356                    memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
357                    j += tag_output_len;
358            }
359    
360          str_out[j] = '\0';          str_out[j] = '\0';
361    
362          return j;          return j;
363  }  }


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

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