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

Contents of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations)
Wed Jul 2 04:17:33 2025 UTC (8 months, 2 weeks ago) by sysadm
Branch: MAIN
Changes since 1.12: +19 -12 lines
Content type: text/x-csrc
Support UTF8 instead of GBK

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

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