| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#include "common.h" |
| 18 |
#include "lml.h" |
#include "lml.h" |
| 19 |
#include "log.h" |
#include "log.h" |
|
#include "common.h" |
|
| 20 |
#include <stdio.h> |
#include <stdio.h> |
| 21 |
#include <string.h> |
#include <string.h> |
| 22 |
#include <sys/param.h> |
#include <sys/param.h> |
| 62 |
return 0; |
return 0; |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
const static char *LML_tag_def[][3] = { |
#define LML_TAG_QUOTE_MAX_LEVEL 10 |
| 66 |
{"left", "[", ""}, |
#define LML_TAG_QUOTE_LEVEL_LOOP 3 |
| 67 |
{"right", "]", NULL}, |
|
| 68 |
{"bold", "\033[1m", ""}, |
static const char *lml_tag_quote_color[] = { |
| 69 |
{"/bold", "\033[21m", NULL}, |
"\033[33m", // yellow |
| 70 |
{"b", "\033[1m", ""}, |
"\033[32m", // green |
| 71 |
{"/b", "\033[21m", NULL}, |
"\033[35m", // magenta |
| 72 |
{"italic", "\033[3m", ""}, |
}; |
| 73 |
{"/italic", "\033[23m", NULL}, |
|
| 74 |
{"i", "\033[3m", ""}, |
static int lml_tag_quote_level = 0; |
| 75 |
{"/i", "\033[23m", NULL}, |
|
| 76 |
{"underline", "\033[4m", ""}, |
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 |
{"/underline", "\033[24m", NULL}, |
{ |
| 78 |
{"u", "\033[4m", ""}, |
if (strcasecmp(tag_name, "quote") == 0) |
| 79 |
{"/u", "\033[24m", NULL}, |
{ |
| 80 |
{"color", NULL, (const char *)lml_tag_color_filter}, |
if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL) |
| 81 |
{"/color", "\033[m", NULL}, |
{ |
| 82 |
{"url", "", ""}, |
lml_tag_quote_level++; |
| 83 |
{"/url", "(Á´½Ó: %s)", NULL}, |
} |
| 84 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "%s", |
| 85 |
|
lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]); |
| 86 |
|
} |
| 87 |
|
else if (strcasecmp(tag_name, "/quote") == 0) |
| 88 |
|
{ |
| 89 |
|
if (lml_tag_quote_level > 0) |
| 90 |
|
{ |
| 91 |
|
lml_tag_quote_level--; |
| 92 |
|
} |
| 93 |
|
return snprintf(tag_output_buf, tag_output_buf_len, "%s", |
| 94 |
|
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
return 0; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
const static char *lml_tag_def[][4] = { |
| 101 |
|
// Definition of tuple: {lml_tag, lml_output, default_param | lml_filter_cb, no_lml_output} |
| 102 |
|
{"left", "[", "", "[left]"}, |
| 103 |
|
{"right", "]", "", "[right]"}, |
| 104 |
|
{"bold", "\033[1m", "", ""}, // does not work in Fterm |
| 105 |
|
{"/bold", "\033[22m", NULL, ""}, |
| 106 |
|
{"b", "\033[1m", "", ""}, |
| 107 |
|
{"/b", "\033[22m", NULL, ""}, |
| 108 |
|
{"italic", "\033[5m", "", ""}, // use blink instead |
| 109 |
|
{"/italic", "\033[m", NULL, ""}, // \033[25m does not work in Fterm |
| 110 |
|
{"i", "\033[5m", "", ""}, |
| 111 |
|
{"/i", "\033[m", NULL, ""}, |
| 112 |
|
{"underline", "\033[4m", "", ""}, |
| 113 |
|
{"/underline", "\033[m", NULL, ""}, // \033[24m does not work in Fterm |
| 114 |
|
{"u", "\033[4m", "", ""}, |
| 115 |
|
{"/u", "\033[m", NULL, ""}, |
| 116 |
|
{"color", NULL, (const char *)lml_tag_color_filter, ""}, |
| 117 |
|
{"/color", "\033[m", NULL, ""}, |
| 118 |
|
{"quote", NULL, (const char *)lml_tag_quote_filter, ""}, |
| 119 |
|
{"/quote", NULL, (const char *)lml_tag_quote_filter, ""}, |
| 120 |
|
{"url", "", "", ""}, |
| 121 |
|
{"/url", "(链接: %s)", NULL, ""}, |
| 122 |
{"link", "", ""}, |
{"link", "", ""}, |
| 123 |
{"/link", "(Á´½Ó: %s)", NULL}, |
{"/link", "(链接: %s)", NULL, ""}, |
| 124 |
{"email", "", ""}, |
{"email", "", ""}, |
| 125 |
{"/email", "(Email: %s)", NULL}, |
{"/email", "(Email: %s)", NULL, ""}, |
| 126 |
{"user", "", ""}, |
{"user", "", ""}, |
| 127 |
{"/user", "(Óû§: %s)", NULL}, |
{"/user", "(用户: %s)", NULL, ""}, |
| 128 |
{"article", "", ""}, |
{"article", "", ""}, |
| 129 |
{"/article", "(ÎÄÕÂ: %s)", NULL}, |
{"/article", "ï¼ˆæ–‡ç« : %s)", NULL, ""}, |
| 130 |
{"image", "(ͼƬ: %s)", ""}, |
{"image", "(图片: %s)", "", "%s"}, |
| 131 |
{"flash", "(Flash: %s)", ""}, |
{"flash", "(Flash: %s)", "", ""}, |
| 132 |
{"bwf", "\033[1;31m****\033[m", ""}, |
{"bwf", "\033[1;31m****\033[m", "", "****"}, |
| 133 |
}; |
}; |
| 134 |
|
|
| 135 |
#define LML_TAG_COUNT 29 |
#define LML_TAG_COUNT 31 |
| 136 |
|
|
| 137 |
static int LML_tag_name_len[LML_TAG_COUNT]; |
static int lml_tag_name_len[LML_TAG_COUNT]; |
| 138 |
static int LML_init = 0; |
static int lml_ready = 0; |
| 139 |
|
|
| 140 |
inline static void lml_init(void) |
inline static void lml_init(void) |
| 141 |
{ |
{ |
| 142 |
int i; |
int i; |
| 143 |
|
|
| 144 |
if (!LML_init) |
if (!lml_ready) |
| 145 |
{ |
{ |
| 146 |
for (i = 0; i < LML_TAG_COUNT; i++) |
for (i = 0; i < LML_TAG_COUNT; i++) |
| 147 |
{ |
{ |
| 148 |
LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]); |
lml_tag_name_len[i] = (int)strlen(lml_tag_def[i][0]); |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
LML_init = 1; |
lml_ready = 1; |
| 152 |
} |
} |
| 153 |
} |
} |
| 154 |
|
|
| 155 |
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 lml_tag) |
| 156 |
{ |
{ |
| 157 |
|
char c; |
| 158 |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 159 |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
| 160 |
int i; |
int i; |
| 164 |
int tag_end_pos = -1; |
int tag_end_pos = -1; |
| 165 |
int tag_param_pos = -1; |
int tag_param_pos = -1; |
| 166 |
int tag_output_len; |
int tag_output_len; |
| 167 |
|
int new_line = 1; |
| 168 |
|
int fb_quote_level = 0; |
| 169 |
|
|
| 170 |
lml_init(); |
lml_init(); |
| 171 |
|
|
| 172 |
|
lml_tag_quote_level = 0; |
| 173 |
|
|
| 174 |
for (i = 0; str_in[i] != '\0'; i++) |
for (i = 0; str_in[i] != '\0'; i++) |
| 175 |
{ |
{ |
| 176 |
|
if (lml_tag && new_line) |
| 177 |
|
{ |
| 178 |
|
if (fb_quote_level > 0) |
| 179 |
|
{ |
| 180 |
|
lml_tag_quote_level -= fb_quote_level; |
| 181 |
|
|
| 182 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 183 |
|
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 184 |
|
if (j + tag_output_len >= buf_len) |
| 185 |
|
{ |
| 186 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 187 |
|
str_out[j] = '\0'; |
| 188 |
|
return j; |
| 189 |
|
} |
| 190 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 191 |
|
j += tag_output_len; |
| 192 |
|
|
| 193 |
|
fb_quote_level = 0; |
| 194 |
|
} |
| 195 |
|
|
| 196 |
|
while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str |
| 197 |
|
{ |
| 198 |
|
fb_quote_level++; |
| 199 |
|
} |
| 200 |
|
|
| 201 |
|
if (fb_quote_level > 0) |
| 202 |
|
{ |
| 203 |
|
lml_tag_quote_level += fb_quote_level; |
| 204 |
|
|
| 205 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 206 |
|
lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]); |
| 207 |
|
if (j + tag_output_len >= buf_len) |
| 208 |
|
{ |
| 209 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 210 |
|
str_out[j] = '\0'; |
| 211 |
|
return j; |
| 212 |
|
} |
| 213 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 214 |
|
j += tag_output_len; |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
new_line = 0; |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
| 221 |
|
{ |
| 222 |
|
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++) |
| 223 |
|
; |
| 224 |
|
|
| 225 |
|
if (str_in[k] == 'm') // Valid |
| 226 |
|
{ |
| 227 |
|
if (j + (k - i + 1) >= buf_len) |
| 228 |
|
{ |
| 229 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len); |
| 230 |
|
str_out[j] = '\0'; |
| 231 |
|
return j; |
| 232 |
|
} |
| 233 |
|
memcpy(str_out + j, str_in + i, (size_t)(k - i + 1)); |
| 234 |
|
j += (k - i + 1); |
| 235 |
|
i = k; |
| 236 |
|
continue; |
| 237 |
|
} |
| 238 |
|
else // reach end of string |
| 239 |
|
{ |
| 240 |
|
break; |
| 241 |
|
} |
| 242 |
|
} |
| 243 |
|
|
| 244 |
|
if (str_in[i] == '\n') |
| 245 |
|
{ |
| 246 |
|
tag_start_pos = -1; // jump out of tag at end of line |
| 247 |
|
new_line = 1; |
| 248 |
|
} |
| 249 |
|
else if (str_in[i] == '\r') |
| 250 |
|
{ |
| 251 |
|
continue; // ignore '\r' |
| 252 |
|
} |
| 253 |
|
|
| 254 |
if (str_in[i] == '[') |
if (str_in[i] == '[') |
| 255 |
{ |
{ |
| 256 |
tag_start_pos = i + 1; |
tag_start_pos = i + 1; |
| 269 |
|
|
| 270 |
for (k = 0; k < LML_TAG_COUNT; k++) |
for (k = 0; k < LML_TAG_COUNT; k++) |
| 271 |
{ |
{ |
| 272 |
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][0], str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0) |
| 273 |
{ |
{ |
| 274 |
tag_param_pos = -1; |
tag_param_pos = -1; |
| 275 |
switch (str_in[tag_start_pos + LML_tag_name_len[k]]) |
switch (str_in[tag_start_pos + lml_tag_name_len[k]]) |
| 276 |
{ |
{ |
| 277 |
case ' ': |
case ' ': |
| 278 |
tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1; |
tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1; |
| 279 |
while (str_in[tag_param_pos] == ' ') |
while (str_in[tag_param_pos] == ' ') |
| 280 |
{ |
{ |
| 281 |
tag_param_pos++; |
tag_param_pos++; |
| 283 |
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)); |
| 284 |
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'; |
| 285 |
case ']': |
case ']': |
| 286 |
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][1] != NULL && lml_tag_def[k][2] != NULL) // Apply default param if not defined |
| 287 |
{ |
{ |
| 288 |
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); |
| 289 |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 290 |
} |
} |
| 291 |
if (LML_tag_def[k][1] != NULL) |
if (lml_tag) |
| 292 |
{ |
{ |
| 293 |
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) |
| 294 |
|
{ |
| 295 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k][1], tag_param_buf); |
| 296 |
|
} |
| 297 |
|
else |
| 298 |
|
{ |
| 299 |
|
tag_output_len = ((lml_tag_filter_cb)lml_tag_def[k][2])( |
| 300 |
|
lml_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
| 301 |
|
} |
| 302 |
} |
} |
| 303 |
else |
else |
| 304 |
{ |
{ |
| 305 |
tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])( |
if (lml_tag_def[k][3] != NULL) |
| 306 |
LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
{ |
| 307 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k][3], tag_param_buf); |
| 308 |
|
} |
| 309 |
|
else |
| 310 |
|
{ |
| 311 |
|
tag_output_len = 0; |
| 312 |
|
} |
| 313 |
} |
} |
| 314 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len) |
| 315 |
{ |
{ |
| 316 |
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); |
| 317 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 318 |
return j; |
return j; |
| 319 |
} |
} |
| 332 |
} |
} |
| 333 |
else if (tag_start_pos == -1) // not in LML tag |
else if (tag_start_pos == -1) // not in LML tag |
| 334 |
{ |
{ |
| 335 |
if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character |
if (str_in[i] & 0b10000000) // head of multi-byte character |
| 336 |
{ |
{ |
| 337 |
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 |
| 338 |
{ |
{ |
| 339 |
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); |
| 340 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 341 |
return j; |
return j; |
| 342 |
} |
} |
| 343 |
str_out[j++] = str_in[i++]; |
|
| 344 |
if (str_in[i] == '\0') |
c = (str_in[i] & 0b01110000) << 1; |
| 345 |
|
while (c & 0b10000000) |
| 346 |
{ |
{ |
| 347 |
str_out[j] = '\0'; |
str_out[j++] = str_in[i++]; |
| 348 |
return j; |
if (str_in[i] == '\0') |
| 349 |
|
{ |
| 350 |
|
str_out[j] = '\0'; |
| 351 |
|
return j; |
| 352 |
|
} |
| 353 |
|
c = (c & 0b01111111) << 1; |
| 354 |
} |
} |
| 355 |
} |
} |
| 356 |
|
|
| 357 |
if (j + 1 >= buf_len - 1) |
if (j + 1 >= buf_len) |
| 358 |
{ |
{ |
| 359 |
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); |
| 360 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 361 |
return j; |
return j; |
| 362 |
} |
} |
| 368 |
} |
} |
| 369 |
} |
} |
| 370 |
|
|
| 371 |
|
if (lml_tag && lml_tag_quote_level > 0) |
| 372 |
|
{ |
| 373 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); |
| 374 |
|
if (j + tag_output_len >= buf_len) |
| 375 |
|
{ |
| 376 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 377 |
|
str_out[j] = '\0'; |
| 378 |
|
return j; |
| 379 |
|
} |
| 380 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 381 |
|
j += tag_output_len; |
| 382 |
|
} |
| 383 |
|
|
| 384 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 385 |
|
|
| 386 |
return j; |
return j; |
| 387 |
} |
} |