From 8f5a6c07caaaf5bd692623d1ed8ffbfa20f564d8 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 23 Jan 2008 08:10:45 +0100 Subject: [PATCH] major code refactoring for more code reuse and smaller code size --- cli.c | 18 +++--- file.c | 78 ++++++++---------------- libuci.c | 6 +- list.c | 183 +++++++++++++++++++++++++++---------------------------- uci.h | 149 +++++++++++++++++++++++++++++++++----------- 5 files changed, 241 insertions(+), 193 deletions(-) diff --git a/cli.c b/cli.c index 2252e5e..3a19b36 100644 --- a/cli.c +++ b/cli.c @@ -33,14 +33,14 @@ static void uci_usage(int argc, char **argv) static void uci_show_section(struct uci_section *p) { - struct uci_option *o; + struct uci_element *e; const char *cname, *sname; - cname = p->package->name; - sname = p->name; + cname = p->package->e.name; + sname = p->e.name; printf("%s.%s=%s\n", cname, sname, p->type); - uci_foreach_entry(option, &p->options, o) { - printf("%s.%s.%s=%s\n", cname, sname, o->name, o->value); + uci_foreach_element(&p->options, e) { + printf("%s.%s.%s=%s\n", cname, sname, e->name, uci_to_option(e)->value); } } @@ -48,7 +48,7 @@ static int uci_show(int argc, char **argv) { char *section = (argc > 2 ? argv[2] : NULL); struct uci_package *package; - struct uci_section *s; + struct uci_element *e; char **configs; char **p; @@ -62,9 +62,9 @@ static int uci_show(int argc, char **argv) uci_perror(ctx, "uci_load"); return 255; } - uci_foreach_entry(section, &package->sections, s) { - if (!section || !strcmp(s->name, section)) - uci_show_section(s); + uci_foreach_element( &package->sections, e) { + if (!section || !strcmp(e->name, section)) + uci_show_section(uci_to_section(e)); } uci_unload(ctx, *p); } diff --git a/file.c b/file.c index 3ccffb7..2ca7fce 100644 --- a/file.c +++ b/file.c @@ -84,10 +84,9 @@ static void uci_file_cleanup(struct uci_context *ctx) return; ctx->pctx = NULL; - if (pctx->package) { - uci_list_del(&pctx->package->list); - uci_drop_config(pctx->package); - } + if (pctx->package) + uci_free_package(pctx->package); + if (pctx->buf) free(pctx->buf); if (pctx->file) @@ -249,7 +248,7 @@ static char *next_arg(struct uci_context *ctx, char **str, bool required) UCI_THROW(ctx, UCI_ERR_PARSE); } - return uci_strdup(ctx, val); + return val; } /* @@ -282,7 +281,7 @@ static void uci_switch_config(struct uci_context *ctx) /* add the last config to main config file list */ if (pctx->package) { - uci_list_add(&ctx->root, &pctx->package->list); + uci_list_add(&ctx->root, &pctx->package->e.list); pctx->package = NULL; pctx->section = NULL; @@ -301,7 +300,7 @@ static void uci_switch_config(struct uci_context *ctx) ignore: ctx->errno = 0; - pctx->package = uci_alloc_config(ctx, name); + pctx->package = uci_alloc_package(ctx, name); } /* @@ -314,18 +313,10 @@ static void uci_parse_package(struct uci_context *ctx, char **str) /* command string null-terminated by strtok */ *str += strlen(*str) + 1; - UCI_TRAP_SAVE(ctx, error); name = next_arg(ctx, str, true); assert_eol(ctx, str); ctx->pctx->name = name; uci_switch_config(ctx); - UCI_TRAP_RESTORE(ctx); - return; - -error: - if (name) - free(name); - UCI_THROW(ctx, ctx->errno); } /* @@ -348,20 +339,10 @@ static void uci_parse_config(struct uci_context *ctx, char **str) /* command string null-terminated by strtok */ *str += strlen(*str) + 1; - UCI_TRAP_SAVE(ctx, error); type = next_arg(ctx, str, true); name = next_arg(ctx, str, false); assert_eol(ctx, str); - ctx->pctx->section = uci_add_section(ctx->pctx->package, type, name); - UCI_TRAP_RESTORE(ctx); - return; - -error: - if (name) - free(name); - if (type) - free(type); - UCI_THROW(ctx, ctx->errno); + ctx->pctx->section = uci_alloc_section(ctx->pctx->package, type, name); } /* @@ -380,20 +361,10 @@ static void uci_parse_option(struct uci_context *ctx, char **str) /* command string null-terminated by strtok */ *str += strlen(*str) + 1; - UCI_TRAP_SAVE(ctx, error); name = next_arg(ctx, str, true); value = next_arg(ctx, str, true); assert_eol(ctx, str); - uci_add_option(ctx->pctx->section, name, value); - UCI_TRAP_RESTORE(ctx); - return; - -error: - if (name) - free(name); - if (value) - free(value); - UCI_THROW(ctx, ctx->errno); + uci_alloc_option(ctx->pctx->section, name, value); } @@ -479,19 +450,20 @@ static char *uci_escape(struct uci_context *ctx, char *str) /* * export a single config package to a file stream */ -static void uci_export_config(struct uci_package *package, FILE *stream) +static void uci_export_package(struct uci_package *p, FILE *stream) { - struct uci_context *ctx = package->ctx; - struct uci_section *s; - struct uci_option *o; - - fprintf(stream, "package '%s'\n", uci_escape(ctx, package->name)); - uci_foreach_entry(section, &package->sections, s) { - fprintf(stream, "\nconfig '%s'", uci_escape(ctx, s->type)); - fprintf(stream, " '%s'\n", uci_escape(ctx, s->name)); - uci_foreach_entry(option, &s->options, o) { - fprintf(stream, "\toption '%s'", uci_escape(ctx, o->name)); - fprintf(stream, " '%s'\n", uci_escape(ctx, o->value)); + struct uci_context *ctx = p->ctx; + struct uci_element *s, *o; + + fprintf(stream, "package '%s'\n", uci_escape(ctx, p->e.name)); + uci_foreach_element(&p->sections, s) { + struct uci_section *sec = uci_to_section(s); + fprintf(stream, "\nconfig '%s'", uci_escape(ctx, sec->type)); + fprintf(stream, " '%s'\n", uci_escape(ctx, sec->e.name)); + uci_foreach_element(&sec->options, o) { + struct uci_option *opt = uci_to_option(o); + fprintf(stream, "\toption '%s'", uci_escape(ctx, opt->e.name)); + fprintf(stream, " '%s'\n", uci_escape(ctx, opt->value)); } } fprintf(stream, "\n"); @@ -499,16 +471,18 @@ static void uci_export_config(struct uci_package *package, FILE *stream) int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package) { + struct uci_element *e; + UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, stream != NULL); if (package) { - uci_export_config(package, stream); + uci_export_package(package, stream); goto done; } - uci_foreach_entry(package, &ctx->root, package) { - uci_export_config(package, stream); + uci_foreach_element(&ctx->root, e) { + uci_export_package(uci_to_package(e), stream); } done: return 0; diff --git a/libuci.c b/libuci.c index c6335d5..92205e8 100644 --- a/libuci.c +++ b/libuci.c @@ -96,11 +96,11 @@ struct uci_context *uci_alloc(void) void uci_free(struct uci_context *ctx) { - struct uci_package *package; + struct uci_element *e, *tmp; uci_cleanup(ctx); - uci_foreach_entry(package, &ctx->root, package) { - uci_drop_config(package); + uci_foreach_element_safe(&ctx->root, tmp, e) { + uci_free_package(uci_to_package(e)); } free(ctx); return; diff --git a/list.c b/list.c index ba5a9f0..357b2e4 100644 --- a/list.c +++ b/list.c @@ -46,138 +46,132 @@ static inline void uci_list_del(struct uci_list *ptr) prev->next = next; next->prev = prev; + + uci_list_init(ptr); } -static void uci_drop_option(struct uci_option *option) +static struct uci_element * +uci_alloc_generic(struct uci_context *ctx, const char *name, int size) { - if (!option) - return; - if (option->name) - free(option->name); - if (option->value) - free(option->value); - free(option); + struct uci_element *e; + void *ptr; + + ptr = uci_malloc(ctx, size + strlen(name) + 1); + e = (struct uci_element *) ptr; + e->name = (char *) ptr + size; + strcpy(e->name, name); + uci_list_init(&e->list); + + return e; } -static struct uci_option *uci_add_option(struct uci_section *section, const char *name, const char *value) +static void +uci_free_element(struct uci_element *e) { - struct uci_package *package = section->package; - struct uci_context *ctx = package->ctx; - struct uci_option *option = NULL; - - UCI_TRAP_SAVE(ctx, error); - option = (struct uci_option *) uci_malloc(ctx, sizeof(struct uci_option)); - option->name = uci_strdup(ctx, name); - option->value = uci_strdup(ctx, value); - uci_list_add(§ion->options, &option->list); - UCI_TRAP_RESTORE(ctx); - return option; - -error: - uci_drop_option(option); - UCI_THROW(ctx, ctx->errno); - return NULL; + if (!e) + return; + + if (!uci_list_empty(&e->list)) + uci_list_del(&e->list); + free(e); } -static void uci_drop_section(struct uci_section *section) +static struct uci_option * +uci_alloc_option(struct uci_section *s, const char *name, const char *value) { - struct uci_option *opt; - - if (!section) - return; + struct uci_package *p = s->package; + struct uci_context *ctx = p->ctx; + struct uci_option *o; - uci_foreach_entry(option, §ion->options, opt) { - uci_list_del(&opt->list); - uci_drop_option(opt); - } + o = uci_alloc_element(ctx, option, name, strlen(value) + 1); + o->value = uci_dataptr(o); + o->section = s; + strcpy(o->value, value); + uci_list_add(&s->options, &o->e.list); - if (section->name) - free(section->name); - if (section->type) - free(section->type); - free(section); + return o; } -static struct uci_section *uci_add_section(struct uci_package *package, const char *type, const char *name) +static inline void +uci_free_option(struct uci_option *o) { - struct uci_section *section = NULL; - struct uci_context *ctx = package->ctx; - - UCI_TRAP_SAVE(ctx, error); - package->n_section++; - section = (struct uci_section *) uci_malloc(ctx, sizeof(struct uci_section)); - section->package = package; - uci_list_init(§ion->list); - uci_list_init(§ion->options); - section->type = uci_strdup(ctx, type); - if (name && name[0]) - section->name = uci_strdup(ctx, name); - else - asprintf(§ion->name, "cfg%d", package->n_section); - uci_list_add(&package->sections, §ion->list); - UCI_TRAP_RESTORE(ctx); - - return section; - -error: - uci_drop_section(section); - UCI_THROW(ctx, ctx->errno); - return NULL; + uci_free_element(&o->e); } -static void uci_drop_config(struct uci_package *package) +static struct uci_section * +uci_alloc_section(struct uci_package *p, const char *type, const char *name) { + struct uci_context *ctx = p->ctx; struct uci_section *s; + char buf[16]; - if(!package) - return; - - uci_foreach_entry(section, &package->sections, s) { - uci_list_del(&s->list); - uci_drop_section(s); + if (!name || !name[0]) { + snprintf(buf, 16, "cfg%d", p->n_section); + name = buf; } - if (package->name) - free(package->name); - free(package); + s = uci_alloc_element(ctx, section, name, strlen(type) + 1); + s->type = uci_dataptr(s); + s->package = p; + strcpy(s->type, type); + uci_list_init(&s->options); + uci_list_add(&p->sections, &s->e.list); + + return s; } +static void +uci_free_section(struct uci_section *s) +{ + struct uci_element *o, *tmp; -static struct uci_package *uci_alloc_config(struct uci_context *ctx, const char *name) + uci_foreach_element_safe(&s->options, tmp, o) { + uci_free_option(uci_to_option(o)); + } + uci_free_element(&s->e); +} + +static struct uci_package * +uci_alloc_package(struct uci_context *ctx, const char *name) { - struct uci_package *package = NULL; - - UCI_TRAP_SAVE(ctx, error); - package = (struct uci_package *) uci_malloc(ctx, sizeof(struct uci_package)); - uci_list_init(&package->list); - uci_list_init(&package->sections); - package->name = uci_strdup(ctx, name); - package->ctx = ctx; - UCI_TRAP_RESTORE(ctx); - return package; - -error: - uci_drop_config(package); - UCI_THROW(ctx, ctx->errno); - return NULL; + struct uci_package *p; + + p = uci_alloc_element(ctx, package, name, 0); + p->ctx = ctx; + uci_list_init(&p->sections); + return p; } +static void +uci_free_package(struct uci_package *p) +{ + struct uci_element *e, *tmp; + + if(!p) + return; + + uci_foreach_element_safe(&p->sections, tmp, e) { + uci_free_section(uci_to_section(e)); + } + uci_free_element(&p->e); +} + + int uci_unload(struct uci_context *ctx, const char *name) { - struct uci_package *package; + struct uci_element *e; UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, name != NULL); - uci_foreach_entry(package, &ctx->root, package) { - if (!strcmp(package->name, name)) + uci_foreach_element(&ctx->root, e) { + if (!strcmp(e->name, name)) goto found; } UCI_THROW(ctx, UCI_ERR_NOTFOUND); found: - uci_list_del(&package->list); - uci_drop_config(package); + uci_free_package(uci_to_package(e)); return 0; } @@ -234,3 +228,4 @@ char **uci_list_configs(struct uci_context *ctx) return configs; } + diff --git a/uci.h b/uci.h index 687454f..96bebf5 100644 --- a/uci.h +++ b/uci.h @@ -15,6 +15,16 @@ #ifndef __LIBUCI_H #define __LIBUCI_H +/* + * you can use these defines to enable debugging behavior for + * apps compiled against libuci: + * + * #define UCI_DEBUG_TYPECAST: + * enable uci_element typecast checking at run time + * + */ + + #include #include @@ -113,6 +123,18 @@ extern int uci_cleanup(struct uci_context *ctx); extern char **uci_list_configs(struct uci_context *ctx); /* UCI data structures */ +enum uci_type { + UCI_TYPE_PACKAGE, + UCI_TYPE_SECTION, + UCI_TYPE_OPTION +}; + +struct uci_element +{ + struct uci_list list; + enum uci_type type; + char *name; +}; struct uci_context { @@ -147,37 +169,28 @@ struct uci_parse_context struct uci_package { - struct uci_list list; + struct uci_element e; struct uci_list sections; struct uci_context *ctx; - char *name; /* private: */ int n_section; }; struct uci_section { - struct uci_list list; + struct uci_element e; struct uci_list options; struct uci_package *package; char *type; - char *name; }; struct uci_option { - struct uci_list list; + struct uci_element e; struct uci_section *section; - char *name; char *value; }; -enum uci_type { - UCI_TYPE_PACKAGE, - UCI_TYPE_SECTION, - UCI_TYPE_OPTION -}; - enum uci_command { UCI_CMD_ADD, UCI_CMD_REMOVE, @@ -188,19 +201,11 @@ struct uci_history { struct uci_list list; enum uci_command cmd; - enum uci_type type; union { - struct { - char *name; - } p; - struct { - char *type; - char *name; - } c; - struct { - char *name; - char *value; - } o; + struct uci_element element; + struct uci_package package; + struct uci_section section; + struct uci_option option; } data; }; @@ -209,31 +214,105 @@ struct uci_history #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) #endif -/* returns true if a list is empty */ -#define uci_list_empty(list) ((list)->next == (list)) +/** + * container_of - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + */ +#define container_of(ptr, type, member) \ + ((type *) ((char *)ptr - offsetof(type,member))) + /** * uci_list_entry: casts an uci_list pointer to the containing struct. * @_type: config, section or option * @_ptr: pointer to the uci_list struct */ -#define uci_list_entry(_type, _ptr) \ - ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list))) +#define element_to(type, ptr) \ + container_of(ptr, struct uci_ ## type, e) + +#define list_to_element(ptr) \ + container_of(ptr, struct uci_element, list) + +/** + * uci_foreach_entry: loop through a list of uci elements + * @_list: pointer to the uci_list struct + * @_ptr: iteration variable, struct uci_element + * + * use like a for loop, e.g: + * uci_foreach(&list, p) { + * ... + * } + */ +#define uci_foreach_element(_list, _ptr) \ + for(_ptr = list_to_element((_list)->next); \ + &_ptr->list != (_list); \ + _ptr = list_to_element(_ptr->list.next)) /** - * uci_foreach_entry: loop through a list of configs, sections or options - * @_type: see uci_list_entry + * uci_foreach_entry_safe: like uci_foreach_safe, but safe for deletion * @_list: pointer to the uci_list struct - * @_ptr: iteration variable + * @_tmp: temporary variable, struct uci_element * + * @_ptr: iteration variable, struct uci_element * * * use like a for loop, e.g: - * uci_foreach(section, &list, p) { + * uci_foreach(&list, p) { * ... * } */ -#define uci_foreach_entry(_type, _list, _ptr) \ - for(_ptr = uci_list_entry(_type, (_list)->next); \ +#define uci_foreach_element_safe(_list, _tmp, _ptr) \ + for(_ptr = list_to_element((_list)->next), \ + _tmp = list_to_element(_ptr->list.next); \ &_ptr->list != (_list); \ - _ptr = uci_list_entry(_type, _ptr->list.next)) + _ptr = _tmp, _tmp = list_to_element(_ptr->list.next)) + +/* returns true if a list is empty */ +#define uci_list_empty(list) ((list)->next == (list)) + +/* element typecasting */ +#ifdef UCI_DEBUG_TYPECAST +static const char *uci_typestr[] = { + [UCI_TYPE_PACKAGE] = "package", + [UCI_TYPE_SECTION] = "section", + [UCI_TYPE_OPTION] = "option" +} + +static void uci_typecast_error(int from, int to) +{ + fprintf(stderr, "Invalid typecast from '%s' to '%s'\n", uci_typestr[from], uci_typestr[to]); +} + +#define BUILD_CAST(type, val) \ + static inline struct uci_ ## type *uci_to_ ## type (struct uci_element *e) \ + { \ + if (e->type != val) { \ + uci_typecast_error(e->type, val); \ + } \ + return (struct uci_ ## type *) e; \ + } + +BUILD_CAST(package, UCI_TYPE_PACKAGE) +BUILD_CAST(section, UCI_TYPE_SECTION) +BUILD_CAST(option, UCI_TYPE_OPTION) + +#else +#define uci_to_package(ptr) container_of(ptr, struct uci_package, e) +#define uci_to_section(ptr) container_of(ptr, struct uci_section, e) +#define uci_to_option(ptr) container_of(ptr, struct uci_option, e) +#endif + +/** + * uci_alloc_element: allocate a generic uci_element, reserve a buffer and typecast + * @ctx: uci context + * @type: {package,section,option} + * @name: string containing the name of the element + * @datasize: additional buffer size to reserve at the end of the struct + */ +#define uci_alloc_element(ctx, type, name, datasize) \ + uci_to_ ## type (uci_alloc_generic(ctx, name, sizeof(struct uci_ ## type) + datasize)) + +#define uci_dataptr(ptr) \ + (((char *) ptr) + sizeof(*ptr)) #endif -- 2.30.2