/[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.2 by sysadm, Mon Jun 2 23:49:47 2025 UTC Revision 1.4 by sysadm, Tue Jun 3 01:26:52 2025 UTC
# Line 24  Line 24 
24  #define LML_TAG_PARAM_BUF_LEN 256  #define LML_TAG_PARAM_BUF_LEN 256
25  #define LML_TAG_OUTPUT_BUF_LEN 1024  #define LML_TAG_OUTPUT_BUF_LEN 1024
26    
27  #define LML_TAG_COUNT 19  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",
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] = {  const static char *LML_tag_def[][3] = {
107          {"left", "[", ""},          {"left", "[", ""},
108          {"right", "]", NULL},          {"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", ""},          {"underline", "\033[4m", ""},
118          {"/underline", "\033[24m", NULL},          {"/underline", "\033[24m", NULL},
119          {"u", "\033[4m", ""},          {"u", "\033[4m", ""},
120          {"/u", "\033[24m", NULL},          {"/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", "", ""},          {"url", "", ""},
126          {"/url", "(Á´½Ó: %s)", NULL},          {"/url", "(Á´½Ó: %s)", NULL},
127          {"link", "", ""},          {"link", "", ""},
# Line 45  const static char *LML_tag_def[][3] = { Line 134  const static char *LML_tag_def[][3] = {
134          {"/article", "(ÎÄÕÂ: %s)", NULL},          {"/article", "(ÎÄÕÂ: %s)", NULL},
135          {"image", "(ͼƬ: %s)", ""},          {"image", "(ͼƬ: %s)", ""},
136          {"flash", "(Flash: %s)", ""},          {"flash", "(Flash: %s)", ""},
137          {"bwf", "\033[31m****\033[m", ""},          {"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 80  int lml_plain(const char *str_in, char * Line 171  int lml_plain(const char *str_in, char *
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 114  int lml_plain(const char *str_in, char * Line 207  int lml_plain(const char *str_in, char *
207                                                          strncpy(tag_param_buf, str_in + tag_param_pos, (size_t)MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN));                                                          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';                                                          tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
209                                                  case ']':                                                  case ']':
210                                                          if (tag_param_pos == -1 && LML_tag_def[k][2] != NULL) // Apply default param if not defined                                                          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, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);                                                                  strncpy(tag_param_buf, LML_tag_def[k][2], LML_TAG_PARAM_BUF_LEN - 1);
213                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';                                                                  tag_param_buf[LML_TAG_PARAM_BUF_LEN - 1] = '\0';
214                                                          }                                                          }
215                                                          tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);                                                          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                                                            }
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