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

Contents of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.33 - (show annotations)
Wed Oct 29 13:45:46 2025 UTC (4 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.32: +34 -20 lines
Content type: text/x-csrc
Refine and bug fix

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

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