| 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> |
| 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 |
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); |
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 |
{ |
{ |
| 66 |
#define LML_TAG_QUOTE_LEVEL_LOOP 3 |
#define LML_TAG_QUOTE_LEVEL_LOOP 3 |
| 67 |
|
|
| 68 |
static const char *lml_tag_quote_color[] = { |
static const char *lml_tag_quote_color[] = { |
| 69 |
"\033[33m", |
"\033[33m", // yellow |
| 70 |
"\033[32m", |
"\033[32m", // green |
| 71 |
"\033[35m", |
"\033[35m", // magenta |
| 72 |
}; |
}; |
| 73 |
|
|
| 74 |
static int lml_tag_quote_level = 0; |
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) |
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, int quote_mode) |
| 77 |
{ |
{ |
|
|
|
| 78 |
if (strcasecmp(tag_name, "quote") == 0) |
if (strcasecmp(tag_name, "quote") == 0) |
| 79 |
{ |
{ |
| 80 |
if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL) |
if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL) |
| 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 |
if (lml_tag_quote_level > 0) |
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")); |
|
return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]); |
|
|
} |
|
|
else |
|
|
{ |
|
|
return snprintf(tag_output_buf, tag_output_buf_len, "\033[m"); |
|
|
} |
|
| 95 |
} |
} |
| 96 |
|
|
| 97 |
return 0; |
return 0; |
| 98 |
} |
} |
| 99 |
|
|
| 100 |
const static char *LML_tag_def[][3] = { |
static int lml_tag_disabled = 0; |
| 101 |
{"left", "[", ""}, |
|
| 102 |
{"right", "]", NULL}, |
static int lml_tag_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode) |
| 103 |
{"bold", "\033[1m", ""}, |
{ |
| 104 |
{"/bold", "\033[21m", NULL}, |
lml_tag_disabled = 1; |
| 105 |
{"b", "\033[1m", ""}, |
|
| 106 |
{"/b", "\033[21m", NULL}, |
return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : "")); |
| 107 |
{"italic", "\033[3m", ""}, |
} |
| 108 |
{"/italic", "\033[23m", NULL}, |
|
| 109 |
{"i", "\033[3m", ""}, |
typedef struct lml_tag_def_t |
| 110 |
{"/i", "\033[23m", NULL}, |
{ |
| 111 |
{"underline", "\033[4m", ""}, |
const char *tag_name; // tag name |
| 112 |
{"/underline", "\033[24m", NULL}, |
const char *tag_output; // output string |
| 113 |
{"u", "\033[4m", ""}, |
const char *default_param; // default param string |
| 114 |
{"/u", "\033[24m", NULL}, |
const char *quote_mode_output; // output string in quote mode |
| 115 |
{"color", NULL, (const char *)lml_tag_color_filter}, |
lml_tag_filter_cb tag_filter_cb; // tag filter callback |
| 116 |
{"/color", "\033[m", NULL}, |
} LML_TAG_DEF; |
| 117 |
{"quote", NULL, (const char *)lml_tag_quote_filter}, |
|
| 118 |
{"/quote", NULL, (const char *)lml_tag_quote_filter}, |
const LML_TAG_DEF lml_tag_def[] = { |
| 119 |
{"url", "", ""}, |
// Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb} |
| 120 |
{"/url", "(Á´½Ó: %s)", NULL}, |
{"plain", NULL, NULL, NULL, lml_tag_disable_filter}, |
| 121 |
{"link", "", ""}, |
{"nolml", "", NULL, "", NULL}, |
| 122 |
{"/link", "(Á´½Ó: %s)", NULL}, |
{"lml", "", NULL, "", NULL}, |
| 123 |
{"email", "", ""}, |
{"left", "[", "", "[left]", NULL}, |
| 124 |
{"/email", "(Email: %s)", NULL}, |
{"right", "]", "", "[right]", NULL}, |
| 125 |
{"user", "", ""}, |
{"bold", "\033[1m", "", "", NULL}, // does not work in Fterm |
| 126 |
{"/user", "(Óû§: %s)", NULL}, |
{"/bold", "\033[22m", NULL, "", NULL}, |
| 127 |
{"article", "", ""}, |
{"b", "\033[1m", "", "", NULL}, |
| 128 |
{"/article", "(ÎÄÕÂ: %s)", NULL}, |
{"/b", "\033[22m", NULL, "", NULL}, |
| 129 |
{"image", "(ͼƬ: %s)", ""}, |
{"italic", "\033[5m", "", "", NULL}, // use blink instead |
| 130 |
{"flash", "(Flash: %s)", ""}, |
{"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm |
| 131 |
{"bwf", "\033[1;31m****\033[m", ""}, |
{"i", "\033[5m", "", "", NULL}, |
| 132 |
|
{"/i", "\033[m", NULL, "", NULL}, |
| 133 |
|
{"underline", "\033[4m", "", "", NULL}, |
| 134 |
|
{"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm |
| 135 |
|
{"u", "\033[4m", "", "", NULL}, |
| 136 |
|
{"/u", "\033[m", NULL, "", NULL}, |
| 137 |
|
{"color", NULL, NULL, "", lml_tag_color_filter}, |
| 138 |
|
{"/color", "\033[m", NULL, "", NULL}, |
| 139 |
|
{"quote", NULL, NULL, "", lml_tag_quote_filter}, |
| 140 |
|
{"/quote", NULL, NULL, "", lml_tag_quote_filter}, |
| 141 |
|
{"url", "", "", "", NULL}, |
| 142 |
|
{"/url", "(链接: %s)", NULL, "", NULL}, |
| 143 |
|
{"link", "", "", "", NULL}, |
| 144 |
|
{"/link", "(链接: %s)", NULL, "", NULL}, |
| 145 |
|
{"email", "", "", "", NULL}, |
| 146 |
|
{"/email", "(Email: %s)", NULL, "", NULL}, |
| 147 |
|
{"user", "", "", "", NULL}, |
| 148 |
|
{"/user", "(用户: %s)", NULL, "", NULL}, |
| 149 |
|
{"article", "", "", "", NULL}, |
| 150 |
|
{"/article", "ï¼ˆæ–‡ç« : %s)", NULL, "", NULL}, |
| 151 |
|
{"image", "(图片: %s)", "", "%s", NULL}, |
| 152 |
|
{"flash", "(Flash: %s)", "", "", NULL}, |
| 153 |
|
{"bwf", "\033[1;31m****\033[m", "", "****", NULL}, |
| 154 |
}; |
}; |
| 155 |
|
|
| 156 |
#define LML_TAG_COUNT 31 |
#define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF)) |
| 157 |
|
|
| 158 |
static int LML_tag_name_len[LML_TAG_COUNT]; |
static int lml_tag_name_len[LML_TAG_COUNT]; |
| 159 |
static int LML_init = 0; |
static int lml_ready = 0; |
| 160 |
|
|
| 161 |
inline static void lml_init(void) |
inline static void lml_init(void) |
| 162 |
{ |
{ |
| 163 |
int i; |
int i; |
| 164 |
|
|
| 165 |
if (!LML_init) |
if (!lml_ready) |
| 166 |
{ |
{ |
| 167 |
for (i = 0; i < LML_TAG_COUNT; i++) |
for (i = 0; i < LML_TAG_COUNT; i++) |
| 168 |
{ |
{ |
| 169 |
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); |
| 170 |
} |
} |
| 171 |
|
|
| 172 |
LML_init = 1; |
lml_ready = 1; |
| 173 |
} |
} |
| 174 |
} |
} |
| 175 |
|
|
| 176 |
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 quote_mode) |
| 177 |
{ |
{ |
| 178 |
|
char c; |
| 179 |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 180 |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
| 181 |
int i; |
int i; |
| 182 |
int j = 0; |
int j = 0; |
| 183 |
int k; |
int k; |
| 184 |
int tag_start_pos = -1; |
int tag_start_pos = -1; |
| 185 |
|
int tag_name_pos = -1; |
| 186 |
int tag_end_pos = -1; |
int tag_end_pos = -1; |
| 187 |
int tag_param_pos = -1; |
int tag_param_pos = -1; |
| 188 |
int tag_output_len; |
int tag_output_len; |
| 189 |
|
int new_line = 1; |
| 190 |
|
int fb_quote_level = 0; |
| 191 |
|
int tag_name_found; |
| 192 |
|
|
| 193 |
lml_init(); |
lml_init(); |
| 194 |
|
|
| 195 |
|
lml_tag_disabled = 0; |
| 196 |
lml_tag_quote_level = 0; |
lml_tag_quote_level = 0; |
| 197 |
|
|
| 198 |
for (i = 0; str_in[i] != '\0'; i++) |
for (i = 0; str_in[i] != '\0'; i++) |
| 199 |
{ |
{ |
| 200 |
if (str_in[i] == '[') |
if (!quote_mode && !lml_tag_disabled && new_line) |
| 201 |
{ |
{ |
| 202 |
tag_start_pos = i + 1; |
if (fb_quote_level > 0) |
| 203 |
|
{ |
| 204 |
|
lml_tag_quote_level -= fb_quote_level; |
| 205 |
|
|
| 206 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 207 |
|
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
| 208 |
|
if (j + tag_output_len >= buf_len) |
| 209 |
|
{ |
| 210 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 211 |
|
str_out[j] = '\0'; |
| 212 |
|
return j; |
| 213 |
|
} |
| 214 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 215 |
|
j += tag_output_len; |
| 216 |
|
|
| 217 |
|
fb_quote_level = 0; |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str |
| 221 |
|
{ |
| 222 |
|
fb_quote_level++; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
if (fb_quote_level > 0) |
| 226 |
|
{ |
| 227 |
|
lml_tag_quote_level += fb_quote_level; |
| 228 |
|
|
| 229 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 230 |
|
lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]); |
| 231 |
|
if (j + tag_output_len >= buf_len) |
| 232 |
|
{ |
| 233 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 234 |
|
str_out[j] = '\0'; |
| 235 |
|
return j; |
| 236 |
|
} |
| 237 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 238 |
|
j += tag_output_len; |
| 239 |
|
} |
| 240 |
|
|
| 241 |
|
new_line = 0; |
| 242 |
} |
} |
| 243 |
else if (str_in[i] == ']') |
|
| 244 |
|
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
| 245 |
{ |
{ |
| 246 |
if (tag_start_pos >= 0) |
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++) |
| 247 |
|
; |
| 248 |
|
|
| 249 |
|
if (str_in[k] == 'm') // Valid |
| 250 |
|
{ |
| 251 |
|
if (j + (k - i + 1) >= buf_len) |
| 252 |
|
{ |
| 253 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len); |
| 254 |
|
str_out[j] = '\0'; |
| 255 |
|
return j; |
| 256 |
|
} |
| 257 |
|
memcpy(str_out + j, str_in + i, (size_t)(k - i + 1)); |
| 258 |
|
j += (k - i + 1); |
| 259 |
|
i = k; |
| 260 |
|
continue; |
| 261 |
|
} |
| 262 |
|
else // reach end of string |
| 263 |
|
{ |
| 264 |
|
break; |
| 265 |
|
} |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
if (str_in[i] == '\n') // jump out of tag at end of line |
| 269 |
|
{ |
| 270 |
|
if (tag_start_pos != -1) // tag is not closed |
| 271 |
|
{ |
| 272 |
|
tag_end_pos = i - 1; |
| 273 |
|
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 274 |
|
if (j + tag_output_len >= buf_len) |
| 275 |
|
{ |
| 276 |
|
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len); |
| 277 |
|
str_out[j] = '\0'; |
| 278 |
|
return j; |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len); |
| 282 |
|
j += tag_output_len; |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
tag_start_pos = -1; |
| 286 |
|
tag_name_pos = -1; |
| 287 |
|
new_line = 1; |
| 288 |
|
} |
| 289 |
|
else if (str_in[i] == '\r') |
| 290 |
|
{ |
| 291 |
|
continue; // ignore '\r' |
| 292 |
|
} |
| 293 |
|
|
| 294 |
|
if (!lml_tag_disabled && str_in[i] == '[') |
| 295 |
|
{ |
| 296 |
|
tag_start_pos = i; |
| 297 |
|
tag_name_pos = i + 1; |
| 298 |
|
} |
| 299 |
|
else if (!lml_tag_disabled && str_in[i] == ']') |
| 300 |
|
{ |
| 301 |
|
if (tag_name_pos >= 0) |
| 302 |
{ |
{ |
| 303 |
tag_end_pos = i; |
tag_end_pos = i; |
| 304 |
|
|
| 305 |
// Skip space characters |
// Skip space characters |
| 306 |
while (str_in[tag_start_pos] == ' ') |
while (str_in[tag_name_pos] == ' ') |
| 307 |
{ |
{ |
| 308 |
tag_start_pos++; |
tag_name_pos++; |
| 309 |
} |
} |
| 310 |
|
|
| 311 |
for (k = 0; k < LML_TAG_COUNT; k++) |
for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++) |
| 312 |
{ |
{ |
| 313 |
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_name_pos, (size_t)lml_tag_name_len[k]) == 0) |
| 314 |
{ |
{ |
| 315 |
tag_param_pos = -1; |
tag_param_pos = -1; |
| 316 |
switch (str_in[tag_start_pos + LML_tag_name_len[k]]) |
switch (str_in[tag_name_pos + lml_tag_name_len[k]]) |
| 317 |
{ |
{ |
| 318 |
case ' ': |
case ' ': |
| 319 |
tag_param_pos = tag_start_pos + LML_tag_name_len[k] + 1; |
tag_name_found = 1; |
| 320 |
|
tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; |
| 321 |
while (str_in[tag_param_pos] == ' ') |
while (str_in[tag_param_pos] == ' ') |
| 322 |
{ |
{ |
| 323 |
tag_param_pos++; |
tag_param_pos++; |
| 325 |
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)); |
| 326 |
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'; |
| 327 |
case ']': |
case ']': |
| 328 |
if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined |
tag_name_found = 1; |
| 329 |
|
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 |
| 330 |
{ |
{ |
| 331 |
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); |
| 332 |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 333 |
} |
} |
| 334 |
if (LML_tag_def[k][1] != NULL) |
if (!quote_mode) |
| 335 |
{ |
{ |
| 336 |
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].tag_output != NULL) |
| 337 |
|
{ |
| 338 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf); |
| 339 |
|
} |
| 340 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 341 |
|
{ |
| 342 |
|
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 343 |
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0); |
| 344 |
|
} |
| 345 |
|
else |
| 346 |
|
{ |
| 347 |
|
tag_output_len = 0; |
| 348 |
|
} |
| 349 |
} |
} |
| 350 |
else |
else // if (quote_mode) |
| 351 |
{ |
{ |
| 352 |
tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])( |
if (lml_tag_def[k].quote_mode_output != NULL) |
| 353 |
LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN); |
{ |
| 354 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf); |
| 355 |
|
} |
| 356 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 357 |
|
{ |
| 358 |
|
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 359 |
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); |
| 360 |
|
} |
| 361 |
|
else |
| 362 |
|
{ |
| 363 |
|
tag_output_len = 0; |
| 364 |
|
} |
| 365 |
} |
} |
| 366 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len) |
| 367 |
{ |
{ |
| 368 |
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); |
| 369 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 370 |
return j; |
return j; |
| 371 |
} |
} |
| 379 |
} |
} |
| 380 |
} |
} |
| 381 |
|
|
| 382 |
|
if (!tag_name_found) |
| 383 |
|
{ |
| 384 |
|
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 385 |
|
if (j + tag_output_len >= buf_len) |
| 386 |
|
{ |
| 387 |
|
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len); |
| 388 |
|
str_out[j] = '\0'; |
| 389 |
|
return j; |
| 390 |
|
} |
| 391 |
|
|
| 392 |
|
memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len); |
| 393 |
|
j += tag_output_len; |
| 394 |
|
} |
| 395 |
|
|
| 396 |
tag_start_pos = -1; |
tag_start_pos = -1; |
| 397 |
|
tag_name_pos = -1; |
| 398 |
} |
} |
| 399 |
} |
} |
| 400 |
else if (tag_start_pos == -1) // not in LML tag |
else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag |
| 401 |
{ |
{ |
| 402 |
if (str_in[i] < 0 || str_in[i] > 127) // GBK chinese character |
if (str_in[i] & 0x80) // head of multi-byte character |
| 403 |
{ |
{ |
| 404 |
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 |
| 405 |
{ |
{ |
| 406 |
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); |
| 407 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 408 |
return j; |
return j; |
| 409 |
} |
} |
| 410 |
str_out[j++] = str_in[i++]; |
|
| 411 |
if (str_in[i] == '\0') |
c = (str_in[i] & 0x70) << 1; |
| 412 |
|
while (c & 0x80) |
| 413 |
{ |
{ |
| 414 |
str_out[j] = '\0'; |
str_out[j++] = str_in[i++]; |
| 415 |
return j; |
if (str_in[i] == '\0') |
| 416 |
|
{ |
| 417 |
|
str_out[j] = '\0'; |
| 418 |
|
return j; |
| 419 |
|
} |
| 420 |
|
c = (c & 0x7f) << 1; |
| 421 |
} |
} |
| 422 |
} |
} |
| 423 |
|
|
| 424 |
if (j + 1 >= buf_len - 1) |
if (j + 1 >= buf_len) |
| 425 |
{ |
{ |
| 426 |
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); |
| 427 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 428 |
return j; |
return j; |
| 429 |
} |
} |
| 435 |
} |
} |
| 436 |
} |
} |
| 437 |
|
|
| 438 |
|
if (tag_start_pos != -1) // tag is not closed |
| 439 |
|
{ |
| 440 |
|
tag_end_pos = i - 1; |
| 441 |
|
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 442 |
|
if (j + tag_output_len >= buf_len) |
| 443 |
|
{ |
| 444 |
|
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len); |
| 445 |
|
str_out[j] = '\0'; |
| 446 |
|
return j; |
| 447 |
|
} |
| 448 |
|
|
| 449 |
|
memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len); |
| 450 |
|
j += tag_output_len; |
| 451 |
|
} |
| 452 |
|
|
| 453 |
|
if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
| 454 |
|
{ |
| 455 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); |
| 456 |
|
if (j + tag_output_len >= buf_len) |
| 457 |
|
{ |
| 458 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 459 |
|
str_out[j] = '\0'; |
| 460 |
|
return j; |
| 461 |
|
} |
| 462 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 463 |
|
j += tag_output_len; |
| 464 |
|
} |
| 465 |
|
|
| 466 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 467 |
|
|
| 468 |
return j; |
return j; |
| 469 |
} |
} |