| 26 |
|
|
| 27 |
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); |
| 28 |
|
|
| 29 |
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) |
| 30 |
{ |
{ |
| 31 |
if (strcasecmp(tag_name, "color") == 0) |
if (strcasecmp(tag_name, "color") == 0) |
| 32 |
{ |
{ |
| 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 |
const static char *lml_tag_def[][4] = { |
typedef struct lml_tag_def_t |
| 110 |
// Definition of tuple: {lml_tag, lml_output, default_param | lml_filter_cb, quote_mode_output} |
{ |
| 111 |
{"plain", NULL, (const char *)lml_tag_disable_filter, NULL}, |
const char *tag_name; // tag name |
| 112 |
{"left", "[", "", "[left]"}, |
const char *tag_output; // output string |
| 113 |
{"right", "]", "", "[right]"}, |
const char *default_param; // default param string |
| 114 |
{"bold", "\033[1m", "", ""}, // does not work in Fterm |
const char *quote_mode_output; // output string in quote mode |
| 115 |
{"/bold", "\033[22m", NULL, ""}, |
lml_tag_filter_cb tag_filter_cb; // tag filter callback |
| 116 |
{"b", "\033[1m", "", ""}, |
} LML_TAG_DEF; |
| 117 |
{"/b", "\033[22m", NULL, ""}, |
|
| 118 |
{"italic", "\033[5m", "", ""}, // use blink instead |
const LML_TAG_DEF lml_tag_def[] = { |
| 119 |
{"/italic", "\033[m", NULL, ""}, // \033[25m does not work in Fterm |
// Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb} |
| 120 |
{"i", "\033[5m", "", ""}, |
{"plain", NULL, NULL, NULL, lml_tag_disable_filter}, |
| 121 |
{"/i", "\033[m", NULL, ""}, |
{"left", "[", "", "[left]", NULL}, |
| 122 |
{"underline", "\033[4m", "", ""}, |
{"right", "]", "", "[right]", NULL}, |
| 123 |
{"/underline", "\033[m", NULL, ""}, // \033[24m does not work in Fterm |
{"bold", "\033[1m", "", "", NULL}, // does not work in Fterm |
| 124 |
{"u", "\033[4m", "", ""}, |
{"/bold", "\033[22m", NULL, "", NULL}, |
| 125 |
{"/u", "\033[m", NULL, ""}, |
{"b", "\033[1m", "", "", NULL}, |
| 126 |
{"color", NULL, (const char *)lml_tag_color_filter, ""}, |
{"/b", "\033[22m", NULL, "", NULL}, |
| 127 |
{"/color", "\033[m", NULL, ""}, |
{"italic", "\033[5m", "", "", NULL}, // use blink instead |
| 128 |
{"quote", NULL, (const char *)lml_tag_quote_filter, ""}, |
{"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm |
| 129 |
{"/quote", NULL, (const char *)lml_tag_quote_filter, ""}, |
{"i", "\033[5m", "", "", NULL}, |
| 130 |
{"url", "", "", ""}, |
{"/i", "\033[m", NULL, "", NULL}, |
| 131 |
{"/url", "(链接: %s)", NULL, ""}, |
{"underline", "\033[4m", "", "", NULL}, |
| 132 |
{"link", "", "", ""}, |
{"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm |
| 133 |
{"/link", "(链接: %s)", NULL, ""}, |
{"u", "\033[4m", "", "", NULL}, |
| 134 |
{"email", "", "", ""}, |
{"/u", "\033[m", NULL, "", NULL}, |
| 135 |
{"/email", "(Email: %s)", NULL, ""}, |
{"color", NULL, NULL, "", lml_tag_color_filter}, |
| 136 |
{"user", "", "", ""}, |
{"/color", "\033[m", NULL, "", NULL}, |
| 137 |
{"/user", "(用户: %s)", NULL, ""}, |
{"quote", NULL, NULL, "", lml_tag_quote_filter}, |
| 138 |
{"article", "", "", ""}, |
{"/quote", NULL, NULL, "", lml_tag_quote_filter}, |
| 139 |
{"/article", "(文章: %s)", NULL, ""}, |
{"url", "", "", "", NULL}, |
| 140 |
{"image", "(图片: %s)", "", "%s"}, |
{"/url", "(链接: %s)", NULL, "", NULL}, |
| 141 |
{"flash", "(Flash: %s)", "", ""}, |
{"link", "", "", "", NULL}, |
| 142 |
{"bwf", "\033[1;31m****\033[m", "", "****"}, |
{"/link", "(链接: %s)", NULL, "", NULL}, |
| 143 |
|
{"email", "", "", "", NULL}, |
| 144 |
|
{"/email", "(Email: %s)", NULL, "", NULL}, |
| 145 |
|
{"user", "", "", "", NULL}, |
| 146 |
|
{"/user", "(用户: %s)", NULL, "", NULL}, |
| 147 |
|
{"article", "", "", "", NULL}, |
| 148 |
|
{"/article", "(文章: %s)", NULL, "", NULL}, |
| 149 |
|
{"image", "(图片: %s)", "", "%s", NULL}, |
| 150 |
|
{"flash", "(Flash: %s)", "", "", NULL}, |
| 151 |
|
{"bwf", "\033[1;31m****\033[m", "", "****", NULL}, |
| 152 |
}; |
}; |
| 153 |
|
|
| 154 |
#define LML_TAG_COUNT 32 |
#define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF)) |
| 155 |
|
|
| 156 |
static int lml_tag_name_len[LML_TAG_COUNT]; |
static int lml_tag_name_len[LML_TAG_COUNT]; |
| 157 |
static int lml_ready = 0; |
static int lml_ready = 0; |
| 164 |
{ |
{ |
| 165 |
for (i = 0; i < LML_TAG_COUNT; i++) |
for (i = 0; i < LML_TAG_COUNT; i++) |
| 166 |
{ |
{ |
| 167 |
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); |
| 168 |
} |
} |
| 169 |
|
|
| 170 |
lml_ready = 1; |
lml_ready = 1; |
| 193 |
|
|
| 194 |
for (i = 0; str_in[i] != '\0'; i++) |
for (i = 0; str_in[i] != '\0'; i++) |
| 195 |
{ |
{ |
| 196 |
if (quote_mode && !lml_tag_disabled && new_line) |
if (!quote_mode && !lml_tag_disabled && new_line) |
| 197 |
{ |
{ |
| 198 |
if (fb_quote_level > 0) |
if (fb_quote_level > 0) |
| 199 |
{ |
{ |
| 289 |
|
|
| 290 |
for (k = 0; k < LML_TAG_COUNT; k++) |
for (k = 0; k < LML_TAG_COUNT; k++) |
| 291 |
{ |
{ |
| 292 |
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_start_pos, (size_t)lml_tag_name_len[k]) == 0) |
| 293 |
{ |
{ |
| 294 |
tag_param_pos = -1; |
tag_param_pos = -1; |
| 295 |
switch (str_in[tag_start_pos + lml_tag_name_len[k]]) |
switch (str_in[tag_start_pos + lml_tag_name_len[k]]) |
| 303 |
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)); |
| 304 |
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'; |
| 305 |
case ']': |
case ']': |
| 306 |
if (tag_param_pos == -1 && lml_tag_def[k][1] != NULL && lml_tag_def[k][2] != 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 |
| 307 |
{ |
{ |
| 308 |
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); |
| 309 |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 310 |
} |
} |
| 311 |
if (quote_mode) |
if (!quote_mode) |
| 312 |
{ |
{ |
| 313 |
if (lml_tag_def[k][1] != NULL) |
if (lml_tag_def[k].tag_output != NULL) |
| 314 |
|
{ |
| 315 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf); |
| 316 |
|
} |
| 317 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 318 |
{ |
{ |
| 319 |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k][1], tag_param_buf); |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 320 |
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0); |
| 321 |
} |
} |
| 322 |
else |
else |
| 323 |
{ |
{ |
| 324 |
tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])( |
tag_output_len = 0; |
|
lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode); |
|
| 325 |
} |
} |
| 326 |
} |
} |
| 327 |
else |
else // quote mode |
| 328 |
{ |
{ |
| 329 |
if (lml_tag_def[k][3] != NULL) |
if (lml_tag_def[k].quote_mode_output != NULL) |
| 330 |
|
{ |
| 331 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf); |
| 332 |
|
} |
| 333 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 334 |
{ |
{ |
| 335 |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k][3], tag_param_buf); |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 336 |
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); |
| 337 |
} |
} |
| 338 |
else |
else |
| 339 |
{ |
{ |
| 340 |
tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])( |
tag_output_len = 0; |
|
lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, quote_mode); |
|
| 341 |
} |
} |
| 342 |
} |
} |
| 343 |
if (j + tag_output_len >= buf_len) |
if (j + tag_output_len >= buf_len) |
| 361 |
} |
} |
| 362 |
else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag |
else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag |
| 363 |
{ |
{ |
| 364 |
if (str_in[i] & 0b10000000) // head of multi-byte character |
if (str_in[i] & 0x80) // head of multi-byte character |
| 365 |
{ |
{ |
| 366 |
if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes |
if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes |
| 367 |
{ |
{ |
| 370 |
return j; |
return j; |
| 371 |
} |
} |
| 372 |
|
|
| 373 |
c = (str_in[i] & 0b01110000) << 1; |
c = (str_in[i] & 0x70) << 1; |
| 374 |
while (c & 0b10000000) |
while (c & 0x80) |
| 375 |
{ |
{ |
| 376 |
str_out[j++] = str_in[i++]; |
str_out[j++] = str_in[i++]; |
| 377 |
if (str_in[i] == '\0') |
if (str_in[i] == '\0') |
| 379 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 380 |
return j; |
return j; |
| 381 |
} |
} |
| 382 |
c = (c & 0b01111111) << 1; |
c = (c & 0x7f) << 1; |
| 383 |
} |
} |
| 384 |
} |
} |
| 385 |
|
|
| 397 |
} |
} |
| 398 |
} |
} |
| 399 |
|
|
| 400 |
if (quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
| 401 |
{ |
{ |
| 402 |
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"); |
| 403 |
if (j + tag_output_len >= buf_len) |
if (j + tag_output_len >= buf_len) |