From 8a3154a1db5078cf211c3ea76171956ba076dd76 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 23 Jan 2008 05:47:56 +0100 Subject: [PATCH] consistency: rename uci_config to uci_package, rename variables as well; add some data structures for config history --- cli.c | 14 +++++++------- file.c | 46 +++++++++++++++++++++++----------------------- libuci.c | 6 +++--- list.c | 56 ++++++++++++++++++++++++++++---------------------------- uci.h | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 5 files changed, 104 insertions(+), 70 deletions(-) diff --git a/cli.c b/cli.c index e02b4f9..1fe7cc7 100644 --- a/cli.c +++ b/cli.c @@ -36,7 +36,7 @@ static void uci_show_section(struct uci_section *p) struct uci_option *o; const char *cname, *sname; - cname = p->config->name; + cname = p->package->name; sname = p->name; printf("%s.%s=%s\n", cname, sname, p->type); uci_foreach_entry(option, &p->options, o) { @@ -47,7 +47,7 @@ static void uci_show_section(struct uci_section *p) static int uci_show(int argc, char **argv) { char *section = (argc > 2 ? argv[2] : NULL); - struct uci_config *cfg; + struct uci_package *package; struct uci_section *s; char **configs; char **p; @@ -58,11 +58,11 @@ static int uci_show(int argc, char **argv) for (p = configs; *p; p++) { if ((argc < 2) || !strcmp(argv[1], *p)) { - if (uci_load(ctx, *p, &cfg) != UCI_OK) { + if (uci_load(ctx, *p, &package) != UCI_OK) { uci_perror(ctx, "uci_load"); return 255; } - uci_foreach_entry(section, &cfg->sections, s) { + uci_foreach_entry(section, &package->sections, s) { if (!section || !strcmp(s->name, section)) uci_show_section(s); } @@ -83,13 +83,13 @@ static int uci_do_export(int argc, char **argv) for (p = configs; *p; p++) { if ((argc < 2) || !strcmp(argv[1], *p)) { - struct uci_config *cfg = NULL; + struct uci_package *package = NULL; int ret; - ret = uci_load(ctx, *p, &cfg); + ret = uci_load(ctx, *p, &package); if (ret) continue; - uci_export(ctx, stdout, cfg); + uci_export(ctx, stdout, package); uci_unload(ctx, *p); } } diff --git a/file.c b/file.c index 54003c1..3ccffb7 100644 --- a/file.c +++ b/file.c @@ -84,9 +84,9 @@ static void uci_file_cleanup(struct uci_context *ctx) return; ctx->pctx = NULL; - if (pctx->cfg) { - uci_list_del(&pctx->cfg->list); - uci_drop_config(pctx->cfg); + if (pctx->package) { + uci_list_del(&pctx->package->list); + uci_drop_config(pctx->package); } if (pctx->buf) free(pctx->buf); @@ -281,10 +281,10 @@ static void uci_switch_config(struct uci_context *ctx) name = pctx->name; /* add the last config to main config file list */ - if (pctx->cfg) { - uci_list_add(&ctx->root, &pctx->cfg->list); + if (pctx->package) { + uci_list_add(&ctx->root, &pctx->package->list); - pctx->cfg = NULL; + pctx->package = NULL; pctx->section = NULL; } @@ -301,7 +301,7 @@ static void uci_switch_config(struct uci_context *ctx) ignore: ctx->errno = 0; - pctx->cfg = uci_alloc_config(ctx, name); + pctx->package = uci_alloc_config(ctx, name); } /* @@ -336,7 +336,7 @@ static void uci_parse_config(struct uci_context *ctx, char **str) char *name = NULL; char *type = NULL; - if (!ctx->pctx->cfg) { + if (!ctx->pctx->package) { if (!ctx->pctx->name) { ctx->pctx->byte = *str - ctx->pctx->buf; ctx->pctx->reason = "attempting to import a file without a package name"; @@ -352,7 +352,7 @@ static void uci_parse_config(struct uci_context *ctx, char **str) type = next_arg(ctx, str, true); name = next_arg(ctx, str, false); assert_eol(ctx, str); - ctx->pctx->section = uci_add_section(ctx->pctx->cfg, type, name); + ctx->pctx->section = uci_add_section(ctx->pctx->package, type, name); UCI_TRAP_RESTORE(ctx); return; @@ -479,14 +479,14 @@ 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_config *cfg, FILE *stream) +static void uci_export_config(struct uci_package *package, FILE *stream) { - struct uci_context *ctx = cfg->ctx; + struct uci_context *ctx = package->ctx; struct uci_section *s; struct uci_option *o; - fprintf(stream, "package '%s'\n", uci_escape(ctx, cfg->name)); - uci_foreach_entry(section, &cfg->sections, s) { + 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) { @@ -497,24 +497,24 @@ static void uci_export_config(struct uci_config *cfg, FILE *stream) fprintf(stream, "\n"); } -int uci_export(struct uci_context *ctx, FILE *stream, struct uci_config *cfg) +int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package) { UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, stream != NULL); - if (cfg) { - uci_export_config(cfg, stream); + if (package) { + uci_export_config(package, stream); goto done; } - uci_foreach_entry(config, &ctx->root, cfg) { - uci_export_config(cfg, stream); + uci_foreach_entry(package, &ctx->root, package) { + uci_export_config(package, stream); } done: return 0; } -int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_config **cfg) +int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package) { struct uci_parse_context *pctx; @@ -539,8 +539,8 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u uci_parse_line(ctx); } - if (cfg) - *cfg = pctx->cfg; + if (package) + *package = pctx->package; pctx->name = NULL; uci_switch_config(ctx); @@ -551,7 +551,7 @@ int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct u return 0; } -int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg) +int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package) { struct stat statbuf; char *filename; @@ -590,6 +590,6 @@ ignore: if (!file) UCI_THROW(ctx, UCI_ERR_IO); - return uci_import(ctx, file, name, cfg); + return uci_import(ctx, file, name, package); } diff --git a/libuci.c b/libuci.c index 47c815a..c6335d5 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_config *cfg; + struct uci_package *package; uci_cleanup(ctx); - uci_foreach_entry(config, &ctx->root, cfg) { - uci_drop_config(cfg); + uci_foreach_entry(package, &ctx->root, package) { + uci_drop_config(package); } free(ctx); return; diff --git a/list.c b/list.c index 00fca3f..01bdb81 100644 --- a/list.c +++ b/list.c @@ -61,8 +61,8 @@ static void uci_drop_option(struct uci_option *option) static struct uci_option *uci_add_option(struct uci_section *section, const char *name, const char *value) { - struct uci_config *cfg = section->config; - struct uci_context *ctx = cfg->ctx; + struct uci_package *package = section->package; + struct uci_context *ctx = package->ctx; struct uci_option *option = NULL; UCI_TRAP_SAVE(ctx, error); @@ -98,23 +98,23 @@ static void uci_drop_section(struct uci_section *section) free(section); } -static struct uci_section *uci_add_section(struct uci_config *cfg, const char *type, const char *name) +static struct uci_section *uci_add_section(struct uci_package *package, const char *type, const char *name) { struct uci_section *section = NULL; - struct uci_context *ctx = cfg->ctx; + struct uci_context *ctx = package->ctx; UCI_TRAP_SAVE(ctx, error); - cfg->n_section++; + package->n_section++; section = (struct uci_section *) uci_malloc(ctx, sizeof(struct uci_section)); - section->config = cfg; + 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", cfg->n_section); - uci_list_add(&cfg->sections, §ion->list); + asprintf(§ion->name, "cfg%d", package->n_section); + uci_list_add(&package->sections, §ion->list); UCI_TRAP_RESTORE(ctx); return section; @@ -125,59 +125,59 @@ error: return NULL; } -static void uci_drop_config(struct uci_config *cfg) +static void uci_drop_config(struct uci_package *package) { struct uci_section *s; - if(!cfg) + if(!package) return; - uci_foreach_entry(section, &cfg->sections, s) { + uci_foreach_entry(section, &package->sections, s) { uci_list_del(&s->list); uci_drop_section(s); } - if (cfg->name) - free(cfg->name); - free(cfg); + if (package->name) + free(package->name); + free(package); } -static struct uci_config *uci_alloc_config(struct uci_context *ctx, const char *name) +static struct uci_package *uci_alloc_config(struct uci_context *ctx, const char *name) { - struct uci_config *cfg = NULL; + struct uci_package *package = NULL; UCI_TRAP_SAVE(ctx, error); - cfg = (struct uci_config *) uci_malloc(ctx, sizeof(struct uci_config)); - uci_list_init(&cfg->list); - uci_list_init(&cfg->sections); - cfg->name = uci_strdup(ctx, name); - cfg->ctx = ctx; + 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 cfg; + return package; error: - uci_drop_config(cfg); + uci_drop_config(package); UCI_THROW(ctx, ctx->errno); return NULL; } int uci_unload(struct uci_context *ctx, const char *name) { - struct uci_config *cfg; + struct uci_package *package; UCI_HANDLE_ERR(ctx); UCI_ASSERT(ctx, name != NULL); - uci_foreach_entry(config, &ctx->root, cfg) { - if (!strcmp(cfg->name, name)) + uci_foreach_entry(package, &ctx->root, package) { + if (!strcmp(package->name, name)) goto found; } UCI_THROW(ctx, UCI_ERR_NOTFOUND); found: - uci_list_del(&cfg->list); - uci_drop_config(cfg); + uci_list_del(&package->list); + uci_drop_config(package); return 0; } diff --git a/uci.h b/uci.h index a34a36b..cca38c2 100644 --- a/uci.h +++ b/uci.h @@ -38,9 +38,10 @@ struct uci_list void *prev; }; -struct uci_config; +struct uci_package; struct uci_section; struct uci_option; +struct uci_history; struct uci_parse_context; @@ -66,27 +67,28 @@ extern void uci_perror(struct uci_context *ctx, const char *str); * @ctx: uci context * @stream: file stream to import from * @name: (optional) assume the config has the given name - * @cfg: (optional) store the last parsed config package in this variable + * @package: (optional) store the last parsed config package in this variable * * the name parameter is for config files that don't explicitly use the 'package <...>' keyword */ -extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_config **cfg); +extern int uci_import(struct uci_context *ctx, FILE *stream, const char *name, struct uci_package **package); /** * uci_export: Export one or all uci config packages * @ctx: uci context * @stream: output stream - * @cfg: (optional) uci config package to export + * @package: (optional) uci config package to export */ -extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_config *cfg); +extern int uci_export(struct uci_context *ctx, FILE *stream, struct uci_package *package); /** * uci_load: Parse an uci config file and store it in the uci context * * @ctx: uci context * @name: name of the config file (relative to the config directory) + * @package: store the loaded config package in this variable */ -extern int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg); +extern int uci_load(struct uci_context *ctx, const char *name, struct uci_package **package); /** * uci_unload: Unload a config file from the uci context @@ -135,7 +137,7 @@ struct uci_parse_context int byte; /* private: */ - struct uci_config *cfg; + struct uci_package *package; struct uci_section *section; FILE *file; const char *name; @@ -143,7 +145,7 @@ struct uci_parse_context int bufsz; }; -struct uci_config +struct uci_package { struct uci_list list; struct uci_list sections; @@ -157,7 +159,7 @@ struct uci_section { struct uci_list list; struct uci_list options; - struct uci_config *config; + struct uci_package *package; char *type; char *name; }; @@ -170,6 +172,38 @@ struct uci_option char *value; }; +enum uci_type { + UCI_TYPE_PACKAGE, + UCI_TYPE_SECTION, + UCI_TYPE_OPTION +}; + +enum uci_command { + UCI_CMD_ADD, + UCI_CMD_REMOVE, + UCI_CMD_CHANGE +}; + +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; + } data; +}; + /* linked list handling */ #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) -- 2.11.0