| 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 |
#define LML_TAG_COUNT 19 |
| 28 |
|
|
| 29 |
const static char *LML_tag_name[][2] = { |
const static char *LML_tag_def[][3] = { |
| 30 |
{"left", "["}, |
{"left", "[", ""}, |
| 31 |
{"right", "]"}, |
{"right", "]", NULL}, |
| 32 |
{"underline", "\033[4m"}, |
{"underline", "\033[4m", ""}, |
| 33 |
{"/underline", "\033[24m"}, |
{"/underline", "\033[24m", NULL}, |
| 34 |
{"u", "\033[4m"}, |
{"u", "\033[4m", ""}, |
| 35 |
{"/u", "\033[24m"}, |
{"/u", "\033[24m", NULL}, |
| 36 |
{"url", ""}, |
{"url", "", ""}, |
| 37 |
{"/url", "(链接: %s)"}, |
{"/url", "(链接: %s)", NULL}, |
| 38 |
{"link", ""}, |
{"link", "", ""}, |
| 39 |
{"/link", "(链接: %s)"}, |
{"/link", "(链接: %s)", NULL}, |
| 40 |
{"email", ""}, |
{"email", "", ""}, |
| 41 |
{"/email", "(Email: %s)"}, |
{"/email", "(Email: %s)", NULL}, |
| 42 |
{"user", ""}, |
{"user", "", ""}, |
| 43 |
{"/user", "(用户: %s)"}, |
{"/user", "(用户: %s)", NULL}, |
| 44 |
{"article", ""}, |
{"article", "", ""}, |
| 45 |
{"/article", "(文章: %s)"}, |
{"/article", "(文章: %s)", NULL}, |
| 46 |
{"hide", ""}, |
{"image", "(图片: %s)", ""}, |
| 47 |
{"/hide", ""}, |
{"flash", "(Flash: %s)", ""}, |
| 48 |
{"image", "(图片: %s)"}, |
{"bwf", "\033[31m****\033[m", ""}, |
|
{"flash", "(Flash: %s)"}, |
|
|
{"bwf", "\033[31m****\033[m"}, |
|
| 49 |
}; |
}; |
| 50 |
|
|
| 51 |
static int LML_tag_name_len[LML_TAG_COUNT]; |
static int LML_tag_name_len[LML_TAG_COUNT]; |
| 59 |
{ |
{ |
| 60 |
for (i = 0; i < LML_TAG_COUNT; i++) |
for (i = 0; i < LML_TAG_COUNT; i++) |
| 61 |
{ |
{ |
| 62 |
LML_tag_name_len[i] = (int)strlen(LML_tag_name[i][0]); |
LML_tag_name_len[i] = (int)strlen(LML_tag_def[i][0]); |
| 63 |
} |
} |
| 64 |
|
|
| 65 |
LML_init = 1; |
LML_init = 1; |
| 68 |
|
|
| 69 |
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) |
| 70 |
{ |
{ |
| 71 |
char tag_param_buf[LML_TAG_PARAM_MAX_LEN + 1]; |
char tag_param_buf[LML_TAG_PARAM_BUF_LEN]; |
| 72 |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
char tag_output_buf[LML_TAG_OUTPUT_BUF_LEN]; |
| 73 |
int i; |
int i; |
| 74 |
int j = 0; |
int j = 0; |
| 75 |
int k; |
int k; |
| 76 |
int tag_start_pos = -1; |
int tag_start_pos = -1; |
| 77 |
int tag_end_pos = -1; |
int tag_end_pos = -1; |
| 78 |
int tag_param_pos; |
int tag_param_pos = -1; |
| 79 |
int tag_output_len; |
int tag_output_len; |
| 80 |
|
|
| 81 |
lml_init(); |
lml_init(); |
| 100 |
|
|
| 101 |
for (k = 0; k < LML_TAG_COUNT; k++) |
for (k = 0; k < LML_TAG_COUNT; k++) |
| 102 |
{ |
{ |
| 103 |
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) |
| 104 |
{ |
{ |
| 105 |
|
tag_param_pos = -1; |
| 106 |
switch (str_in[tag_start_pos + LML_tag_name_len[k]]) |
switch (str_in[tag_start_pos + LML_tag_name_len[k]]) |
| 107 |
{ |
{ |
| 108 |
case ' ': |
case ' ': |
| 111 |
{ |
{ |
| 112 |
tag_param_pos++; |
tag_param_pos++; |
| 113 |
} |
} |
| 114 |
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)); |
| 115 |
|
tag_param_buf[MIN(tag_end_pos - tag_param_pos, LML_TAG_PARAM_BUF_LEN)] = '\0'; |
| 116 |
|
case ']': |
| 117 |
|
if (tag_param_pos == -1 && LML_tag_def[k][2] != NULL) // Apply default param if not defined |
| 118 |
{ |
{ |
| 119 |
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); |
| 120 |
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'; |
| 121 |
} |
} |
| 122 |
case ']': |
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_def[k][1], tag_param_buf); |
|
tag_output_len = snprintf(tag_output_buf, LML_TAG_OUTPUT_BUF_LEN, LML_tag_name[k][1], tag_param_buf); |
|
| 123 |
if (j + tag_output_len >= buf_len - 1) |
if (j + tag_output_len >= buf_len - 1) |
| 124 |
{ |
{ |
| 125 |
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); |