| 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> |
| 81 |
{ |
{ |
| 82 |
lml_tag_quote_level++; |
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]); |
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) |
else if (strcasecmp(tag_name, "/quote") == 0) |
| 88 |
{ |
{ |
| 90 |
{ |
{ |
| 91 |
lml_tag_quote_level--; |
lml_tag_quote_level--; |
| 92 |
} |
} |
| 93 |
return snprintf(tag_output_buf, tag_output_buf_len, |
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")); |
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 95 |
} |
} |
| 96 |
|
|
| 117 |
{"quote", NULL, (const char *)lml_tag_quote_filter}, |
{"quote", NULL, (const char *)lml_tag_quote_filter}, |
| 118 |
{"/quote", NULL, (const char *)lml_tag_quote_filter}, |
{"/quote", NULL, (const char *)lml_tag_quote_filter}, |
| 119 |
{"url", "", ""}, |
{"url", "", ""}, |
| 120 |
{"/url", "(链接: %s)", NULL}, |
{"/url", "(閾炬帴: %s)", NULL}, |
| 121 |
{"link", "", ""}, |
{"link", "", ""}, |
| 122 |
{"/link", "(链接: %s)", NULL}, |
{"/link", "(閾炬帴: %s)", NULL}, |
| 123 |
{"email", "", ""}, |
{"email", "", ""}, |
| 124 |
{"/email", "(Email: %s)", NULL}, |
{"/email", "(Email: %s)", NULL}, |
| 125 |
{"user", "", ""}, |
{"user", "", ""}, |
| 126 |
{"/user", "(用户: %s)", NULL}, |
{"/user", "(鐢ㄦ埛: %s)", NULL}, |
| 127 |
{"article", "", ""}, |
{"article", "", ""}, |
| 128 |
{"/article", "(文章: %s)", NULL}, |
{"/article", "(鏂囩珷: %s)", NULL}, |
| 129 |
{"image", "(图片: %s)", ""}, |
{"image", "(鍥剧墖: %s)", ""}, |
| 130 |
{"flash", "(Flash: %s)", ""}, |
{"flash", "(Flash: %s)", ""}, |
| 131 |
{"bwf", "\033[1;31m****\033[m", ""}, |
{"bwf", "\033[1;31m****\033[m", ""}, |
| 132 |
}; |
}; |
| 153 |
|
|
| 154 |
int lml_plain(const char *str_in, char *str_out, int buf_len) |
int lml_plain(const char *str_in, char *str_out, int buf_len) |
| 155 |
{ |
{ |
| 156 |
|
char c; |
| 157 |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 158 |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
| 159 |
int i; |
int i; |
| 178 |
{ |
{ |
| 179 |
lml_tag_quote_level -= fb_quote_level; |
lml_tag_quote_level -= fb_quote_level; |
| 180 |
|
|
| 181 |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 182 |
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 183 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len) |
| 184 |
{ |
{ |
| 185 |
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); |
| 186 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 187 |
return j; |
return j; |
| 188 |
} |
} |
| 201 |
{ |
{ |
| 202 |
lml_tag_quote_level += fb_quote_level; |
lml_tag_quote_level += fb_quote_level; |
| 203 |
|
|
| 204 |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 205 |
lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]); |
lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]); |
| 206 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len) |
| 207 |
{ |
{ |
| 208 |
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); |
| 209 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 210 |
return j; |
return j; |
| 211 |
} |
} |
| 216 |
new_line = 0; |
new_line = 0; |
| 217 |
} |
} |
| 218 |
|
|
| 219 |
|
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
| 220 |
|
{ |
| 221 |
|
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++) |
| 222 |
|
; |
| 223 |
|
|
| 224 |
|
if (str_in[k] == 'm') // Valid |
| 225 |
|
{ |
| 226 |
|
if (j + (k - i + 1) >= buf_len) |
| 227 |
|
{ |
| 228 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len); |
| 229 |
|
str_out[j] = '\0'; |
| 230 |
|
return j; |
| 231 |
|
} |
| 232 |
|
memcpy(str_out + j, str_in + i, (size_t)(k - i + 1)); |
| 233 |
|
j += (k - i + 1); |
| 234 |
|
i = k; |
| 235 |
|
continue; |
| 236 |
|
} |
| 237 |
|
else // reach end of string |
| 238 |
|
{ |
| 239 |
|
break; |
| 240 |
|
} |
| 241 |
|
} |
| 242 |
|
|
| 243 |
if (str_in[i] == '\n') |
if (str_in[i] == '\n') |
| 244 |
{ |
{ |
| 245 |
tag_start_pos = -1; // jump out of tag at end of line |
tag_start_pos = -1; // jump out of tag at end of line |
| 296 |
tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])( |
tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])( |
| 297 |
LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
| 298 |
} |
} |
| 299 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len) |
| 300 |
{ |
{ |
| 301 |
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); |
| 302 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 303 |
return j; |
return j; |
| 304 |
} |
} |
| 317 |
} |
} |
| 318 |
else if (tag_start_pos == -1) // not in LML tag |
else if (tag_start_pos == -1) // not in LML tag |
| 319 |
{ |
{ |
| 320 |
if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character |
if (str_in[i] & 0b10000000) // head of multi-byte character |
| 321 |
{ |
{ |
| 322 |
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 |
| 323 |
{ |
{ |
| 324 |
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); |
| 325 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 326 |
return j; |
return j; |
| 327 |
} |
} |
| 328 |
str_out[j++] = str_in[i++]; |
|
| 329 |
if (str_in[i] == '\0') |
c = (str_in[i] & 0b01110000) << 1; |
| 330 |
|
while (c & 0b10000000) |
| 331 |
{ |
{ |
| 332 |
str_out[j] = '\0'; |
str_out[j++] = str_in[i++]; |
| 333 |
return j; |
if (str_in[i] == '\0') |
| 334 |
|
{ |
| 335 |
|
str_out[j] = '\0'; |
| 336 |
|
return j; |
| 337 |
|
} |
| 338 |
|
c = (c & 0b01111111) << 1; |
| 339 |
} |
} |
| 340 |
} |
} |
| 341 |
|
|
| 342 |
if (j + 1 >= buf_len - 1) |
if (j + 1 >= buf_len) |
| 343 |
{ |
{ |
| 344 |
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); |
| 345 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 346 |
return j; |
return j; |
| 347 |
} |
} |
| 356 |
if (lml_tag_quote_level > 0) |
if (lml_tag_quote_level > 0) |
| 357 |
{ |
{ |
| 358 |
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"); |
| 359 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len) |
| 360 |
{ |
{ |
| 361 |
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); |
| 362 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 363 |
return j; |
return j; |
| 364 |
} |
} |