interface-file: split out auto keyword handling

This commit is contained in:
Ariadne Conill 2020-08-20 03:41:07 -06:00
parent d36a522470
commit b57aba1a97

View file

@ -58,23 +58,8 @@ maybe_remap_token(const char *token)
return tokbuf; return tokbuf;
} }
/* map keywords to parser functions */ /* XXX: remove this global variable somehow */
struct parser_keyword { static struct lif_interface *cur_iface = NULL;
const char *token;
bool (*handle)(struct lif_dict *collection, struct lif_interface *cur_iface, const char *filename, size_t lineno, char *token, char *bufp);
};
static const struct parser_keyword keywords[] = {
};
static int
keyword_cmp(const void *a, const void *b)
{
const char *key = a;
const struct parser_keyword *token = b;
return strcmp(key, token->token);
}
static void static void
report_error(const char *filename, size_t lineno, const char *errfmt, ...) report_error(const char *filename, size_t lineno, const char *errfmt, ...)
@ -89,11 +74,50 @@ report_error(const char *filename, size_t lineno, const char *errfmt, ...)
fprintf(stderr, "%s:%zu: %s\n", filename, lineno, errbuf); fprintf(stderr, "%s:%zu: %s\n", filename, lineno, errbuf);
} }
static bool
handle_auto(struct lif_dict *collection, const char *filename, size_t lineno, char *token, char *bufp)
{
(void) filename;
(void) lineno;
(void) token;
char *ifname = lif_next_token(&bufp);
if (!*ifname && cur_iface == NULL)
return false;
else
{
cur_iface = lif_interface_collection_find(collection, ifname);
if (cur_iface == NULL)
return false;
}
cur_iface->is_auto = true;
return true;
}
/* map keywords to parser functions */
struct parser_keyword {
const char *token;
bool (*handle)(struct lif_dict *collection, const char *filename, size_t lineno, char *token, char *bufp);
};
static const struct parser_keyword keywords[] = {
{"auto", handle_auto},
};
static int
keyword_cmp(const void *a, const void *b)
{
const char *key = a;
const struct parser_keyword *token = b;
return strcmp(key, token->token);
}
bool bool
lif_interface_file_parse(struct lif_dict *collection, const char *filename) lif_interface_file_parse(struct lif_dict *collection, const char *filename)
{ {
lif_interface_collection_init(collection); lif_interface_collection_init(collection);
struct lif_interface *cur_iface = NULL;
FILE *f = fopen(filename, "r"); FILE *f = fopen(filename, "r");
if (f == NULL) if (f == NULL)
@ -116,7 +140,7 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
if (parserkw != NULL) if (parserkw != NULL)
{ {
if (!parserkw->handle(collection, cur_iface, filename, lineno, token, bufp)) if (!parserkw->handle(collection, filename, lineno, token, bufp))
goto parse_error; goto parse_error;
} }
else if (!strcmp(token, "source")) else if (!strcmp(token, "source"))
@ -134,20 +158,6 @@ lif_interface_file_parse(struct lif_dict *collection, const char *filename)
lif_interface_file_parse(collection, source_filename); lif_interface_file_parse(collection, source_filename);
} }
else if (!strcmp(token, "auto"))
{
char *ifname = lif_next_token(&bufp);
if (!*ifname && cur_iface == NULL)
goto parse_error;
else
{
cur_iface = lif_interface_collection_find(collection, ifname);
if (cur_iface == NULL)
goto parse_error;
}
cur_iface->is_auto = true;
}
else if (!strcmp(token, "iface")) else if (!strcmp(token, "iface"))
{ {
char *ifname = lif_next_token(&bufp); char *ifname = lif_next_token(&bufp);