Browse Source

Single function srp_compl_xpath instead srp_compl_xpath_running, srp_compl_xpath_candidate

Serj Kalichev 6 months ago
parent
commit
ab32d81361
5 changed files with 69 additions and 58 deletions
  1. 53 0
      src/kly.c
  2. 1 35
      src/pline.c
  3. 1 3
      src/plugin.c
  4. 4 2
      src/private.h
  5. 10 18
      src/syms.c

+ 53 - 0
src/kly.c

@@ -456,3 +456,56 @@ size_t klyd_visible_child_num(const struct lyd_node *node)
 
 	return num;
 }
+
+
+bool_t kly_str2ds(const char *str, size_t len, sr_datastore_t *ds)
+{
+	if (!str)
+		return BOOL_FALSE;
+	if (len == 0)
+		return BOOL_FALSE;
+	if (!ds)
+		return BOOL_FALSE;
+
+	if (faux_str_cmpn(str, "candidate", len) == 0)
+		*ds = SR_DS_CANDIDATE;
+	else if (faux_str_cmpn(str, "running", len) == 0)
+		*ds = SR_DS_RUNNING;
+	else if (faux_str_cmpn(str, "operational", len) == 0)
+		*ds = SR_DS_OPERATIONAL;
+	else if (faux_str_cmpn(str, "startup", len) == 0)
+		*ds = SR_DS_STARTUP;
+#ifdef SR_DS_FACTORY_DEFAULT
+	else if (faux_str_cmpn(str, "factory-default", len) == 0)
+		*ds = SR_DS_FACTORY_DEFAULT;
+#endif
+	else // No DS prefix found
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
+
+bool_t kly_parse_ext_xpath(const char *xpath, const char **raw_xpath,
+	sr_datastore_t *ds)
+{
+	char *space = NULL;
+
+	if (!xpath)
+		return BOOL_FALSE;
+	if (!raw_xpath)
+		return BOOL_FALSE;
+	if (!ds)
+		return BOOL_FALSE;
+
+	*ds = SRP_REPO_EDIT; // Default
+	*raw_xpath = xpath;
+	space = strchr(xpath, ' ');
+	if (space) {
+		size_t len = space - xpath;
+		if (kly_str2ds(xpath, len, ds))
+			*raw_xpath = space + 1;
+	}
+
+	return BOOL_TRUE;
+}

+ 1 - 35
src/pline.c

@@ -465,39 +465,6 @@ static void pline_add_compl_leafref(pline_t *pline, const struct lysc_node *node
 }
 
 
-static bool_t parse_ext_xpath(const char *xpath, const char **raw_xpath,
-	sr_datastore_t *ds)
-{
-	char *space = NULL;
-	size_t len = 0;
-
-	*ds = SRP_REPO_EDIT; // Default
-	space = strchr(xpath, ' ');
-	if (space) {
-		*raw_xpath = space + 1;
-		len = space - xpath;
-		if (faux_str_cmpn(xpath, "candidate", len) == 0)
-			*ds = SR_DS_CANDIDATE;
-		else if (faux_str_cmpn(xpath, "running", len) == 0)
-			*ds = SR_DS_RUNNING;
-		else if (faux_str_cmpn(xpath, "operational", len) == 0)
-			*ds = SR_DS_OPERATIONAL;
-		else if (faux_str_cmpn(xpath, "startup", len) == 0)
-			*ds = SR_DS_STARTUP;
-#ifdef SR_DS_FACTORY_DEFAULT
-		else if (faux_str_cmpn(xpath, "factory-default", len) == 0)
-			*ds = SR_DS_FACTORY_DEFAULT;
-#endif
-		else // No DS prefix found
-			*raw_xpath = xpath;
-	} else {
-		*raw_xpath = xpath;
-	}
-
-	return BOOL_TRUE;
-}
-
-
 static void pline_add_compl_leaf(pline_t *pline, const struct lysc_node *node,
 	const char *xpath, pat_e pat)
 {
@@ -526,12 +493,11 @@ static void pline_add_compl_leaf(pline_t *pline, const struct lysc_node *node,
 	if (ext_xpath) {
 		const char *raw_xpath = NULL;
 		sr_datastore_t ds = SRP_REPO_EDIT;
-		if (parse_ext_xpath(ext_xpath, &raw_xpath, &ds))
+		if (kly_parse_ext_xpath(ext_xpath, &raw_xpath, &ds))
 			pline_add_compl(pline, PCOMPL_TYPE, NULL, raw_xpath, ds, pat);
 	}
 	pline_add_compl(pline, PCOMPL_TYPE, node, xpath, SRP_REPO_EDIT, pat);
 	pline_add_compl_leafref(pline, node, type, xpath, pat);
-
 }
 
 

+ 1 - 3
src/plugin.c

@@ -82,9 +82,7 @@ int kplugin_sysrepo_init(kcontext_t *context)
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("srp_prompt_edit_path", srp_prompt_edit_path,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
-	kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath_running", srp_compl_xpath_running,
-		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
-	kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath_candidate", srp_compl_xpath_candidate,
+	kplugin_add_syms(plugin, ksym_new_ext("srp_compl_xpath", srp_compl_xpath,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 
 	// Operations

+ 4 - 2
src/private.h

@@ -50,8 +50,7 @@ int srp_help_insert(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_running(kcontext_t *context);
-int srp_compl_xpath_candidate(kcontext_t *context);
+int srp_compl_xpath(kcontext_t *context);
 
 // Operations
 int srp_set(kcontext_t *context);
@@ -108,6 +107,9 @@ char *klysc_leafref_xpath(const struct lysc_node *node,
 const char *klysc_identityref_prefix(struct lysc_type_identityref *type,
 	const char *name);
 size_t klyd_visible_child_num(const struct lyd_node *node);
+bool_t kly_str2ds(const char *str, size_t len, sr_datastore_t *ds);
+bool_t kly_parse_ext_xpath(const char *xpath, const char **raw_xpath,
+	sr_datastore_t *ds);
 
 C_DECL_END
 

+ 10 - 18
src/syms.c

@@ -1111,25 +1111,29 @@ err:
 }
 
 
-static int srp_compl_xpath_common(kcontext_t *context,
-	const sr_datastore_t datastore)
+int srp_compl_xpath_common(kcontext_t *context)
 {
 	sr_session_ctx_t *sess = NULL;
 	sr_val_t *vals = NULL;
 	size_t val_num = 0;
 	size_t i = 0;
 	const char *script = NULL;
+	const char *raw_xpath = NULL;
+	sr_datastore_t ds = SRP_REPO_EDIT;
 
 	assert(context);
 	script = kcontext_script(context);
 	if (faux_str_is_empty(script))
 		return -1;
 
+	if (!kly_parse_ext_xpath(script, &raw_xpath, &ds))
+		return -1;
+
 	sess = srp_udata_sr_sess(context);
-	if (datastore != SRP_REPO_EDIT)
-		sr_session_switch_ds(sess, datastore);
+	if (ds != SRP_REPO_EDIT)
+		sr_session_switch_ds(sess, ds);
 
-	sr_get_items(sess, script, 0, 0, &vals, &val_num);
+	sr_get_items(sess, raw_xpath, 0, 0, &vals, &val_num);
 	for (i = 0; i < val_num; i++) {
 		char *tmp = sr_val_to_str(&vals[i]);
 		if (!tmp)
@@ -1139,20 +1143,8 @@ static int srp_compl_xpath_common(kcontext_t *context,
 	}
 	sr_free_values(vals, val_num);
 
-	if (datastore != SRP_REPO_EDIT)
+	if (ds != SRP_REPO_EDIT)
 		sr_session_switch_ds(sess, SRP_REPO_EDIT);
 
 	return 0;
 }
-
-
-int srp_compl_xpath_running(kcontext_t *context)
-{
-	return srp_compl_xpath_common(context, SR_DS_RUNNING);
-}
-
-
-int srp_compl_xpath_candidate(kcontext_t *context)
-{
-	return srp_compl_xpath_common(context, SR_DS_CANDIDATE);
-}