/[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.3 by sysadm, Tue Jun 3 00:57:27 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    const static char *LML_tag_def[][3] = {
66            {"left", "[", ""},
67            {"right", "]", NULL},
68            {"bold", "\033[1m", ""},
69            {"/bold", "\033[21m", NULL},
70            {"b", "\033[1m", ""},
71            {"/b", "\033[21m", NULL},
72            {"italic", "\033[3m", ""},
73            {"/italic", "\033[23m", NULL},
74            {"i", "\033[3m", ""},
75            {"/i", "\033[23m", NULL},
76            {"underline", "\033[4m", ""},
77            {"/underline", "\033[24m", NULL},
78            {"u", "\033[4m", ""},
79            {"/u", "\033[24m", NULL},
80            {"color", NULL, (const char *)lml_tag_color_filter},
81            {"/color", "\033[m", NULL},
82            {"url", "", ""},
83            {"/url", "(链接: %s)", NULL},
84            {"link", "", ""},
85            {"/link", "(链接: %s)", NULL},
86            {"email", "", ""},
87            {"/email", "(Email: %s)", NULL},
88            {"user", "", ""},
89            {"/user", "(用户: %s)", NULL},
90            {"article", "", ""},
91            {"/article", "(文章: %s)", NULL},
92            {"image", "(图片: %s)", ""},
93            {"flash", "(Flash: %s)", ""},
94            {"bwf", "\033[1;31m****\033[m", ""},
95  };  };
96    
97    #define LML_TAG_COUNT 29
98    
99  static int LML_tag_name_len[LML_TAG_COUNT];  static int LML_tag_name_len[LML_TAG_COUNT];
100  static int LML_init = 0;  static int LML_init = 0;
101    
# Line 60  inline static void lml_init(void) Line 107  inline static void lml_init(void)
107          {          {
108                  for (i = 0; i < LML_TAG_COUNT; i++)                  for (i = 0; i < LML_TAG_COUNT; i++)
109                  {                  {
110                          LML_tag_name_len[i] = (int)strlen(LML_tag_name[i][0]);                          LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]);
111                  }                  }
112    
113                  LML_init = 1;                  LML_init = 1;
# Line 69  inline static void lml_init(void) Line 116  inline static void lml_init(void)
116    
117  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)
118  {  {
119          char tag_param_buf[LML_TAG_PARAM_MAX_LEN + 1];          char tag_param_buf[LML_TAG_PARAM_BUF_LEN];
120          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];          char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN];
121          int i;          int i;
122          int j = 0;          int j = 0;
123          int k;          int k;
124          int tag_start_pos = -1;          int tag_start_pos = -1;
125          int tag_end_pos = -1;          int tag_end_pos = -1;
126          int tag_param_pos;          int tag_param_pos = -1;
127          int tag_output_len;          int tag_output_len;
128    
129          lml_init();          lml_init();
# Line 101  int lml_plain(const char *str_in, char * Line 148  int lml_plain(const char *str_in, char *
148    
149                                  for (k = 0; k < LML_TAG_COUNT; k++)                                  for (k = 0; k < LML_TAG_COUNT; k++)
150                                  {                                  {
151                                          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)
152                                          {                                          {
153                                                    tag_param_pos = -1;
154                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])                                                  switch (str_in[tag_start_pos + LML_tag_name_len[k]])
155                                                  {                                                  {
156                                                  case ' ':                                                  case ' ':
# Line 111  int lml_plain(const char *str_in, char * Line 159  int lml_plain(const char *str_in, char *
159                                                          {                                                          {
160                                                                  tag_param_pos++;                                                                  tag_param_pos++;
161                                                          }                                                          }
162                                                          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));
163                                                            tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0';
164                                                    case ']':
165                                                            if (tag_param_pos == -1 && LML_tag_def[k][1] != NULL && LML_tag_def[k][2] != NULL) // Apply default param if not defined
166                                                          {                                                          {
167                                                                  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);
168                                                                  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';
169                                                            }
170                                                            if (LML_tag_def[k][1] != NULL)
171                                                            {
172                                                                    tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf);
173                                                            }
174                                                            else
175                                                            {
176                                                                    tag_output_len = ((lml_tag_filter_cb)LML_tag_def[k][2])(
177                                                                            LML_tag_def[k][0], tag_param_buf, tag_output_buf, LML_TAG_OUTPUT_BUF_LEN);
178                                                          }                                                          }
                                                 case ']':  
                                                         tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_name[k][1], tag_param_buf);  
179                                                          if (j + tag_output_len >= buf_len - 1)                                                          if (j + tag_output_len >= buf_len - 1)
180                                                          {                                                          {
181                                                                  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