Serj Kalichev 1 рік тому
батько
коміт
4176eca857
3 змінених файлів з 41 додано та 219 видалено
  1. 31 22
      pline.c
  2. 7 21
      pline.h
  3. 3 176
      ytree.c

+ 31 - 22
pline.c

@@ -22,7 +22,7 @@
 #define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
 
 
-pexpr_t *pexpr_new(void)
+static pexpr_t *pexpr_new(void)
 {
 	pexpr_t *pexpr = NULL;
 
@@ -40,7 +40,7 @@ pexpr_t *pexpr_new(void)
 }
 
 
-void pexpr_free(pexpr_t *pexpr)
+static void pexpr_free(pexpr_t *pexpr)
 {
 	if (!pexpr)
 		return;
@@ -52,7 +52,7 @@ void pexpr_free(pexpr_t *pexpr)
 }
 
 
-pcompl_t *pcompl_new(void)
+static pcompl_t *pcompl_new(void)
 {
 	pcompl_t *pcompl = NULL;
 
@@ -70,7 +70,7 @@ pcompl_t *pcompl_new(void)
 }
 
 
-void pcompl_free(pcompl_t *pcompl)
+static void pcompl_free(pcompl_t *pcompl)
 {
 	if (!pcompl)
 		return;
@@ -81,7 +81,7 @@ void pcompl_free(pcompl_t *pcompl)
 }
 
 
-pline_t *pline_new(void)
+pline_t *pline_new(sr_session_ctx_t *sess)
 {
 	pline_t *pline = NULL;
 
@@ -91,6 +91,7 @@ pline_t *pline_new(void)
 		return NULL;
 
 	// Init
+	pline->sess = sess;
 	pline->exprs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
 		NULL, NULL, (faux_list_free_fn)pexpr_free);
 	pline->compls = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
@@ -111,7 +112,7 @@ void pline_free(pline_t *pline)
 	faux_free(pline);
 }
 
-pexpr_t *pline_add_expr(pline_t *pline, const char *xpath)
+static pexpr_t *pline_add_expr(pline_t *pline, const char *xpath)
 {
 	pexpr_t *pexpr = NULL;
 
@@ -124,7 +125,7 @@ pexpr_t *pline_add_expr(pline_t *pline, const char *xpath)
 }
 
 
-pexpr_t *pline_current_expr(pline_t *pline)
+static pexpr_t *pline_current_expr(pline_t *pline)
 {
 	assert(pline);
 
@@ -135,7 +136,7 @@ pexpr_t *pline_current_expr(pline_t *pline)
 }
 
 
-void pline_add_compl(pline_t *pline,
+static void pline_add_compl(pline_t *pline,
 	pcompl_type_e type, const struct lysc_node *node, char *xpath)
 {
 	pcompl_t *pcompl = NULL;
@@ -151,7 +152,7 @@ void pline_add_compl(pline_t *pline,
 }
 
 
-void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
+static void pline_add_compl_subtree(pline_t *pline, const struct lys_module *module,
 	const struct lysc_node *node)
 {
 	const struct lysc_node *subtree = NULL;
@@ -204,11 +205,9 @@ void pline_debug(pline_t *pline)
 }
 
 
-static int
-sr_ly_module_is_internal(const struct lys_module *ly_mod);
+int sr_ly_module_is_internal(const struct lys_module *ly_mod);
 
-int
-sr_module_is_internal(const struct lys_module *ly_mod);
+int sr_module_is_internal(const struct lys_module *ly_mod);
 
 
 // Don't use standard lys_find_child() because it checks given module to be
@@ -274,7 +273,7 @@ static const char *identityref_prefix(struct lysc_type_identityref *type,
 }
 
 
-bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
+static bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
 	pline_t *pline)
 {
 	faux_argv_node_t *arg = faux_argv_iter(argv);
@@ -476,13 +475,21 @@ bool_t pline_parse_module(const struct lys_module *module, faux_argv_t *argv,
 }
 
 
-pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags)
+pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, uint32_t flags)
 {
+	const struct ly_ctx *ctx = NULL;
 	struct lys_module *module = NULL;
-	pline_t *pline = pline_new();
+	pline_t *pline = NULL;
 	uint32_t i = 0;
 
-	assert(ctx);
+	assert(sess);
+	if (!sess)
+		return NULL;
+
+	pline = pline_new(sess);
+	if (!pline)
+		return NULL;
+	ctx = sr_session_acquire_context(pline->sess);
 	if (!ctx)
 		return NULL;
 
@@ -501,6 +508,8 @@ pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags
 			break; // Found
 	}
 
+	sr_session_release_context(pline->sess);
+
 	return pline;
 }
 
