/[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.28 by sysadm, Fri Oct 24 07:45:06 2025 UTC Revision 1.34 by sysadm, Wed Oct 29 14:45:43 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 118  typedef struct lml_tag_def_t Line 119  typedef struct lml_tag_def_t
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},          {"nolml", "", NULL, "", NULL}, // deprecated
123          {"lml", "", NULL, "", NULL},          {"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
# Line 173  inline static void lml_init(void) Line 178  inline static void lml_init(void)
178          }          }
179  }  }
180    
181    #define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len) \
182    { \
183            if ((out_buf_offset) + (tag_out_len) >= (out_buf_len)) \
184            { \
185                    log_error("Buffer is not longer enough for output string %d >= %d\n", (out_buf_offset) + (tag_out_len), (out_buf_len)); \
186                    out_buf[out_buf_offset] = '\0'; \
187                    return (out_buf_offset); \
188            } \
189            memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len)); \
190            (out_buf_offset) += (tag_out_len); \
191    }
192    
193  int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)  int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)
194  {  {
195          char c;          char c;
# Line 199  int lml_render(const char *str_in, char Line 216  int lml_render(const char *str_in, char
216          {          {
217                  if (!quote_mode && !lml_tag_disabled && new_line)                  if (!quote_mode && !lml_tag_disabled && new_line)
218                  {                  {
219                          if (fb_quote_level > 0)                          fb_quote_level = 0;
                         {  
                                 lml_tag_quote_level -= fb_quote_level;  
   
                                 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",  
                                                                                   (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));  
                                 if (j + tag_output_len >= buf_len)  
                                 {  
                                         log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);  
                                         str_out[j] = '\0';  
                                         return j;  
                                 }  
                                 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                 j += tag_output_len;  
   
                                 fb_quote_level = 0;  
                         }  
220    
221                          while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str                          while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
222                          {                          {
223                                  fb_quote_level++;                                  fb_quote_level++;
224                          }                          }
225    
226                          if (fb_quote_level > 0)                          lml_tag_quote_level += fb_quote_level;
                         {  
                                 lml_tag_quote_level += fb_quote_level;  
227    
228                            if (lml_tag_quote_level > 0)
229                            {
230                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
231                                                                                    lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);                                                                                    lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
232                                  if (j + tag_output_len >= buf_len)                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
                                 {  
                                         log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);  
                                         str_out[j] = '\0';  
                                         return j;  
                                 }  
                                 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                 j += tag_output_len;  
233                          }                          }
234    
235                          new_line = 0;                          new_line = 0;
# Line 248  int lml_render(const char *str_in, char Line 242  int lml_render(const char *str_in, char
242    
243                          if (str_in[k] == 'm') // Valid                          if (str_in[k] == 'm') // Valid
244                          {                          {
245                                  if (j + (k - i + 1) >= buf_len)                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1);
                                 {  
                                         log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);  
                                         str_out[j] = '\0';  
                                         return j;  
                                 }  
                                 memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));  
                                 j += (k - i + 1);  
246                                  i = k;                                  i = k;
247                                  continue;                                  continue;
248                          }                          }
# Line 271  int lml_render(const char *str_in, char Line 258  int lml_render(const char *str_in, char
258                          {                          {
259                                  tag_end_pos = i - 1;                                  tag_end_pos = i - 1;
260                                  tag_output_len = tag_end_pos - tag_start_pos + 1;                                  tag_output_len = tag_end_pos - tag_start_pos + 1;
261                                  if (j + tag_output_len >= buf_len)                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
262                                  {                          }
                                         log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);  
                                         str_out[j] = '\0';  
                                         return j;  
                                 }  
