| 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-2025 by Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "common.h" |
#include "common.h" |
| 10 |
#include "lml.h" |
#include "lml.h" |
| 11 |
#include "log.h" |
#include "log.h" |
| 12 |
#include "str_process.h" |
#include "str_process.h" |
| 13 |
|
#include <ctype.h> |
| 14 |
#include <stdio.h> |
#include <stdio.h> |
| 15 |
#include <string.h> |
#include <string.h> |
| 16 |
#include <sys/param.h> |
#include <sys/param.h> |
| 18 |
#define LML_TAG_PARAM_BUF_LEN 256 |
#define LML_TAG_PARAM_BUF_LEN 256 |
| 19 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
#define LML_TAG_OUTPUT_BUF_LEN 1024 |
| 20 |
|
|
| 21 |
|
clock_t lml_total_exec_duration = 0; |
| 22 |
|
|
| 23 |
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); |
| 24 |
|
|
| 25 |
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) |
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) |
| 190 |
|
|
| 191 |
int lml_render(const char *str_in, char *str_out, int buf_len, int width, int quote_mode) |
int lml_render(const char *str_in, char *str_out, int buf_len, int width, int quote_mode) |
| 192 |
{ |
{ |
| 193 |
|
clock_t clock_begin; |
| 194 |
|
clock_t clock_end; |
| 195 |
|
|
| 196 |
char c; |
char c; |
| 197 |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 198 |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
| 209 |
int tag_name_found; |
int tag_name_found; |
| 210 |
int line_width = 0; |
int line_width = 0; |
| 211 |
|
|
| 212 |
|
clock_begin = clock(); |
| 213 |
|
|
| 214 |
lml_init(); |
lml_init(); |
| 215 |
|
|
| 216 |
lml_tag_disabled = 0; |
lml_tag_disabled = 0; |
| 251 |
new_line = 0; |
new_line = 0; |
| 252 |
} |
} |
| 253 |
|
|
| 254 |
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
if (lml_tag_disabled && new_line) |
| 255 |
|
{ |
| 256 |
|
new_line = 0; |
| 257 |
|
} |
| 258 |
|
|
| 259 |
|
if (!quote_mode && !lml_tag_disabled && str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence |
| 260 |
{ |
{ |
| 261 |
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm' && str_in[k] != '\033'; k++) |
for (k = i + 2; isdigit(str_in[k]) || str_in[k] == ';' || str_in[k] == '?'; k++) |
| 262 |
; |
; |
| 263 |
|
|
| 264 |
if (str_in[k] != 'm') // invalid |
if (str_in[k] == 'm') // valid -- copy directly |
| 265 |
|
{ |
| 266 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1, line_width); |
| 267 |
|
} |
| 268 |
|
else if (isalpha(str_in[k])) |
| 269 |
|
{ |
| 270 |
|
// unsupported ANSI CSI command |
| 271 |
|
} |
| 272 |
|
else |
| 273 |
{ |
{ |
| 274 |
k--; |
k--; |
| 275 |
} |
} |
| 276 |
|
|
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1, line_width); |
|
| 277 |
i = k; |
i = k; |
| 278 |
continue; |
continue; |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
if (str_in[i] == '\n') // jump out of tag at end of line |
if (str_in[i] == '\n') // jump out of tag at end of line |
| 282 |
{ |
{ |
| 283 |
if (tag_start_pos != -1) // tag is not closed |
if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed |
| 284 |
{ |
{ |
| 285 |
tag_end_pos = i - 1; |
tag_end_pos = i - 1; |
| 286 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 287 |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); |
|
| 288 |
|
if (line_width + tag_output_len > width) |
| 289 |
|
{ |
| 290 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 291 |
|
new_line = 1; |
| 292 |
|
line_width = 0; |
| 293 |
|
i--; // redo at current i |
| 294 |
|
} |
| 295 |
|
else |
| 296 |
|
{ |
| 297 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); |
| 298 |
|
} |
| 299 |
} |
} |
| 300 |
|
|
| 301 |
if (fb_quote_level > 0) |
if (!lml_tag_disabled && fb_quote_level > 0) |
| 302 |
{ |
{ |
| 303 |
lml_tag_quote_level -= fb_quote_level; |
lml_tag_quote_level -= fb_quote_level; |
| 304 |
|
|
| 308 |
fb_quote_level = 0; |
fb_quote_level = 0; |
| 309 |
} |
} |
| 310 |
|
|
| 311 |
|
if (new_line) |
| 312 |
|
{ |
| 313 |
|
continue; |
| 314 |
|
} |
| 315 |
|
|
| 316 |
tag_start_pos = -1; |
tag_start_pos = -1; |
| 317 |
tag_name_pos = -1; |
tag_name_pos = -1; |
| 318 |
new_line = 1; |
new_line = 1; |
| 319 |
line_width = 0; |
line_width = -1; |
| 320 |
} |
} |
| 321 |
else if (str_in[i] == '\r' || str_in[i] == '\7') |
else if (str_in[i] == '\r' || str_in[i] == '\7') |
| 322 |
{ |
{ |
| 404 |
|
|
| 405 |
if (!tag_name_found) |
if (!tag_name_found) |
| 406 |
{ |
{ |
| 407 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
if (line_width + 1 > width) |
| 408 |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width); |
{ |
| 409 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 410 |
|
new_line = 1; |
| 411 |
|
line_width = 0; |
| 412 |
|
i--; // redo at current i |
| 413 |
|
continue; |
| 414 |
|
} |
| 415 |
|
|
| 416 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width); |
| 417 |
|
i = tag_start_pos; // restart from tag_start_pos + 1 |
| 418 |
|
tag_start_pos = -1; |
| 419 |
|
tag_name_pos = -1; |
| 420 |
|
continue; |
| 421 |
} |
} |
| 422 |
|
|
| 423 |
tag_start_pos = -1; |
tag_start_pos = -1; |
| 430 |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width); |
| 431 |
new_line = 1; |
new_line = 1; |
| 432 |
line_width = 0; |
line_width = 0; |
| 433 |
i--; // redo at current $i |
i--; // redo at current i |
| 434 |
continue; |
continue; |
| 435 |
} |
} |
| 436 |
|
|
| 458 |
} |
} |
| 459 |
} |
} |
| 460 |
|
|
| 461 |
if (tag_start_pos != -1) // tag is not closed |
if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed |
| 462 |
{ |
{ |
| 463 |
tag_end_pos = i - 1; |
tag_end_pos = i - 1; |
| 464 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 473 |
|
|
| 474 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |
| 475 |
|
|
| 476 |
|
clock_end = clock(); |
| 477 |
|
lml_total_exec_duration += (clock_end - clock_begin); |
| 478 |
|
|
| 479 |
return j; |
return j; |
| 480 |
} |
} |