/[LeafOK_CVS]/lbbs/src/lml.c
ViewVC logotype

Contents of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.49 - (show annotations)
Sat Nov 22 10:35:51 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.48: +18 -0 lines
Content type: text/x-csrc
Fix bug: return immediately when the last char (at offset = tag_out_len - 1) of tag_out is NULL

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * lml
4 * - LML render
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #ifdef HAVE_CONFIG_H
10 #include "config.h"
11 #endif
12
13 #include "common.h"
14 #include "lml.h"
15 #include "log.h"
16 #include "str_process.h"
17 #include <ctype.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/param.h>
21
22 enum _lml_constant_t
23 {
24 LML_TAG_PARAM_BUF_LEN = 256,
25 LML_TAG_OUTPUT_BUF_LEN = 1024,
26 LML_TAG_QUOTE_MAX_LEVEL = 10,
27 };
28
29 clock_t lml_total_exec_duration = 0;
30
31 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);
32
33 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)
34 {
35 if (strcasecmp(tag_name, "color") == 0)
36 {
37 if (strcasecmp(tag_param_buf, "red") == 0)
38 {
39 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;31m");
40 }
41 else if (strcasecmp(tag_param_buf, "green") == 0)
42 {
43 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;32m");
44 }
45 else if (strcasecmp(tag_param_buf, "yellow") == 0)
46 {
47 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;33m");
48 }
49 else if (strcasecmp(tag_param_buf, "blue") == 0)
50 {
51 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;34m");
52 }
53 else if (strcasecmp(tag_param_buf, "magenta") == 0)
54 {
55 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;35m");
56 }
57 else if (strcasecmp(tag_param_buf, "cyan") == 0)
58 {
59 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;36m");
60 }
61 else if (strcasecmp(tag_param_buf, "black") == 0)
62 {
63 return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;30;47m");
64 }
65 }
66 return 0;
67 }
68
69 static const char *lml_tag_quote_color[] = {
70 "\033[33m", // yellow
71 "\033[32m", // green
72 "\033[35m", // magenta
73 };
74
75 static const int LML_TAG_QUOTE_LEVEL_LOOP = (int)(sizeof(lml_tag_quote_color) / sizeof(const char *));
76
77 static int lml_tag_quote_level = 0;
78
79 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)
80 {
81 if (strcasecmp(tag_name, "quote") == 0)
82 {
83 if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
84 {
85 lml_tag_quote_level++;
86 }
87 return snprintf(tag_output_buf, tag_output_buf_len, "%s",
88 lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
89 }
90 else if (strcasecmp(tag_name, "/quote") == 0)
91 {
92 if (lml_tag_quote_level > 0)
93 {
94 lml_tag_quote_level--;
95 }
96 return snprintf(tag_output_buf, tag_output_buf_len, "%s",
97 (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
98 }
99
100 return 0;
101 }
102
103 static int lml_tag_disabled = 0;
104
105 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)
106 {
107 lml_tag_disabled = 1;
108
109 return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));
110 }
111
112 typedef struct lml_tag_def_t
113 {
114 const char *tag_name; // tag name
115 const char *tag_output; // output string
116 const char *default_param; // default param string
117 const char *quote_mode_output; // output string in quote mode
118 lml_tag_filter_cb tag_filter_cb; // tag filter callback
119 } LML_TAG_DEF;
120
121 const LML_TAG_DEF lml_tag_def[] = {
122 // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
123 {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
124 {"nolml", "", NULL, "", NULL}, // deprecated
125 {"lml", "", NULL, "", NULL}, // deprecated
126 {"align", "", "", "", NULL}, // N/A
127 {"/align", "", "", "", NULL}, // N/A
128 {"size", "", "", "", NULL}, // N/A
129 {"/size", "", "", "", NULL}, // N/A
130 {"left", "[", "", "[left]", NULL},
131 {"right", "]", "", "[right]", NULL},
132 {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
133 {"/bold", "\033[22m", NULL, "", NULL},
134 {"b", "\033[1m", "", "", NULL},
135 {"/b", "\033[22m", NULL, "", NULL},
136 {"italic", "\033[5m", "", "", NULL}, // use blink instead
137 {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
138 {"i", "\033[5m", "", "", NULL},
139 {"/i", "\033[m", NULL, "", NULL},
140 {"underline", "\033[4m", "", "", NULL},
141 {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm
142 {"u", "\033[4m", "", "", NULL},
143 {"/u", "\033[m", NULL, "", NULL},
144 {"color", NULL, NULL, "", lml_tag_color_filter},
145 {"/color", "\033[m", NULL, "", NULL},
146 {"quote", NULL, NULL, "", lml_tag_quote_filter},
147 {"/quote", NULL, NULL, "", lml_tag_quote_filter},
148 {"url", "", "", "", NULL},
149 {"/url", "(链接: %s)", NULL, "", NULL},
150 {"link", "", "", "", NULL},
151 {"/link", "(链接: %s)", NULL, "", NULL},
152 {"email", "", "", "", NULL},
153 {"/email", "(Email: %s)", NULL, "", NULL},
154 {"user", "", "", "", NULL},
155 {"/user", "(用户: %s)", NULL, "", NULL},
156 {"article", "", "", "", NULL},
157 {"/article", "(文章: %s)", NULL, "", NULL},
158 {"image", "(图片: %s)", "", "%s", NULL},
159 {"flash", "(Flash: %s)", "", "", NULL},
160 {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
161 };
162
163 static const int lml_tag_count = sizeof(lml_tag_def) / sizeof(LML_TAG_DEF);
164 static int lml_tag_name_len[sizeof(lml_tag_def) / sizeof(LML_TAG_DEF)];
165 static int lml_ready = 0;
166
167 inline static void lml_init(void)
168 {
169 int i;
170
171 if (!lml_ready)
172 {
173 for (i = 0; i < lml_tag_count; i++)
174 {
175 lml_tag_name_len[i] = (int)strlen(lml_tag_def[i].tag_name);
176 }
177
178 lml_ready = 1;
179 }
180 }
181
182 #define CHECK_AND_APPEND_OUTPUT(out_buf, out_buf_len, out_buf_offset, tag_out, tag_out_len, line_width) \
183 if ((tag_out_len) > 0) \
184 { \
185 if ((out_buf_offset) + (tag_out_len) >= (out_buf_len)) \
186 { \
187 log_error("Buffer is not longer enough for output string %d >= %d\n", (out_buf_offset) + (tag_out_len), (out_buf_len)); \
188 out_buf[out_buf_offset] = '\0'; \
189 return (out_buf_offset); \
190 } \
191 memcpy((out_buf) + (out_buf_offset), (tag_out), (size_t)(tag_out_len)); \
192 *((out_buf) + (out_buf_offset) + (size_t)(tag_out_len)) = '\0'; \
193 (line_width) += str_length((out_buf) + (out_buf_offset), 1); \
194 (out_buf_offset) += (tag_out_len); \
195 if ((tag_out)[(tag_out_len) - 1] == '\0') \
196 { \
197 (out_buf_offset)--; \
198 return (out_buf_offset); \
199 } \
200 }
201
202 int lml_render(const char *str_in, char *str_out, int buf_len, int width, int quote_mode)
203 {
204 clock_t clock_begin;
205 clock_t clock_end;
206
207 char c;
208 char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
209 char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
210 int i;
211 int j = 0;
212 int k;
213 int tag_start_pos = -1;
214 int tag_name_pos = -1;
215 int tag_end_pos = -1;
216 int tag_param_pos = -1;
217 int tag_output_len;
218 int new_line = 1;
219 int fb_quote_level = 0;
220 int tag_name_found;
221 int line_width = 0;
222 char tab_spaces[TAB_SIZE + 1];
223 int tab_width = 0;
224
225 #ifdef _DEBUG
226 size_t str_in_len = strlen(str_in);
227 #endif
228
229 clock_begin = clock();
230
231 lml_init();
232
233 lml_tag_disabled = 0;
234 lml_tag_quote_level = 0;
235
236 if (width <= 0)
237 {
238 width = INT_MAX;
239 }
240
241 for (i = 0; str_in[i] != '\0'; i++)
242 {
243 #ifdef _DEBUG
244 if (i >= str_in_len)
245 {
246 log_error("Bug: i(%d) >= str_in_len(%d)\n", i, str_in_len);
247 break;
248 }
249 #endif
250
251 if (!lml_tag_disabled && new_line)
252 {
253 while (str_in[i] == ':' && str_in[i + 1] == ' ') // FB2000 quote leading str
254 {
255 fb_quote_level++;
256 lml_tag_quote_level++;
257 i += 2;
258 }
259
260 if (!quote_mode && lml_tag_quote_level > 0)
261 {
262 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
263 lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
264 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
265 }
266
267 for (k = 0; k < fb_quote_level; k++)
268 {
269 tag_output_buf[k * 2] = ':';
270 tag_output_buf[k * 2 + 1] = ' ';
271 }
272 tag_output_buf[fb_quote_level * 2] = '\0';
273 tag_output_len = fb_quote_level * 2;
274 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
275
276 new_line = 0;
277 }
278
279 if (lml_tag_disabled && new_line)
280 {
281 new_line = 0;
282 }
283
284 if (!quote_mode && !lml_tag_disabled && str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence
285 {
286 for (k = i + 2; isdigit((int)str_in[k]) || str_in[k] == ';' || str_in[k] == '?'; k++)
287 ;
288
289 if (str_in[k] == 'm') // valid -- copy directly
290 {
291 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, k - i + 1, line_width);
292 }
293 else if (isalpha((int)str_in[k]))
294 {
295 // unsupported ANSI CSI command
296 }
297 else
298 {
299 k--;
300 }
301
302 i = k;
303 continue;
304 }
305
306 if (str_in[i] == '\n') // jump out of tag at end of line
307 {
308 if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed
309 {
310 if (line_width + 1 > width)
311 {
312 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
313 new_line = 1;
314 line_width = 0;
315 i--; // redo at current i
316 continue;
317 }
318
319 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width);
320 i = tag_start_pos; // restart from tag_start_pos + 1
321 tag_start_pos = -1;
322 tag_name_pos = -1;
323 continue;
324 }
325
326 if (!lml_tag_disabled && fb_quote_level > 0)
327 {
328 lml_tag_quote_level -= fb_quote_level;
329
330 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, (quote_mode ? "" : "\033[m"));
331 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
332
333 fb_quote_level = 0;
334 }
335
336 if (new_line)
337 {
338 continue;
339 }
340
341 tag_start_pos = -1;
342 tag_name_pos = -1;
343 new_line = 1;
344 line_width = -1;
345 }
346 else if (str_in[i] == '\r' || str_in[i] == '\7')
347 {
348 continue; // Skip special characters
349 }
350 else if (str_in[i] == '\t')
351 {
352 tab_width = TAB_SIZE - (line_width % TAB_SIZE);
353 if (line_width + tab_width > width)
354 {
355 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
356 new_line = 1;
357 line_width = 0;
358 // skip current Tab
359 continue;
360 }
361
362 for (k = 0; k < tab_width; k++)
363 {
364 tab_spaces[k] = ' ';
365 }
366 tab_spaces[tab_width] = '\0';
367 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tab_spaces, tab_width, line_width);
368 continue;
369 }
370
371 if (!lml_tag_disabled && str_in[i] == '[')
372 {
373 if (tag_start_pos != -1) // tag is not closed
374 {
375 if (line_width + 1 > width)
376 {
377 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
378 new_line = 1;
379 line_width = 0;
380 i--; // redo at current i
381 continue;
382 }
383
384 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width);
385 i = tag_start_pos; // restart from tag_start_pos + 1
386 tag_start_pos = -1;
387 tag_name_pos = -1;
388 continue;
389 }
390
391 tag_start_pos = i;
392 tag_name_pos = i + 1;
393 }
394 else if (!lml_tag_disabled && str_in[i] == ']' && tag_name_pos >= 0)
395 {
396 tag_end_pos = i;
397
398 // Skip space characters
399 while (str_in[tag_name_pos] == ' ')
400 {
401 tag_name_pos++;
402 }
403
404 for (tag_name_found = 0, k = 0; k < lml_tag_count; k++)
405 {
406 if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_name_pos, (size_t)lml_tag_name_len[k]) == 0)
407 {
408 tag_param_pos = -1;
409 switch (str_in[tag_name_pos + lml_tag_name_len[k]])
410 {
411 case ' ':
412 tag_name_found = 1;
413 tag_param_pos = tag_name_pos + lml_tag_name_len[k] + 1;
414 while (str_in[tag_param_pos] == ' ')
415 {
416 tag_param_pos++;
417 }
418 strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
419 tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
420 case ']':
421 tag_name_found = 1;
422 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
423 {
424 strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
425 tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
426 }
427 tag_output_len = 0;
428 if (!quote_mode)
429 {
430 if (lml_tag_def[k].tag_output != NULL)
431 {
432 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
433 }
434 else if (lml_tag_def[k].tag_filter_cb != NULL)
435 {
436 tag_output_len = lml_tag_def[k].tag_filter_cb(
437 lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
438 }
439 }
440 else // if (quote_mode)
441 {
442 if (lml_tag_def[k].quote_mode_output != NULL)
443 {
444 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
445 }
446 else if (lml_tag_def[k].tag_filter_cb != NULL)
447 {
448 tag_output_len = lml_tag_def[k].tag_filter_cb(
449 lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
450 }
451 }
452 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
453 break;
454 default: // tag_name not match
455 continue;
456 }
457 break;
458 }
459 }
460
461 if (!tag_name_found)
462 {
463 if (line_width + 1 > width)
464 {
465 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
466 new_line = 1;
467 line_width = 0;
468 i--; // redo at current i
469 continue;
470 }
471
472 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "[", 1, line_width);
473 i = tag_start_pos; // restart from tag_start_pos + 1
474 tag_start_pos = -1;
475 tag_name_pos = -1;
476 continue;
477 }
478
479 tag_start_pos = -1;
480 tag_name_pos = -1;
481 }
482 else if (lml_tag_disabled || tag_name_pos == -1) // not in LML tag
483 {
484 if (line_width + (str_in[i] & 0x80 ? 2 : 1) > width)
485 {
486 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, "\n", 1, line_width);
487 new_line = 1;
488 line_width = 0;
489 i--; // redo at current i
490 continue;
491 }
492
493 tag_output_len = 1;
494 if (str_in[i] & 0x80) // head of multi-byte character
495 {
496 c = (str_in[i] & 0x70) << 1;
497 while (c & 0x80)
498 {
499 if (str_in[i + tag_output_len] == '\0')
500 {
501 break;
502 }
503 tag_output_len++;
504 c = (c & 0x7f) << 1;
505 }
506 }
507
508 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + i, tag_output_len, line_width);
509 i += (tag_output_len - 1);
510 }
511 else // in LML tag
512 {
513 // Do nothing
514 }
515 }
516
517 if (!lml_tag_disabled && tag_start_pos != -1) // tag is not closed
518 {
519 tag_end_pos = i - 1;
520 tag_output_len = tag_end_pos - tag_start_pos + 1;
521 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, str_in + tag_start_pos, tag_output_len, line_width);
522 }
523
524 if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
525 {
526 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
527 CHECK_AND_APPEND_OUTPUT(str_out, buf_len, j, tag_output_buf, tag_output_len, line_width);
528 }
529
530 str_out[j] = '\0';
531
532 clock_end = clock();
533 lml_total_exec_duration += (clock_end - clock_begin);
534
535 return j;
536 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1