lib/param_list: Add new parameter list routines
[petitboot] / lib / param_list / param_list.c
1
2 #define _GNU_SOURCE
3
4 #include <assert.h>
5 #include <string.h>
6
7 #include <log/log.h>
8 #include <talloc/talloc.h>
9 #include <param_list/param_list.h>
10
11 const char **common_known_params(void)
12 {
13         static const char *common[] = {
14                 "auto-boot?",
15                 "petitboot,network",
16                 "petitboot,timeout",
17                 "petitboot,bootdevs",
18                 "petitboot,language",
19                 "petitboot,debug?",
20                 "petitboot,write?",
21                 "petitboot,snapshots?",
22                 "petitboot,console",
23                 "petitboot,http_proxy",
24                 "petitboot,https_proxy",
25                 NULL,
26         };
27
28         return common;
29 }
30
31 void param_list_init(struct param_list *pl, const char *known_params[])
32 {
33         assert(known_params);
34         list_init(&pl->params);
35         pl->known_params = known_params;
36 }
37
38 bool param_list_is_known_n(const struct param_list *pl, const char *name,
39         unsigned int name_len)
40 {
41         const char **known;
42
43         assert(pl->known_params);
44
45         param_list_for_each_known_param(pl, known) {
46                 if (name_len == strlen(*known) && !strncmp(name, *known, name_len)) {
47                         return true;
48                 }
49         }
50
51         return false;
52 }
53
54 bool param_list_is_known(const struct param_list *pl, const char *name)
55 {
56         const char **known;
57
58         assert(pl->known_params);
59
60         param_list_for_each_known_param(pl, known) {
61                 if (!strcmp(name, *known)) {
62                         return true;
63                 }
64         }
65
66         return false;
67 }
68
69 struct param *param_list_get_param(struct param_list *pl, const char *name)
70 {
71         struct param *param;
72
73         param_list_for_each(pl, param) {
74                 if (!strcmp(param->name, name))
75                         return param;
76         }
77         return NULL;
78 }
79
80 void param_list_set(struct param_list *pl, const char *name, const char *value,
81         bool modified_on_create)
82 {
83         struct param *param;
84
85         param_list_for_each(pl, param) {
86                 if (strcmp(param->name, name))
87                         continue;
88
89                 if (!strcmp(param->value, value))
90                         return;
91
92                 /* Update existing list entry. */
93                 talloc_free(param->value);
94                 param->value = talloc_strdup(param, value);
95                 param->modified = true;
96                 pb_debug_fn("Updated: %s:%s\n", name, value);
97                 return;
98         }
99
100         /* Add new entry to list. */
101         param = talloc(pl, struct param);
102         param->modified = modified_on_create;
103         param->name = talloc_strdup(pl, name);
104         param->value = talloc_strdup(pl, value);
105         list_add(&pl->params, &param->list);
106         pb_debug_fn("Created: %s:%s\n", name, value);
107 }
108
109 void param_list_set_non_empty(struct param_list *pl, const char *name, const char *value,
110         bool modified_on_create)
111 {
112         if (!param_list_get_value(pl, name) && !strlen(value)) {
113                 return;
114         }
115
116         param_list_set(pl, name, value, modified_on_create);
117 }
118