@@ -523,7 +532,7 @@ static void identityref(struct lysc_ident *ident)
 }
 
 
-void pline_print_type_completions(const struct lysc_type *type)
+static void pline_print_type_completions(const struct lysc_type *type)
 {
 	assert(type);
 
@@ -573,7 +582,7 @@ void pline_print_type_completions(const struct lysc_type *type)
 }
 
 
-void pline_print_type_help(const struct lysc_node *node,
+static void pline_print_type_help(const struct lysc_node *node,
 	const struct lysc_type *type)
 {
 	assert(type);
@@ -666,8 +675,7 @@ void pline_print_type_help(const struct lysc_node *node,
 }
 
 
-void pline_print_completions(const pline_t *pline,
-	sr_session_ctx_t *sess, bool_t help)
+void pline_print_completions(const pline_t *pline, bool_t help)
 {
 	faux_list_node_t *iter = NULL;
 	pcompl_t *pcompl = NULL;
@@ -682,7 +690,8 @@ void pline_print_completions(const pline_t *pline,
 			size_t val_num = 0;
 			size_t i = 0;
 
-			sr_get_items(sess, pcompl->xpath, 0, 0, &vals, &val_num);
+			sr_get_items(pline->sess, pcompl->xpath,
+				0, 0, &vals, &val_num);
 			for (i = 0; i < val_num; i++) {
 				char *tmp = sr_val_to_str(&vals[i]);
 				if (!tmp)

+ 7 - 21
pline.h

@@ -8,6 +8,7 @@
 #include <sysrepo.h>
 #include <sysrepo/xpath.h>
 
+
 // Plain EXPRession
 typedef struct {
 	char *xpath;
@@ -33,36 +34,21 @@ typedef struct {
 
 // Plain LINE
 typedef struct pline_s {
+	sr_session_ctx_t *sess;
 	faux_list_t *exprs;
 	faux_list_t *compls;
 } pline_t;
 
+
 C_DECL_BEGIN
 
+pline_t *pline_new(sr_session_ctx_t *sess);
+pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, uint32_t flags);
 
-pline_t *pline_new(void);
 void pline_free(pline_t *pline);
-pline_t *pline_parse(const struct ly_ctx *ctx, faux_argv_t *argv, uint32_t flags);
-pexpr_t *pline_current_expr(pline_t *pline);
-void pline_debug(pline_t *pline);
-void pline_print_completions(const pline_t *pline,
-	sr_session_ctx_t *sess, bool_t help);
-
-//void pline_set_quotes(pline_t *fargv, const char *quotes);
 
-//ssize_t pline_len(const pline_t *fargv);
-//pline_node_t *pline_iter(const pline_t *fargv);
-//const char *pline_each(pline_node_t **iter);
-//const char *pline_current(pline_node_t *iter);
-//const char *pline_index(const pline_t *fargv, size_t index);
-
-//ssize_t pline_parse(pline_t *fargv, const char *str);
-//bool_t pline_add(pline_t *fargv, const char *arg);
-
-//bool_t pline_is_continuable(const pline_t *fargv);
-//void pline_set_continuable(pline_t *fargv, bool_t continuable);
-
-//bool_t pline_is_last(pline_node_t *iter);
+void pline_debug(pline_t *pline);
+void pline_print_completions(const pline_t *pline, bool_t help);
 
 C_DECL_END
 

+ 3 - 176
ytree.c

@@ -12,21 +12,6 @@
 
 #include "pline.h"
 
-#define NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST)
-
-
-struct ipath {
-	struct ly_path *path_arr;
-	const char *value;
-};
-
-
-
-static void process_node(const struct lysc_node *node, size_t level);
-static void iterate_nodes(const struct lysc_node *node, size_t level);
-
-
-
 
 static int
 sr_ly_module_is_internal(const struct lys_module *ly_mod)
@@ -87,153 +72,6 @@ sr_module_is_internal(const struct lys_module *ly_mod)
     return 0;
 }
 
-static void identityref(struct lysc_ident *ident)
-{
-	LY_ARRAY_COUNT_TYPE u = 0;
-
-	if (!ident)
-		return;
-
-	if (!ident->derived) {
-		printf(" %s", ident->name);
-		return;
-	}
-
-	LY_ARRAY_FOR(ident->derived, u) {
-		identityref(ident->derived[u]);
-	}
-
-}
-
-static void process_node(const struct lysc_node *node, size_t level)
-{
-	if (!node)
-		return;
-
-	printf("%*c %s:%s [%s]",
-		(int)(level * 2), ' ',
-		node->module->name,
-		node->name,
-		lys_nodetype2str(node->nodetype));
-
-	if (node->nodetype & LYS_LIST) {
-		const struct lysc_node *iter = NULL;
-		LY_LIST_FOR(lysc_node_child(node), iter) {
-			if (lysc_is_key(iter))
-				printf(" %s", iter->name);
-		}
-
-	} else if (node->nodetype & LYS_LEAF) {
-		const struct lysc_node_leaf *leaf =
-			(const struct lysc_node_leaf *)node;
-		const struct lysc_type *type = leaf->type;
-		if (type->basetype == LY_TYPE_IDENT) {
-			struct lysc_type_identityref *iref =
-				(struct lysc_type_identityref *)type;
-			LY_ARRAY_COUNT_TYPE u = 0;
-			LY_ARRAY_FOR(iref->bases, u) {
-				identityref(iref->bases[u]);
-			}
-		}
-	}
-
-
-	printf("\n");
-	iterate_nodes(lysc_node_child(node), level + 1);
-}
-
-
-static void iterate_nodes(const struct lysc_node *node, size_t level)
-{
-	const struct lysc_node *iter = NULL;
-
-	if (!node)
-		return;
-
-	LY_LIST_FOR(node, iter) {
-		if (!(iter->nodetype & (
-			LYS_CONTAINER |
-			LYS_LIST |
-			LYS_LEAF |
-			LYS_LEAFLIST
-			)))
-			continue;
-		if (!(iter->flags & LYS_CONFIG_W))
-			continue;
-		process_node(iter, level);
-	}
-}
-
-
-void show(const struct ly_ctx *ctx)
-{
-	struct lys_module *module = NULL;
-	uint32_t i = 0;
-
-	// Iterate all modules
-	i = 0;
-	while ((module = ly_ctx_get_module_iter(ctx, &i))) {
-		if (sr_module_is_internal(module))
-			continue;
-		if (!module->compiled)
-			continue;
-		if (!module->implemented)
-			continue;
-		if (!module->compiled->data)
-			continue;
-		printf("%s\n", module->name);
-		iterate_nodes(module->compiled->data, 1);
-	}
-
-}
-
-
-const struct lysc_node *ppath2path_node(const struct lys_module *module,
-	const struct lysc_node *parent, const faux_argv_t *argv)
-{
-	const struct lysc_node *node = NULL;
-	const char *search = faux_argv_index(argv, 0);
-
-	printf("searching for %s\n", search);
-
-	node = lys_find_child(node, module, search, 0,
-		NODETYPE_CONF, 0);
-
-	if (node)
-		printf("%s\n", search);
-
-	return node;
-}
-
-
-void ppath2path(const struct ly_ctx *ctx, const char *ppath)
-{
-	faux_argv_t *argv = NULL;
-	struct lys_module *module = NULL;
-	uint32_t i = 0;
-
-	argv = faux_argv_new();
-	faux_argv_parse(argv, ppath);
-
-	// Iterate all modules
-	i = 0;
-	while ((module = ly_ctx_get_module_iter(ctx, &i))) {
-		if (sr_module_is_internal(module))
-			continue;
-		if (!module->compiled)
-			continue;
-		if (!module->implemented)
-			continue;
-		if (!module->compiled->data)
-			continue;
-		printf("Module %s\n", module->name);
-		if (ppath2path_node(module, NULL, argv)) {
-			printf("Found\n");
-		}
-	}
-
-}
-
 
 int main(int argc, char **argv)
 {
@@ -241,7 +79,6 @@ int main(int argc, char **argv)
 	int err = SR_ERR_OK;
 	sr_conn_ctx_t *conn = NULL;
 	sr_session_ctx_t *sess = NULL;
-	const struct ly_ctx *ctx = NULL;
 	faux_argv_t *args = faux_argv_new();
 	pline_t *pline = NULL;
 
@@ -255,27 +92,17 @@ int main(int argc, char **argv)
 		printf("Error2\n");
 		goto out;
 	}
-	ctx = sr_acquire_context(conn);
-	if (!ctx) {
-		printf("Cannot acquire context\n");
-		goto out;
-	}
 
 	faux_argv_parse(args, argv[1]);
 	faux_argv_del_continuable(args);
-	pline = pline_parse(ctx, args, 0);
+	pline = pline_parse(sess, args, 0);
 	faux_argv_free(args);
-	pline_debug(pline);
-	pline_print_completions(pline, sess, BOOL_TRUE);
+//	pline_debug(pline);
+	pline_print_completions(pline, BOOL_TRUE);
 	pline_free(pline);
 
-//	ppath2path(ctx, "interfaces interface eth0 type");
-
-//	show(ctx);
-
 	ret = 0;
 out:
-	sr_release_context(conn);
 	sr_disconnect(conn);
 
 	return 0;