| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
lml.h - description |
/* |
| 3 |
------------------- |
* lml |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - LML render |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2026 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
| 8 |
/*************************************************************************** |
|
| 9 |
* * |
#ifdef HAVE_CONFIG_H |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
#include "config.h" |
| 11 |
* it under the terms of the GNU General Public License as published by * |
#endif |
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 12 |
|
|
| 13 |
#include "common.h" |
#include "common.h" |
| 14 |
#include "lml.h" |
#include "lml.h" |
| 15 |
#include "log.h" |
#include "log.h" |
| 16 |
|
#include "str_process.h" |
| 17 |
|
#include "trie_dict.h" |
| 18 |
|
#include <ctype.h> |
| 19 |
#include <stdio.h> |
#include <stdio.h> |
| 20 |
#include <string.h> |
#include <string.h> |
| 21 |
#include <sys/param.h> |
#include <sys/param.h> |
| 22 |
|
|
| 23 |
#define LML_TAG_PARAM_BUF_LEN 256 |
enum _lml_constant_t |
| 24 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
{ |
| 25 |
|
LML_TAG_NAME_BUF_LEN = 21, |
| 26 |
|
LML_TAG_PARAM_BUF_LEN = 256, |
| 27 |
|
LML_TAG_OUTPUT_BUF_LEN = 1024, |
| 28 |
|
LML_TAG_QUOTE_MAX_LEVEL = 10, |
| 29 |
|
}; |
| 30 |
|
|
| 31 |
|
clock_t lml_total_exec_duration = 0; |
| 32 |
|
|
| 33 |
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); |
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); |
| 34 |
|
|
| 68 |
return 0; |
return 0; |
| 69 |
} |
} |
| 70 |
|
|
|
#define LML_TAG_QUOTE_MAX_LEVEL 10 |
|
|
#define LML_TAG_QUOTE_LEVEL_LOOP 3 |
|
|
|
|
| 71 |
static const char *lml_tag_quote_color[] = { |
static const char *lml_tag_quote_color[] = { |
| 72 |
"\033[33m", // yellow |
"\033[33m", // yellow |
| 73 |
"\033[32m", // green |
"\033[32m", // green |
| 74 |
"\033[35m", // magenta |
"\033[35m", // magenta |
| 75 |
}; |
}; |
| 76 |
|
|
| 77 |
|
static const int LML_TAG_QUOTE_LEVEL_LOOP = (int)(sizeof(lml_tag_quote_color) / sizeof(const char *)); |
| 78 |
|
|
| 79 |
static int lml_tag_quote_level = 0; |
static int lml_tag_quote_level = 0; |
| 80 |
|
|
| 81 |
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) |
| 113 |
|
|
| 114 |
typedef struct lml_tag_def_t |
typedef struct lml_tag_def_t |
| 115 |
{ |
{ |
| 116 |
const char *tag_name; // tag name |
const char *tag_name; // tag name |
| 117 |
const char *tag_output; // output string |
const char *tag_output; // output string |
| 118 |
const char *default_param; // default param string |
const char *default_param; // default param string |
| 119 |
const char *quote_mode_output; // output string in quote mode |
const char *quote_mode_output; // output string in quote mode |
| 120 |
lml_tag_filter_cb tag_filter_cb; // tag filter callback |
lml_tag_filter_cb tag_filter_cb; // tag filter callback |
| 121 |
} LML_TAG_DEF; |
} LML_TAG_DEF; |
| 122 |
|
|
| 123 |
const LML_TAG_DEF lml_tag_def[] = { |
const LML_TAG_DEF lml_tag_def[] = { |
| 124 |
// 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} |
| 125 |
{"plain", NULL, NULL, NULL, lml_tag_disable_filter}, |
{"plain", NULL, NULL, NULL, lml_tag_disable_filter}, |
| 126 |
|
{"nolml", "", NULL, "", NULL}, // deprecated |
| 127 |
|
{"lml", "", NULL, "", NULL}, // deprecated |
| 128 |
|
{"align", "", "", "", NULL}, // N/A |
| 129 |
|
{"/align", "", "", "", NULL}, // N/A |
| 130 |
|
{"size", "", "", "", NULL}, // N/A |
| 131 |
|
{"/size", "", "", "", NULL}, // N/A |
| 132 |
{"left", "[", "", "[left]", NULL}, |
{"left", "[", "", "[left]", NULL}, |
| 133 |
{"right", "]", "", "[right]", NULL}, |
{"right", "]", "", "[right]", NULL}, |
| 134 |
{"bold", "\033[1m", "", "", NULL}, // does not work in Fterm |
{"bold", "\033[1m", "", "", NULL}, // does not work in Fterm |
| 135 |
{"/bold", "\033[22m", NULL, "", NULL}, |
{"/bold", "\033[22m", NULL, "", NULL}, |
| 136 |
{"b", "\033[1m", "", "", NULL}, |
{"b", "\033[1m", "", "", NULL}, |
| 137 |
{"/b", "\033[22m", NULL, "", NULL}, |
{"/b", "\033[22m", NULL, "", NULL}, |
| 138 |
{"italic", "\033[5m", "", "", NULL}, // use blink instead |
{"italic", "\033[5m", "", "", NULL}, // use blink instead |
| 139 |
{"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm |
{"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm |
| 140 |
{"i", "\033[5m", "", "", NULL}, |
{"i", "\033[5m", "", "", NULL}, |
| 141 |
{"/i", "\033[m", NULL, "", NULL}, |
{"/i", "\033[m", NULL, "", NULL}, |
| 162 |
{"bwf", "\033[1;31m****\033[m", "", "****", NULL}, |
{"bwf", "\033[1;31m****\033[m", "", "****", NULL}, |
| 163 |
}; |
}; |
| 164 |
|
|
| 165 |
#define LML_TAG_COUNT 32 |
static const int lml_tag_count = sizeof(lml_tag_def) / sizeof(LML_TAG_DEF); |
| 166 |
|
static int lml_tag_name_len[sizeof(lml_tag_def) / sizeof(LML_TAG_DEF)]; |
| 167 |
static int lml_tag_name_len[LML_TAG_COUNT]; |
static TRIE_NODE *p_lml_tag_dict; |
| 168 |
static int lml_ready = 0; |
static int lml_ready = 0; |
| 169 |
|
|
| 170 |
inline static void lml_init(void) |
int lml_init(void) |
| 171 |
{ |
{ |
| 172 |
|
char tag_name_buf[LML_TAG_NAME_BUF_LEN]; |
| 173 |
int i; |
int i; |
| 174 |
|
int j; |
| 175 |
|
|
| 176 |
if (!lml_ready) |
if (lml_ready) |
| 177 |
|
{ |
| 178 |
|
lml_cleanup(); |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
p_lml_tag_dict = trie_dict_create(); |
| 182 |
|
if (p_lml_tag_dict == NULL) |
| 183 |
{ |
{ |
| 184 |
for (i = 0; i < LML_TAG_COUNT; i++) |
log_error("trie_dict_create(lml_tag_dict) error"); |
| 185 |
|
return -1; |
| 186 |
|
} |
| 187 |
|
|
| 188 |
|
for (i = 0; i < lml_tag_count; i++) |
| 189 |
|
{ |
| 190 |
|
for (j = 0; j < sizeof(tag_name_buf) - 1 && lml_tag_def[i].tag_name[j] != '\0'; j++) |
| 191 |
{ |
{ |
| 192 |
lml_tag_name_len[i] = (int)strlen(lml_tag_def[i].tag_name); |
tag_name_buf[j] = (char)tolower(lml_tag_def[i].tag_name[j]); |
| 193 |
} |
} |
| 194 |
|
tag_name_buf[j] = '\0'; |
| 195 |
|
lml_tag_name_len[i] = j; |
| 196 |
|
|
| 197 |
lml_ready = 1; |
if (trie_dict_set(p_lml_tag_dict, tag_name_buf, (int64_t)i) != 1) |
| 198 |
|
{ |
| 199 |
|
log_error("trie_dict_set(lml_tag_dict, %s, %d) error", tag_name_buf, i); |
| 200 |
|
lml_cleanup(); |
| 201 |
|
return -1; |
| 202 |
|
} |
| 203 |
} |
} |
| 204 |
|
|
| 205 |
|
lml_ready = 1; |
| 206 |
|
|
| 207 |
|
return 0; |
| 208 |
} |
} |
| 209 |
|
|
| 210 |
int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode) |
void lml_cleanup(void) |
| 211 |
{ |
{ |
| 212 |
|
if (p_lml_tag_dict != NULL) |
| 213 |
|
{ |
| 214 |
|
trie_dict_destroy(p_lml_tag_dict); |
| 215 |
|
p_lml_tag_dict = NULL; |
| 216 |
|
} |
| 217 |
|
|
| 218 |
|
lml_ready = 0; |
| 219 |
|
} |
| 220 |
|
|
| 221 |
|
#define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len, line_width) \ |
| 222 |
|
if ((tag_out_len) > 0) \ |
| 223 |
|
{ \ |
| 224 |
|
if ((out_buf_offset) + (tag_out_len) >= (out_buf_len)) \ |
| 225 |
|
{ \ |
| 226 |
|
log_error("Buffer is not longer enough for output string %d >= %d", (out_buf_offset) + (tag_out_len), (out_buf_len)); \ |
| 227 |
|
out_buf[out_buf_offset] = '\0'; \ |
| 228 |
|
return (out_buf_offset); \ |
| 229 |
|
} \ |
| 230 |
|
memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len)); \ |
| 231 |
|
*((out_buf) + (out_buf_offset) + (size_t)(tag_out_len)) = '\0'; \ |
| 232 |
|
(line_width) += str_length((out_buf) + (out_buf_offset), 1); \ |
| 233 |
|
(out_buf_offset) += (tag_out_len); \ |
| 234 |
|
} |
| 235 |
|
|
| 236 |
|
int lml_render(const char *str_in, char *str_out, int buf_len, int width, int quote_mode) |
| 237 |
|
{ |
| 238 |
|
clock_t clock_begin; |
| 239 |
|
clock_t clock_end; |
| 240 |
|
|
| 241 |
char c; |
char c; |
| 242 |
|
char tag_name_buf[LML_TAG_NAME_BUF_LEN]; |
| 243 |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 244 |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
| 245 |
int i; |
int i; |
| 246 |
int j = 0; |
int j = 0; |
| 247 |
int k; |
int k; |
| 248 |
|
int last_i = -1; |
| 249 |
|
int64_t tag_index; |
| 250 |
int tag_start_pos = -1; |
int tag_start_pos = -1; |
| 251 |
|
int tag_name_pos = -1; |
| 252 |
int tag_end_pos = -1; |
int tag_end_pos = -1; |
| 253 |
int tag_param_pos = -1; |
int tag_param_pos = -1; |
| 254 |
int tag_output_len; |
int tag_output_len; |
| 255 |
int new_line = 1; |
int new_line = 1; |
| 256 |
int fb_quote_level = 0; |
int fb_quote_level = 0; |
| 257 |
|
int line_width = 0; |
| 258 |
|
char tab_spaces[TAB_SIZE + 1]; |
| 259 |
|
int tab_width = 0; |
| 260 |
|
|
| 261 |
|
clock_begin = clock(); |
| 262 |
|
|
| 263 |
|
#ifdef _DEBUG |
| 264 |
|
size_t str_in_len = strlen(str_in); |
| 265 |
|
#endif |
| 266 |
|
|
| 267 |
lml_init(); |
if (!lml_ready) |
| 268 |
|
{ |
| 269 |
|
log_error("LML module is not initialized"); |
| 270 |
|
return -1; |
| 271 |
|
} |
| 272 |
|
|
| 273 |
lml_tag_disabled = 0; |
lml_tag_disabled = 0; |
| 274 |
lml_tag_quote_level = 0; |
lml_tag_quote_level = 0; |
| 275 |
|
|
| 276 |
|
if (width <= 0) |
| 277 |
|
{ |
| 278 |
|
width = INT_MAX; |
| 279 |
|
} |
| 280 |
|
|
| 281 |
for (i = 0; str_in[i] != '\0'; i++) |
for (i = 0; str_in[i] != '\0'; i++) |
| 282 |
{ |
{ |
| 283 |
if (!quote_mode && !lml_tag_disabled && new_line) |
#ifdef _DEBUG |
| 284 |
|
if (i >= str_in_len) |
| 285 |
{ |
{ |
| 286 |
if (fb_quote_level > 0) |
log_error("Bug: i(%d) >= str_in_len(%d)", i, str_in_len); |
| 287 |
{ |
break; |
| 288 |
lml_tag_quote_level -= fb_quote_level; |
} |
| 289 |
|
#endif |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s", |
|
|
(lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m")); |
|
|
if (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); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
|
|
|
|
fb_quote_level = 0; |
|
|
} |
|
| 290 |
|
|
| 291 |
while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str |
if (!lml_tag_disabled && new_line) |
| 292 |
|
{ |
| 293 |
|
while (str_in[i] == ':' && str_in[i + 1] == ' ') // FB2000 quote leading str |
| 294 |
{ |
{ |
| 295 |
fb_quote_level++; |
fb_quote_level++; |
| 296 |
|
lml_tag_quote_level++; |
| 297 |
|
i += 2; |
| 298 |
} |
} |
| 299 |
|
|
| 300 |
if (fb_quote_level > 0) |
if (!quote_mode && lml_tag_quote_level > 0) |
| 301 |
{ |
{ |
|
lml_tag_quote_level += fb_quote_level; |
|
|
|
|
| 302 |
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", |
| 303 |
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]); |
| 304 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); |
|
{ |
|
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
| 305 |
} |
} |
| 306 |
|
|
| 307 |
|
for (k = 0; k < fb_quote_level; k++) |
| 308 |
|
{ |
| 309 |
|
tag_output_buf[k * 2] = ':'; |
| 310 |
|
tag_output_buf[k * 2 + 1] = ' '; |
| 311 |
|
} |
| 312 |
|
tag_output_buf[fb_quote_level * 2] = '\0'; |
| 313 |
|
tag_output_len = fb_quote_level * 2; |
| 314 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); |
| 315 |
|
|
| 316 |
new_line = 0; |
new_line = 0; |
| 317 |
|
i--; // redo at current i |
| 318 |
|
continue; |
| 319 |
} |
} |
| 320 |
|
|
| 321 |
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
if (lml_tag_disabled && new_line) |
| 322 |
{ |
{ |
| 323 |
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++) |
new_line = 0; |
| 324 |
|
} |
| 325 |
|
|
| 326 |
|
if (!quote_mode && !lml_tag_disabled && str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence |
| 327 |
|
{ |
| 328 |
|
for (k = i + 2; isdigit((int)str_in[k]) || str_in[k] == ';' || str_in[k] == '?'; k++) |
| 329 |
; |
; |
| 330 |
|
|
| 331 |
if (str_in[k] == 'm') // Valid |
if (str_in[k] == 'm') // valid -- copy directly |
| 332 |
|
{ |
| 333 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1, line_width); |
| 334 |
|
} |
| 335 |
|
else if (isalpha((int)str_in[k])) |
| 336 |
{ |
{ |
| 337 |
if (j + (k - i + 1) >= buf_len) |
// unsupported ANSI CSI command |
| 338 |
|
} |
| 339 |
|
else |
| 340 |
|
{ |
| 341 |
|
k--; |
| 342 |
|
} |
| 343 |
|
|
| 344 |
|
i = k; |
| 345 |
|
continue; |
| 346 |
|
} |
| 347 |
|
|
| 348 |
|
if (str_in[i] == '\n') // jump out of tag at end of line |
| 349 |
|
{ |
| 350 |
|
if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed |
| 351 |
|
{ |
| 352 |
|
if (line_width + 1 > width) |
| 353 |
{ |
{ |
| 354 |
log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len); |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 355 |
str_out[j] = '\0'; |
new_line = 1; |
| 356 |
return j; |
line_width = 0; |
| 357 |
|
i--; // redo at current i |
| 358 |
|
continue; |
| 359 |
} |
} |
| 360 |
memcpy(str_out + j, str_in + i, (size_t)(k - i + 1)); |
|
| 361 |
j += (k - i + 1); |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width); |
| 362 |
i = k; |
i = tag_start_pos; // restart from tag_start_pos + 1 |
| 363 |
|
tag_start_pos = -1; |
| 364 |
|
tag_name_pos = -1; |
| 365 |
continue; |
continue; |
| 366 |
} |
} |
| 367 |
else // reach end of string |
|
| 368 |
|
if (!lml_tag_disabled && fb_quote_level > 0) |
| 369 |
{ |
{ |
| 370 |
break; |
lml_tag_quote_level -= fb_quote_level; |
| 371 |
|
|
| 372 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, (quote_mode ? "" : "\033[m")); |
| 373 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); |
| 374 |
|
|
| 375 |
|
fb_quote_level = 0; |
| 376 |
} |
} |
|
} |
|
| 377 |
|
|
| 378 |
if (str_in[i] == '\n') |
if (new_line) |
| 379 |
{ |
{ |
| 380 |
tag_start_pos = -1; // jump out of tag at end of line |
continue; |
| 381 |
|
} |
| 382 |
|
|
| 383 |
|
tag_start_pos = -1; |
| 384 |
|
tag_name_pos = -1; |
| 385 |
new_line = 1; |
new_line = 1; |
| 386 |
|
line_width = -1; |
| 387 |
} |
} |
| 388 |
else if (str_in[i] == '\r') |
else if (str_in[i] == '\r' || str_in[i] == '\7') |
| 389 |
{ |
{ |
| 390 |
continue; // ignore '\r' |
continue; // Skip special characters |
| 391 |
|
} |
| 392 |
|
else if (str_in[i] == '\t') |
| 393 |
|
{ |
| 394 |
|
tab_width = TAB_SIZE - (line_width % TAB_SIZE); |
| 395 |
|
if (line_width + tab_width > width) |
| 396 |
|
{ |
| 397 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 398 |
|
new_line = 1; |
| 399 |
|
line_width = 0; |
| 400 |
|
// skip current Tab |
| 401 |
|
continue; |
| 402 |
|
} |
| 403 |
|
|
| 404 |
|
for (k = 0; k < tab_width; k++) |
| 405 |
|
{ |
| 406 |
|
tab_spaces[k] = ' '; |
| 407 |
|
} |
| 408 |
|
tab_spaces[tab_width] = '\0'; |
| 409 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tab_spaces, tab_width, line_width); |
| 410 |
|
continue; |
| 411 |
} |
} |
| 412 |
|
|
| 413 |
if (!lml_tag_disabled && str_in[i] == '[') |
if (!lml_tag_disabled && str_in[i] == '[') |
| 414 |
{ |
{ |
| 415 |
tag_start_pos = i + 1; |
if (tag_start_pos != -1) // tag is not closed |
| 416 |
|
{ |
| 417 |
|
if (line_width + 1 > width) |
| 418 |
|
{ |
| 419 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 420 |
|
new_line = 1; |
| 421 |
|
line_width = 0; |
| 422 |
|
i--; // redo at current i |
| 423 |
|
continue; |
| 424 |
|
} |
| 425 |
|
|
| 426 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width); |
| 427 |
|
i = tag_start_pos; // restart from tag_start_pos + 1 |
| 428 |
|
tag_start_pos = -1; |
| 429 |
|
tag_name_pos = -1; |
| 430 |
|
continue; |
| 431 |
|
} |
| 432 |
|
|
| 433 |
|
tag_start_pos = i; |
| 434 |
|
tag_name_pos = i + 1; |
| 435 |
} |
} |
| 436 |
else if (!lml_tag_disabled && str_in[i] == ']') |
else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0) |
| 437 |
{ |
{ |
| 438 |
if (tag_start_pos >= 0) |
tag_end_pos = i; |
| 439 |
|
|
| 440 |
|
// Skip space characters |
| 441 |
|
while (str_in[tag_name_pos] == ' ') |
| 442 |
{ |
{ |
| 443 |
tag_end_pos = i; |
tag_name_pos++; |
| 444 |
|
} |
| 445 |
|
|
| 446 |
// Skip space characters |
for (k = 0; k < sizeof(tag_name_buf) - 1 && tag_name_pos + k < tag_end_pos; k++) |
| 447 |
while (str_in[tag_start_pos] == ' ') |
{ |
| 448 |
|
if (str_in[tag_name_pos + k] == ' ' || str_in[tag_name_pos + k] == ']') |
| 449 |
|
{ |
| 450 |
|
break; |
| 451 |
|
} |
| 452 |
|
tag_name_buf[k] = (char)tolower(str_in[tag_name_pos + k]); |
| 453 |
|
} |
| 454 |
|
tag_name_buf[k] = '\0'; |
| 455 |
|
|
| 456 |
|
k = -1; |
| 457 |
|
if (tag_name_buf[0] != '\0') |
| 458 |
|
{ |
| 459 |
|
tag_index = -1; |
| 460 |
|
if (trie_dict_get(p_lml_tag_dict, tag_name_buf, (int64_t *)&tag_index) < 0) |
| 461 |
|
{ |
| 462 |
|
log_error("trie_dict_get(lml_tag_dict, %s) error", tag_name_buf); |
| 463 |
|
} |
| 464 |
|
else |
| 465 |
{ |
{ |
| 466 |
tag_start_pos++; |
k = (int)tag_index; |
| 467 |
} |
} |
| 468 |
|
} |
| 469 |
|
|
| 470 |
for (k = 0; k < LML_TAG_COUNT; k++) |
if (k != -1) |
| 471 |
|
{ |
| 472 |
|
tag_param_pos = -1; |
| 473 |
|
switch (str_in[tag_name_pos + lml_tag_name_len[k]]) |
| 474 |
{ |
{ |
| 475 |
if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0) |
case ' ': |
| 476 |
|
tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; |
| 477 |
|
while (str_in[tag_param_pos] == ' ') |
| 478 |
|
{ |
| 479 |
|
tag_param_pos++; |
| 480 |
|
} |
| 481 |
|
strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); |
| 482 |
|
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
| 483 |
|
case ']': |
| 484 |
|
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 |
| 485 |
|
{ |
| 486 |
|
strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); |
| 487 |
|
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 488 |
|
} |
| 489 |
|
tag_output_len = 0; |
| 490 |
|
if (!quote_mode) |
| 491 |
|
{ |
| 492 |
|
if (lml_tag_def[k].tag_output != NULL) |
| 493 |
|
{ |
| 494 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf); |
| 495 |
|
} |
| 496 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 497 |
|
{ |
| 498 |
|
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 499 |
|
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0); |
| 500 |
|
} |
| 501 |
|
} |
| 502 |
|
else // if (quote_mode) |
| 503 |
{ |
{ |
| 504 |
tag_param_pos = -1; |
if (lml_tag_def[k].quote_mode_output != NULL) |
| 505 |
switch (str_in[tag_start_pos + lml_tag_name_len[k]]) |
{ |
| 506 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf); |
| 507 |
|
} |
| 508 |
|
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 509 |
{ |
{ |
| 510 |
case ' ': |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 511 |
tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1; |
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); |
| 512 |
while (str_in[tag_param_pos] == ' ') |
} |
| 513 |
{ |
} |
| 514 |
tag_param_pos++; |
|
| 515 |
} |
if (line_width + tag_output_len > width) |
| 516 |
strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); |
{ |
| 517 |
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
if (i > last_i) |
| 518 |
case ']': |
{ |
| 519 |
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 |
last_i = i; |
| 520 |
{ |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 521 |
strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); |
new_line = 1; |
| 522 |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
line_width = 0; |
| 523 |
} |
i--; // redo at current i |
|
if (!quote_mode) |
|
|
{ |
|
|
if (lml_tag_def[k].tag_output != NULL) |
|
|
{ |
|
|
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; |
|
|
} |
|
|
} |
|
|
else // quote mode |
|
|
{ |
|
|
if (lml_tag_def[k].quote_mode_output != NULL) |
|
|
{ |
|
|
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; |
|
|
} |
|
|
} |
|
|
if (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); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
|
break; |
|
|
default: // tag_name not match |
|
| 524 |
continue; |
continue; |
| 525 |
} |
} |
| 526 |
break; |
else |
| 527 |
|
{ |
| 528 |
|
continue; // Output current tag in plain text if line width exceeded |
| 529 |
|
} |
| 530 |
} |
} |
| 531 |
|
|
| 532 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); |
| 533 |
|
break; |
| 534 |
|
default: |
| 535 |
|
log_error("Bug: tag name not match"); |
| 536 |
|
} |
| 537 |
|
} |
| 538 |
|
else // tag_name not found |
| 539 |
|
{ |
| 540 |
|
if (line_width + 1 > width) |
| 541 |
|
{ |
| 542 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 543 |
|
new_line = 1; |
| 544 |
|
line_width = 0; |
| 545 |
|
i--; // redo at current i |
| 546 |
|
continue; |
| 547 |
} |
} |
| 548 |
|
|
| 549 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width); |
| 550 |
|
i = tag_start_pos; // restart from tag_start_pos + 1 |
| 551 |
tag_start_pos = -1; |
tag_start_pos = -1; |
| 552 |
|
tag_name_pos = -1; |
| 553 |
|
continue; |
| 554 |
} |
} |
| 555 |
|
|
| 556 |
|
tag_start_pos = -1; |
| 557 |
|
tag_name_pos = -1; |
| 558 |
} |
} |
| 559 |
else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag |
else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag |
| 560 |
{ |
{ |
| 561 |
if (str_in[i] & 0x80) // head of multi-byte character |
if (line_width + (str_in[i] & 0x80 ? 2 : 1) > width) |
| 562 |
{ |
{ |
| 563 |
if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 564 |
{ |
new_line = 1; |
| 565 |
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len); |
line_width = 0; |
| 566 |
str_out[j] = '\0'; |
i--; // redo at current i |
| 567 |
return j; |
continue; |
| 568 |
} |
} |
| 569 |
|
|
| 570 |
|
tag_output_len = 1; |
| 571 |
|
if (str_in[i] & 0x80) // head of multi-byte character |
| 572 |
|
{ |
| 573 |
c = (str_in[i] & 0x70) << 1; |
c = (str_in[i] & 0x70) << 1; |
| 574 |
while (c & 0x80) |
while (c & 0x80) |
| 575 |
{ |
{ |
| 576 |
str_out[j++] = str_in[i++]; |
if (str_in[i + tag_output_len] == '\0') |
|
if (str_in[i] == '\0') |
|
| 577 |
{ |
{ |
| 578 |
str_out[j] = '\0'; |
break; |
|
return j; |
|
| 579 |
} |
} |
| 580 |
|
tag_output_len++; |
| 581 |
c = (c & 0x7f) << 1; |
c = (c & 0x7f) << 1; |
| 582 |
} |
} |
| 583 |
} |
} |
| 584 |
|
|
| 585 |
if (j + 1 >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, tag_output_len, line_width); |
| 586 |
{ |
i += (tag_output_len - 1); |
|
log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
str_out[j++] = str_in[i]; |
|
| 587 |
} |
} |
| 588 |
else // in LML tag |
else // in LML tag |
| 589 |
{ |
{ |
| 591 |
} |
} |
| 592 |
} |
} |
| 593 |
|
|
| 594 |
|
if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed |
| 595 |
|
{ |
| 596 |
|
tag_end_pos = i - 1; |
| 597 |
|
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 598 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); |
| 599 |
|
} |
| 600 |
|
|
| 601 |
if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
| 602 |
{ |
{ |
| 603 |
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"); |
| 604 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width); |
|
{ |
|
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
| 605 |
} |
} |
| 606 |
|
|
| 607 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 608 |
|
|
| 609 |
|
clock_end = clock(); |
| 610 |
|
lml_total_exec_duration += (clock_end - clock_begin); |
| 611 |
|
|
| 612 |
return j; |
return j; |
| 613 |
} |
} |