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.
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.
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.
16 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
23 #include <uci_config.h>
27 static struct uci_ptr ptr;
29 static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
31 memset(&ptr, 0, sizeof(ptr));
36 return uci_lookup_ptr(ctx, &ptr, NULL, true);
39 struct uci_context* ucix_init(const char *config_file)
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)
45 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
51 struct uci_context* ucix_init_path(const char *path, const char *config_file)
53 struct uci_context *ctx = uci_alloc_context();
55 uci_set_confdir(ctx, path);
56 if(uci_load(ctx, config_file, NULL) != UCI_OK)
58 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
64 int ucix_load(struct uci_context *ctx, const char *config_file)
66 if(uci_load(ctx, config_file, NULL) != UCI_OK)
68 printf("%s/%s is missing or corrupt\n", ctx->savedir, config_file);
74 void ucix_cleanup(struct uci_context *ctx)
76 uci_free_context(ctx);
79 void ucix_save(struct uci_context *ctx)
81 uci_set_savedir(ctx, "/tmp/.uci/");
85 void ucix_save_state(struct uci_context *ctx)
87 uci_set_savedir(ctx, "/var/state/");
91 const char* ucix_get_option(struct uci_context *ctx, const char *p, const char *s, const char *o)
93 struct uci_element *e = NULL;
94 const char *value = NULL;
95 if(ucix_get_ptr(ctx, p, s, o, NULL))
97 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
102 case UCI_TYPE_SECTION:
103 value = uci_to_section(e)->type;
106 case UCI_TYPE_OPTION:
107 switch(ptr.o->type) {
108 case UCI_TYPE_STRING:
109 value = ptr.o->v.string;
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)
128 struct uci_element *e = NULL;
131 if(ucix_get_ptr(ctx, p, s, o, NULL))
133 if (!(ptr.flags & UCI_LOOKUP_COMPLETE))
136 if(e->type == UCI_TYPE_OPTION)
141 uci_foreach_element(&ptr.o->v.list, e) {
147 case UCI_TYPE_STRING:
148 if( (value = strdup(ptr.o->v.string)) != NULL )
151 for( ts = value; 1; ts = NULL )
153 if( (tt = strtok_r(ts, " \t", &tp)) != NULL )
174 int ucix_get_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int def)
176 const char *tmp = ucix_get_option(ctx, p, s, o);
184 void ucix_add_section(struct uci_context *ctx, const char *p, const char *s, const char *t)
186 if(ucix_get_ptr(ctx, p, s, NULL, t))
191 void ucix_add_option(struct uci_context *ctx, const char *p, const char *s, const char *o, const char *t)
193 if(ucix_get_ptr(ctx, p, s, o, (t)?(t):("")))
198 void ucix_add_option_int(struct uci_context *ctx, const char *p, const char *s, const char *o, int t)
201 snprintf(tmp, 64, "%d", t);
202 ucix_add_option(ctx, p, s, o, tmp);
205 void ucix_del(struct uci_context *ctx, const char *p, const char *s, const char *o)
207 if(!ucix_get_ptr(ctx, p, s, o, NULL))
208 uci_delete(ctx, &ptr);
211 void ucix_revert(struct uci_context *ctx, const char *p, const char *s, const char *o)
213 if(!ucix_get_ptr(ctx, p, s, o, NULL))
214 uci_revert(ctx, &ptr);
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)
221 struct uci_element *e;
222 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
224 uci_foreach_element(&ptr.p->sections, e)
225 if (!strcmp(t, uci_to_section(e)->type))
226 cb(ctx, e->name, priv);
229 int ucix_commit(struct uci_context *ctx, const char *p)
231 if(ucix_get_ptr(ctx, p, NULL, NULL, NULL))
233 return uci_commit(ctx, &ptr.p, false);