more config functions
[project/uci.git] / list.c
1 /*
2  * libuci - Library for the Unified Configuration Interface
3  * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
4  *
5  * this program is free software; you can redistribute it and/or modify
6  * it under the terms of the gnu lesser general public license version 2.1
7  * as published by the free software foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 /* initialize a list head/item */
16 static inline void uci_list_init(struct uci_list *ptr)
17 {
18         ptr->prev = ptr;
19         ptr->next = ptr;
20 }
21
22 /* inserts a new list entry between two consecutive entries */
23 static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
24 {
25         prev->next = ptr;
26         next->prev = ptr;
27         ptr->prev = prev;
28         ptr->next = next;
29 }
30
31 /* inserts a new list entry at the tail of the list */
32 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
33 {
34         /* NB: head->prev points at the tail */
35         __uci_list_add(head->prev, head, ptr);
36 }
37
38 static inline void uci_list_del(struct uci_list *ptr)
39 {
40         struct uci_list *next, *prev;
41
42         next = ptr->next;
43         prev = ptr->prev;
44
45         prev->next = next;
46         next->prev = prev;
47 }
48
49 static void uci_drop_option(struct uci_option *option)
50 {
51         if (!option)
52                 return;
53         if (option->name)
54                 free(option->name);
55         if (option->value)
56                 free(option->value);
57         free(option);
58 }
59
60 static struct uci_option *uci_add_option(struct uci_section *section, const char *name, const char *value)
61 {
62         struct uci_config *cfg = section->config;
63         struct uci_context *ctx = cfg->ctx;
64         struct uci_option *option = NULL;
65
66         UCI_TRAP_SAVE(ctx, error);
67         option = (struct uci_option *) uci_malloc(ctx, sizeof(struct uci_option));
68         option->name = uci_strdup(ctx, name);
69         option->value = uci_strdup(ctx, value);
70         uci_list_add(&section->options, &option->list);
71         UCI_TRAP_RESTORE(ctx);
72
73 error:
74         uci_drop_option(option);
75         UCI_THROW(ctx, ctx->errno);
76         return NULL;
77 }
78
79 static void uci_drop_section(struct uci_section *section)
80 {
81         struct uci_option *opt;
82
83         if (!section)
84                 return;
85
86         uci_foreach_entry(option, &section->options, opt) {
87                 uci_list_del(&opt->list);
88                 uci_drop_option(opt);
89         }
90
91         if (section->name)
92                 free(section->name);
93         if (section->type)
94                 free(section->type);
95         free(section);
96 }
97
98 static struct uci_section *uci_add_section(struct uci_config *cfg, const char *type, const char *name)
99 {
100         struct uci_section *section = NULL;
101         struct uci_context *ctx = cfg->ctx;
102
103         UCI_TRAP_SAVE(ctx, error);
104         section = (struct uci_section *) uci_malloc(ctx, sizeof(struct uci_section));
105         section->config = cfg;
106         uci_list_init(&section->list);
107         uci_list_init(&section->options);
108         section->type = uci_strdup(ctx, type);
109         if (name)
110                 section->name = uci_strdup(ctx, name);
111         uci_list_add(&cfg->sections, &section->list);
112         UCI_TRAP_RESTORE(ctx);
113
114         return section;
115
116 error:
117         uci_drop_section(section);
118         UCI_THROW(ctx, ctx->errno);
119         return NULL;
120 }
121
122 static void uci_drop_file(struct uci_config *cfg)
123 {
124         struct uci_section *s;
125
126         if(!cfg)
127                 return;
128
129         uci_foreach_entry(section, &cfg->sections, s) {
130                 uci_list_del(&s->list);
131                 uci_drop_section(s);
132         }
133
134         if (cfg->name)
135                 free(cfg->name);
136         free(cfg);
137 }
138
139
140 static struct uci_config *uci_alloc_file(struct uci_context *ctx, const char *name)
141 {
142         struct uci_config *cfg = NULL;
143
144         UCI_TRAP_SAVE(ctx, error);
145         cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config));
146         uci_list_init(&cfg->list);
147         uci_list_init(&cfg->sections);
148         cfg->name = uci_strdup(ctx, name);
149         cfg->ctx = ctx;
150         UCI_TRAP_RESTORE(ctx);
151         return cfg;
152
153 error:
154         uci_drop_file(cfg);
155         UCI_THROW(ctx, ctx->errno);
156         return NULL;
157 }
158