/[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.22 by sysadm, Sat Oct 18 12:06:10 2025 UTC Revision 1.27 by sysadm, Fri Oct 24 07:27:02 2025 UTC
# Line 103  static int lml_tag_disable_filter(const Line 103  static int lml_tag_disable_filter(const
103  {  {
104          lml_tag_disabled = 1;          lml_tag_disabled = 1;
105    
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    
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},
122            {"lml", "", NULL, "", NULL},
123          {"left", "[", "", "[left]", NULL},          {"left", "[", "", "[left]", NULL},
124          {"right", "]", "", "[right]", NULL},          {"right", "]", "", "[right]", NULL},
125          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
126          {"/bold", "\033[22m", NULL, "", NULL},          {"/bold", "\033[22m", NULL, "", NULL},
127          {"b", "\033[1m", "", "", NULL},          {"b", "\033[1m", "", "", NULL},
128          {"/b", "\033[22m", NULL, "", NULL},          {"/b", "\033[22m", NULL, "", NULL},
129          {"italic", "\033[5m", "", "", NULL}, // use blink instead          {"italic", "\033[5m", "", "", NULL},   // use blink instead
130          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
131          {"i", "\033[5m", "", "", NULL},          {"i", "\033[5m", "", "", NULL},
132          {"/i", "\033[m", NULL, "", NULL},          {"/i", "\033[m", NULL, "", NULL},
# Line 151  const LML_TAG_DEF lml_tag_def[] = { Line 153  const LML_TAG_DEF lml_tag_def[] = {
153          {"bwf", "\033[1;31m****\033[m", "", "****", NULL},          {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
154  };  };
155    
156  #define LML_TAG_COUNT 32  #define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF))
157    
158  static int lml_tag_name_len[LML_TAG_COUNT];  static int lml_tag_name_len[LML_TAG_COUNT];
159  static int lml_ready = 0;  static int lml_ready = 0;
# Line 180  int lml_render(const char *str_in, char Line 182  int lml_render(const char *str_in, char
182          int j = 0;          int j = 0;
183          int k;          int k;
184          int tag_start_pos = -1;          int tag_start_pos = -1;
185            int tag_name_pos = -1;
186          int tag_end_pos = -1;          int tag_end_pos = -1;
187          int tag_param_pos = -1;          int tag_param_pos = -1;
188          int tag_output_len;          int tag_output_len;
189          int new_line = 1;          int new_line = 1;
190          int fb_quote_level = 0;          int fb_quote_level = 0;
191            int tag_name_found;
192    
193          lml_init();          lml_init();
194    
# Line 193  int lml_render(const char *str_in, char Line 197  int lml_render(const char *str_in, char
197    
198          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
199          {          {
200                  if (quote_mode && !lml_tag_disabled && new_line)                  if (!quote_mode && !lml_tag_disabled && new_line)
201                  {                  {
202                          if (fb_quote_level > 0)                          if (fb_quote_level > 0)
203                          {                          {
# Line 263  int lml_render(const char *str_in, char Line 267  int lml_render(const char *str_in, char
267    
268                  if (str_in[i] == '\n')                  if (str_in[i] == '\n')
269                  {                  {
270                          tag_start_pos = -1; // jump out of tag at end of line                          tag_name_pos = -1; // jump out of tag at end of line
271                          new_line = 1;                          new_line = 1;
272                  }                  }
273                  else if (str_in[i] == '\r')                  else if (str_in[i] == '\r')
# Line 273  int lml_render(const char *str_in, char Line 277  int lml_render(const char *str_in, char
277    
278                  if (!lml_tag_disabled && str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
279                  {                  {
280                          tag_start_pos = i + 1;                          tag_start_pos = i;
281                            tag_name_pos = i + 1;
282                  }                  }
283                  else if (!lml_tag_disabled && str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']')
284                  {                  {
285                          if (tag_start_pos >= 0)                          if (tag_name_pos >= 0)
286                          {                          {
287                                  tag_end_pos = i;                                  tag_end_pos = i;
288    
289                                  // Skip space characters                                  // Skip space characters
290                                  while (str_in[tag_start_pos] == ' ')                                  while (str_in[tag_name_pos] == ' ')
291                                  {                                  {
292                                          tag_start_pos++;                                          tag_name_pos++;
293                                  }                                  }
294    
295                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)
296                                  {                                  {
297                                          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)
298                                          {                                          {
299                                                  tag_param_pos = -1;                                                  tag_param_pos = -1;
300                                                  switch (str_in[tag_start_pos + lml_tag_name_len[k]])                                                  switch (str_in[tag_name_pos + lml_tag_name_len[k]])
301                                                  {                                                  {
302                                                  case ' ':                                                  case ' ':
303                                                          tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;                                                          tag_name_found = 1;
304                                                            tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
305                                                          while (str_in[tag_param_pos] == ' ')                                                          while (str_in[tag_param_pos] == ' ')
306                                                          {                                                          {
307                                                                  tag_param_pos++;                                                                  tag_param_pos++;
# Line 303  int lml_render(const char *str_in, char Line 309  int lml_render(const char *str_in, char
309                                                          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));
310                                                          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';
311                                                  case ']':                                                  case ']':
312                                                            tag_name_found = 1;
313                                                          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
314                                                          {                                                          {
315                                                                  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);
316                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
317                                                          }                                                          }
318                                                          if (quote_mode)                                                          if (!quote_mode)
319                                                          {                                                          {
320                                                                  if (lml_tag_def[k].tag_output != NULL)                                                                  if (lml_tag_def[k].tag_output != NULL)
321                                                                  {                                                                  {
322                                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);                                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
323                                                                  }                                                                  }
324                                                                  else                                                                  else if (lml_tag_def[k].tag_filter_cb != NULL)
325                                                                  {                                                                  {
326                                                                          tag_output_len = lml_tag_def[k].tag_filter_cb(                                                                          tag_output_len = lml_tag_def[k].tag_filter_cb(
327                                                                                  lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode);                                                                                  lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
328                                                                    }
329                                                                    else
330                                                                    {
331                                                                            tag_output_len = 0;
332                                                                  }                                                                  }
333                                                          }                                                          }
334                                                          else                                                          else // if (quote_mode)
335                                                          {                                                          {
336                                                                  if (lml_tag_def[k].quote_mode_output != NULL)                                                                  if (lml_tag_def[k].quote_mode_output != NULL)
337                                                                  {                                                                  {
338                                                                          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);
339                                                                  }                                                                  }
340                                                                  else                                                                  else if (lml_tag_def[k].tag_filter_cb != NULL)
341                                                                  {                                                                  {
342                                                                          tag_output_len = lml_tag_def[k].tag_filter_cb(                                                                          tag_output_len = lml_tag_def[k].tag_filter_cb(
343                                                                                  lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode);                                                                                  lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
344                                                                    }
345                                                                    else
346                                                                    {
347                                                                            tag_output_len = 0;
348                                                                  }                                                                  }
349                                                          }                                                          }
350                                                          if (j + tag_output_len >= buf_len)                                                          if (j + tag_output_len >= buf_len)
# Line 348  int lml_render(const char *str_in, char Line 363  int lml_render(const char *str_in, char
363                                          }                                          }
364                                  }                                  }
365    
366                                    if (!tag_name_found)
367                                    {
368                                            tag_output_len = tag_end_pos - tag_start_pos + 1;
369                                            if (j + tag_output_len >= buf_len)
370                                            {
371                                                    log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
372                                                    str_out[j] = '\0';
373                                                    return j;
374                                            }
375    
376                                            memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
377                                            j += tag_output_len;
378                                    }
379    
380                                  tag_start_pos = -1;                                  tag_start_pos = -1;
381                                    tag_name_pos = -1;
382                          }                          }
383                  }                  }
384                  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
385                  {                  {
386                          if (str_in[i] & 0x80) // head of multi-byte character                          if (str_in[i] & 0x80) // head of multi-byte character
387                          {                          {
# Line 389  int lml_render(const char *str_in, char Line 419  int lml_render(const char *str_in, char
419                  }                  }
420          }          }
421    
422          if (quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)          if (tag_start_pos != -1) // tag is not closed
423            {
424                    tag_end_pos = i - 1;
425                    tag_output_len = tag_end_pos - tag_start_pos + 1;
426                    if (j + tag_output_len >= buf_len)
427                    {
428                            log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len);
429                            str_out[j] = '\0';
430                            return j;
431                    }
432    
433                    memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len);
434                    j += tag_output_len;
435            }
436    
437            if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
438          {          {
439                  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");
440                  if (j + tag_output_len >= buf_len)                  if (j + tag_output_len >= buf_len)


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

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