Skip to content

Commit

Permalink
conf: trim prefix space for multiline option
Browse files Browse the repository at this point in the history
  • Loading branch information
pymumu committed Jun 15, 2023
1 parent 08567c4 commit a6e5ceb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/dns_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ static int _conf_domain_rule_address(char *domain, const char *domain_address)
field = ptr;
ptr = strstr(ptr, ",");

if (field == NULL) {
if (field == NULL || *field == '\0') {
break;
}

Expand Down Expand Up @@ -1657,7 +1657,8 @@ static int _conf_domain_rule_address(char *domain, const char *domain_address)
_dns_rule_put(address);
}

tlog(TLOG_ERROR, "add address %s, %s failed", domain, domain_address);
tlog(TLOG_ERROR, "add address %s, %s at %s:%d failed", domain, domain_address, conf_get_conf_file(),
conf_get_current_lineno());
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions src/include/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ int load_conf(const char *file, struct config_item items[], conf_error_handler h

void load_exit(void);

int conf_get_current_lineno(void);

const char *conf_get_conf_file(void);

const char *conf_get_conf_fullpath(const char *path, char *fullpath, size_t path_len);
Expand Down
43 changes: 41 additions & 2 deletions src/lib/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@
#include <unistd.h>

static const char *current_conf_file = NULL;
static int current_conf_lineno = 0;

const char *conf_get_conf_file(void)
{
return current_conf_file;
}

int conf_get_current_lineno(void)
{
return current_conf_lineno;
}

static char *get_dir_name(char *path)
{
if (strstr(path, "/") == NULL) {
Expand Down Expand Up @@ -355,6 +361,8 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro
int line_no = 0;
int line_len = 0;
int read_len = 0;
int is_last_line_wrap = 0;
int current_line_wrap = 0;
const char *last_file = NULL;

if (handler == NULL) {
Expand All @@ -368,14 +376,44 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro

line_no = 0;
while (fgets(line + line_len, MAX_LINE_LEN - line_len, fp)) {
current_line_wrap = 0;
line_no++;
read_len = strnlen(line + line_len, sizeof(line));
if (read_len >= 2 && *(line + line_len + read_len - 2) == '\\') {
line_len += read_len - 2;
read_len -= 1;
current_line_wrap = 1;
}

/* comment in wrap line, skip */
if (is_last_line_wrap && read_len > 0) {
if (*(line + line_len) == '#') {
continue;
}
}

/* trim prefix spaces in wrap line */
if ((current_line_wrap == 1 || is_last_line_wrap == 1) && read_len > 0) {
is_last_line_wrap = current_line_wrap;
read_len -= 1;
for (i = 0; i < read_len; i++) {
char *ptr = line + line_len + i;
if (*ptr == ' ' || *ptr == '\t') {
continue;
}

memmove(line + line_len, ptr, read_len - i + 1);
line_len += read_len - i;
break;
}

line[line_len] = '\0';
continue;
if (current_line_wrap) {
continue;
}
}

line_len = 0;
is_last_line_wrap = 0;

filed_num = sscanf(line, "%63s %8191[^\r\n]s", key, value);
if (filed_num <= 0) {
Expand Down Expand Up @@ -419,6 +457,7 @@ static int load_conf_file(const char *file, struct config_item *items, conf_erro
/* call item function */
last_file = current_conf_file;
current_conf_file = file;
current_conf_lineno = line_no;
call_ret = items[i].item_func(items[i].item, items[i].data, argc, argv);
ret = handler(file, line_no, call_ret);
if (ret != 0) {
Expand Down

0 comments on commit a6e5ceb

Please sign in to comment.