/[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.25 by sysadm, Fri Oct 24 03:57:45 2025 UTC Revision 1.30 by sysadm, Wed Oct 29 02:23:18 2025 UTC
# Line 63  static int lml_tag_color_filter(const ch Line 63  static int lml_tag_color_filter(const ch
63  }  }
64    
65  #define LML_TAG_QUOTE_MAX_LEVEL 10  #define LML_TAG_QUOTE_MAX_LEVEL 10
 #define LML_TAG_QUOTE_LEVEL_LOOP 3  
66    
67  static const char *lml_tag_quote_color[] = {  static const char *lml_tag_quote_color[] = {
68          "\033[33m", // yellow          "\033[33m", // yellow
# Line 71  static const char *lml_tag_quote_color[] Line 70  static const char *lml_tag_quote_color[]
70          "\033[35m", // magenta          "\033[35m", // magenta
71  };  };
72    
73    static const int LML_TAG_QUOTE_LEVEL_LOOP = (int)(sizeof(lml_tag_quote_color) / sizeof(const char *));
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, int quote_mode)  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)
# Line 106  static int lml_tag_disable_filter(const Line 107  static int lml_tag_disable_filter(const
107          return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));          return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));
108  }  }
109    
 static int lml_tag_user_disabled = 0;  
   
 static int lml_tag_user_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)  
 {  
         lml_tag_user_disabled = 1;  
         tag_output_buf[0] = '\0';  
         return 0;  
 }  
   
 static int lml_tag_user_enable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)  
 {  
         lml_tag_user_disabled = 0;  
         tag_output_buf[0] = '\0';  
         return 0;  
 }  
   
