| |
| #include "piped.h" |
| #include "piped_uci.h" |
| #include "piped_util.h" |
| |
| static inline void pd_uci_set_ptr(const char *p, const char *s, const char *o, |
| const char *v, struct uci_ptr *ptr) |
| { |
| memset(ptr, 0, sizeof(*ptr)); |
| ptr->package = p; |
| ptr->section = s; |
| ptr->option = o; |
| ptr->value = v; |
| } |
| |
| void pd_uci_done(struct uci_context *c, bool commit) |
| { |
| struct uci_package *p; |
| |
| if (commit) { |
| if ((p = uci_lookup_package(c, UCI_PKG_NW))) |
| uci_commit(c, &p, false); |
| if ((p = uci_lookup_package(c, UCI_PKG_DHCP))) |
| uci_commit(c, &p, false); |
| } |
| uci_free_context(c); |
| } |
| |
| int pd_uci_get_ptr(struct uci_context *c, const char *p, const char *s, |
| const char *o, const char *v, struct uci_ptr *ptr) |
| { |
| int rc; |
| pd_uci_set_ptr(p, s, o, v, ptr); |
| rc = uci_lookup_ptr(c, ptr, NULL, false); |
| if (rc || !(ptr->flags & UCI_LOOKUP_COMPLETE)) |
| return 1; |
| if (ptr->last->type != UCI_TYPE_OPTION) |
| return 1; |
| return 0; |
| } |
| |
| char *pd_uci_get_opt(struct uci_context *c, const char *p, const char *s, |
| const char *o) |
| { |
| struct uci_ptr ptr; |
| |
| if (pd_uci_get_ptr(c, p, s, o, NULL, &ptr)) |
| return NULL; |
| if (ptr.o->type != UCI_TYPE_STRING) |
| return NULL; |
| return ptr.o->v.string; |
| } |
| |
| char *pd_uci_get_list_first(struct uci_context *c, const char *p, const char *s, |
| const char *o) |
| { |
| struct uci_ptr ptr; |
| struct uci_element *e; |
| if (pd_uci_get_ptr(c, p, s, o, NULL, &ptr)) |
| return NULL; |
| if (ptr.o->type != UCI_TYPE_LIST) |
| return NULL; |
| e = list_to_element((&ptr.o->v.list)->next); |
| if (!e) |
| return NULL; |
| return e->name; |
| } |
| |
| struct uci_list *pd_uci_get_list(struct uci_context *c, const char *p, |
| const char *s, const char *o) |
| { |
| struct uci_ptr ptr; |
| if (pd_uci_get_ptr(c, p, s, o, NULL, &ptr)) |
| return NULL; |
| if (ptr.o->type != UCI_TYPE_LIST) |
| return NULL; |
| return &ptr.o->v.list; |
| } |
| |
| int pd_uci_set_opt(struct uci_context *c, const char *p, const char *s, |
| const char *o, const char *v) |
| { |
| |
| struct uci_ptr ptr; |
| pd_uci_set_ptr(p, s, o, v, &ptr); |
| return uci_set(c, &ptr); |
| } |
| |
| int pd_uci_add_list(struct uci_context *c, const char *p, const char *s, |
| const char *o, const char *v) |
| { |
| struct uci_ptr ptr; |
| pd_uci_set_ptr(p, s, o, v, &ptr); |
| return uci_add_list(c, &ptr); |
| } |
| |
| int pd_uci_del_from_list(struct uci_context *c, const char *p, const char *s, |
| const char *o, const char *v) |
| { |
| struct uci_ptr ptr; |
| pd_uci_set_ptr(p, s, o, v, &ptr); |
| return uci_del_list(c, &ptr); |
| } |
| |
| int pd_uci_del_opt(struct uci_context *c, const char *p, const char *s, |
| const char *o, const char *v) |
| { |
| struct uci_ptr ptr; |
| pd_uci_set_ptr(p, s, o, v, &ptr); |
| return uci_delete(c, &ptr); |
| } |
| |
| int pd_uci_del(struct uci_context *c, const char *p, const char *s, |
| const char *o, const char *v) |
| { |
| struct uci_ptr ptr; |
| if (pd_uci_get_ptr(c, p, s, o, NULL, &ptr)) |
| return 1; |
| |
| ptr.value = v; |
| return uci_delete(c, &ptr); |
| } |
| |
| int pd_uci_list_len(struct uci_list *l) |
| { |
| struct uci_element *e; |
| int n = 0; |
| |
| uci_foreach_element(l, e) |
| n++; |
| return n; |
| } |
| |
| int pd_uci_parse_bool(char *b) |
| { |
| if (!strcmp(b, "true") || !strcmp(b, "1")) |
| return 1; |
| else if (!strcmp(b, "false") || !strcmp(b, "0")) |
| return 0; |
| return -1; |
| } |