--- lbbs/src/lml.c 2025/06/03 00:57:27 1.3 +++ lbbs/src/lml.c 2025/10/24 07:45:06 1.28 @@ -14,9 +14,9 @@ * * ***************************************************************************/ +#include "common.h" #include "lml.h" #include "log.h" -#include "common.h" #include #include #include @@ -24,9 +24,9 @@ #define LML_TAG_PARAM_BUF_LEN 256 #define LML_TAG_OUTPUT_BUF_LEN 1024 -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); +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); -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) +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) { if (strcasecmp(tag_name, "color") == 0) { @@ -62,99 +62,262 @@ static int lml_tag_color_filter(const ch return 0; } -const static char *LML_tag_def[][3] = { - {"left", "[", ""}, - {"right", "]", NULL}, - {"bold", "\033[1m", ""}, - {"/bold", "\033[21m", NULL}, - {"b", "\033[1m", ""}, - {"/b", "\033[21m", NULL}, - {"italic", "\033[3m", ""}, - {"/italic", "\033[23m", NULL}, - {"i", "\033[3m", ""}, - {"/i", "\033[23m", NULL}, - {"underline", "\033[4m", ""}, - {"/underline", "\033[24m", NULL}, - {"u", "\033[4m", ""}, - {"/u", "\033[24m", NULL}, - {"color", NULL, (const char *)lml_tag_color_filter}, - {"/color", "\033[m", NULL}, - {"url", "", ""}, - {"/url", "(Á´½Ó: %s)", NULL}, - {"link", "", ""}, - {"/link", "(Á´½Ó: %s)", NULL}, - {"email", "", ""}, - {"/email", "(Email: %s)", NULL}, - {"user", "", ""}, - {"/user", "(Óû§: %s)", NULL}, - {"article", "", ""}, - {"/article", "(ÎÄÕÂ: %s)", NULL}, - {"image", "(ͼƬ: %s)", ""}, - {"flash", "(Flash: %s)", ""}, - {"bwf", "\033[1;31m****\033[m", ""}, +#define LML_TAG_QUOTE_MAX_LEVEL 10 +#define LML_TAG_QUOTE_LEVEL_LOOP 3 + +static const char *lml_tag_quote_color[] = { + "\033[33m", // yellow + "\033[32m", // green + "\033[35m", // magenta +}; + +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, int quote_mode) +{ + if (strcasecmp(tag_name, "quote") == 0) + { + if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL) + { + lml_tag_quote_level++; + } + return snprintf(tag_output_buf, tag_output_buf_len, "%s", + lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]); + } + else if (strcasecmp(tag_name, "/quote") == 0) + { + if (lml_tag_quote_level > 0) + { + lml_tag_quote_level--; + } + return snprintf(tag_output_buf, 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")); + } + + return 0; +} + +static int lml_tag_disabled = 0; + +static int lml_tag_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_disabled = 1; + + return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : "")); +} + +typedef struct lml_tag_def_t +{ + const char *tag_name; // tag name + const char *tag_output; // output string + const char *default_param; // default param string + const char *quote_mode_output; // output string in quote mode + lml_tag_filter_cb tag_filter_cb; // tag filter callback +} LML_TAG_DEF; + +const LML_TAG_DEF lml_tag_def[] = { + // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb} + {"plain", NULL, NULL, NULL, lml_tag_disable_filter}, + {"nolml", "", NULL, "", NULL}, + {"lml", "", NULL, "", NULL}, + {"left", "[", "", "[left]", NULL}, + {"right", "]", "", "[right]", NULL}, + {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm + {"/bold", "\033[22m", NULL, "", NULL}, + {"b", "\033[1m", "", "", NULL}, + {"/b", "\033[22m", NULL, "", NULL}, + {"italic", "\033[5m", "", "", NULL}, // use blink instead + {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm + {"i", "\033[5m", "", "", NULL}, + {"/i", "\033[m", NULL, "", NULL}, + {"underline", "\033[4m", "", "", NULL}, + {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm + {"u", "\033[4m", "", "", NULL}, + {"/u", "\033[m", NULL, "", NULL}, + {"color", NULL, NULL, "", lml_tag_color_filter}, + {"/color", "\033[m", NULL, "", NULL}, + {"quote", NULL, NULL, "", lml_tag_quote_filter}, + {"/quote", NULL, NULL, "", lml_tag_quote_filter}, + {"url", "", "", "", NULL}, + {"/url", "(链接: %s)", NULL, "", NULL}, + {"link", "", "", "", NULL}, + {"/link", "(链接: %s)", NULL, "", NULL}, + {"email", "", "", "", NULL}, + {"/email", "(Email: %s)", NULL, "", NULL}, + {"user", "", "", "", NULL}, + {"/user", "(用户: %s)", NULL, "", NULL}, + {"article", "", "", "", NULL}, + {"/article", "(文章: %s)", NULL, "", NULL}, + {"image", "(图片: %s)", "", "%s", NULL}, + {"flash", "(Flash: %s)", "", "", NULL}, + {"bwf", "\033[1;31m****\033[m", "", "****", NULL}, }; -#define LML_TAG_COUNT 29 +#define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF)) -static int LML_tag_name_len[LML_TAG_COUNT]; -static int LML_init = 0; +static int lml_tag_name_len[LML_TAG_COUNT]; +static int lml_ready = 0; inline static void lml_init(void) { int i; - if (!LML_init) + if (!lml_ready) { for (i = 0; i < LML_TAG_COUNT; i++) { - LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]); + lml_tag_name_len[i] = (int)strlen(lml_tag_def[i].tag_name); } - LML_init = 1; + lml_ready = 1; } } -int lml_plain(const char *str_in, char *str_out, int buf_len) +int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode) { + char c; char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; int i; int j = 0; int k; int tag_start_pos = -1; + int tag_name_pos = -1; int tag_end_pos = -1; int tag_param_pos = -1; int tag_output_len; + int new_line = 1; + int fb_quote_level = 0; + int tag_name_found; lml_init(); + lml_tag_disabled = 0; + lml_tag_quote_level = 0; + for (i = 0; str_in[i] != '\0'; i++) { - if (str_in[i] == '[') + if (!quote_mode && !lml_tag_disabled && new_line) { - tag_start_pos = i + 1; + if (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; + } + + while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str + { + fb_quote_level++; + } + + if (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_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; + } + + new_line = 0; } - else if (str_in[i] == ']') + + if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly { - if (tag_start_pos >= 0) + 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') // jump out of tag at end of line + { + if (tag_start_pos != -1) // tag is not closed + { + tag_end_pos = i - 1; + tag_output_len = tag_end_pos - tag_start_pos + 1; + if (j + tag_output_len >= buf_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; + new_line = 1; + } + else if (str_in[i] == '\r') + { + continue; // ignore '\r' + } + + if (!lml_tag_disabled && str_in[i] == '[') + { + tag_start_pos = i; + tag_name_pos = i + 1; + } + else if (!lml_tag_disabled && str_in[i] == ']') + { + if (tag_name_pos >= 0) { tag_end_pos = i; // Skip space characters - while (str_in[tag_start_pos] == ' ') + while (str_in[tag_name_pos] == ' ') { - tag_start_pos++; + tag_name_pos++; } - for (k = 0; k < LML_TAG_COUNT; k++) + for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++) { - if (strncasecmp(LML_tag_def[k][0], 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) { tag_param_pos = -1; - switch (str_in[tag_start_pos + LML_tag_name_len[k]]) + switch (str_in[tag_name_pos + lml_tag_name_len[k]]) { case ' ': - tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1; + tag_name_found = 1; + tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; while (str_in[tag_param_pos] == ' ') { tag_param_pos++; @@ -162,23 +325,47 @@ int lml_plain(const char *str_in, char * strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; case ']': - if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined + tag_name_found = 1; + 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 { - strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1); + strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; } - if (LML_tag_def[k][1] != NULL) + if (!quote_mode) { - tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf); + if (lml_tag_def[k].tag_output != NULL) + { + 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; + } } - else + else // if (quote_mode) { - 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 (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); + } + 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; + } } - 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; } @@ -192,30 +379,51 @@ int lml_plain(const char *str_in, char * } } + if (!tag_name_found) + { + tag_output_len = tag_end_pos - tag_start_pos + 1; + if (j + tag_output_len >= buf_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; } } - else if (tag_start_pos == -1) // not in LML tag + else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag { - if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character + if (str_in[i] & 0x80) // head of multi-byte character { - if (j + 2 >= buf_len - 1) + 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 + 2, buf_len - 1); + log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len); str_out[j] = '\0'; return j; } - str_out[j++] = str_in[i++]; - if (str_in[i] == '\0') + + c = (str_in[i] & 0x70) << 1; + while (c & 0x80) { - str_out[j] = '\0'; - return j; + str_out[j++] = str_in[i++]; + if (str_in[i] == '\0') + { + str_out[j] = '\0'; + return j; + } + c = (c & 0x7f) << 1; } } - 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; } @@ -227,7 +435,35 @@ int lml_plain(const char *str_in, char * } } + if (tag_start_pos != -1) // tag is not closed + { + tag_end_pos = i - 1; + tag_output_len = tag_end_pos - tag_start_pos + 1; + if (j + tag_output_len >= buf_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; + } + + 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"); + 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; + } + str_out[j] = '\0'; return j; -} \ No newline at end of file +}