exit(255);
}
+static void uci_show_section(struct uci_section *p)
+{
+ struct uci_option *o;
+ const char *cname, *sname;
+
+ cname = p->config->name;
+ sname = p->name;
+ uci_foreach_entry(option, &p->options, o) {
+ printf("%s.%s.%s=%s\n", cname, sname, o->name, o->value);
+ }
+}
+
static void uci_show_file(const char *name)
{
struct uci_config *cfg;
- uci_load(ctx, name, &cfg);
+ struct uci_section *p;
+
+ if (uci_load(ctx, name, &cfg) != UCI_OK) {
+ uci_perror(ctx, "uci_load");
+ return;
+ }
+
+ uci_list_empty(&cfg->sections);
+ uci_foreach_entry(section, &cfg->sections, p) {
+ uci_show_section(p);
+ }
uci_unload(ctx, name);
}
/* inserts a new list entry between two consecutive entries */
static inline void __uci_list_add(struct uci_list *prev, struct uci_list *next, struct uci_list *ptr)
{
- prev->next = ptr;
next->prev = ptr;
ptr->prev = prev;
ptr->next = next;
+ prev->next = ptr;
}
/* inserts a new list entry at the tail of the list */
option->value = uci_strdup(ctx, value);
uci_list_add(§ion->options, &option->list);
UCI_TRAP_RESTORE(ctx);
+ return option;
error:
uci_drop_option(option);
return 0;
}
+static inline char *get_filename(char *path)
+{
+ char *p;
+
+ p = strrchr(path, '/');
+ p++;
+ if (!*p)
+ return NULL;
+ return p;
+}
+
char **uci_list_configs(struct uci_context *ctx)
{
char **configs;
return NULL;
size = sizeof(char *) * (globbuf.gl_pathc + 1);
- for(i = 0; i < globbuf.gl_pathc; i++)
- size += strlen(globbuf.gl_pathv[i]) + 1;
+ for(i = 0; i < globbuf.gl_pathc; i++) {
+ char *p;
+
+ p = get_filename(globbuf.gl_pathv[i]);
+ if (!p)
+ continue;
+
+ size += strlen(p) + 1;
+ }
configs = malloc(size);
if (!configs)
memset(configs, 0, size);
buf = (char *) &configs[globbuf.gl_pathc + 1];
for(i = 0; i < globbuf.gl_pathc; i++) {
+ char *p;
+
+ p = get_filename(globbuf.gl_pathv[i]);
+ if (!p)
+ continue;
+
configs[i] = buf;
- strcpy(buf, globbuf.gl_pathv[i]);
+ strcpy(buf, p);
buf += strlen(buf) + 1;
}
return configs;
*/
static void uci_parse_config(struct uci_context *ctx, char **str)
{
- char *type, *name;
+ char *name = NULL;
+ char *type = NULL;
/* 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->cfg, type, name);
+ UCI_TRAP_RESTORE(ctx);
+ return;
+
+error:
+ if (name)
+ free(name);
+ if (type)
+ free(type);
+ UCI_THROW(ctx, ctx->errno);
}
/*
*/
static void uci_parse_option(struct uci_context *ctx, char **str)
{
- char *name, *value;
+ char *name = NULL;
+ char *value = NULL;
+ if (!ctx->pctx->section) {
+ ctx->pctx->byte = *str - ctx->pctx->buf;
+ ctx->pctx->reason = "option command found before the first section";
+ UCI_THROW(ctx, UCI_ERR_PARSE);
+ }
/* 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_TRAP_RESTORE(ctx);
ignore:
+ ctx->errno = 0;
+
/* make sure no memory from previous parse attempts is leaked */
uci_parse_cleanup(ctx);
}
if ((stat(filename, &statbuf) < 0) ||
- ((statbuf.st_mode & S_IFMT) != S_IFREG))
+ ((statbuf.st_mode & S_IFMT) != S_IFREG)) {
UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+ }
pctx->file = fopen(filename, "r");
if (filename != name)
/* private: */
struct uci_config *cfg;
+ struct uci_section *section;
FILE *file;
char *buf;
char *reason;
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
-#define uci_list_empty(list) (list->next == ptr)
+#define uci_list_empty(list) ((list)->next == (list))
#define uci_list_entry(_type, _ptr) \
((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))