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