#include <limits.h>
#include <stdio.h>
#include <ctype.h>
+#include <errno.h>
#include "ucimap.h"
#include "uci_internal.h"
struct uci_alloc {
- enum ucimap_type type;
- union {
- void **ptr;
- } data;
+ void *ptr;
};
struct uci_alloc_custom {
return ((char *) sd - sd->sm->smap_offset);
}
+static inline struct ucimap_section_data *
+ucimap_ptr_section(struct uci_sectionmap *sm, void *ptr) {
+ ptr = (char *) ptr + sm->smap_offset;
+ return ptr;
+}
+
static inline union ucimap_data *
ucimap_get_data(struct ucimap_section_data *sd, struct uci_optmap *om)
{
}
static void
-ucimap_free_item(struct uci_alloc *a)
-{
- switch(a->type & UCIMAP_TYPE) {
- case UCIMAP_SIMPLE:
- case UCIMAP_LIST:
- free(a->data.ptr);
- break;
- }
-}
-
-static void
ucimap_add_alloc(struct ucimap_section_data *sd, void *ptr)
{
struct uci_alloc *a = &sd->allocmap[sd->allocmap_len++];
- a->type = UCIMAP_SIMPLE;
- a->data.ptr = ptr;
+ a->ptr = ptr;
}
void
sd->sm->free(map, section);
for (i = 0; i < sd->allocmap_len; i++) {
- ucimap_free_item(&sd->allocmap[i]);
+ free(sd->allocmap[i].ptr);
}
if (sd->alloc_custom) {
return true;
}
+void
+ucimap_free_item(struct ucimap_section_data *sd, void *item)
+{
+ struct uci_alloc_custom *ac;
+ struct uci_alloc *a;
+ void *ptr = *((void **) item);
+ int i;
+
+ if (!ptr)
+ return;
+
+ *((void **)item) = NULL;
+ for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
+ if (a->ptr != ptr)
+ continue;
+
+ if (i != sd->allocmap_len - 1)
+ a->ptr = sd->allocmap[sd->allocmap_len - 1].ptr;
+
+ sd->allocmap_len--;
+ return;
+ }
+
+ for (i = 0, ac = sd->alloc_custom; i < sd->alloc_custom_len; i++, ac++) {
+ if (ac->ptr != ptr)
+ continue;
+
+ if (i != sd->alloc_custom_len - 1)
+ memcpy(ac, &sd->alloc_custom[sd->alloc_custom_len - 1],
+ sizeof(struct uci_alloc_custom));
+
+ ac->om->free(ac->section, ac->om, ac->ptr);
+ sd->alloc_custom_len--;
+ return;
+ }
+}
+
+int
+ucimap_resize_list(struct ucimap_section_data *sd, struct ucimap_list **list, int items)
+{
+ struct ucimap_list *new;
+ struct uci_alloc *a;
+ int i, offset = 0;
+ int size = sizeof(struct ucimap_list) + items * sizeof(union ucimap_data);
+
+ if (!*list) {
+ new = calloc(1, size);
+
+ ucimap_add_alloc(sd, new);
+ goto set;
+ }
+
+ for (i = 0, a = sd->allocmap; i < sd->allocmap_len; i++, a++) {
+ if (a->ptr != *list)
+ continue;
+
+ goto realloc;
+ }
+ return -ENOENT;
+
+realloc:
+ if (items > (*list)->size)
+ offset = (items - (*list)->size) * sizeof(union ucimap_data);
+
+ a->ptr = realloc(a->ptr, size);
+ if (offset)
+ memset((char *) a->ptr + offset, 0, size - offset);
+ new = a->ptr;
+
+set:
+ new->size = items;
+ *list = new;
+ return 0;
+}
+
static void
ucimap_add_fixup(struct ucimap_section_data *sd, union ucimap_data *data, struct uci_optmap *om, const char *str)
{
char *s;
int val;
- if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type))
+ if (ucimap_is_list(om->type) && !ucimap_is_fixup(om->type)) {
+ if (unlikely(data->list->size <= data->list->n_items)) {
+ /* should not happen */
+ DPRINTF("ERROR: overflow while filling a list\n");
+ return;
+ }
+
data = &data->list->item[data->list->n_items++];
+ }
switch(om->type & UCIMAP_SUBTYPE) {
case UCIMAP_STRING:
{
unsigned int type;
+ if (unlikely(sm->type_name != om->type_name) &&
+ unlikely(strcmp(sm->type_name, om->type_name) != 0)) {
+ DPRINTF("Option '%s' of section type '%s' refereces unknown "
+ "section type '%s', should be '%s'.\n",
+ om->name, sm->type, om->type_name, sm->type_name);
+ return false;
+ }
+
if (om->detected_type < 0)
return true;
n_alloc_custom += n_elements_custom;
size = sizeof(struct ucimap_list) +
n_elements * sizeof(union ucimap_data);
+
data->list = malloc(size);
+ if (!data->list)
+ goto error_mem;
+
+ data->list->size = n_elements;
memset(data->list, 0, size);
} else {
ucimap_count_alloc(om, &n_alloc, &n_alloc_custom);
}
}
+static char *
+ucimap_data_to_string(struct ucimap_section_data *sd, struct uci_optmap *om, union ucimap_data *data)
+{
+ static char buf[32];
+ char *str = NULL;
+
+ switch(om->type & UCIMAP_SUBTYPE) {
+ case UCIMAP_STRING:
+ str = data->s;
+ break;
+ case UCIMAP_INT:
+ sprintf(buf, "%d", data->i);
+ str = buf;
+ break;
+ case UCIMAP_BOOL:
+ sprintf(buf, "%d", !!data->b);
+ str = buf;
+ break;
+ case UCIMAP_SECTION:
+ if (data->ptr)
+ str = (char *) ucimap_ptr_section(om->data.sm, data->ptr)->section_name;
+ else
+ str = "";
+ break;
+ case UCIMAP_CUSTOM:
+ break;
+ default:
+ return NULL;
+ }
+
+ if (om->format) {
+ union ucimap_data tdata;
+
+ if (ucimap_is_custom(om->type)) {
+ tdata.s = (char *)data;
+ data = &tdata;
+ }
+
+ if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
+ return NULL;
+
+ if (!str)
+ str = "";
+ }
+ return str;
+}
+
int
ucimap_store_section(struct uci_map *map, struct uci_package *p, struct ucimap_section_data *sd)
{
ucimap_foreach_option(sm, om) {
union ucimap_data *data;
- static char buf[32];
- char *str = NULL;
i++;
- if (ucimap_is_list(om->type))
- continue;
-
data = ucimap_get_data(sd, om);
if (!TEST_BIT(sd->cmap, i - 1))
continue;
ucimap_fill_ptr(&ptr, s, om->name);
- switch(om->type & UCIMAP_SUBTYPE) {
- case UCIMAP_STRING:
- str = data->s;
- break;
- case UCIMAP_INT:
- sprintf(buf, "%d", data->i);
- str = buf;
- break;
- case UCIMAP_BOOL:
- sprintf(buf, "%d", !!data->b);
- str = buf;
- break;
- case UCIMAP_CUSTOM:
- break;
- default:
- continue;
- }
- if (om->format) {
- union ucimap_data tdata, *data;
+ if (ucimap_is_list(om->type)) {
+ struct ucimap_list *list = data->list;
+ bool first = true;
+ int j;
- data = ucimap_get_data(sd, om);
- if (ucimap_is_custom(om->type)) {
- tdata.s = (char *)data;
- data = &tdata;
- }
+ for (j = 0; j < list->n_items; j++) {
+ ptr.value = ucimap_data_to_string(sd, om, &list->item[j]);
+ if (!ptr.value)
+ continue;
- if (om->format(ucimap_section_ptr(sd), om, data, &str) < 0)
+ if (first) {
+ ret = uci_set(s->package->ctx, &ptr);
+ first = false;
+ } else {
+ ret = uci_add_list(s->package->ctx, &ptr);
+ }
+ if (ret)
+ return ret;
+ }
+ } else {
+ ptr.value = ucimap_data_to_string(sd, om, data);
+ if (!ptr.value)
continue;
- }
- if (!str)
- continue;
- ptr.value = str;
- ret = uci_set(s->package->ctx, &ptr);
- if (ret)
- return ret;
+ ret = uci_set(s->package->ctx, &ptr);
+ if (ret)
+ return ret;
+ }
CLR_BIT(sd->cmap, i - 1);
}