version 1.51, 2017/01/09 19:27:00 |
version 1.52, 2017/01/09 21:28:56 |
|
|
load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet) |
load_cfg(const char *path, struct client *c, struct cmdq_item *item, int quiet) |
{ |
{ |
FILE *f; |
FILE *f; |
char delim[3] = { '\\', '\\', '\0' }; |
const char delim[3] = { '\\', '\\', '\0' }; |
u_int found; |
u_int found = 0; |
size_t line = 0; |
size_t line = 0; |
char *buf, *cause1, *p; |
char *buf, *cause1, *p, *q, *s; |
struct cmd_list *cmdlist; |
struct cmd_list *cmdlist; |
struct cmdq_item *new_item; |
struct cmdq_item *new_item; |
|
int condition = 0; |
|
struct format_tree *ft; |
|
|
log_debug("loading %s", path); |
log_debug("loading %s", path); |
if ((f = fopen(path, "rb")) == NULL) { |
if ((f = fopen(path, "rb")) == NULL) { |
|
|
return (-1); |
return (-1); |
} |
} |
|
|
found = 0; |
|
while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) { |
while ((buf = fparseln(f, NULL, &line, delim, 0)) != NULL) { |
log_debug("%s: %s", path, buf); |
log_debug("%s: %s", path, buf); |
|
|
/* Skip empty lines. */ |
|
p = buf; |
p = buf; |
while (isspace((u_char) *p)) |
while (isspace((u_char)*p)) |
p++; |
p++; |
if (*p == '\0') { |
if (*p == '\0') { |
free(buf); |
free(buf); |
continue; |
continue; |
} |
} |
|
q = p + strlen(p) - 1; |
|
while (q != p && isspace((u_char)*q)) |
|
*q-- = '\0'; |
|
|
/* Parse and run the command. */ |
if (condition != 0 && strcmp(p, "%endif") == 0) { |
|
condition = 0; |
|
continue; |
|
} |
|
if (strncmp(p, "%if ", 4) == 0) { |
|
if (condition != 0) { |
|
cfg_add_cause("%s:%zu: nested %%if", path, |
|
line); |
|
continue; |
|
} |
|
ft = format_create(NULL, FORMAT_NOJOBS); |
|
|
|
s = p + 3; |
|
while (isspace((u_char)*s)) |
|
s++; |
|
s = format_expand(ft, s); |
|
if (*s != '\0' && (s[0] != '0' || s[1] != '\0')) |
|
condition = 1; |
|
else |
|
condition = -1; |
|
free(s); |
|
|
|
format_free(ft); |
|
continue; |
|
} |
|
if (condition == -1) |
|
continue; |
|
|
if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) { |
if (cmd_string_parse(p, &cmdlist, path, line, &cause1) != 0) { |
free(buf); |
free(buf); |
if (cause1 == NULL) |
if (cause1 == NULL) |