110  typedef struct lml_tag_def_t  typedef struct lml_tag_def_t
111  {  {
112          const char *tag_name; // tag name          const char *tag_name;                    // tag name
113          const char *tag_output; // output string          const char *tag_output;                  // output string
114          const char *default_param; // default param string          const char *default_param;               // default param string
115          const char *quote_mode_output; // output string in quote mode          const char *quote_mode_output;   // output string in quote mode
116          lml_tag_filter_cb tag_filter_cb; // tag filter callback          lml_tag_filter_cb tag_filter_cb; // tag filter callback
117  } LML_TAG_DEF;  } LML_TAG_DEF;
118    
119  const LML_TAG_DEF lml_tag_def[] = {  const LML_TAG_DEF lml_tag_def[] = {
120          // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}          // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
121          {"plain", NULL, NULL, NULL, lml_tag_disable_filter},          {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
122          {"nolml", NULL, NULL, NULL, lml_tag_user_disable_filter},          {"nolml", "", NULL, "", NULL}, // deprecated
123          {"lml", NULL, NULL, NULL, lml_tag_user_enable_filter},          {"lml", "", NULL, "", NULL},   // deprecated
124            {"align", "", "", "", NULL},   // N/A
125            {"/align", "", "", "", NULL},  // N/A
126            {"size", "", "", "", NULL},        // N/A
127            {"/size", "", "", "", NULL},   // N/A
128          {"left", "[", "", "[left]", NULL},          {"left", "[", "", "[left]", NULL},
129          {"right", "]", "", "[right]", NULL},          {"right", "]", "", "[right]", NULL},
130          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
131          {"/bold", "\033[22m", NULL, "", NULL},          {"/bold", "\033[22m", NULL, "", NULL},
132          {"b", "\033[1m", "", "", NULL},          {"b", "\033[1m", "", "", NULL},
133          {"/b", "\033[22m", NULL, "", NULL},          {"/b", "\033[22m", NULL, "", NULL},
134          {"italic", "\033[5m", "", "", NULL}, // use blink instead          {"italic", "\033[5m", "", "", NULL},   // use blink instead
135          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
136          {"i", "\033[5m", "", "", NULL},          {"i", "\033[5m", "", "", NULL},
137          {"/i", "\033[m", NULL, "", NULL},          {"/i", "\033[m", NULL, "", NULL},
# Line 198  int lml_render(const char *str_in, char Line 187  int lml_render(const char *str_in, char
187          int j = 0;          int j = 0;
188          int k;          int k;
189          int tag_start_pos = -1;          int tag_start_pos = -1;
190            int tag_name_pos = -1;
191          int tag_end_pos = -1;          int tag_end_pos = -1;
192          int tag_param_pos = -1;          int tag_param_pos = -1;
193          int tag_output_len;          int tag_output_len;
194          int new_line = 1;          int new_line = 1;
195          int fb_quote_level = 0;          int fb_quote_level = 0;
196            int tag_name_found;
197    
198          lml_init();          lml_init();
199    
200          lml_tag_disabled = 0;          lml_tag_disabled = 0;
         lml_tag_user_disabled = 0;  
201          lml_tag_quote_level = 0;          lml_tag_quote_level = 0;
202    
203          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
# Line 280  int lml_render(const char *str_in, char Line 270  int lml_render(const char *str_in, char
270                          }                          }
271                  }                  }
272    
273                  if (str_in[i] == '\n')                  if (str_in[i] == '\n') // jump out of tag at end of line
274                  {                  {
275                          tag_start_pos = -1; // jump out of tag at end of line                          if (tag_start_pos != -1) // tag is not closed
276                            {
277                                    tag_end_pos = i - 1;
278                                    tag_output_len = tag_end_pos - tag_start_pos + 1;
279                                    if (j + tag_output_len >= buf_len)
280                                    {
281                                            log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
282                                            str_out[j] = '\0';
283                                            return j;
284                                    }
285    
286                                    memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
287                                    j += tag_output_len;
288                            }
289    
290                            tag_start_pos = -1;
291                            tag_name_pos = -1;
292                          new_line = 1;                          new_line = 1;
293                  }                  }
294                  else if (str_in[i] == '\r')                  else if (str_in[i] == '\r')
# Line 292  int lml_render(const char *str_in, char Line 298  int lml_render(const char *str_in, char
298    
299                  if (!lml_tag_disabled && str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
300                  {                  {
301                          tag_start_pos = i + 1;                          tag_start_pos = i;
302                            tag_name_pos = i + 1;
303                  }                  }
304                  else if (!lml_tag_disabled && str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']')
305                  {                  {
306                          if (tag_start_pos >= 0)                          if (tag_name_pos >= 0)
307                          {                          {
308                                  tag_end_pos = i;                                  tag_end_pos = i;
309    
310                                  // Skip space characters                                  // Skip space characters
311                                  while (str_in[tag_start_pos] == ' ')                                  while (str_in[tag_name_pos] == ' ')
312                                  {                                  {
313                                          tag_start_pos++;                                          tag_name_pos++;
314                                  }                                  }
315    
316                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)
317                                  {                                  {
318                                          if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0)                                          if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0)
319                                          {                                          {
320                                                  tag_param_pos = -1;                                                  tag_param_pos = -1;
321                                                  switch (str_in[tag_start_pos + lml_tag_name_len[k]])                                                  switch (str_in[tag_name_pos + lml_tag_name_len[k]])
322                                                  {                                                  {
323                                                  case ' ':                                                  case ' ':
324                                                          tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;                                                          tag_name_found = 1;
325                                                            tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
326                                                          while (str_in[tag_param_pos] == ' ')                                                          while (str_in[tag_param_pos] == ' ')
327                                                          {                                                          {
328                                                                  tag_param_pos++;                                                                  tag_param_pos++;
# Line 322  int lml_render(const char *str_in, char Line 330  int lml_render(const char *str_in, char
330                                                          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));
331                                                          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';
332                                                  case ']':                                                  case ']':
333                                                            tag_name_found = 1;
334                                                          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                                                          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
335                                                          {                                                          {
336                                                                  strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);                                                                  strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
337                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
338                                                          }                                                          }
339                                                          if (!quote_mode && !lml_tag_user_disabled)                                                          if (!quote_mode)
340                                                          {                                                          {
341                                                                  if (lml_tag_def[k].tag_output != NULL)                                                                  if (lml_tag_def[k].tag_output != NULL)
342                                                                  {                                                                  {
# Line 343  int lml_render(const char *str_in, char Line 352  int lml_render(const char *str_in, char
352                                                                          tag_output_len = 0;                                                                          tag_output_len = 0;
353                                                                  }                                                                  }
354                                                          }                                                          }
355                                                          else // if (quote_mode || lml_tag_user_disabled)                                                          else // if (quote_mode)
356                                                          {                                                          {
357                                                                  if (lml_tag_def[k].quote_mode_output != NULL)                                                                  if (lml_tag_def[k].quote_mode_output != NULL)
358                                                                  {                                                                  {
# Line 375  int lml_render(const char *str_in, char Line 384  int lml_render(const char *str_in, char
384                                          }                                          }
385                                  }                                  }
386    
387                                    if (!tag_name_found)
388                                    {
389                                            tag_output_len = tag_end_pos - tag_start_pos + 1;
390                                            if (j + tag_output_len >= buf_len)
391                                            {
392                                                    log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
393                                                    str_out[j] = '\0';
394                                                    return j;
395                                            }
396    
397                                            memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
398                                            j += tag_output_len;
399                                    }
400    
401                                  tag_start_pos = -1;                                  tag_start_pos = -1;
402                                    tag_name_pos = -1;
403                          }                          }
404                  }                  }
405                  else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag
406                  {                  {
407                          if (str_in[i] & 0x80) // head of multi-byte character                          if (str_in[i] & 0x80) // head of multi-byte character
408                          {                          {
# Line 416  int lml_render(const char *str_in, char Line 440  int lml_render(const char *str_in, char
440                  }                  }
441          }          }
442    
443            if (tag_start_pos != -1) // tag is not closed
444            {
445                    tag_end_pos = i - 1;
446                    tag_output_len = tag_end_pos - tag_start_pos + 1;
447                    if (j + tag_output_len >= buf_len)
448                    {
449                            log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
450                            str_out[j] = '\0';
451                            return j;
452                    }
453    
454                    memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
455                    j += tag_output_len;
456            }
457    
458          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
459          {          {
460                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");


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

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