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) {
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;
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);
}
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);
}
}
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);
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;
}
ignore:
ctx->errno = 0;
- pctx->cfg = uci_alloc_config(ctx, name);
+ pctx->package = uci_alloc_config(ctx, name);
}
/*
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";
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;
/*
* 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) {
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;
uci_parse_line(ctx);
}
- if (cfg)
- *cfg = pctx->cfg;
+ if (package)
+ *package = pctx->package;
pctx->name = NULL;
uci_switch_config(ctx);
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;
if (!file)
UCI_THROW(ctx, UCI_ERR_IO);
- return uci_import(ctx, file, name, cfg);
+ return uci_import(ctx, file, name, package);
}
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;
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);
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;
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;
}
void *prev;
};
-struct uci_config;
+struct uci_package;
struct uci_section;
struct uci_option;
+struct uci_history;
struct uci_parse_context;
* @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
int byte;
/* private: */
- struct uci_config *cfg;
+ struct uci_package *package;
struct uci_section *section;
FILE *file;
const char *name;
int bufsz;
};
-struct uci_config
+struct uci_package
{
struct uci_list list;
struct uci_list sections;
{
struct uci_list list;
struct uci_list options;
- struct uci_config *config;
+ struct uci_package *package;
char *type;
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,
+ 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)