libifupdown: add config file loader

This commit is contained in:
Ariadne Conill 2020-09-14 17:15:18 -06:00
parent fafa5ed7f9
commit 02d044c391
4 changed files with 94 additions and 1 deletions

View file

@ -35,7 +35,8 @@ LIBIFUPDOWN_SRC = \
libifupdown/environment.c \ libifupdown/environment.c \
libifupdown/execute.c \ libifupdown/execute.c \
libifupdown/lifecycle.c \ libifupdown/lifecycle.c \
libifupdown/config-parser.c libifupdown/config-parser.c \
libifupdown/config-file.c
LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o} LIBIFUPDOWN_OBJ = ${LIBIFUPDOWN_SRC:.c=.o}
LIBIFUPDOWN_LIB = libifupdown.a LIBIFUPDOWN_LIB = libifupdown.a

61
libifupdown/config-file.c Normal file
View file

@ -0,0 +1,61 @@
/*
* libifupdown/config-file.c
* Purpose: config file loading
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "libifupdown/libifupdown.h"
struct lif_config_file lif_config = {
.allow_addon_scripts = true,
};
static bool
set_bool_value(const char *key, const char *value, void *opaque)
{
(void) key;
if (*value == '1' ||
*value == 'Y' || *value == 'y' ||
*value == 'T' || *value == 't')
*(bool *) opaque = true;
else if (*value == '0' ||
*value == 'N' || *value == 'n' ||
*value == 'F' || *value == 'f')
*(bool *) opaque = false;
else
return false;
return true;
}
static struct lif_config_handler handlers[] = {
{"allow_addon_scripts", set_bool_value, &lif_config.allow_addon_scripts},
};
bool
lif_config_load(const char *filename)
{
FILE *fd = fopen(filename, "r");
if (fd == NULL)
{
fprintf(stderr, "ifupdown-ng: cannot open config %s: %s\n",
filename, strerror(errno));
return false;
}
return lif_config_parse_file(fd, filename, handlers, ARRAY_SIZE(handlers));
}

29
libifupdown/config-file.h Normal file
View file

@ -0,0 +1,29 @@
/*
* libifupdown/config-file.h
* Purpose: config file loading
*
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* This software is provided 'as is' and without any warranty, express or
* implied. In no event shall the authors be liable for any damages arising
* from the use of this software.
*/
#ifndef LIBIFUPDOWN__CONFIG_FILE_H
#define LIBIFUPDOWN__CONFIG_FILE_H
#include <stdbool.h>
struct lif_config_file {
bool allow_addon_scripts;
};
extern struct lif_config_file lif_config;
extern bool lif_config_load(const char *filename);
#endif

View file

@ -27,6 +27,8 @@
#include "libifupdown/execute.h" #include "libifupdown/execute.h"
#include "libifupdown/lifecycle.h" #include "libifupdown/lifecycle.h"
#include "libifupdown/tokenize.h" #include "libifupdown/tokenize.h"
#include "libifupdown/config-file.h"
#include "libifupdown/config-parser.h"
#ifndef ARRAY_SIZE #ifndef ARRAY_SIZE
# define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x)) # define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x))