--- lbbs/src/lml.c 2025/06/03 02:30:18 1.5 +++ lbbs/src/lml.c 2025/06/14 10:10:28 1.9 @@ -75,7 +75,6 @@ static int lml_tag_quote_level = 0; 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) { - if (strcasecmp(tag_name, "quote") == 0) { if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL) @@ -100,18 +99,18 @@ static int lml_tag_quote_filter(const ch const static char *LML_tag_def[][3] = { {"left", "[", ""}, {"right", "]", NULL}, - {"bold", "\033[1m", ""}, + {"bold", "\033[1m", ""}, // does not work in Fterm {"/bold", "\033[22m", NULL}, {"b", "\033[1m", ""}, {"/b", "\033[22m", NULL}, - {"italic", "\033[5m", ""}, // use blink instead - {"/italic", "\033[25m", NULL}, + {"italic", "\033[5m", ""}, // use blink instead + {"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm {"i", "\033[5m", ""}, - {"/i", "\033[25m", NULL}, + {"/i", "\033[m", NULL}, {"underline", "\033[4m", ""}, - {"/underline", "\033[24m", NULL}, + {"/underline", "\033[m", NULL}, // \033[24m does not work in Fterm {"u", "\033[4m", ""}, - {"/u", "\033[24m", NULL}, + {"/u", "\033[m", NULL}, {"color", NULL, (const char *)lml_tag_color_filter}, {"/color", "\033[m", NULL}, {"quote", NULL, (const char *)lml_tag_quote_filter}, @@ -179,9 +178,9 @@ int lml_plain(const char *str_in, char * tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, (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 - 1) + 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 - 1); + 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; } @@ -202,9 +201,9 @@ int lml_plain(const char *str_in, char * tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]); - if (j + tag_output_len >= buf_len - 1) + 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 - 1); + 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; } @@ -215,6 +214,30 @@ int lml_plain(const char *str_in, char * new_line = 0; } + if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly + { + for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++) + ; + + if (str_in[k] == 'm') // Valid + { + if (j + (k - i + 1) >= buf_len) + { + 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; + } + else // reach end of string + { + break; + } + } + if (str_in[i] == '\n') { tag_start_pos = -1; // jump out of tag at end of line @@ -271,9 +294,9 @@ int lml_plain(const char *str_in, char * tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])( LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); } - if (j + tag_output_len >= buf_len - 1) + 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 - 1); + 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; } @@ -294,9 +317,9 @@ int lml_plain(const char *str_in, char * { if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character { - if (j + 2 >= buf_len - 1) + if (j + 2 >= buf_len) { - log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 2, buf_len - 1); + log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 2, buf_len); str_out[j] = '\0'; return j; } @@ -308,9 +331,9 @@ int lml_plain(const char *str_in, char * } } - if (j + 1 >= buf_len - 1) + if (j + 1 >= buf_len) { - log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_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; } @@ -325,9 +348,9 @@ int lml_plain(const char *str_in, char * if (lml_tag_quote_level > 0) { tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); - if (j + tag_output_len >= buf_len - 1) + 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 - 1); + 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; }