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();
21 uci_add_delta_path(ctx, "/var/state");
22 if(uci_load(ctx, config_file, NULL) != UCI_OK)
24 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
30 struct uci_context* ucix_init_path(const char *path, const char *config_file)
32 struct uci_context *ctx = uci_alloc_context();
35 uci_set_savedir(ctx, path);
37 if(uci_load(ctx, config_file, NULL) != UCI_OK)
39 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
45 void ucix_cleanup(struct uci_context *ctx)
47 uci_free_context(ctx);
50 int ucix_save(struct uci_context *ctx, const char *p)
52 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
54 uci_set_savedir(ctx, "/tmp/.uci/");
59 int ucix_save_state(struct uci_context *ctx, const char *p)
61 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
63 uci_set_savedir(ctx, "/var/state/");
68 int ucix_get_option_list(struct uci_context *ctx, const char *p,
69 const char *s, const char *o, struct list_head *l)
71 struct uci_element *e = NULL;
72 if(ucix_get_ptr(ctx, p, s, o, NULL))
74 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
82 uci_foreach_element(&ptr.o->v.list, e)
84 struct ucilist *ul = malloc(sizeof(struct ucilist));
85 ul->val = strdup((e->name)?(e->name):(""));
86 INIT_LIST_HEAD(&ul->list);
87 list_add(&ul->list, l);
101 char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o)
103 struct uci_element *e = NULL;
104 const char *value = NULL;
105 if(ucix_get_ptr(ctx, p, s, o, NULL))
107 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
112 case UCI_TYPE_SECTION:
113 value = uci_to_section(e)->type;
115 case UCI_TYPE_OPTION:
116 switch(ptr.o->type) {
117 case UCI_TYPE_STRING:
118 value = ptr.o->v.string;
129 return (value) ? (strdup(value)):(NULL);
132 int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
134 char *tmp = ucix_get_option(ctx, p, s, o);
145 void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
147 if(ucix_get_ptr(ctx, p, s, NULL, t))
152 void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
154 if(ucix_get_ptr(ctx, p, s, o, (t)?(t):("")))
159 void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
162 snprintf(tmp, 64, "%d", t);
163 ucix_add_option(ctx, p, s, o, tmp);
166 void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
168 if(!ucix_get_ptr(ctx, p, s, o, NULL))
169 uci_delete(ctx, &ptr);
172 void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
174 if(!ucix_get_ptr(ctx, p, s, o, NULL))
175 uci_revert(ctx, &ptr);
178 void ucix_for_each_section_type(struct uci_context *ctx,
179 const char *p, const char *t,
180 void (*cb)(const char*, void*), void *priv)
182 struct uci_element *e;
183 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
185 uci_foreach_element(&ptr.p->sections, e)
186 if (!strcmp(t, uci_to_section(e)->type))
190 void ucix_for_each_section_option(struct uci_context *ctx,
191 const char *p, const char *s,
192 void (*cb)(const char*, const char*, void*), void *priv)
194 struct uci_element *e;
195 if(ucix_get_ptr(ctx, p, s, NULL, NULL))
197 uci_foreach_element(&ptr.s->options, e)
199 struct uci_option *o = uci_to_option(e);
200 cb(o->e.name, o->v.string, priv);
204 int ucix_commit(struct uci_context *ctx, const char *p)
206 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
208 return uci_commit(ctx, &ptr.p, false);