contrib/package: make relayd dependency conditional, else luci build will break brcm-2.4
[project/luci.git] / contrib / fwd / src / ucix.c
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 *
16 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
17 */
18
19 #include <string.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22
23 #include <uci_config.h>
24 #include <uci.h>
25 #include "ucix.h"
26
27 static struct uci_ptr ptr;
28
29 static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
30 {
31 memset(&ptr, 0, sizeof(ptr));
32 ptr.package = p;
33 ptr.section = s;
34 ptr.option = o;
35 ptr.value = t;
36 return uci_lookup_ptr(ctx, &ptr, NULL, true);
37 }
38
39 struct uci_context* ucix_init(const char *config_file)
40 {
41 struct uci_context *ctx = uci_alloc_context();
42 uci_add_history_path(ctx, "/var/state");
43 if(uci_load(ctx, config_file, NULL) != UCI_OK)
44 {
45 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
46 return NULL;
47 }
48 return ctx;
49 }
50
51 struct uci_context* ucix_init_path(const char *path, const char *config_file)
52 {
53 struct uci_context *ctx = uci_alloc_context();
54 if(path)
55 uci_set_confdir(ctx, path);
56 if(uci_load(ctx, config_file, NULL) != UCI_OK)
57 {
58 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
59 return NULL;
60 }
61 return ctx;
62 }
63
64 int ucix_load(struct uci_context *ctx, const char *config_file)
65 {
66 if(uci_load(ctx, config_file, NULL) != UCI_OK)
67 {
68 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
69 return 0;
70 }
71 return 1;
72 }
73
74 void ucix_cleanup(struct uci_context *ctx)
75 {
76 uci_free_context(ctx);
77 }
78
79 void ucix_save(struct uci_context *ctx)
80 {
81 uci_set_savedir(ctx, "/tmp/.uci/");
82 uci_save(ctx, NULL);
83 }
84
85 void ucix_save_state(struct uci_context *ctx)
86 {
87 uci_set_savedir(ctx, "/var/state/");
88 uci_save(ctx, NULL);
89 }
90
91 const char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o)
92 {
93 struct uci_element *e = NULL;
94 const char *value = NULL;
95 if(ucix_get_ptr(ctx, p, s, o, NULL))
96 return NULL;
97 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
98 return NULL;
99 e = ptr.last;
100 switch (e->type)
101 {
102 case UCI_TYPE_SECTION:
103 value = uci_to_section(e)->type;
104 break;
105
106 case UCI_TYPE_OPTION:
107 switch(ptr.o->type) {
108 case UCI_TYPE_STRING:
109 value = ptr.o->v.string;
110 break;
111 default:
112 value = NULL;
113 break;
114 }
115 break;
116
117 default:
118 return 0;
119 }
120
121 return value;
122 }
123
124 int ucix_for_each_list(
125 struct uci_context *ctx, const char *p, const char *s, const char *o,
126 void (*cb)(const char*, void*), void *priv)
127 {
128 struct uci_element *e = NULL;
129 char *value = NULL;
130 int count = 0;
131 if(ucix_get_ptr(ctx, p, s, o, NULL))
132 return -1;
133 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
134 return -1;
135 e = ptr.last;
136 if(e->type == UCI_TYPE_OPTION)
137 {
138 switch(ptr.o->type)
139 {
140 case UCI_TYPE_LIST:
141 uci_foreach_element(&ptr.o->v.list, e) {
142 cb(e->name, priv);
143 count++;
144 }
145 break;
146
147 case UCI_TYPE_STRING:
148 if( (value = strdup(ptr.o->v.string)) != NULL )
149 {
150 char *ts, *tt, *tp;
151 for( ts = value; 1; ts = NULL )
152 {
153 if( (tt = strtok_r(ts, " \t", &tp)) != NULL )
154 {
155 cb(tt, priv);
156 count++;
157 }
158 else
159 {
160 break;
161 }
162 }
163 free(value);
164 }
165 break;
166 }
167
168 return count;
169 }
170
171 return -1;
172 }
173
174 int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
175 {
176 const char *tmp = ucix_get_option(ctx, p, s, o);
177 int ret = def;
178
179 if (tmp)
180 ret = atoi(tmp);
181 return ret;
182 }
183
184 void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
185 {
186 if(ucix_get_ptr(ctx, p, s, NULL, t))
187 return;
188 uci_set(ctx, &ptr);
189 }
190
191 void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
192 {
193 if(ucix_get_ptr(ctx, p, s, o, (t)?(t):("")))
194 return;
195 uci_set(ctx, &ptr);
196 }
197
198 void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
199 {
200 char tmp[64];
201 snprintf(tmp, 64, "%d", t);
202 ucix_add_option(ctx, p, s, o, tmp);
203 }
204
205 void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
206 {
207 if(!ucix_get_ptr(ctx, p, s, o, NULL))
208 uci_delete(ctx, &ptr);
209 }
210
211 void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
212 {
213 if(!ucix_get_ptr(ctx, p, s, o, NULL))
214 uci_revert(ctx, &ptr);
215 }
216
217 void ucix_for_each_section_type(struct uci_context *ctx,
218 const char *p, const char *t,
219 void (*cb)(struct uci_context *, const char*, void*), void *priv)
220 {
221 struct uci_element *e;
222 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
223 return;
224 uci_foreach_element(&ptr.p->sections, e)
225 if (!strcmp(t, uci_to_section(e)->type))
226 cb(ctx, e->name, priv);
227 }
228
229 int ucix_commit(struct uci_context *ctx, const char *p)
230 {
231 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
232 return 1;
233 return uci_commit(ctx, &ptr.p, false);
234 }
235
236