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

Contents of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.25 - (show annotations)
Fri Oct 24 03:57:45 2025 UTC (4 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.24: +21 -2 lines
Content type: text/x-csrc
Support [lml] and [nolml] tags

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 #define LML_TAG_QUOTE_LEVEL_LOOP 3
67
68 static const char *lml_tag_quote_color[] = {
69 "\033[33m", // yellow
70 "\033[32m", // green
71 "\033[35m", // magenta
72 };
73
74 static int lml_tag_quote_level = 0;
75
76 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)
77 {
78 if (strcasecmp(tag_name, "quote") == 0)
79 {
80 if (lml_tag_quote_level <= LML_TAG_QUOTE_MAX_LEVEL)
81 {
82 lml_tag_quote_level++;
83 }
84 return snprintf(tag_output_buf, tag_output_buf_len, "%s",
85 lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
86 }
87 else if (strcasecmp(tag_name, "/quote") == 0)
88 {
89 if (lml_tag_quote_level > 0)
90 {
91 lml_tag_quote_level--;
92 }
93 return snprintf(tag_output_buf, tag_output_buf_len, "%s",
94 (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
95 }
96
97 return 0;
98 }
99
100 static int lml_tag_disabled = 0;
101
102 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)
103 {
104 lml_tag_disabled = 1;
105
106 return snprintf(tag_output_buf, tag_output_buf_len, "%s", (quote_mode ? "[plain]" : ""));
107 }
108
109 static int lml_tag_user_disabled = 0;
110
111 static int lml_tag_user_disable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
112 {
113 lml_tag_user_disabled = 1;
114 tag_output_buf[0] = '\0';
115 return 0;
116 }
117
118 static int lml_tag_user_enable_filter(const char *tag_name, const char *tag_param_buf, char *tag_output_buf, size_t tag_output_buf_len, int quote_mode)
119 {
120 lml_tag_user_disabled = 0;
121 tag_output_buf[0] = '\0';
122 return 0;
123 }
124
125 typedef struct lml_tag_def_t
126 {
127 const char *tag_name; // tag name
128 const char *tag_output; // output string
129 const char *default_param; // default param string
130 const char *quote_mode_output; // output string in quote mode
131 lml_tag_filter_cb tag_filter_cb; // tag filter callback
132 } LML_TAG_DEF;
133
134 const LML_TAG_DEF lml_tag_def[] = {
135 // Definition of tuple: {lml_tag, lml_output, default_param, quote_mode_output, lml_filter_cb}
136 {"plain", NULL, NULL, NULL, lml_tag_disable_filter},
137 {"nolml", NULL, NULL, NULL, lml_tag_user_disable_filter},
138 {"lml", NULL, NULL, NULL, lml_tag_user_enable_filter},
139 {"left", "[", "", "[left]", NULL},
140 {"right", "]", "", "[right]", NULL},
141 {"bold", "\033[1m", "", "", NULL}, // does not work in Fterm
142 {"/bold", "\033[22m", NULL, "", NULL},
143 {"b", "\033[1m", "", "", NULL},
144 {"/b", "\033[22m", NULL, "", NULL},
145 {"italic", "\033[5m", "", "", NULL}, // use blink instead
146 {"/italic", "\033[m", NULL, "", NULL}, // \033[25m does not work in Fterm
147 {"i", "\033[5m", "", "", NULL},
148 {"/i", "\033[m", NULL, "", NULL},
149 {"underline", "\033[4m", "", "", NULL},
150 {"/underline", "\033[m", NULL, "", NULL}, // \033[24m does not work in Fterm
151 {"u", "\033[4m", "", "", NULL},
152 {"/u", "\033[m", NULL, "", NULL},
153 {"color", NULL, NULL, "", lml_tag_color_filter},
154 {"/color", "\033[m", NULL, "", NULL},
155 {"quote", NULL, NULL, "", lml_tag_quote_filter},
156 {"/quote", NULL, NULL, "", lml_tag_quote_filter},
157 {"url", "", "", "", NULL},
158 {"/url", "(链接: %s)", NULL, "", NULL},
159 {"link", "", "", "", NULL},
160 {"/link", "(链接: %s)", NULL, "", NULL},
161 {"email", "", "", "", NULL},
162 {"/email", "(Email: %s)", NULL, "", NULL},
163 {"user", "", "", "", NULL},
164 {"/user", "(用户: %s)", NULL, "", NULL},
165 {"article", "", "", "", NULL},
166 {"/article", "(文章: %s)", NULL, "", NULL},
167 {"image", "(图片: %s)", "", "%s", NULL},
168 {"flash", "(Flash: %s)", "", "", NULL},
169 {"bwf", "\033[1;31m****\033[m", "", "****", NULL},
170 };
171
172 #define LML_TAG_COUNT (sizeof(lml_tag_def) / sizeof(LML_TAG_DEF))
173
174 static int lml_tag_name_len[LML_TAG_COUNT];
175 static int lml_ready = 0;
176
177 inline static void lml_init(void)
178 {
179 int i;
180
181 if (!lml_ready)
182 {
183 for (i = 0; i < LML_TAG_COUNT; i++)
184 {
185 lml_tag_name_len[i] = (int)strlen(lml_tag_def[i].tag_name);
186 }
187
188 lml_ready = 1;
189 }
190 }
191
192 int lml_render(const char *str_in, char *str_out, int buf_len, int quote_mode)
193 {
194 char c;
195 char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
196 char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
197 int i;
198 int j = 0;
199 int k;
200 int tag_start_pos = -1;
201 int tag_end_pos = -1;
202 int tag_param_pos = -1;
203 int tag_output_len;
204 int new_line = 1;
205 int fb_quote_level = 0;
206
207 lml_init();
208
209 lml_tag_disabled = 0;
210 lml_tag_user_disabled = 0;
211 lml_tag_quote_level = 0;
212
213 for (i = 0; str_in[i] != '\0'; i++)
214 {
215 if (!quote_mode && !lml_tag_disabled && new_line)
216 {
217 if (fb_quote_level > 0)
218 {
219 lml_tag_quote_level -= fb_quote_level;
220
221 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
222 (lml_tag_quote_level > 0 ? lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP] : "\033[m"));
223 if (j + tag_output_len >= buf_len)
224 {
225 log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
226 str_out[j] = '\0';
227 return j;
228 }
229 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
230 j += tag_output_len;
231
232 fb_quote_level = 0;
233 }
234
235 while (str_in[i + fb_quote_level * 2] == ':' && str_in[i + fb_quote_level * 2 + 1] == ' ') // FB2000 quote leading str
236 {
237 fb_quote_level++;
238 }
239
240 if (fb_quote_level > 0)
241 {
242 lml_tag_quote_level += fb_quote_level;
243
244 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "%s",
245 lml_tag_quote_color[(lml_tag_quote_level) % LML_TAG_QUOTE_LEVEL_LOOP]);
246 if (j + tag_output_len >= buf_len)
247 {
248 log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
249 str_out[j] = '\0';
250 return j;
251 }
252 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
253 j += tag_output_len;
254 }
255
256 new_line = 0;
257 }
258
259 if (str_in[i] == '\033' && str_in[i + 1] == '[') // Escape sequence -- copy directly
260 {
261 for (k = i + 2; str_in[k] != '\0' && str_in[k] != 'm'; k++)
262 ;
263
264 if (str_in[k] == 'm') // Valid
265 {
266 if (j + (k - i + 1) >= buf_len)
267 {
268 log_error("Buffer is not longer enough for output string %d >= %d\n", j + (k - i + 1), buf_len);
269 str_out[j] = '\0';
270 return j;
271 }
272 memcpy(str_out + j, str_in + i, (size_t)(k - i + 1));
273 j += (k - i + 1);
274 i = k;
275 continue;
276 }
277 else // reach end of string
278 {
279 break;
280 }
281 }
282
283 if (str_in[i] == '\n')
284 {
285 tag_start_pos = -1; // jump out of tag at end of line
286 new_line = 1;
287 }
288 else if (str_in[i] == '\r')
289 {
290 continue; // ignore '\r'
291 }
292
293 if (!lml_tag_disabled && str_in[i] == '[')
294 {
295 tag_start_pos = i + 1;
296 }
297 else if (!lml_tag_disabled && str_in[i] == ']')
298 {
299 if (tag_start_pos >= 0)
300 {
301 tag_end_pos = i;
302
303 // Skip space characters
304 while (str_in[tag_start_pos] == ' ')
305 {
306 tag_start_pos++;
307 }
308
309 for (k = 0; k < LML_TAG_COUNT; k++)
310 {
311 if (strncasecmp(lml_tag_def[k].tag_name, str_in + tag_start_pos, (size_t)lml_tag_name_len[k]) == 0)
312 {
313 tag_param_pos = -1;
314 switch (str_in[tag_start_pos + lml_tag_name_len[k]])
315 {
316 case ' ':
317 tag_param_pos = tag_start_pos + lml_tag_name_len[k] + 1;
318 while (str_in[tag_param_pos] == ' ')
319 {
320 tag_param_pos++;
321 }
322 strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
323 tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
324 case ']':
325 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
326 {
327 strncpy(tag_param_buf, lml_tag_def[k].default_param, LML_TAG_PARAM_BUF_LEN - 1);
328 tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
329 }
330 if (!quote_mode && !lml_tag_user_disabled)
331 {
332 if (lml_tag_def[k].tag_output != NULL)
333 {
334 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].tag_output, tag_param_buf);
335 }
336 else if (lml_tag_def[k].tag_filter_cb != NULL)
337 {
338 tag_output_len = lml_tag_def[k].tag_filter_cb(
339 lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 0);
340 }
341 else
342 {
343 tag_output_len = 0;
344 }
345 }
346 else // if (quote_mode || lml_tag_user_disabled)
347 {
348 if (lml_tag_def[k].quote_mode_output != NULL)
349 {
350 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, lml_tag_def[k].quote_mode_output, tag_param_buf);
351 }
352 else if (lml_tag_def[k].tag_filter_cb != NULL)
353 {
354 tag_output_len = lml_tag_def[k].tag_filter_cb(
355 lml_tag_def[k].tag_name, tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, 1);
356 }
357 else
358 {
359 tag_output_len = 0;
360 }
361 }
362 if (j + tag_output_len >= buf_len)
363 {
364 log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
365 str_out[j] = '\0';
366 return j;
367 }
368 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
369 j += tag_output_len;
370 break;
371 default: // tag_name not match
372 continue;
373 }
374 break;
375 }
376 }
377
378 tag_start_pos = -1;
379 }
380 }
381 else if (lml_tag_disabled || tag_start_pos == -1) // not in LML tag
382 {
383 if (str_in[i] & 0x80) // head of multi-byte character
384 {
385 if (j + 4 >= buf_len) // Assuming UTF-8 CJK characters use 4 bytes, though most of them actually use 3 bytes
386 {
387 log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 4, buf_len);
388 str_out[j] = '\0';
389 return j;
390 }
391
392 c = (str_in[i] & 0x70) << 1;
393 while (c & 0x80)
394 {
395 str_out[j++] = str_in[i++];
396 if (str_in[i] == '\0')
397 {
398 str_out[j] = '\0';
399 return j;
400 }
401 c = (c & 0x7f) << 1;
402 }
403 }
404
405 if (j + 1 >= buf_len)
406 {
407 log_error("Buffer is not longer enough for output string %ld >= %d\n", j + 1, buf_len);
408 str_out[j] = '\0';
409 return j;
410 }
411 str_out[j++] = str_in[i];
412 }
413 else // in LML tag
414 {
415 // Do nothing
416 }
417 }
418
419 if (!quote_mode && !lml_tag_disabled && lml_tag_quote_level > 0)
420 {
421 tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, "\033[m");
422 if (j + tag_output_len >= buf_len)
423 {
424 log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len);
425 str_out[j] = '\0';
426 return j;
427 }
428 memcpy(str_out + j, tag_output_buf, (size_t)tag_output_len);
429 j += tag_output_len;
430 }
431
432 str_out[j] = '\0';
433
434 return j;
435 }

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