263    
264                                  memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);                          if (fb_quote_level > 0)
265                                  j += tag_output_len;                          {
266                                    lml_tag_quote_level -= fb_quote_level;
267    
268                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
269                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
270                          }                          }
271    
272                          tag_start_pos = -1;                          tag_start_pos = -1;
# Line 293  int lml_render(const char *str_in, char Line 280  int lml_render(const char *str_in, char
280    
281                  if (!lml_tag_disabled && str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
282                  {                  {
283                            if (tag_start_pos != -1) // tag is not closed
284                            {
285                                    tag_end_pos = i - 1;
286                                    tag_output_len = tag_end_pos - tag_start_pos + 1;
287                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
288                            }
289    
290                          tag_start_pos = i;                          tag_start_pos = i;
291                          tag_name_pos = i + 1;                          tag_name_pos = i + 1;
292                  }                  }
293                  else if (!lml_tag_disabled && str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0)
294                  {                  {
295                          if (tag_name_pos >= 0)                          tag_end_pos = i;
                         {  
                                 tag_end_pos = i;  
296    
297                                  // Skip space characters                          // Skip space characters
298                                  while (str_in[tag_name_pos] == ' ')                          while (str_in[tag_name_pos] == ' ')
299                                  {                          {
300                                          tag_name_pos++;                                  tag_name_pos++;
301                                  }                          }
302    
303                                  for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)                          for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)
304                            {
305                                    if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0)
306                                  {                                  {
307                                          if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0)                                          tag_param_pos = -1;
308                                            switch (str_in[tag_name_pos + lml_tag_name_len[k]])
309                                          {                                          {
310                                                  tag_param_pos = -1;                                          case ' ':
311                                                  switch (str_in[tag_name_pos + lml_tag_name_len[k]])                                                  tag_name_found = 1;
312                                                    tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
313                                                    while (str_in[tag_param_pos] == ' ')
314                                                  {                                                  {
315                                                  case ' ':                                                          tag_param_pos++;
316                                                          tag_name_found = 1;                                                  }
317                                                          tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;                                                  strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
318                                                          while (str_in[tag_param_pos] == ' ')                                                  tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
319                                                          {                                          case ']':
320                                                                  tag_param_pos++;                                                  tag_name_found = 1;
321                                                          }                                                  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
322                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));                                                  {
323                                                          tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';                                                          strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
324                                                  case ']':                                                          tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
325                                                          tag_name_found = 1;                                                  }
326                                                          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                                                  tag_output_len = 0;
327                                                    if (!quote_mode)
328                                                    {
329                                                            if (lml_tag_def[k].tag_output != NULL)
330                                                          {                                                          {
331                                                                  strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);                                                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
                                                                 tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';  
332                                                          }                                                          }
333                                                          if (!quote_mode)                                                          else if (lml_tag_def[k].tag_filter_cb != NULL)
334                                                          {                                                          {
335                                                                  if (lml_tag_def[k].tag_output != NULL)                                                                  tag_output_len = lml_tag_def[k].tag_filter_cb(
336                                                                  {                                                                          lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
                                                                         tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);  
                                                                 }  
                                                                 else if (lml_tag_def[k].tag_filter_cb != NULL)  
                                                                 {  
                                                                         tag_output_len = lml_tag_def[k].tag_filter_cb(  
                                                                                 lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);  
                                                                 }  
                                                                 else  
                                                                 {  
                                                                         tag_output_len = 0;  
                                                                 }  
337                                                          }                                                          }
338                                                          else // if (quote_mode)                                                  }
339                                                    else // if (quote_mode)
340                                                    {
341                                                            if (lml_tag_def[k].quote_mode_output != NULL)
342                                                          {                                                          {
343                                                                  if (lml_tag_def[k].quote_mode_output != NULL)                                                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
                                                                 {  
                                                                         tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);  
                                                                 }  
                                                                 else if (lml_tag_def[k].tag_filter_cb != NULL)  
                                                                 {  
                                                                         tag_output_len = lml_tag_def[k].tag_filter_cb(  
                                                                                 lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);  
                                                                 }  
                                                                 else  
                                                                 {  
                                                                         tag_output_len = 0;  
                                                                 }  
344                                                          }                                                          }
345                                                          if (j + tag_output_len >= buf_len)                                                          else if (lml_tag_def[k].tag_filter_cb != NULL)
346                                                          {                                                          {
347                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);                                                                  tag_output_len = lml_tag_def[k].tag_filter_cb(
348                                                                  str_out[j] = '\0';                                                                          lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
                                                                 return j;  
349                                                          }                                                          }
                                                         memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                                         j += tag_output_len;  
                                                         break;  
                                                 default: // tag_name not match  
                                                         continue;  
350                                                  }                                                  }
351                                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
352                                                  break;                                                  break;
353                                            default: // tag_name not match
354                                                    continue;
355                                          }                                          }
356                                            break;
357                                  }                                  }
358                            }
359    
360                                  if (!tag_name_found)                          if (!tag_name_found)
361                                  {                          {
362                                          tag_output_len = tag_end_pos - tag_start_pos + 1;                                  tag_output_len = tag_end_pos - tag_start_pos + 1;
363                                          if (j + tag_output_len >= buf_len)                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
                                         {  
                                                 log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);  
                                                 str_out[j] = '\0';  
                                                 return j;  
                                         }  
   
                                         memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);  
                                         j += tag_output_len;  
                                 }  
   
                                 tag_start_pos = -1;  
                                 tag_name_pos = -1;  
364                          }                          }
365    
366                            tag_start_pos = -1;
367                            tag_name_pos = -1;
368                  }                  }
369                  else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag                  else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag
370                  {                  {
# Line 439  int lml_render(const char *str_in, char Line 408  int lml_render(const char *str_in, char
408          {          {
409                  tag_end_pos = i - 1;                  tag_end_pos = i - 1;
410                  tag_output_len = tag_end_pos - tag_start_pos + 1;                  tag_output_len = tag_end_pos - tag_start_pos + 1;
411                  if (j + tag_output_len >= buf_len)                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len);
                 {  
                         log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);  
                         str_out[j] = '\0';  
                         return j;  
                 }  
   
                 memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);  
                 j += tag_output_len;  
412          }          }
413    
414          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
415          {          {
416                  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");
417                  if (j + tag_output_len >= buf_len)                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len);
                 {  
                         log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);  
                         str_out[j] = '\0';  
                         return j;  
                 }  
                 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                 j += tag_output_len;  
418          }          }
419    
420          str_out[j] = '\0';          str_out[j] = '\0';


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

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