chiark / gitweb /
implement hashmap_copy() and hashmap_merge()
[elogind.git] / load-fragment.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <assert.h>
4 #include <errno.h>
5 #include <string.h>
6
7 #include "name.h"
8 #include "strv.h"
9 #include "conf-parser.h"
10 #include "load-fragment.h"
11
12 int config_parse_names(
13                 const char *filename,
14                 unsigned line,
15                 const char *section,
16                 const char *lvalue,
17                 const char *rvalue,
18                 void *data,
19                 void *userdata) {
20
21         Set **set = data;
22         Name *name = userdata;
23         char *w;
24         size_t l;
25         char *state;
26
27         assert(filename);
28         assert(lvalue);
29         assert(rvalue);
30         assert(data);
31
32         FOREACH_WORD(w, &l, rvalue, state) {
33                 char *t;
34                 int r;
35                 Name *other;
36
37                 if (!(t = strndup(w, l)))
38                         return -ENOMEM;
39
40                 r = manager_load_name(name->meta.manager, t, &other);
41                 free(t);
42
43                 if (r < 0)
44                         return r;
45
46                 if (!*set)
47                         if (!(*set = set_new(trivial_hash_func, trivial_compare_func)))
48                                 return -ENOMEM;
49
50                 if ((r = set_put(*set, other)) < 0)
51                         return r;
52         }
53
54         return 0;
55 }
56
57 int name_load_fragment(Name *n) {
58
59         const ConfigItem items[] = {
60                 { "Names",         config_parse_strv,   &n->meta.names,          "Meta" },
61                 { "Description",   config_parse_string, &n->meta.description,    "Meta" },
62                 { "Requires",      config_parse_names,  &n->meta.requires,       "Meta" },
63                 { "SoftRequires",  config_parse_names,  &n->meta.soft_requires,  "Meta" },
64                 { "Wants",         config_parse_names,  &n->meta.wants,          "Meta" },
65                 { "Requisite",     config_parse_names,  &n->meta.requisite,      "Meta" },
66                 { "SoftRequisite", config_parse_names,  &n->meta.soft_requisite, "Meta" },
67                 { "Conflicts",     config_parse_names,  &n->meta.conflicts,      "Meta" },
68                 { "Before",        config_parse_names,  &n->meta.before,         "Meta" },
69                 { "After",         config_parse_names,  &n->meta.after,          "Meta" },
70                 { NULL, NULL, NULL, NULL }
71         };
72
73         char **t, **l;
74         int r;
75
76         assert(n);
77         assert(n->meta.state == NAME_STUB);
78
79         /* We copy the strv here so that we can iterate through it
80          * while being safe for modification */
81         if (!(l = strv_copy(n->meta.names)))
82                 return -ENOMEM;
83
84         STRV_FOREACH(t, n->meta.names)
85                 if ((r = config_parse(*t, items, n)) < 0)
86                         goto fail;
87
88         return 0;
89
90 fail:
91
92         strv_free(l);
93         return 0;
94 }