Browse Source

cleanups/fixes, more cli stuff

Felix Fietkau 16 years ago
parent
commit
fe3c9bb510
4 changed files with 89 additions and 9 deletions
  1. 23 1
      cli.c
  2. 29 4
      list.c
  3. 35 3
      parse.c
  4. 2 1
      uci.h

+ 23 - 1
cli.c

@@ -28,10 +28,32 @@ static void uci_usage(int argc, char **argv)
 	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);
 }
 

+ 29 - 4
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)
 {
-	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 */
@@ -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);
+	return option;
 
 error:
 	uci_drop_option(option);
@@ -178,6 +179,17 @@ found:
 	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;
@@ -189,8 +201,15 @@ char **uci_list_configs(struct uci_context *ctx)
 		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)
@@ -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++) {
+		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;

+ 35 - 3
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)
 {
-	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);
 }
 
 /*
@@ -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)
 {
-	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);
 }
 
 /*
@@ -319,6 +348,8 @@ int uci_load(struct uci_context *ctx, const char *name, struct uci_config **cfg)
 	UCI_TRAP_RESTORE(ctx);
 
 ignore:
+	ctx->errno = 0;
+
 	/* make sure no memory from previous parse attempts is leaked */
 	uci_parse_cleanup(ctx);
 
@@ -340,8 +371,9 @@ ignore:
 	}
 
 	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)

+ 2 - 1
uci.h

@@ -112,6 +112,7 @@ struct uci_parse_context
 
 	/* private: */
 	struct uci_config *cfg;
+	struct uci_section *section;
 	FILE *file;
 	char *buf;
 	char *reason;
@@ -148,7 +149,7 @@ struct uci_option
 #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)))