Browse Source

Juniper-like show, identity withowt prefix

Serj Kalichev 1 year ago
parent
commit
a687b5968f
3 changed files with 52 additions and 15 deletions
  1. 3 2
      src/pline.h
  2. 45 12
      src/show.c
  3. 4 1
      src/syms.h

+ 3 - 2
src/pline.h

@@ -112,8 +112,9 @@ typedef struct pline_s {
 
 // Parse options
 typedef enum {
-	PPARSE_SINGLE_KEY_W_STMT = 0x01,
-	PPARSE_MULTI_KEYS_W_STMT = 0x02,
+	PPARSE_SINGLE_KEY_W_STMT	= 0x00000001,
+	PPARSE_MULTI_KEYS_W_STMT	= 0x00000002,
+	PPARSE_JUNIPER_SHOW		= 0x00010000,
 } pparse_flags_e;
 
 

+ 45 - 12
src/show.c

@@ -18,7 +18,9 @@
 #include "pline.h"
 #include "show.h"
 
-#define LEVEL_SPACES_NUM 2
+#define LEVEL_SPACES_NUM 4
+
+#define JUN(flags) (flags & PPARSE_JUNIPER_SHOW)
 
 
 static void show_container(const struct lyd_node *node, size_t level, uint32_t flags);
@@ -29,14 +31,44 @@ static void show_node(const struct lyd_node *node, size_t level, uint32_t flags)
 static void show_subtree(const struct lyd_node *nodes_list, size_t level, uint32_t flags);
 
 
+static const char *get_value(const struct lyd_node *node)
+{
+	const struct lysc_node *schema = NULL;
+	const struct lysc_type *type = NULL;
+	const struct lyd_value *value = NULL;
+
+	if (!node)
+		return NULL;
+
+	schema = node->schema;
+	if (!(schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
+		return NULL;
+
+	if (schema->nodetype & LYS_LEAF)
+		type = ((const struct lysc_node_leaf *)schema)->type;
+	else
+		type = ((const struct lysc_node_leaflist *)schema)->type;
+
+	if (type->basetype != LY_TYPE_IDENT)
+		return lyd_get_value(node);
+
+	// Identity
+	value = &((const struct lyd_node_term *)node)->value;
+
+	return value->ident->name;
+}
+
+
 static void show_container(const struct lyd_node *node, size_t level, uint32_t flags)
 {
 	if (!node)
 		return;
 
-	printf("%*s%s\n", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name);
-
+	printf("%*s%s%s\n", (int)(level * LEVEL_SPACES_NUM), "",
+		node->schema->name, JUN(flags) ? " {" : "");
 	show_subtree(lyd_child(node), level + 1, flags);
+	if (JUN(flags))
+		printf("%*s%s\n", (int)(level * LEVEL_SPACES_NUM), "", "}");
 }
 
 
@@ -60,11 +92,12 @@ static void show_list(const struct lyd_node *node, size_t level, uint32_t flags)
 			continue;
 		if (with_stmt)
 			printf(" %s", iter->schema->name);
-		printf(" %s", lyd_get_value(iter));
+		printf(" %s", get_value(iter));
 	}
-	printf("\n");
-
+	printf("%s\n", JUN(flags) ? " {" : "");
 	show_subtree(lyd_child(node), level + 1, flags);
+	if (JUN(flags))
+		printf("%*s%s\n", (int)(level * LEVEL_SPACES_NUM), "", "}");
 }
 
 
@@ -81,9 +114,9 @@ static void show_leaf(const struct lyd_node *node, size_t level, uint32_t flags)
 
 	leaf = (struct lysc_node_leaf *)node->schema;
 	if (leaf->type->basetype != LY_TYPE_EMPTY)
-		printf(" %s", lyd_get_value(node));
+		printf(" %s", get_value(node));
 
-	printf("\n");
+	printf("%s\n", JUN(flags) ? ";" : "");
 }
 
 
@@ -92,8 +125,8 @@ static void show_leaflist(const struct lyd_node *node, size_t level, uint32_t fl
 	if (!node)
 		return;
 
-	printf("%*s%s %s\n", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name,
-		lyd_get_value(node));
+	printf("%*s%s %s%s\n", (int)(level * LEVEL_SPACES_NUM), "", node->schema->name,
+		get_value(node), JUN(flags) ? ";" : "");
 }
 
 
@@ -167,7 +200,7 @@ static char *list_keys_str(const struct lyd_node *node)
 			continue;
 		if (keys)
 			faux_str_cat(&keys, " ");
-		faux_str_cat(&keys, lyd_get_value(iter));
+		faux_str_cat(&keys, get_value(iter));
 	}
 
 	return keys;
@@ -195,7 +228,7 @@ static int leaflist_compare(const void *first, const void *second)
 	const struct lyd_node *f = (const struct lyd_node *)first;
 	const struct lyd_node *s = (const struct lyd_node *)second;
 
-	return faux_str_numcmp(lyd_get_value(f), lyd_get_value(s));
+	return faux_str_numcmp(get_value(f), get_value(s));
 }
 
 

+ 4 - 1
src/syms.h

@@ -14,7 +14,10 @@
 #define SRP_REPO_EDIT SR_DS_CANDIDATE
 
 // Defaut parse options
-#define SRP_DEFAULT_PARSE_OPTS (PPARSE_MULTI_KEYS_W_STMT)
+#define SRP_DEFAULT_PARSE_OPTS ( \
+	PPARSE_MULTI_KEYS_W_STMT | \
+	PPARSE_JUNIPER_SHOW \
+	)
 
 
 C_DECL_BEGIN