Browse Source

srp_compl_xpath() to show completion for specified xpath. Can be used in operational mode

Serj Kalichev 1 year ago
parent
commit
28a53176c6
3 changed files with 40 additions and 0 deletions
  1. 2 0
      src/plugin.c
  2. 1 0
      src/private.h
  3. 37 0
      src/syms.c

+ 2 - 0
src/plugin.c

@@ -62,6 +62,8 @@ int kplugin_sysrepo_init(kcontext_t *context)
 		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
 		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath", srp_compl_xpath,
+		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
 
 	// Operations
 	kplugin_add_syms(plugin, ksym_new("srp_set", srp_set));

+ 1 - 0
src/private.h

@@ -44,6 +44,7 @@ int srp_help(kcontext_t *context);
 int srp_compl_insert_to(kcontext_t *context);
 int srp_help_insert_to(kcontext_t *context);
 int srp_prompt_edit_path(kcontext_t *context);
+int srp_compl_xpath(kcontext_t *context);
 
 // Operations
 int srp_set(kcontext_t *context);

+ 37 - 0
src/syms.c

@@ -18,6 +18,7 @@
 
 #include <sysrepo.h>
 #include <sysrepo/xpath.h>
+#include <sysrepo/values.h>
 
 #include "pline.h"
 #include "private.h"
@@ -1126,3 +1127,39 @@ err:
 	return ret;
 }
 
+
+int srp_compl_xpath(kcontext_t *context)
+{
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	sr_val_t *vals = NULL;
+	size_t val_num = 0;
+	size_t i = 0;
+	const char *script = NULL;
+
+	assert(context);
+	script = kcontext_script(context);
+	if (faux_str_is_empty(script))
+		return -1;
+
+	if (sr_connect(SR_CONN_DEFAULT, &conn))
+		return -1;
+	if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
+		sr_disconnect(conn);
+		return -1;
+	}
+
+	sr_get_items(sess, script, 0, 0, &vals, &val_num);
+	for (i = 0; i < val_num; i++) {
+		char *tmp = sr_val_to_str(&vals[i]);
+		if (!tmp)
+			continue;
+		printf("%s\n", tmp);
+		free(tmp);
+	}
+	sr_free_values(vals, val_num);
+
+	sr_disconnect(conn);
+
+	return 0;
+}