| 63 |
} |
} |
| 64 |
|
|
| 65 |
#define LML_TAG_QUOTE_MAX_LEVEL 10 |
#define LML_TAG_QUOTE_MAX_LEVEL 10 |
|
#define LML_TAG_QUOTE_LEVEL_LOOP 3 |
|
| 66 |
|
|
| 67 |
static const char *lml_tag_quote_color[] = { |
static const char *lml_tag_quote_color[] = { |
| 68 |
"\033[33m", // yellow |
"\033[33m", // yellow |
| 70 |
"\033[35m", // magenta |
"\033[35m", // magenta |
| 71 |
}; |
}; |
| 72 |
|
|
| 73 |
|
static const int LML_TAG_QUOTE_LEVEL_LOOP = (int)(sizeof(lml_tag_quote_color) / sizeof(const char *)); |
| 74 |
|
|
| 75 |
static int lml_tag_quote_level = 0; |
static int lml_tag_quote_level = 0; |
| 76 |
|
|
| 77 |
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) |
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) |
| 119 |
const LML_TAG_DEF lml_tag_def[] = { |
const LML_TAG_DEF lml_tag_def[] = { |
| 120 |
// Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb} |
// Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb} |
| 121 |
{"plain", NULL, NULL, NULL, lml_tag_disable_filter}, |
{"plain", NULL, NULL, NULL, lml_tag_disable_filter}, |
| 122 |
{"nolml", "", NULL, "", NULL}, |
{"nolml", "", NULL, "", NULL}, // deprecated |
| 123 |
{"lml", "", NULL, "", NULL}, |
{"lml", "", NULL, "", NULL}, // deprecated |
| 124 |
|
{"align", "", "", "", NULL}, // N/A |
| 125 |
|
{"/align", "", "", "", NULL}, // N/A |
| 126 |
|
{"size", "", "", "", NULL}, // N/A |
| 127 |
|
{"/size", "", "", "", NULL}, // N/A |
| 128 |
{"left", "[", "", "[left]", NULL}, |
{"left", "[", "", "[left]", NULL}, |
| 129 |
{"right", "]", "", "[right]", NULL}, |
{"right", "]", "", "[right]", NULL}, |
| 130 |
{"bold", "\033[1m", "", "", NULL}, // does not work in Fterm |
{"bold", "\033[1m", "", "", NULL}, // does not work in Fterm |
| 232 |
lml_tag_quote_level += fb_quote_level; |
lml_tag_quote_level += fb_quote_level; |
| 233 |
|
|
| 234 |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
| 235 |
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]); |
| 236 |
if (j + tag_output_len >= buf_len) |
if (j + tag_output_len >= buf_len) |
| 237 |
{ |
{ |
| 238 |
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 301 |
tag_start_pos = i; |
tag_start_pos = i; |
| 302 |
tag_name_pos = i + 1; |
tag_name_pos = i + 1; |
| 303 |
} |
} |
| 304 |
else if (!lml_tag_disabled && str_in[i] == ']') |
else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0) |
| 305 |
{ |
{ |
| 306 |
if (tag_name_pos >= 0) |
tag_end_pos = i; |
|
{ |
|
|
tag_end_pos = i; |
|
| 307 |
|
|
| 308 |
// Skip space characters |
// Skip space characters |
| 309 |
while (str_in[tag_name_pos] == ' ') |
while (str_in[tag_name_pos] == ' ') |
| 310 |
{ |
{ |
| 311 |
tag_name_pos++; |
tag_name_pos++; |
| 312 |
} |
} |
| 313 |
|
|
| 314 |
for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++) |
for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++) |
| 315 |
|
{ |
| 316 |
|
if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0) |
| 317 |
{ |
{ |
| 318 |
if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0) |
tag_param_pos = -1; |
| 319 |
|
switch (str_in[tag_name_pos + lml_tag_name_len[k]]) |
| 320 |
{ |
{ |
| 321 |
tag_param_pos = -1; |
case ' ': |
| 322 |
switch (str_in[tag_name_pos + lml_tag_name_len[k]]) |
tag_name_found = 1; |
| 323 |
|
tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; |
| 324 |
|
while (str_in[tag_param_pos] == ' ') |
| 325 |
|
{ |
| 326 |
|
tag_param_pos++; |
| 327 |
|
} |
| 328 |
|
strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); |
| 329 |
|
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
| 330 |
|
case ']': |
| 331 |
|
tag_name_found = 1; |
| 332 |
|
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 |
| 333 |
|
{ |
| 334 |
|
strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); |
| 335 |
|
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 336 |
|
} |
| 337 |
|
if (!quote_mode) |
| 338 |
{ |
{ |
| 339 |
case ' ': |
if (lml_tag_def[k].tag_output != NULL) |
| 340 |
tag_name_found = 1; |
{ |
| 341 |
tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf); |
| 342 |
while (str_in[tag_param_pos] == ' ') |
} |
| 343 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 344 |
{ |
{ |
| 345 |
tag_param_pos++; |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 346 |
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0); |
| 347 |
} |
} |
| 348 |
strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); |
else |
|
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
|
|
case ']': |
|
|
tag_name_found = 1; |
|
|
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 |
|
| 349 |
{ |
{ |
| 350 |
strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); |
tag_output_len = 0; |
|
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
|
| 351 |
} |
} |
| 352 |
if (!quote_mode) |
} |
| 353 |
|
else // if (quote_mode) |
| 354 |
|
{ |
| 355 |
|
if (lml_tag_def[k].quote_mode_output != NULL) |
| 356 |
{ |
{ |
| 357 |
if (lml_tag_def[k].tag_output != NULL) |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf); |
|
{ |
|
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf); |
|
|
} |
|
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
|
|
{ |
|
|
tag_output_len = lml_tag_def[k].tag_filter_cb( |
|
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0); |
|
|
} |
|
|
else |
|
|
{ |
|
|
tag_output_len = 0; |
|
|
} |
|
| 358 |
} |
} |
| 359 |
else // if (quote_mode) |
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 360 |
{ |
{ |
| 361 |
if (lml_tag_def[k].quote_mode_output != NULL) |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 362 |
{ |
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf); |
|
|
} |
|
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
|
|
{ |
|
|
tag_output_len = lml_tag_def[k].tag_filter_cb( |
|
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); |
|
|
} |
|
|
else |
|
|
{ |
|
|
tag_output_len = 0; |
|
|
} |
|
| 363 |
} |
} |
| 364 |
if (j + tag_output_len >= buf_len) |
else |
| 365 |
{ |
{ |
| 366 |
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
tag_output_len = 0; |
|
str_out[j] = '\0'; |
|
|
return j; |
|
| 367 |
} |
} |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
|
break; |
|
|
default: // tag_name not match |
|
|
continue; |
|
| 368 |
} |
} |
| 369 |
|
if (j + tag_output_len >= buf_len) |
| 370 |
|
{ |
| 371 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
| 372 |
|
str_out[j] = '\0'; |
| 373 |
|
return j; |
| 374 |
|
} |
| 375 |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
| 376 |
|
j += tag_output_len; |
| 377 |
break; |
break; |
| 378 |
|
default: // tag_name not match |
| 379 |
|
continue; |
| 380 |
} |
} |
| 381 |
|
break; |
| 382 |
} |
} |
| 383 |
|
} |
| 384 |
|
|
| 385 |
if (!tag_name_found) |
if (!tag_name_found) |
| 386 |
|
{ |
| 387 |
|
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 388 |
|
if (j + tag_output_len >= buf_len) |
| 389 |
{ |
{ |
| 390 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len); |
| 391 |
if (j + tag_output_len >= buf_len) |
str_out[j] = '\0'; |
| 392 |
{ |
return j; |
|
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + tag_output_len, buf_len); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
|
|
|
memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
| 393 |
} |
} |
| 394 |
|
|
| 395 |
tag_start_pos = -1; |
memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len); |
| 396 |
tag_name_pos = -1; |
j += tag_output_len; |
| 397 |
} |
} |
| 398 |
|
|
| 399 |
|
tag_start_pos = -1; |
| 400 |
|
tag_name_pos = -1; |
| 401 |
} |
} |
| 402 |
else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag |
else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag |
| 403 |
{ |
{ |