4 #include "include/ucix.h"
6 static struct uci_ptr ptr;
8 static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
10 memset(&ptr, 0, sizeof(ptr));
15 return uci_lookup_ptr(ctx, &ptr, NULL, true);
18 struct uci_context* ucix_init(const char *config_file)
20 struct uci_context *ctx = uci_alloc_context();
23 uci_add_delta_path(ctx, "/var/state");
24 if(uci_load(ctx, config_file, NULL) != UCI_OK)
26 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
32 struct uci_context* ucix_init_path(const char *path, const char *config_file)
34 struct uci_context *ctx = uci_alloc_context();
39 uci_set_savedir(ctx, path);
41 if(uci_load(ctx, config_file, NULL) != UCI_OK)
43 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
49 void ucix_cleanup(struct uci_context *ctx)
53 uci_free_context(ctx);
57 int ucix_save(struct uci_context *ctx, const char *p)
59 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
61 uci_set_savedir(ctx, "/tmp/.uci/");
66 int ucix_save_state(struct uci_context *ctx, const char *p)
68 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
70 uci_set_savedir(ctx, "/var/state/");
75 int ucix_get_option_list(struct uci_context *ctx, const char *p,
76 const char *s, const char *o, struct list_head *l)
78 struct uci_element *e = NULL;
79 if(ucix_get_ptr(ctx, p, s, o, NULL))
81 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
89 uci_foreach_element(&ptr.o->v.list, e)
91 struct ucilist *ul = malloc(sizeof(struct ucilist));
92 ul->val = strdup((e->name)?(e->name):(""));
93 INIT_LIST_HEAD(&ul->list);
94 list_add(&ul->list, l);
108 char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o)
110 struct uci_element *e = NULL;
111 const char *value = NULL;
112 if(ucix_get_ptr(ctx, p, s, o, NULL))
114 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
119 case UCI_TYPE_SECTION:
120 value = uci_to_section(e)->type;
122 case UCI_TYPE_OPTION:
123 switch(ptr.o->type) {
124 case UCI_TYPE_STRING:
125 value = ptr.o->v.string;
136 return (value) ? (strdup(value)):(NULL);
139 int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
141 char *tmp = ucix_get_option(ctx, p, s, o);
152 void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
154 if(ucix_get_ptr(ctx, p, s, NULL, t))
159 void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
161 if(ucix_get_ptr(ctx, p, s, o, (t)?(t):("")))
166 void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
169 snprintf(tmp, 64, "%d", t);
170 ucix_add_option(ctx, p, s, o, tmp);
173 void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
175 if(!ucix_get_ptr(ctx, p, s, o, NULL))
176 uci_delete(ctx, &ptr);
179 void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
181 if(!ucix_get_ptr(ctx, p, s, o, NULL))
182 uci_revert(ctx, &ptr);
185 void ucix_for_each_section_type(struct uci_context *ctx,
186 const char *p, const char *t,
187 void (*cb)(const char*, void*), void *priv)
189 struct uci_element *e;
190 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
192 uci_foreach_element(&ptr.p->sections, e)
193 if (!strcmp(t, uci_to_section(e)->type))
197 void ucix_for_each_section_option(struct uci_context *ctx,
198 const char *p, const char *s,
199 void (*cb)(const char*, const char*, void*), void *priv)
201 struct uci_element *e;
202 if(ucix_get_ptr(ctx, p, s, NULL, NULL))
204 uci_foreach_element(&ptr.s->options, e)
206 struct uci_option *o = uci_to_option(e);
207 cb(o->e.name, o->v.string, priv);
211 int ucix_commit(struct uci_context *ctx, const char *p)
213 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
215 return uci_commit(ctx, &ptr.p, false);