| 24 |
#define LML_TAG_PARAM_BUF_LEN 256 |
#define LML_TAG_PARAM_BUF_LEN 256 |
| 25 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
| 26 |
|
|
| 27 |
#define LML_TAG_COUNT 19 |
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); |
| 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) |
| 30 |
|
{ |
| 31 |
|
if (strcasecmp(tag_name, "color") == 0) |
| 32 |
|
{ |
| 33 |
|
if (strcasecmp(tag_param_buf, "red") == 0) |
| 34 |
|
{ |
| 35 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;31m"); |
| 36 |
|
} |
| 37 |
|
else if (strcasecmp(tag_param_buf, "green") == 0) |
| 38 |
|
{ |
| 39 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;32m"); |
| 40 |
|
} |
| 41 |
|
else if (strcasecmp(tag_param_buf, "yellow") == 0) |
| 42 |
|
{ |
| 43 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;33m"); |
| 44 |
|
} |
| 45 |
|
else if (strcasecmp(tag_param_buf, "blue") == 0) |
| 46 |
|
{ |
| 47 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;34m"); |
| 48 |
|
} |
| 49 |
|
else if (strcasecmp(tag_param_buf, "magenta") == 0) |
| 50 |
|
{ |
| 51 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;35m"); |
| 52 |
|
} |
| 53 |
|
else if (strcasecmp(tag_param_buf, "cyan") == 0) |
| 54 |
|
{ |
| 55 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;36m"); |
| 56 |
|
} |
| 57 |
|
else if (strcasecmp(tag_param_buf, "black") == 0) |
| 58 |
|
{ |
| 59 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;30;47m"); |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
|
return 0; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
#define LML_TAG_QUOTE_MAX_LEVEL 10 |
| 66 |
|
#define LML_TAG_QUOTE_LEVEL_LOOP 3 |
| 67 |
|
|
| 68 |
|
static const char *lml_tag_quote_color[] = { |
| 69 |
|
"\033[33m", // yellow |
| 70 |
|
"\033[32m", // green |
| 71 |
|
"\033[35m", // magenta |
| 72 |
|
}; |
| 73 |
|
|
| 74 |
|
static int lml_tag_quote_level = 0; |
| 75 |
|
|
| 76 |
|
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) |
| 77 |
|
{ |
| 78 |
|
if (strcasecmp(tag_name, "quote") == 0) |
| 79 |
|
{ |
| 80 |
|
if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL) |
| 81 |
|
{ |
| 82 |
|
lml_tag_quote_level++; |
| 83 |
|
} |
| 84 |
|
return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]); |
| 85 |
|
} |
| 86 |
|
else if (strcasecmp(tag_name, "/quote") == 0) |
| 87 |
|
{ |
| 88 |
|
if (lml_tag_quote_level > 0) |
| 89 |
|
{ |
| 90 |
|
lml_tag_quote_level--; |
| 91 |
|
} |
| 92 |
|
return snprintf(tag_output_buf, tag_output_buf_len, |
| 93 |
|
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
return 0; |
| 97 |
|
} |
| 98 |
|
|
| 99 |
const static char *LML_tag_def[][3] = { |
const static char *LML_tag_def[][3] = { |
| 100 |
{"left", "[", ""}, |
{"left", "[", ""}, |
| 101 |
{"right", "]", NULL}, |
{"right", "]", NULL}, |
| 102 |
|
{"bold", "\033[1m", ""}, // does not work in Fterm |
| 103 |
|
{"/bold", "\033[22m", NULL}, |
| 104 |
|
{"b", "\033[1m", ""}, |
| 105 |
|
{"/b", "\033[22m", NULL}, |
| 106 |
|
{"italic", "\033[5m", ""}, // use blink instead |
| 107 |
|
{"/italic", "\033[m", NULL}, // \033[25m does not work in Fterm |
| 108 |
|
{"i", "\033[5m", ""}, |
| 109 |
|
{"/i", "\033[m", NULL}, |
| 110 |
{"underline", "\033[4m", ""}, |
{"underline", "\033[4m", ""}, |
| 111 |
{"/underline", "\033[24m", NULL}, |
{"/underline", "\033[m", NULL}, // \033[24m does not work in Fterm |
| 112 |
{"u", "\033[4m", ""}, |
{"u", "\033[4m", ""}, |
| 113 |
{"/u", "\033[24m", NULL}, |
{"/u", "\033[m", NULL}, |
| 114 |
|
{"color", NULL, (const char *)lml_tag_color_filter}, |
| 115 |
|
{"/color", "\033[m", NULL}, |
| 116 |
|
{"quote", NULL, (const char *)lml_tag_quote_filter}, |
| 117 |
|
{"/quote", NULL, (const char *)lml_tag_quote_filter}, |
| 118 |
{"url", "", ""}, |
{"url", "", ""}, |
| 119 |
{"/url", "(Á´½Ó: %s)", NULL}, |
{"/url", "(Á´½Ó: %s)", NULL}, |
| 120 |
{"link", "", ""}, |
{"link", "", ""}, |
| 127 |
{"/article", "(ÎÄÕÂ: %s)", NULL}, |
{"/article", "(ÎÄÕÂ: %s)", NULL}, |
| 128 |
{"image", "(ͼƬ: %s)", ""}, |
{"image", "(ͼƬ: %s)", ""}, |
| 129 |
{"flash", "(Flash: %s)", ""}, |
{"flash", "(Flash: %s)", ""}, |
| 130 |
{"bwf", "\033[31m****\033[m", ""}, |
{"bwf", "\033[1;31m****\033[m", ""}, |
| 131 |
}; |
}; |
| 132 |
|
|
| 133 |
|
#define LML_TAG_COUNT 31 |
| 134 |
|
|
| 135 |
static int LML_tag_name_len[LML_TAG_COUNT]; |
static int LML_tag_name_len[LML_TAG_COUNT]; |
| 136 |
static int LML_init = 0; |
static int LML_init = 0; |
| 137 |
|
|
| 161 |
int tag_end_pos = -1; |
int tag_end_pos = -1; |
| 162 |
int tag_param_pos = -1; |
int tag_param_pos = -1; |
| 163 |
int tag_output_len; |
int tag_output_len; |
| 164 |
|
int new_line = 1; |
| 165 |
|
int fb_quote_level = 0; |
| 166 |
|
|
| 167 |
lml_init(); |
lml_init(); |
| 168 |
|
|
| 169 |
|
lml_tag_quote_level = 0; |
| 170 |
|
|
| 171 |
for (i = 0; str_in[i] != '\0'; i++) |
for (i = 0; str_in[i] != '\0'; i++) |
| 172 |
{ |
{ |
| 173 |
|
if (new_line) |
| 174 |
|
{ |
| 175 |
|
if (fb_quote_level > 0) |
| 176 |
|
{ |
| 177 |
|
lml_tag_quote_level -= fb_quote_level; |
| 178 |
|
|
| 179 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, |
| 180 |
|
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 181 |
|
if (j + tag_output_len >= buf_len - 1) |
| 182 |
|
{ |
| 183 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1); |
| 184 |
|
str_out[j] = '\0'; |
| 185 |
|
return j; |
| 186 |
|
} |
| 187 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 188 |
|
j += tag_output_len; |
| 189 |
|
|
| 190 |
|
fb_quote_level = 0; |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str |
| 194 |
|
{ |
| 195 |
|
fb_quote_level++; |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
if (fb_quote_level > 0) |
| 199 |
|
{ |
| 200 |
|
lml_tag_quote_level += fb_quote_level; |
| 201 |
|
|
| 202 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, |
| 203 |
|
lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]); |
| 204 |
|
if (j + tag_output_len >= buf_len - 1) |
| 205 |
|
{ |
| 206 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1); |
| 207 |
|
str_out[j] = '\0'; |
| 208 |
|
return j; |
| 209 |
|
} |
| 210 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 211 |
|
j += tag_output_len; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
new_line = 0; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
if (str_in[i] == '\n') |
| 218 |
|
{ |
| 219 |
|
tag_start_pos = -1; // jump out of tag at end of line |
| 220 |
|
new_line = 1; |
| 221 |
|
} |
| 222 |
|
else if (str_in[i] == '\r') |
| 223 |
|
{ |
| 224 |
|
continue; // ignore '\r' |
| 225 |
|
} |
| 226 |
|
|
| 227 |
if (str_in[i] == '[') |
if (str_in[i] == '[') |
| 228 |
{ |
{ |
| 229 |
tag_start_pos = i + 1; |
tag_start_pos = i + 1; |
| 256 |
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)); |
| 257 |
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'; |
| 258 |
case ']': |
case ']': |
| 259 |
if (tag_param_pos == -1 && LML_tag_def[k][2] != NULL) // Apply default param if not defined |
if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined |
| 260 |
{ |
{ |
| 261 |
strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1); |
strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1); |
| 262 |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 263 |
} |
} |
| 264 |
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][1] != NULL) |
| 265 |
|
{ |
| 266 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf); |
| 267 |
|
} |
| 268 |
|
else |
| 269 |
|
{ |
| 270 |
|
tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])( |
| 271 |
|
LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
| 272 |
|
} |
| 273 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len - 1) |
| 274 |
{ |
{ |
| 275 |
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 - 1); |
| 321 |
} |
} |
| 322 |
} |
} |
| 323 |
|
|
| 324 |
|
if (lml_tag_quote_level > 0) |
| 325 |
|
{ |
| 326 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); |
| 327 |
|
if (j + tag_output_len >= buf_len - 1) |
| 328 |
|
{ |
| 329 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1); |
| 330 |
|
str_out[j] = '\0'; |
| 331 |
|
return j; |
| 332 |
|
} |
| 333 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 334 |
|
j += tag_output_len; |
| 335 |
|
} |
| 336 |
|
|
| 337 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 338 |
|
|
| 339 |
return j; |
return j; |
| 340 |
} |
} |