Commit d093352f authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Add ability to return an error message in the local interface.

parent c7b44dac
......@@ -295,7 +295,7 @@ main(int argc, char **argv)
config_files[num_config_files++] = optarg;
break;
case 'C':
rc = parse_config_from_string(optarg, strlen(optarg));
rc = parse_config_from_string(optarg, strlen(optarg), NULL);
if(rc != CONFIG_ACTION_DONE) {
fprintf(stderr,
"Couldn't parse configuration from command line.\n");
......
......@@ -853,11 +853,14 @@ parse_option(int c, gnc_t gnc, void *closure, char *token)
}
static int
parse_config_line(int c, gnc_t gnc, void *closure, int *action_return)
parse_config_line(int c, gnc_t gnc, void *closure,
int *action_return, const char **message_return)
{
char *token;
if(action_return)
*action_return = CONFIG_ACTION_DONE;
if(message_return)
*message_return = NULL;
c = skip_whitespace(c, gnc, closure);
if(c < 0 || c == '\n' || c == '#')
......@@ -1002,7 +1005,7 @@ parse_config_from_file(const char *filename, int *line_return)
return 0;
while(1) {
c = parse_config_line(c, (gnc_t)gnc_file, &s, NULL);
c = parse_config_line(c, (gnc_t)gnc_file, &s, NULL, NULL);
if(c < -1) {
*line_return = s.line;
return -1;
......@@ -1030,19 +1033,22 @@ gnc_buf(struct buf_state *s)
}
int
parse_config_from_string(char *string, int n)
parse_config_from_string(char *string, int n, const char **message_return)
{
int c, action;
const char *message;
struct buf_state s = { string, 0, n };
c = gnc_buf(&s);
if(c < 0)
return -1;
c = parse_config_line(c, (gnc_t)gnc_buf, &s, &action);
if(c == -1)
c = parse_config_line(c, (gnc_t)gnc_buf, &s, &action, &message);
if(c == -1) {
if(message_return)
*message_return = message;
return action;
else
} else
return -1;
}
......
......@@ -58,7 +58,7 @@ extern struct interface_conf *default_interface_conf;
void flush_ifconf(struct interface_conf *if_conf);
int parse_config_from_file(const char *filename, int *line_return);
int parse_config_from_string(char *string, int n);
int parse_config_from_string(char *string, int n, const char **message_return);
void renumber_filters(void);
int input_filter(const unsigned char *id,
......
......@@ -315,7 +315,8 @@ local_read(struct local_socket *s)
{
int rc;
char *eol;
char *reply = "ok\n";
char reply[100] = "ok\n";
const char *message = NULL;
if(s->buf == NULL)
s->buf = malloc(LOCAL_BUFSIZE);
......@@ -336,13 +337,13 @@ local_read(struct local_socket *s)
if(eol == NULL)
return 1;
rc = parse_config_from_string(s->buf, eol + 1 - s->buf);
rc = parse_config_from_string(s->buf, eol + 1 - s->buf, &message);
switch(rc) {
case CONFIG_ACTION_DONE:
break;
case CONFIG_ACTION_QUIT:
shutdown(s->fd, 1);
reply = NULL;
reply[0] = '\0';
break;
case CONFIG_ACTION_DUMP:
local_notify_all_1(s);
......@@ -355,13 +356,14 @@ local_read(struct local_socket *s)
s->monitor = 0;
break;
case CONFIG_ACTION_NO:
reply = "no\n";
snprintf(reply, sizeof(reply), "no%s%s\n",
message ? " " : "", message ? message : "");
break;
default:
reply = "bad\n";
snprintf(reply, sizeof(reply), "bad\n");
}
if(reply != NULL) {
if(reply[0] != '\0') {
rc = write_timeout(s->fd, reply, strlen(reply));
if(rc < 0)
goto fail;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment