--- lbbs/src/lml.c 2025/10/30 03:18:56 1.35 +++ lbbs/src/lml.c 2025/10/30 07:51:47 1.36 @@ -17,6 +17,7 @@ #include "common.h" #include "lml.h" #include "log.h" +#include "str_process.h" #include #include #include @@ -178,7 +179,7 @@ inline static void lml_init(void) } } -#define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len) \ +#define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len, line_width) \ { \ if ((out_buf_offset) + (tag_out_len) >= (out_buf_len)) \ { \ @@ -187,10 +188,12 @@ inline static void lml_init(void) return (out_buf_offset); \ } \ memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len)); \ + *((out_buf) + (out_buf_offset) + (size_t)(tag_out_len)) = '\0'; \ + (line_width) += str_length((out_buf) + (out_buf_offset), 1); \ (out_buf_offset) += (tag_out_len); \ } -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 width, int quote_mode) { char c; char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; @@ -206,32 +209,45 @@ int lml_render(const char *str_in, char int new_line = 1; int fb_quote_level = 0; int tag_name_found; + int line_width = 0; lml_init(); lml_tag_disabled = 0; lml_tag_quote_level = 0; + if (width <= 0) + { + width = INT_MAX; + } + for (i = 0; str_in[i] != '\0'; i++) { - if (!quote_mode && !lml_tag_disabled && new_line) + if (!lml_tag_disabled && new_line) { - fb_quote_level = 0; - - while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str + while (str_in[i] == ':' && str_in[i + 1] == ' ') // FB2000 quote leading str { fb_quote_level++; + lml_tag_quote_level++; + i += 2; } - lml_tag_quote_level += fb_quote_level; - - if (lml_tag_quote_level > 0) + if (!quote_mode && lml_tag_quote_level > 0) { 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]); - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); } + for (k = 0; k < fb_quote_level; k++) + { + tag_output_buf[k * 2] = ':'; + tag_output_buf[k * 2 + 1] = ' '; + } + tag_output_buf[fb_quote_level * 2] = '\0'; + tag_output_len = fb_quote_level * 2; + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); + new_line = 0; } @@ -245,7 +261,7 @@ int lml_render(const char *str_in, char k--; } - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1, line_width); i = k; continue; } @@ -256,24 +272,27 @@ int lml_render(const char *str_in, char { tag_end_pos = i - 1; tag_output_len = tag_end_pos - tag_start_pos + 1; - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); } if (fb_quote_level > 0) { lml_tag_quote_level -= fb_quote_level; - tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len); + tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, (quote_mode ? "" : "\033[m")); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); + + fb_quote_level = 0; } tag_start_pos = -1; tag_name_pos = -1; new_line = 1; + line_width = 0; } - else if (str_in[i] == '\r') + else if (str_in[i] == '\r' || str_in[i] == '\7') { - continue; // ignore '\r' + continue; // Skip special characters } if (!lml_tag_disabled && str_in[i] == '[') @@ -282,7 +301,7 @@ int lml_render(const char *str_in, char { tag_end_pos = i - 1; tag_output_len = tag_end_pos - tag_start_pos + 1; - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); } tag_start_pos = i; @@ -346,7 +365,7 @@ int lml_render(const char *str_in, char lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); } } - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); break; default: // tag_name not match continue; @@ -358,7 +377,7 @@ int lml_render(const char *str_in, char if (!tag_name_found) { tag_output_len = tag_end_pos - tag_start_pos + 1; - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); } tag_start_pos = -1; @@ -366,35 +385,32 @@ int lml_render(const char *str_in, char } else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag { - if (str_in[i] & 0x80) // head of multi-byte character + if (line_width + (str_in[i] & 0x80 ? 2 : 1) > width) { - if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes - { - log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len); - str_out[j] = '\0'; - return j; - } + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); + new_line = 1; + line_width = 0; + i--; // redo at current $i + continue; + } + tag_output_len = 1; + if (str_in[i] & 0x80) // head of multi-byte character + { c = (str_in[i] & 0x70) << 1; while (c & 0x80) { - str_out[j++] = str_in[i++]; - if (str_in[i] == '\0') + if (str_in[i + tag_output_len] == '\0') { - str_out[j] = '\0'; - return j; + break; } + tag_output_len++; c = (c & 0x7f) << 1; } } - if (j + 1 >= buf_len) - { - 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]; + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, tag_output_len, line_width); + i += (tag_output_len - 1); } else // in LML tag { @@ -406,13 +422,13 @@ int lml_render(const char *str_in, char { tag_end_pos = i - 1; tag_output_len = tag_end_pos - tag_start_pos + 1; - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); } if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) { tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); - CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len); + CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); } str_out[j] = '\0';