| 178 |
} |
} |
| 179 |
} |
} |
| 180 |
|
|
| 181 |
|
#define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len) \ |
| 182 |
|
{ \ |
| 183 |
|
if ((out_buf_offset) + (tag_out_len) >= (out_buf_len)) \ |
| 184 |
|
{ \ |
| 185 |
|
log_error("Buffer is not longer enough for output string %d >= %d\n", (out_buf_offset) + (tag_out_len), (out_buf_len)); \ |
| 186 |
|
out_buf[out_buf_offset] = '\0'; \ |
| 187 |
|
return (out_buf_offset); \ |
| 188 |
|
} \ |
| 189 |
|
memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len)); \ |
| 190 |
|
(out_buf_offset) += (tag_out_len); \ |
| 191 |
|
} |
| 192 |
|
|
| 193 |
int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode) |
int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode) |
| 194 |
{ |
{ |
| 195 |
char c; |
char c; |
| 216 |
{ |
{ |
| 217 |
if (!quote_mode && !lml_tag_disabled && new_line) |
if (!quote_mode && !lml_tag_disabled && new_line) |
| 218 |
{ |
{ |
| 219 |
if (fb_quote_level > 0) |
fb_quote_level = 0; |
|
{ |
|
|
lml_tag_quote_level -= fb_quote_level; |
|
|
|
|
|
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; |
|
|
} |
|
| 220 |
|
|
| 221 |
while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str |
while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str |
| 222 |
{ |
{ |
| 223 |
fb_quote_level++; |
fb_quote_level++; |
| 224 |
} |
} |
| 225 |
|
|
| 226 |
if (fb_quote_level > 0) |
lml_tag_quote_level += fb_quote_level; |
|
{ |
|
|
lml_tag_quote_level += fb_quote_level; |
|
| 227 |
|
|
| 228 |
|
if (lml_tag_quote_level > 0) |
| 229 |
|
{ |
| 230 |
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", |
| 231 |
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]); |
| 232 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_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; |
|
| 233 |
} |
} |
| 234 |
|
|
| 235 |
new_line = 0; |
new_line = 0; |
| 237 |
|
|
| 238 |
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly |
| 239 |
{ |
{ |
| 240 |
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++) |
for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm' && str_in[k] != '\033'; k++) |
| 241 |
; |
; |
| 242 |
|
|
| 243 |
if (str_in[k] == 'm') // Valid |
if (str_in[k] != 'm') // invalid |
|
{ |
|
|
if (j + (k - i + 1) >= buf_len) |
|
|
{ |
|
|
log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len); |
|
|
str_out[j] = '\0'; |
|
|
return j; |
|
|
} |
|
|
memcpy(str_out + j, str_in + i, (size_t)(k - i + 1)); |
|
|
j += (k - i + 1); |
|
|
i = k; |
|
|
continue; |
|
|
} |
|
|
else // reach end of string |
|
| 244 |
{ |
{ |
| 245 |
break; |
k--; |
| 246 |
} |
} |
| 247 |
|
|
| 248 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1); |
| 249 |
|
i = k; |
| 250 |
|
continue; |
| 251 |
} |
} |
| 252 |
|
|
| 253 |
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 |
| 256 |
{ |
{ |
| 257 |
tag_end_pos = i - 1; |
tag_end_pos = i - 1; |
| 258 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 259 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); |
| 260 |
{ |
} |
|
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; |
|
|
} |
|
| 261 |
|
|
| 262 |
memcpy(str_out + j, str_in + tag_start_pos, (size_t)tag_output_len); |
if (fb_quote_level > 0) |
| 263 |
j += tag_output_len; |
{ |
| 264 |
|
lml_tag_quote_level -= fb_quote_level; |
| 265 |
|
|
| 266 |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m"); |
| 267 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len); |
| 268 |
} |
} |
| 269 |
|
|
| 270 |
tag_start_pos = -1; |
tag_start_pos = -1; |
| 278 |
|
|
| 279 |
if (!lml_tag_disabled && str_in[i] == '[') |
if (!lml_tag_disabled && str_in[i] == '[') |
| 280 |
{ |
{ |
| 281 |
|
if (tag_start_pos != -1) // tag is not closed |
| 282 |
|
{ |
| 283 |
|
tag_end_pos = i - 1; |
| 284 |
|
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 285 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); |
| 286 |
|
} |
| 287 |
|
|
| 288 |
tag_start_pos = i; |
tag_start_pos = i; |
| 289 |
tag_name_pos = i + 1; |
tag_name_pos = i + 1; |
| 290 |
} |
} |
| 291 |
else if (!lml_tag_disabled && str_in[i] == ']') |
else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0) |
| 292 |
{ |
{ |
| 293 |
if (tag_name_pos >= 0) |
tag_end_pos = i; |
|
{ |
|
|
tag_end_pos = i; |
|
| 294 |
|
|
| 295 |
// Skip space characters |
// Skip space characters |
| 296 |
while (str_in[tag_name_pos] == ' ') |
while (str_in[tag_name_pos] == ' ') |
| 297 |
{ |
{ |
| 298 |
tag_name_pos++; |
tag_name_pos++; |
| 299 |
} |
} |
| 300 |
|
|
| 301 |
for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++) |
for (tag_name_found = 0, k = 0; k < LML_TAG_COUNT; k++) |
| 302 |
|
{ |
| 303 |
|
if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0) |
| 304 |
{ |
{ |
| 305 |
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; |
| 306 |
|
switch (str_in[tag_name_pos + lml_tag_name_len[k]]) |
| 307 |
{ |
{ |
| 308 |
tag_param_pos = -1; |
case ' ': |
| 309 |
switch (str_in[tag_name_pos + lml_tag_name_len[k]]) |
tag_name_found = 1; |
| 310 |
|
tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; |
| 311 |
|
while (str_in[tag_param_pos] == ' ') |
| 312 |
{ |
{ |
| 313 |
case ' ': |
tag_param_pos++; |
| 314 |
tag_name_found = 1; |
} |
| 315 |
tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1; |
strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); |
| 316 |
while (str_in[tag_param_pos] == ' ') |
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
| 317 |
{ |
case ']': |
| 318 |
tag_param_pos++; |
tag_name_found = 1; |
| 319 |
} |
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 |
| 320 |
strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)); |
{ |
| 321 |
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); |
| 322 |
case ']': |
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
| 323 |
tag_name_found = 1; |
} |
| 324 |
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 |
tag_output_len = 0; |
| 325 |
|
if (!quote_mode) |
| 326 |
|
{ |
| 327 |
|
if (lml_tag_def[k].tag_output != NULL) |
| 328 |
{ |
{ |
| 329 |
strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1); |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf); |
|
tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0'; |
|
| 330 |
} |
} |
| 331 |
if (!quote_mode) |
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 332 |
{ |
{ |
| 333 |
if (lml_tag_def[k].tag_output != NULL) |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 334 |
{ |
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0); |
|
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; |
|
|
} |
|
| 335 |
} |
} |
| 336 |
else // if (quote_mode) |
} |
| 337 |
|
else // if (quote_mode) |
| 338 |
|
{ |
| 339 |
|
if (lml_tag_def[k].quote_mode_output != NULL) |
| 340 |
{ |
{ |
| 341 |
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); |
|
{ |
|
|
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; |
|
|
} |
|
| 342 |
} |
} |
| 343 |
if (j + tag_output_len >= buf_len) |
else if (lml_tag_def[k].tag_filter_cb != NULL) |
| 344 |
{ |
{ |
| 345 |
log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len); |
tag_output_len = lml_tag_def[k].tag_filter_cb( |
| 346 |
str_out[j] = '\0'; |
lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1); |
|
return j; |
|
| 347 |
} |
} |
|
memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len); |
|
|
j += tag_output_len; |
|
|
break; |
|
|
default: // tag_name not match |
|
|
continue; |
|
| 348 |
} |
} |
| 349 |
|
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len); |
| 350 |
break; |
break; |
| 351 |
|
default: // tag_name not match |
| 352 |
|
continue; |
| 353 |
} |
} |
| 354 |
|
break; |
| 355 |
} |
} |
| 356 |
|
} |
| 357 |
|
|
| 358 |
if (!tag_name_found) |
if (!tag_name_found) |
| 359 |
{ |
{ |
| 360 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 361 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); |
|
{ |
|
|
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; |
|
|
} |
|
|
|
|
|
tag_start_pos = -1; |
|
|
tag_name_pos = -1; |
|
| 362 |
} |
} |
| 363 |
|
|
| 364 |
|
tag_start_pos = -1; |
| 365 |
|
tag_name_pos = -1; |
| 366 |
} |
} |
| 367 |
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 |
| 368 |
{ |
{ |
| 406 |
{ |
{ |
| 407 |
tag_end_pos = i - 1; |
tag_end_pos = i - 1; |
| 408 |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
tag_output_len = tag_end_pos - tag_start_pos + 1; |
| 409 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len); |
|
{ |
|
|
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; |
|
| 410 |
} |
} |
| 411 |
|
|
| 412 |
if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0) |
| 413 |
{ |
{ |
| 414 |
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"); |
| 415 |
if (j + tag_output_len >= buf_len) |
CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_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; |
|
| 416 |
} |
} |
| 417 |
|
|
| 418 |
str_out[j] = '\0'; |
str_out[j] = '\0'; |