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

Diff of /lbbs/src/lml.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Mon Jun 2 15:01:55 2025 UTC Revision 1.4 by sysadm, Tue Jun 3 01:26:52 2025 UTC
# Line 16  Line 16 
16    
17  #include "lml.h"  #include "lml.h"
18  #include "log.h"  #include "log.h"
19    #include "common.h"
20  #include <stdio.h>  #include <stdio.h>
21  #include <string.h>  #include <string.h>
22  #include <sys/param.h>  #include <sys/param.h>
23    
24  #define LML_TAG_PARAM_MAX_LEN 20  #define LML_TAG_PARAM_BUF_LEN 256
25  #define LML_TAG_OUTPUT_BUF_LEN 256  #define LML_TAG_OUTPUT_BUF_LEN 1024
26    
27  #define LML_TAG_COUNT 21  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  const static char *LML_tag_name[][2] = {  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          {"left", "["},  {
31          {"right", "]"},          if (strcasecmp(tag_name, "color") == 0)
32          {"underline", "\033[4m"},          {
33          {"/underline", "\033[24m"},                  if (strcasecmp(tag_param_buf, "red") == 0)
34          {"u", "\033[4m"},                  {
35          {"/u", "\033[24m"},                          return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;31m");
36          {"url", ""},                  }
37          {"/url", "(链接: %s)"},                  else if (strcasecmp(tag_param_buf, "green") == 0)
38          {"link", ""},                  {
39          {"/link", "(链接: %s)"},                          return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;32m");
40          {"email", ""},                  }
41          {"/email", "(Email: %s)"},                  else if (strcasecmp(tag_param_buf, "yellow") == 0)
42          {"user", ""},                  {
43          {"/user", "(用户: %s)"},                          return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;33m");
44          {"article", ""},                  }
45          {"/article", "(文章: %s)"},                  else if (strcasecmp(tag_param_buf, "blue") == 0)
46          {"hide", ""},                  {
47          {"/hide", ""},                          return snprintf(tag_output_buf, tag_output_buf_len, "\033[1;34m");
48          {"image", "(图片: %s)"},                  }
49          {"flash", "(Flash: %s)"},                  else if (strcasecmp(tag_param_buf, "magenta") == 0)
50          {"bwf", "\033[31m****\033[m"},                  {
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",
70            "\033[32m",
71            "\033[35m",
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    
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, 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                    if (lml_tag_quote_level > 0)
94                    {
95                            return snprintf(tag_output_buf, tag_output_buf_len, lml_tag_quote_color[lml_tag_quote_level % LML_TAG_QUOTE_LEVEL_LOOP]);
96                    }
97                    else
98                    {
99                            return snprintf(tag_output_buf, tag_output_buf_len, "\033[m");
100                    }
101            }
102    
103            return 0;
104    }
105    
106    const static char *LML_tag_def[][3] = {
107            {"left", "[", ""},
108            {"right", "]", NULL},
109            {"bold", "\033[1m", ""},
110            {"/bold", "\033[21m", NULL},
111            {"b", "\033[1m", ""},
112            {"/b", "\033[21m", NULL},
113            {"italic", "\033[3m", ""},
114            {"/italic", "\033[23m", NULL},
115            {"i", "\033[3m", ""},
116            {"/i", "\033[23m", NULL},
117            {"underline", "\033[4m", ""},
118            {"/underline", "\033[24m", NULL},
119            {"u", "\033[4m", ""},
120            {"/u", "\033[24m", NULL},
121            {"color", NULL, (const char *)lml_tag_color_filter},
122            {"/color", "\033[m", NULL},
123            {"quote", NULL, (const char *)lml_tag_quote_filter},
124            {"/quote", NULL, (const char *)lml_tag_quote_filter},
125            {"url", "", ""},
126            {"/url", "(链接: %s)", NULL},
127            {"link", "", ""},
128            {"/link", "(链接: %s)", NULL},
129            {"email", "", ""},
130            {"/email", "(Email: %s)", NULL},
131            {"user", "", ""},
132            {"/user", "(用户: %s)", NULL},
133            {"article", "", ""},
134            {"/article", "(文章: %s)", NULL},
135            {"image", "(图片: %s)", ""},
136            {"flash", "(Flash: %s)", ""},
137            {"bwf", "\033[1;31m****\033[m", ""},
138  };  };
139    
140    #define LML_TAG_COUNT 31
141    
142  static int LML_tag_name_len[LML_TAG_COUNT];  static int LML_tag_name_len[LML_TAG_COUNT];
143  static int LML_init = 0;  static int LML_init = 0;
144    
# Line 60  inline static void lml_init(void) Line 150  inline static void lml_init(void)
150          {          {
151                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
152                  {                  {
153                          LML_tag_name_len[i] = (int)strlen(LML_tag_name[i][0]);                          LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]);
154                  }                  }
155    
156                  LML_init = 1;                  LML_init = 1;
# Line 69  inline static void lml_init(void) Line 159  inline static void lml_init(void)
159    
160  int lml_plain(const char *str_in, char *str_out, int buf_len)  int lml_plain(const char *str_in, char *str_out, int buf_len)
161  {  {
162          char tag_param_buf[LML_TAG_PARAM_MAX_LEN + 1];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
163          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
164          int i;          int i;
165          int j = 0;          int j = 0;
166          int k;          int k;
167          int tag_start_pos = -1;          int tag_start_pos = -1;
168          int tag_end_pos = -1;          int tag_end_pos = -1;
169          int tag_param_pos;          int tag_param_pos = -1;
170          int tag_output_len;          int tag_output_len;
171    
172          lml_init();          lml_init();
173    
174            lml_tag_quote_level = 0;
175    
176          for (i = 0; str_in[i] != '\0'; i++)          for (i = 0; str_in[i] != '\0'; i++)
177          {          {
178                  if (str_in[i] == '[')                  if (str_in[i] == '[')
# Line 101  int lml_plain(const char *str_in, char * Line 193  int lml_plain(const char *str_in, char *
193    
194                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (k = 0; k < LML_TAG_COUNT; k++)
195                                  {                                  {
196                                          if (strncasecmp(LML_tag_name[k][0], str_in + tag_start_pos, (size_t)LML_tag_name_len[k]) == 0)                                          if (strncasecmp(LML_tag_def[k][0], str_in + tag_start_pos, (size_t)LML_tag_name_len[k]) == 0)
197                                          {                                          {
198                                                    tag_param_pos = -1;
199                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])
200                                                  {                                                  {
201                                                  case ' ':                                                  case ' ':
# Line 111  int lml_plain(const char *str_in, char * Line 204  int lml_plain(const char *str_in, char *
204                                                          {                                                          {
205                                                                  tag_param_pos++;                                                                  tag_param_pos++;
206                                                          }                                                          }
207                                                          if (tag_end_pos - tag_param_pos > 0)                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));
208                                                            tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
209                                                    case ']':
210                                                            if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined
211                                                          {                                                          {
212                                                                  strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_MAX_LEN));                                                                  strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);
213                                                                  tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_MAX_LEN)] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
214                                                            }
215                                                            if (LML_tag_def[k][1] != NULL)
216                                                            {
217                                                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);
218                                                            }
219                                                            else
220                                                            {
221                                                                    tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(
222                                                                            LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);
223                                                          }                                                          }
                                                 case ']':  
                                                         tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_name[k][1], tag_param_buf);  
224                                                          if (j + tag_output_len >= buf_len - 1)                                                          if (j + tag_output_len >= buf_len - 1)
225                                                          {                                                          {
226                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);                                                                  log_error("Buffer is not longer enough for output string %d >= %d\n", j + tag_output_len, buf_len - 1);


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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