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.
17 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
23 #include "include/log.h"
26 struct uci_package
*p
= NULL
;
28 struct uci_context
* uci_init(char *config_file
)
30 struct uci_context
*ctx
= uci_alloc_context();
31 uci_add_delta_path(ctx
, "/var/state");
32 if(uci_load(ctx
, config_file
, &p
) != UCI_OK
)
34 log_printf("/etc/config/%s is missing or corrupt\n", config_file
);
40 void uci_cleanup(struct uci_context
*ctx
)
43 uci_free_context(ctx
);
46 void uci_save_state(struct uci_context
*ctx
)
51 char* uci_get_option(struct uci_context
*ctx
, char *section
, char *option
)
53 struct uci_element
*e
= NULL
;
60 memset(&ptr
, 0, sizeof(ptr
));
61 ptr
.package
= p
->e
.name
;
62 ptr
.section
= section
;
64 if (uci_lookup_ptr(ctx
, &ptr
, NULL
, true) != UCI_OK
)
67 if (!(ptr
.flags
& UCI_LOOKUP_COMPLETE
))
73 case UCI_TYPE_SECTION
:
74 value
= uci_to_section(e
)->type
;
79 value
= ptr
.o
->v
.string
;
93 int uci_get_option_int(struct uci_context
*ctx
, char *section
, char *option
, int def
)
95 char *tmp
= uci_get_option(ctx
, section
, option
);
103 void uci_for_each_section_type(char *type
, void (*cb
)(char*, void*), void *priv
)
105 struct uci_element
*e
;
110 uci_foreach_element(&p
->sections
, e
)
111 if (!strcmp(type
, uci_to_section(e
)->type
))