cleanups/fixes, more cli stuff
authorFelix Fietkau <nbd@openwrt.org>
Sun, 20 Jan 2008 23:45:12 +0000 (00:45 +0100)
committerFelix Fietkau <nbd@openwrt.org>
Sun, 20 Jan 2008 23:45:12 +0000 (00:45 +0100)
cli.c
list.c
parse.c
uci.h

diff --git a/cli.c b/cli.c
index 5e0697d..23386c1 100644 (file)
--- a/cli.c
+++ b/cli.c
@@ -28,10 +28,32 @@ static void uci_usage(int argc, char **argv)
        exit(255);
 }
 
        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;
 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);
 }
 
        uci_unload(ctx, name);
 }
 
diff --git a/list.c b/list.c
index 8d73f11..48bc17f 100644 (file)
--- a/list.c
+++ b/list.c
@@ -24,10 +24,10 @@ static inline void uci_list_init(struct uci_list *ptr)
 /* 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)
 {
 /* 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;
        next->prev = ptr;
        ptr->prev = prev;
        ptr->next = next;
+       prev->next = ptr;
 }
 
 /* inserts a new list entry at the tail of the list */
 }
 
 /* inserts a new list entry at the tail of the list */
@@ -71,6 +71,7 @@ static struct uci_option *uci_add_option(struct uci_section *section, const char
        option->value = uci_strdup(ctx, value);
        uci_list_add(&section->options, &option->list);
        UCI_TRAP_RESTORE(ctx);
        option->value = uci_strdup(ctx, value);
        uci_list_add(&section->options, &option->list);
        UCI_TRAP_RESTORE(ctx);
+       return option;
 
 error:
        uci_drop_option(option);
 
 error:
        uci_drop_option(option);
@@ -178,6 +179,17 @@ found:
        return 0;
 }
 
        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;
 char **uci_list_configs(struct uci_context *ctx)
 {
        char **configs;
@@ -189,8 +201,15 @@ char **uci_list_configs(struct uci_context *ctx)
                return NULL;
 
        size = sizeof(char *) * (globbuf.gl_pathc + 1);
                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)
 
        configs = malloc(size);
        if (!configs)
@@ -199,8 +218,14 @@ char **uci_list_configs(struct uci_context *ctx)
        memset(configs, 0, size);
        buf = (char *) &configs[globbuf.gl_pathc + 1];
        for(i = 0; i < globbuf.gl_pathc; i++) {
        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;
                configs[i] = buf;
-               strcpy(buf, globbuf.gl_pathv[i]);
+               strcpy(buf, p);
                buf += strlen(buf) + 1;
        }
        return configs;
                buf += strlen(buf) + 1;
        }
        return configs;
diff --git a/parse.c b/parse.c
index e63f900..1297b80 100644 (file)
--- a/parse.c
+++ b/parse.c
@@ -246,14 +246,26 @@ static void assert_eol(struct uci_context *ctx, char **str)
  */
 static void uci_parse_config(struct uci_context *ctx, char **str)
 {
  */
 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;
 
 
        /* 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);
        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);
 }
 
 /*
 }
 
 /*
@@ -261,14 +273,31 @@ static void uci_parse_config(struct uci_context *ctx, char **str)
  */
 static void uci_parse_option(struct uci_context *ctx, char **str)
 {
  */
 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;
 
        /* 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);
        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);
 }
 
 /*
 }
 
 /*
@@ -319,6 +348,8 @@ int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg)
        UCI_TRAP_RESTORE(ctx);
 
 ignore:
        UCI_TRAP_RESTORE(ctx);
 
 ignore:
+       ctx->errno = 0;
+
        /* make sure no memory from previous parse attempts is leaked */
        uci_parse_cleanup(ctx);
 
        /* make sure no memory from previous parse attempts is leaked */
        uci_parse_cleanup(ctx);
 
@@ -340,8 +371,9 @@ ignore:
        }
 
        if ((stat(filename, &statbuf) < 0) ||
        }
 
        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);
                UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+       }
 
        pctx->file = fopen(filename, "r");
        if (filename != name)
 
        pctx->file = fopen(filename, "r");
        if (filename != name)
diff --git a/uci.h b/uci.h
index 6dc00f1..5e6baf6 100644 (file)
--- a/uci.h
+++ b/uci.h
@@ -112,6 +112,7 @@ struct uci_parse_context
 
        /* private: */
        struct uci_config *cfg;
 
        /* private: */
        struct uci_config *cfg;
+       struct uci_section *section;
        FILE *file;
        char *buf;
        char *reason;
        FILE *file;
        char *buf;
        char *reason;
@@ -148,7 +149,7 @@ struct uci_option
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 
 #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)))
 
 #define uci_list_entry(_type, _ptr) \
        ((struct uci_ ## _type *) ((char *)(_ptr) - offsetof(struct uci_ ## _type,list)))