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