/[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.43 by sysadm, Tue Nov 4 14:58:56 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                          lml.h  -  description  /*
3                                                           -------------------   * lml
4          Copyright            : (C) 2004-2025 by Leaflet   *   - LML render
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
 /***************************************************************************  
  *                                                                         *  
  *   This program is free software; you can redistribute it and/or modify  *  
  *   it under the terms of the GNU General Public License as published by  *  
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
8    
9  #include "common.h"  #include "common.h"
10  #include "lml.h"  #include "lml.h"
11  #include "log.h"  #include "log.h"
12    #include "str_process.h"
13    #include <ctype.h>
14  #include <stdio.h>  #include <stdio.h>
15  #include <string.h>  #include <string.h>
16  #include <sys/param.h>  #include <sys/param.h>
# Line 24  Line 18 
18  #define LML_TAG_PARAM_BUF_LEN 256  #define LML_TAG_PARAM_BUF_LEN 256
19  #define LML_TAG_OUTPUT_BUF_LEN 1024  #define LML_TAG_OUTPUT_BUF_LEN 1024
20    
21    clock_t lml_total_exec_duration = 0;
22    
23  typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode);  typedef int (*lml_tag_filter_cb)(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode);
24    
25  static int lml_tag_color_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_color_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
# Line 63  static int lml_tag_color_filter(const ch Line 59  static int lml_tag_color_filter(const ch
59  }  }
60    
61  #define LML_TAG_QUOTE_MAX_LEVEL 10  #define LML_TAG_QUOTE_MAX_LEVEL 10
 #define LML_TAG_QUOTE_LEVEL_LOOP 3  
62    
63  static const char *lml_tag_quote_color[] = {  static const char *lml_tag_quote_color[] = {
64          "\033[33m", // yellow          "\033[33m", // yellow
# Line 71  static const char *lml_tag_quote_color[] Line 66  static const char *lml_tag_quote_color[]
66          "\033[35m", // magenta          "\033[35m", // magenta
67  };  };
68    
69    static const int LML_TAG_QUOTE_LEVEL_LOOP = (int)(sizeof(lml_tag_quote_color) / sizeof(const char *));
70    
71  static int lml_tag_quote_level = 0;  static int lml_tag_quote_level = 0;
72    
73  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 103  static int lml_tag_disable_filter(const
103          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]" : ""));
104  }  }
105    
 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;  
 }  
   
106  typedef struct lml_tag_def_t  typedef struct lml_tag_def_t
107  {  {
108          const char *tag_name; // tag name          const char *tag_name;                    // tag name
109          const char *tag_output; // output string          const char *tag_output;                  // output string
110          const char *default_param; // default param string          const char *default_param;               // default param string
111          const char *quote_mode_output; // output string in quote mode          const char *quote_mode_output;   // output string in quote mode
112          lml_tag_filter_cb tag_filter_cb; // tag filter callback          lml_tag_filter_cb tag_filter_cb; // tag filter callback
113  } LML_TAG_DEF;  } LML_TAG_DEF;
114    
115  const LML_TAG_DEF lml_tag_def[] = {  const LML_TAG_DEF lml_tag_def[] = {
116          // 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}
117          {"plain", NULL, NULL, NULL, lml_tag_disable_filter},          {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
118          {"nolml", NULL, NULL, NULL, lml_tag_user_disable_filter},          {"nolml", "", NULL, "", NULL}, // deprecated
119          {"lml", NULL, NULL, NULL, lml_tag_user_enable_filter},          {"lml", "", NULL, "", NULL},   // deprecated
120            {"align", "", "", "", NULL},   // N/A
121            {"/align", "", "", "", NULL},  // N/A
122            {"size", "", "", "", NULL},        // N/A
123            {"/size", "", "", "", NULL},   // N/A
124          {"left", "[", "", "[left]", NULL},          {"left", "[", "", "[left]", NULL},
125          {"right", "]", "", "[right]", NULL},          {"right", "]", "", "[right]", NULL},
126          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm          {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
127          {"/bold", "\033[22m", NULL, "", NULL},          {"/bold", "\033[22m", NULL, "", NULL},
128          {"b", "\033[1m", "", "", NULL},          {"b", "\033[1m", "", "", NULL},
129          {"/b", "\033[22m", NULL, "", NULL},          {"/b", "\033[22m", NULL, "", NULL},
130          {"italic", "\033[5m", "", "", NULL}, // use blink instead          {"italic", "\033[5m", "", "", NULL},   // use blink instead
131          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm          {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
132          {"i", "\033[5m", "", "", NULL},          {"i", "\033[5m", "", "", NULL},
133          {"/i", "\033[m", NULL, "", NULL},          {"/i", "\033[m", NULL, "", NULL},
# Line 189  inline static void lml_init(void) Line 174  inline static void lml_init(void)
174          }          }
175  }  }
176    
177  int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)  #define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len, line_width)                             \
178            {                                                                                                                               \
179                    if ((out_buf_offset) + (tag_out_len) >= (out_buf_len))                                                                      \
180                    {                                                                                                                           \
181                            log_error("Buffer is not longer enough for output string %d >= %d\n", (out_buf_offset) + (tag_out_len), (out_buf_len)); \
182                            out_buf[out_buf_offset] = '\0';                                                                                         \
183                            return (out_buf_offset);                                                                                                \
184                    }                                                                                                                           \
185                    memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len));                                                     \
186                    *((out_buf) + (out_buf_offset) + (size_t)(tag_out_len)) = '\0';                                                             \
187                    (line_width) += str_length((out_buf) + (out_buf_offset), 1);                                                                \
188                    (out_buf_offset) += (tag_out_len);                                                                                          \
189            }
190    
191    int lml_render(const char *str_in, char *str_out, int buf_len, int width, int quote_mode)
192  {  {
193            clock_t clock_begin;
194            clock_t clock_end;
195    
196          char c;          char c;
197          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
198          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
# Line 198  int lml_render(const char *str_in, char Line 200  int lml_render(const char *str_in, char
200          int j = 0;          int j = 0;
201          int k;          int k;
202          int tag_start_pos = -1;          int tag_start_pos = -1;
203            int tag_name_pos = -1;
204          int tag_end_pos = -1;          int tag_end_pos = -1;
205          int tag_param_pos = -1;          int tag_param_pos = -1;
206          int tag_output_len;          int tag_output_len;
207          int new_line = 1;          int new_line = 1;
208          int fb_quote_level = 0;          int fb_quote_level = 0;
209            int tag_name_found;
210            int line_width = 0;
211    
212            clock_begin = clock();
213    
214          lml_init();          lml_init();
215    
216          lml_tag_disabled = 0;          lml_tag_disabled = 0;
         lml_tag_user_disabled = 0;  
217          lml_tag_quote_level = 0;          lml_tag_quote_level = 0;
218    
219            if (width <= 0)
220            {
221                    width = INT_MAX;
222            }
223    
224          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
225          {          {
226                  if (!quote_mode && !lml_tag_disabled && new_line)                  if (!lml_tag_disabled && new_line)
227                  {                  {
228                          if (fb_quote_level > 0)                          while (str_in[i] == ':' && str_in[i + 1] == ' ') // FB2000 quote leading str
229                          {                          {
230                                  lml_tag_quote_level -= fb_quote_level;                                  fb_quote_level++;
231                                    lml_tag_quote_level++;
232                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",                                  i += 2;
                                                                                   (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;  
233                          }                          }
234    
235                          while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str                          if (!quote_mode && lml_tag_quote_level > 0)
236                          {                          {
237                                  fb_quote_level++;                                  tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
238                                                                                      lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
239                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
240                          }                          }
241    
242                          if (fb_quote_level > 0)                          for (k = 0; k < fb_quote_level; k++)
243                          {                          {
244                                  lml_tag_quote_level += fb_quote_level;                                  tag_output_buf[k * 2] = ':';
245                                    tag_output_buf[k * 2 + 1] = ' ';
                                 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",  
                                                                                   lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);  
                                 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;  
246                          }                          }
247                            tag_output_buf[fb_quote_level * 2] = '\0';
248                            tag_output_len = fb_quote_level * 2;
249                            CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
250    
251                          new_line = 0;                          new_line = 0;
252                  }                  }
253    
254                  if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly                  if (lml_tag_disabled && new_line)
255                  {                  {
256                          for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)                          new_line = 0;
257                    }
258    
259                    if (!quote_mode && !lml_tag_disabled && str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence
260                    {
261                            for (k = i + 2; isdigit(str_in[k]) || str_in[k] == ';' || str_in[k] == '?'; k++)
262                                  ;                                  ;
263    
264                          if (str_in[k] == 'm') // Valid                          if (str_in[k] == 'm') // valid -- copy directly
265                          {                          {
266                                  if (j + (k - i + 1) >= buf_len)                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1, line_width);
                                 {  
                                         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);  
                                 i = k;  
                                 continue;  
267                          }                          }
268                          else // reach end of string                          else if (isalpha(str_in[k]))
269                          {                          {
270                                  break;                                  // unsupported ANSI CSI command
271                          }                          }
272                            else
273                            {
274                                    k--;
275                            }
276    
277                            i = k;
278                            continue;
279                  }                  }
280    
281                  if (str_in[i] == '\n')                  if (str_in[i] == '\n') // jump out of tag at end of line
282                  {                  {
283                          tag_start_pos = -1; // jump out of tag at end of line                          if (!lml_tag_disabled && 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    
288                                    if (line_width + tag_output_len > width)
289                                    {
290                                            CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
291                                            new_line = 1;
292                                            line_width = 0;
293                                            i--; // redo at current i
294                                    }
295                                    else
296                                    {
297                                            CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width);
298                                    }
299                            }
300    
301                            if (!lml_tag_disabled && fb_quote_level > 0)
302                            {
303                                    lml_tag_quote_level -= fb_quote_level;
304    
305                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, (quote_mode ? "" : "\033[m"));
306                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
307    
308                                    fb_quote_level = 0;
309                            }
310    
311                            if (new_line)
312                            {
313                                    continue;
314                            }
315    
316                            tag_start_pos = -1;
317                            tag_name_pos = -1;
318                          new_line = 1;                          new_line = 1;
319                            line_width = -1;
320                  }                  }
321                  else if (str_in[i] == '\r')                  else if (str_in[i] == '\r' || str_in[i] == '\7')
322                  {                  {
323                          continue; // ignore '\r'                          continue; // Skip special characters
324                  }                  }
325    
326                  if (!lml_tag_disabled && str_in[i] == '[')                  if (!lml_tag_disabled && str_in[i] == '[')
327                  {                  {
328                          tag_start_pos = i + 1;                          if (tag_start_pos != -1) // tag is not closed
329                            {
330                                    tag_end_pos = i - 1;
331                                    tag_output_len = tag_end_pos - tag_start_pos + 1;
332                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width);
333                            }
334    
335                            tag_start_pos = i;
336                            tag_name_pos = i + 1;
337                  }                  }
338                  else if (!lml_tag_disabled && str_in[i] == ']')                  else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0)
339                  {                  {
340                          if (tag_start_pos >= 0)                          tag_end_pos = i;
                         {  
                                 tag_end_pos = i;  
341    
342                                  // Skip space characters                          // Skip space characters
343                                  while (str_in[tag_start_pos] == ' ')                          while (str_in[tag_name_pos] == ' ')
344                                  {                          {
345                                          tag_start_pos++;                                  tag_name_pos++;
346                                  }                          }
347    
348                                  for (k = 0; k < LML_TAG_COUNT; k++)                          for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++)
349                            {
350                                    if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0)
351                                  {                                  {
352                                          if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0)                                          tag_param_pos = -1;
353                                            switch (str_in[tag_name_pos + lml_tag_name_len[k]])
354                                          {                                          {
355                                                  tag_param_pos = -1;                                          case ' ':
356                                                  switch (str_in[tag_start_pos + lml_tag_name_len[k]])                                                  tag_name_found = 1;
357                                                    tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
358                                                    while (str_in[tag_param_pos] == ' ')
359                                                  {                                                  {
360                                                  case ' ':                                                          tag_param_pos++;
361                                                          tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;                                                  }
362                                                          while (str_in[tag_param_pos] == ' ')                                                  strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
363                                                          {                                                  tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
364                                                                  tag_param_pos++;                                          case ']':
365                                                          }                                                  tag_name_found = 1;
366                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));                                                  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
367                                                          tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';                                                  {
368                                                  case ']':                                                          strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
369                                                          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_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
370                                                    }
371                                                    tag_output_len = 0;
372                                                    if (!quote_mode)
373                                                    {
374                                                            if (lml_tag_def[k].tag_output != NULL)
375                                                          {                                                          {
376                                                                  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';  
377                                                          }                                                          }
378                                                          if (!quote_mode && !lml_tag_user_disabled)                                                          else if (lml_tag_def[k].tag_filter_cb != NULL)
379                                                          {                                                          {
380                                                                  if (lml_tag_def[k].tag_output != NULL)                                                                  tag_output_len = lml_tag_def[k].tag_filter_cb(
381                                                                  {                                                                          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;  
                                                                 }  
382                                                          }                                                          }
383                                                          else // if (quote_mode || lml_tag_user_disabled)                                                  }
384                                                    else // if (quote_mode)
385                                                    {
386                                                            if (lml_tag_def[k].quote_mode_output != NULL)
387                                                          {                                                          {
388                                                                  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;  
                                                                 }  
389                                                          }                                                          }
390                                                          if (j + tag_output_len >= buf_len)                                                          else if (lml_tag_def[k].tag_filter_cb != NULL)
391                                                          {                                                          {
392                                                                  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(
393                                                                  str_out[j] = '\0';                                                                          lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
                                                                 return j;  
394                                                          }                                                          }
                                                         memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);  
                                                         j += tag_output_len;  
                                                         break;  
                                                 default: // tag_name not match  
                                                         continue;  
395                                                  }                                                  }
396                                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
397                                                  break;                                                  break;
398                                            default: // tag_name not match
399                                                    continue;
400                                          }                                          }
401                                            break;
402                                    }
403                            }
404    
405                            if (!tag_name_found)
406                            {
407                                    if (line_width + 1 > width)
408                                    {
409                                            CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
410                                            new_line = 1;
411                                            line_width = 0;
412                                            i--; // redo at current i
413                                            continue;
414                                  }                                  }
415    
416                                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width);
417                                    i = tag_start_pos; // restart from tag_start_pos + 1
418                                  tag_start_pos = -1;                                  tag_start_pos = -1;
419                                    tag_name_pos = -1;
420                                    continue;
421                          }                          }
422    
423                            tag_start_pos = -1;
424                            tag_name_pos = -1;
425                  }                  }
426                  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
427                  {                  {
428                          if (str_in[i] & 0x80) // head of multi-byte character                          if (line_width + (str_in[i] & 0x80 ? 2 : 1) > width)
429                          {                          {
430                                  if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes                                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
431                                  {                                  new_line = 1;
432                                          log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len);                                  line_width = 0;
433                                          str_out[j] = '\0';                                  i--; // redo at current i
434                                          return j;                                  continue;
435                                  }                          }
436    
437                            tag_output_len = 1;
438                            if (str_in[i] & 0x80) // head of multi-byte character
439                            {
440                                  c = (str_in[i] & 0x70) << 1;                                  c = (str_in[i] & 0x70) << 1;
441                                  while (c & 0x80)                                  while (c & 0x80)
442                                  {                                  {
443                                          str_out[j++] = str_in[i++];                                          if (str_in[i + tag_output_len] == '\0')
                                         if (str_in[i] == '\0')  
444                                          {                                          {
445                                                  str_out[j] = '\0';                                                  break;
                                                 return j;  
446                                          }                                          }
447                                            tag_output_len++;
448                                          c = (c & 0x7f) << 1;                                          c = (c & 0x7f) << 1;
449                                  }                                  }
450                          }                          }
451    
452                          if (j + 1 >= buf_len)                          CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, tag_output_len, line_width);
453                          {                          i += (tag_output_len - 1);
                                 log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len);  
                                 str_out[j] = '\0';  
                                 return j;  
                         }  
                         str_out[j++] = str_in[i];  
454                  }                  }
455                  else // in LML tag                  else // in LML tag
456                  {                  {
# Line 416  int lml_render(const char *str_in, char Line 458  int lml_render(const char *str_in, char
458                  }                  }
459          }          }
460    
461            if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed
462            {
463                    tag_end_pos = i - 1;
464                    tag_output_len = tag_end_pos - tag_start_pos + 1;
465                    CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width);
466            }
467    
468          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)          if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
469          {          {
470                  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");
471                  if (j + tag_output_len >= buf_len)                  CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
                 {  
                         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;  
472          }          }
473    
474          str_out[j] = '\0';          str_out[j] = '\0';
475    
476            clock_end = clock();
477            lml_total_exec_duration += (clock_end - clock_begin);
478    
479          return j;          return j;
480  }  }


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

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