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