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

Contents of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations)
Sun Oct 5 08:14:06 2025 UTC (5 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.20: +1 -1 lines
Content type: text/x-csrc
Update LML_TAG_COUNT

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

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