remove unnecessary null pointer check
[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 #include <glob.h>
16
17 /* initialize a list head/item */
18 static inline void uci_list_init(struct uci_list *ptr)
19 {
20         ptr->prev = ptr;
21         ptr->next = ptr;
22 }
23
24 /* inserts a new list entry between two consecutive entries */
25 static inline void uci_list_insert(struct uci_list *list, struct uci_list *ptr)
26 {
27         list->next->prev = ptr;
28         ptr->prev = list;
29         ptr->next = list->next;
30         list->next = ptr;
31 }
32
33 /* inserts a new list entry at the tail of the list */
34 static inline void uci_list_add(struct uci_list *head, struct uci_list *ptr)
35 {
36         /* NB: head->prev points at the tail */
37         uci_list_insert(head->prev, ptr);
38 }
39
40 static inline void uci_list_del(struct uci_list *ptr)
41 {
42         struct uci_list *next, *prev;
43
44         next = ptr->next;
45         prev = ptr->prev;
46
47         prev->next = next;
48         next->prev = prev;
49
50         uci_list_init(ptr);
51 }
52
53 static struct uci_element *
54 uci_alloc_generic(struct uci_context *ctx, int type, const char *name, int size)
55 {
56         struct uci_element *e;
57         void *ptr;
58
59         ptr = uci_malloc(ctx, size + strlen(name) + 1);
60         e = (struct uci_element *) ptr;
61         e->type = type;
62         e->name = (char *) ptr + size;
63         strcpy(e->name, name);
64         uci_list_init(&e->list);
65
66         return e;
67 }
68
69 static void
70 uci_free_element(struct uci_element *e)
71 {
72         if (!uci_list_empty(&e->list))
73                 uci_list_del(&e->list);
74         free(e);
75 }
76
77 static struct uci_option *
78 uci_alloc_option(struct uci_section *s, const char *name, const char *value)
79 {
80         struct uci_package *p = s->package;
81         struct uci_context *ctx = p->ctx;
82         struct uci_option *o;
83
84         o = uci_alloc_element(ctx, option, name, strlen(value) + 1);
85         o->value = uci_dataptr(o);
86         o->section = s;
87         strcpy(o->value, value);
88         uci_list_add(&s->options, &o->e.list);
89
90         return o;
91 }
92
93 static inline void
94 uci_free_option(struct uci_option *o)
95 {
96         uci_free_element(&o->e);
97 }
98
99 static struct uci_section *
100 uci_alloc_section(struct uci_package *p, const char *type, const char *name)
101 {
102         struct uci_context *ctx = p->ctx;
103         struct uci_section *s;
104         char buf[16];
105
106         if (!name || !name[0]) {
107                 snprintf(buf, 16, "cfg%d", p->n_section);
108                 name = buf;
109         }
110
111         s = uci_alloc_element(ctx, section, name, strlen(type) + 1);
112         s->type = uci_dataptr(s);
113         s->package = p;
114         strcpy(s->type, type);
115         uci_list_init(&s->options);
116         uci_list_add(&p->sections, &s->e.list);
117
118         return s;
119 }
120
121 static void
122 uci_free_section(struct uci_section *s)
123 {
124         struct uci_element *o, *tmp;
125
126         uci_foreach_element_safe(&s->options, tmp, o) {
127                 uci_free_option(uci_to_option(o));
128         }
129         uci_free_element(&s->e);
130 }
131
132 static struct uci_package *
133 uci_alloc_package(struct uci_context *ctx, const char *name)
134 {
135         struct uci_package *p;
136
137         p = uci_alloc_element(ctx, package, name, 0);
138         p->ctx = ctx;
139         uci_list_init(&p->sections);
140         return p;
141 }
142
143 static void
144 uci_free_package(struct uci_package *p)
145 {
146         struct uci_element *e, *tmp;
147
148         if(!p)
149                 return;
150
151         uci_foreach_element_safe(&p->sections, tmp, e) {
152                 uci_free_section(uci_to_section(e));
153         }
154         uci_free_element(&p->e);
155 }
156
157 static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, char *name)
158 {
159         struct uci_element *e;
160
161         uci_foreach_element(list, e) {
162                 if (!strcmp(e->name, name))
163                         return e;
164         }
165         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
166 }
167
168 int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option)
169 {
170         struct uci_element *e;
171         struct uci_package *p;
172         struct uci_section *s;
173         struct uci_option *o;
174
175         UCI_HANDLE_ERR(ctx);
176         UCI_ASSERT(ctx, res != NULL);
177         UCI_ASSERT(ctx, package != NULL);
178
179         e = uci_lookup_list(ctx, &ctx->root, package);
180         if (!section)
181                 goto found;
182
183         p = uci_to_package(e);
184         e = uci_lookup_list(ctx, &p->sections, section);
185         if (!option)
186                 goto found;
187
188         s = uci_to_section(e);
189         e = uci_lookup_list(ctx, &s->options, option);
190
191 found:
192         *res = e;
193         return 0;
194 }
195
196 int uci_unload(struct uci_context *ctx, const char *name)
197 {
198         struct uci_element *e;
199
200         UCI_HANDLE_ERR(ctx);
201         UCI_ASSERT(ctx, name != NULL);
202
203         uci_foreach_element(&ctx->root, e) {
204                 if (!strcmp(e->name, name))
205                         goto found;
206         }
207         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
208
209 found:
210         uci_free_package(uci_to_package(e));
211
212         return 0;
213 }
214
215