File: config-file.c

package info (click to toggle)
ifupdown-ng 0.12.1-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ansic: 3,572; sh: 980; makefile: 233
file content (75 lines) | stat: -rw-r--r-- 2,309 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * 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,
	.allow_any_iface_as_template = true,
	.auto_executor_selection = true,
	.compat_create_interfaces = true,
	.compat_ifupdown2_bridge_ports_inherit_vlans = true,
	.implicit_template_conversion = true,
	.use_hostname_for_dhcp = 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},
	{"allow_any_iface_as_template", set_bool_value, &lif_config.allow_any_iface_as_template},
	{"auto_executor_selection", set_bool_value, &lif_config.auto_executor_selection},
	{"compat_create_interfaces", set_bool_value, &lif_config.compat_create_interfaces},
	{"compat_ifupdown2_bridge_ports_inherit_vlans", set_bool_value, &lif_config.compat_ifupdown2_bridge_ports_inherit_vlans},
	{"implicit_template_conversion", set_bool_value, &lif_config.implicit_template_conversion},
	{"use_hostname_for_dhcp", set_bool_value, &lif_config.use_hostname_for_dhcp},
};

bool
lif_config_load(const char *filename)
{
	FILE *fd = fopen(filename, "r");

	if (fd == NULL)
	{
#if 0
		fprintf(stderr, "ifupdown-ng: cannot open config %s: %s\n",
			filename, strerror(errno));
#endif
		return false;
	}

	return lif_config_parse_file(fd, filename, handlers, ARRAY_SIZE(handlers));
}