refactor uci show code
[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_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
26 {
27         next->prev = ptr;
28         ptr->prev = prev;
29         ptr->next = next;
30         prev->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_add(head->prev, head, 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
51 static void uci_drop_option(struct uci_option *option)
52 {
53         if (!option)
54                 return;
55         if (option->name)
56                 free(option->name);
57         if (option->value)
58                 free(option->value);
59         free(option);
60 }
61
62 static struct uci_option *uci_add_option(struct uci_section *section, const char *name, const char *value)
63 {
64         struct uci_config *cfg = section->config;
65         struct uci_context *ctx = cfg->ctx;
66         struct uci_option *option = NULL;
67
68         UCI_TRAP_SAVE(ctx, error);
69         option = (struct uci_option *) uci_malloc(ctx, sizeof(struct uci_option));
70         option->name = uci_strdup(ctx, name);
71         option->value = uci_strdup(ctx, value);
72         uci_list_add(&section->options, &option->list);
73         UCI_TRAP_RESTORE(ctx);
74         return option;
75
76 error:
77         uci_drop_option(option);
78         UCI_THROW(ctx, ctx->errno);
79         return NULL;
80 }
81
82 static void uci_drop_section(struct uci_section *section)
83 {
84         struct uci_option *opt;
85
86         if (!section)
87                 return;
88
89         uci_foreach_entry(option, &section->options, opt) {
90                 uci_list_del(&opt->list);
91                 uci_drop_option(opt);
92         }
93
94         if (section->name)
95                 free(section->name);
96         if (section->type)
97                 free(section->type);
98         free(section);
99 }
100
101 static struct uci_section *uci_add_section(struct uci_config *cfg, const char *type, const char *name)
102 {
103         struct uci_section *section = NULL;
104         struct uci_context *ctx = cfg->ctx;
105
106         UCI_TRAP_SAVE(ctx, error);
107         cfg->n_section++;
108         section = (struct uci_section *) uci_malloc(ctx, sizeof(struct uci_section));
109         section->config = cfg;
110         uci_list_init(&section->list);
111         uci_list_init(&section->options);
112         section->type = uci_strdup(ctx, type);
113         if (name && name[0])
114                 section->name = uci_strdup(ctx, name);
115         else
116                 asprintf(&section->name, "cfg%d", cfg->n_section);
117         uci_list_add(&cfg->sections, &section->list);
118         UCI_TRAP_RESTORE(ctx);
119
120         return section;
121
122 error:
123         uci_drop_section(section);
124         UCI_THROW(ctx, ctx->errno);
125         return NULL;
126 }
127
128 static void uci_drop_config(struct uci_config *cfg)
129 {
130         struct uci_section *s;
131
132         if(!cfg)
133                 return;
134
135         uci_foreach_entry(section, &cfg->sections, s) {
136                 uci_list_del(&s->list);
137                 uci_drop_section(s);
138         }
139
140         if (cfg->name)
141                 free(cfg->name);
142         free(cfg);
143 }
144
145
146 static struct uci_config *uci_alloc_config(struct uci_context *ctx, const char *name)
147 {
148         struct uci_config *cfg = NULL;
149
150         UCI_TRAP_SAVE(ctx, error);
151         cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config));
152         uci_list_init(&cfg->list);
153         uci_list_init(&cfg->sections);
154         cfg->name = uci_strdup(ctx, name);
155         cfg->ctx = ctx;
156         UCI_TRAP_RESTORE(ctx);
157         return cfg;
158
159 error:
160         uci_drop_config(cfg);
161         UCI_THROW(ctx, ctx->errno);
162         return NULL;
163 }
164
165 int uci_unload(struct uci_context *ctx, const char *name)
166 {
167         struct uci_config *cfg;
168
169         UCI_HANDLE_ERR(ctx);
170         UCI_ASSERT(ctx, name != NULL);
171
172         uci_foreach_entry(config, &ctx->root, cfg) {
173                 if (!strcmp(cfg->name, name))
174                         goto found;
175         }
176         UCI_THROW(ctx, UCI_ERR_NOTFOUND);
177
178 found:
179         uci_list_del(&cfg->list);
180         uci_drop_config(cfg);
181
182         return 0;
183 }
184
185 static inline char *get_filename(char *path)
186 {
187         char *p;
188
189         p = strrchr(path, '/');
190         p++;
191         if (!*p)
192                 return NULL;
193         return p;
194 }
195
196 char **uci_list_configs()
197 {
198         char **configs;
199         glob_t globbuf;
200         int size, i;
201         char *buf;
202
203         if (glob(UCI_CONFDIR "/*", GLOB_MARK, NULL, &globbuf) != 0)
204                 return NULL;
205
206         size = sizeof(char *) * (globbuf.gl_pathc + 1);
207         for(i = 0; i < globbuf.gl_pathc; i++) {
208                 char *p;
209
210                 p = get_filename(globbuf.gl_pathv[i]);
211                 if (!p)
212                         continue;
213
214                 size += strlen(p) + 1;
215         }
216
217         configs = malloc(size);
218         if (!configs)
219                 return NULL;
220
221         memset(configs, 0, size);
222         buf = (char *) &configs[globbuf.gl_pathc + 1];
223         for(i = 0; i < globbuf.gl_pathc; i++) {
224                 char *p;
225
226                 p = get_filename(globbuf.gl_pathv[i]);
227                 if (!p)
228                         continue;
229
230                 configs[i] = buf;
231                 strcpy(buf, p);
232                 buf += strlen(buf) + 1;
233         }
234         return configs;
235 }
236