if (!configs)
return 0;
+ if (argc >= 2) {
+ if (uci_load(ctx, argv[1], &package) != UCI_OK) {
+ uci_perror(ctx, NULL);
+ return 1;
+ }
+ uci_show_package(package, section);
+ uci_unload(ctx, package);
+ return 0;
+ }
+
for (p = configs; *p; p++) {
if ((argc < 2) || !strcmp(argv[1], *p)) {
if (uci_load(ctx, *p, &package) != UCI_OK) {
- uci_perror(ctx, "uci_load");
- return 255;
+ uci_perror(ctx, NULL);
+ return 1;
}
uci_show_package(package, section);
- uci_unload(ctx, *p);
+ uci_unload(ctx, package);
}
}
if (ret)
continue;
uci_export(ctx, stdout, package);
- uci_unload(ctx, *p);
+ uci_unload(ctx, package);
}
}
return 0;
return 1;
}
- if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
+ if (uci_lookup(ctx, &e, p, section, option) != UCI_OK)
return 1;
switch(e->type) {
static void uci_switch_config(struct uci_context *ctx)
{
struct uci_parse_context *pctx;
+ struct uci_element *e;
const char *name;
pctx = ctx->pctx;
* ignore errors here, e.g. if the config was not found
*/
UCI_TRAP_SAVE(ctx, ignore);
- uci_unload(ctx, name);
+ e = uci_lookup_list(ctx, &ctx->root, name);
+ if (e)
+ uci_unload(ctx, uci_to_package(e));
UCI_TRAP_RESTORE(ctx);
ignore:
ctx->errno = 0;
{
struct stat statbuf;
char *filename;
- bool confpath;
+ bool confdir;
FILE *file;
int fd;
switch (name[0]) {
case '.':
+ if (name[1] != '/')
+ UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+ /* fall through */
case '/':
/* absolute/relative path outside of /etc/config */
- filename = (char *) name;
- confpath = false;
+ filename = uci_strdup(ctx, name);
+ name = strrchr(name, '/') + 1;
+ confdir = false;
break;
default:
filename = uci_malloc(ctx, strlen(name) + sizeof(UCI_CONFDIR) + 2);
sprintf(filename, UCI_CONFDIR "/%s", name);
- confpath = true;
+ confdir = true;
break;
}
}
fd = open(filename, O_RDONLY);
- if (filename != name)
- free(filename);
+ if (fd <= 0)
+ UCI_THROW(ctx, UCI_ERR_IO);
flock(fd, LOCK_SH);
file = fdopen(fd, "r");
uci_import(ctx, file, name, package);
UCI_TRAP_RESTORE(ctx);
+ if (*package) {
+ (*package)->path = filename;
+ (*package)->confdir = confdir;
+ }
+
done:
flock(fd, LOCK_UN);
fclose(file);
if(!p)
return;
+ if (p->path)
+ free(p->path);
uci_foreach_element_safe(&p->sections, tmp, e) {
uci_free_section(uci_to_section(e));
}
}
-static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, char *name)
+static struct uci_element *uci_lookup_list(struct uci_context *ctx, struct uci_list *list, const char *name)
{
struct uci_element *e;
UCI_THROW(ctx, UCI_ERR_NOTFOUND);
}
-int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option)
+int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *p, char *section, char *option)
{
struct uci_element *e;
- struct uci_package *p;
struct uci_section *s;
struct uci_option *o;
UCI_HANDLE_ERR(ctx);
UCI_ASSERT(ctx, res != NULL);
- UCI_ASSERT(ctx, package != NULL);
+ UCI_ASSERT(ctx, p != NULL);
- e = uci_lookup_list(ctx, &ctx->root, package);
- if (!section)
- goto found;
-
- p = uci_to_package(e);
e = uci_lookup_list(ctx, &p->sections, section);
if (!option)
goto found;
UCI_ASSERT(ctx, package != NULL);
UCI_ASSERT(ctx, section != NULL);
- fprintf(stderr, "uci_set: '%s' '%s' '%s' = '%s'\n", package, section, option, value);
-
/*
* look up the package, section and option (if set)
* if the section/option is to be modified and it is not found
return 0;
}
-int uci_unload(struct uci_context *ctx, const char *name)
+int uci_unload(struct uci_context *ctx, struct uci_package *p)
{
struct uci_element *e;
UCI_HANDLE_ERR(ctx);
- UCI_ASSERT(ctx, name != NULL);
-
- uci_foreach_element(&ctx->root, e) {
- if (!strcmp(e->name, name))
- goto found;
- }
- UCI_THROW(ctx, UCI_ERR_NOTFOUND);
-
-found:
- uci_free_package(uci_to_package(e));
+ UCI_ASSERT(ctx, p != NULL);
+ uci_free_package(p);
return 0;
}
#define UCI_DEBUG_TYPECAST
#endif
+#include <stdbool.h>
#include <setjmp.h>
#include <stdio.h>
* uci_unload: Unload a config file from the uci context
*
* @ctx: uci context
- * @name: name of the config file
+ * @package: pointer to the uci_package struct
*/
-extern int uci_unload(struct uci_context *ctx, const char *name);
+extern int uci_unload(struct uci_context *ctx, struct uci_package *p);
/**
* uci_cleanup: Clean up after an error
*
* @ctx: uci context
* @res: where to store the result
- * @package: config package
+ * @package: uci_package struct
* @section: config section (optional)
* @option: option to search for (optional)
*
* If section is omitted, then a pointer to the config package is returned
* If option is omitted, then a pointer to the config section is returned
*/
-extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, char *package, char *section, char *option);
+extern int uci_lookup(struct uci_context *ctx, struct uci_element **res, struct uci_package *package, char *section, char *option);
/**
* uci_set_element_value: Replace an element's value with a new one
struct uci_element e;
struct uci_list sections;
struct uci_context *ctx;
+ bool confdir;
+ char *path;
+
/* private: */
int n_section;
struct uci_list history;