Bläddra i källkod

implement uci get

Felix Fietkau 16 år sedan
förälder
incheckning
e6279f8712
2 ändrade filer med 50 tillägg och 3 borttagningar
  1. 42 0
      cli.c
  2. 8 3
      uci.h

+ 42 - 0
cli.c

@@ -94,12 +94,54 @@ static int uci_do_export(int argc, char **argv)
 	return 0;
 }
 
+
+
+static int uci_do_get(int argc, char **argv)
+{
+	char *package = NULL;
+	char *section = NULL;
+	char *option = NULL;
+	struct uci_package *p = NULL;
+	struct uci_element *e = NULL;
+	char *value = NULL;
+
+	package = strtok(argv[1], ".");
+	if (!package)
+		return 1;
+
+	section = strtok(NULL, ".");
+	if (section)
+		option = strtok(NULL, ".");
+
+	if (uci_load(ctx, package, &p) != UCI_OK) {
+		uci_perror(ctx, "uci");
+		return 1;
+	}
+	if (uci_lookup(ctx, &e, package, section, option) != UCI_OK)
+		return 1;
+	switch(e->type) {
+	case UCI_TYPE_SECTION:
+		value = uci_to_section(e)->type;
+		break;
+	case UCI_TYPE_OPTION:
+		value = uci_to_option(e)->value;
+		break;
+	default:
+		/* should not happen */
+		return 1;
+	}
+	printf("%s\n", value);
+	return 0;
+}
+
 static int uci_cmd(int argc, char **argv)
 {
 	if (!strcasecmp(argv[0], "show"))
 		return uci_show(argc, argv);
 	if (!strcasecmp(argv[0], "export"))
 		return uci_do_export(argc, argv);
+	if (!strcasecmp(argv[0], "get"))
+		return uci_do_get(argc, argv);
 	return 255;
 }
 

+ 8 - 3
uci.h

@@ -144,9 +144,9 @@ extern char **uci_list_configs(struct uci_context *ctx);
 
 /* UCI data structures */
 enum uci_type {
-	uci_type_package = 0,
-	uci_type_section = 1,
-	uci_type_option = 2
+	UCI_TYPE_PACKAGE = 0,
+	UCI_TYPE_SECTION = 1,
+	UCI_TYPE_OPTION = 2
 };
 
 struct uci_element
@@ -290,6 +290,11 @@ struct uci_history
 /* returns true if a list is empty */
 #define uci_list_empty(list) ((list)->next == (list))
 
+/* wrappers for dynamic type handling */
+#define uci_type_package UCI_TYPE_PACKAGE
+#define uci_type_section UCI_TYPE_SECTION
+#define uci_type_option UCI_TYPE_OPTION
+
 /* element typecasting */
 #ifdef UCI_DEBUG_TYPECAST
 static const char *uci_typestr[] = {