Forráskód Böngészése

Unfinished pline_node_exists() refactoring

Serj Kalichev 8 hónapja
szülő
commit
b7a0b28926
1 módosított fájl, 101 hozzáadás és 2 törlés
  1. 101 2
      src/pline.c

+ 101 - 2
src/pline.c

@@ -1311,6 +1311,7 @@ static void pline_print_type_help(const struct lysc_node *node,
 }
 
 
+/*
 static bool_t pline_node_exists(sr_session_ctx_t* sess, const char *xpath)
 {
 	sr_val_t *vals = NULL;
@@ -1332,6 +1333,102 @@ static bool_t pline_node_exists(sr_session_ctx_t* sess, const char *xpath)
 
 	return found;
 }
+*/
+
+
+static bool_t pline_find_node_within_tree(const struct lyd_node *nodes_list,
+	const struct lysc_node *node)
+{
+	const struct lyd_node *iter = NULL;
+
+	if (!nodes_list)
+		return BOOL_FALSE;
+
+	LY_LIST_FOR(nodes_list, iter) {
+		const char *default_value = NULL;
+		char *value = NULL;
+
+syslog(LOG_ERR, "iter: %s, node: %s", iter->schema->name, node->name);
+		if (iter->schema != node) {
+syslog(LOG_ERR, "iter != node");
+			if (pline_find_node_within_tree(lyd_child(nodes_list),
+				node))
+				return BOOL_TRUE;
+			continue;
+		}
+		if (iter->flags & LYD_DEFAULT) {
+syslog(LOG_ERR, "default");
+			continue;
+}
+		default_value = klysc_node_ext_default(iter->schema);
+		value = klyd_node_value(iter);
+		// Don't show "default" keys with default values
+		if (default_value && faux_str_cmp(default_value, value) == 0) {
+			faux_str_free(value);
+			continue;
+		}
+		faux_str_free(value);
+		return BOOL_TRUE;
+	}
+
+	return BOOL_FALSE;
+}
+
+
+static bool_t pline_node_exists(sr_session_ctx_t* sess, const char *xpath,
+	const struct lysc_node *node)
+{
+	sr_data_t *data = NULL;
+	bool_t found = BOOL_FALSE;
+
+	if (!xpath)
+		return BOOL_FALSE;
+syslog(LOG_ERR, "xpath = %s", xpath);
+	if (sr_get_data(sess, xpath, 1, 0, 0, &data) != SR_ERR_OK)
+		return BOOL_FALSE;
+	if (!data) // Not found
+		return BOOL_FALSE;
+
+	if (pline_find_node_within_tree(data->tree, node))
+		found = BOOL_TRUE;
+	sr_release_data(data);
+
+	return found;
+
+
+/*
+	nodes_list = data->tree;
+
+	while (nodes_list && (edepth > 0)) {
+		nodes_list = lyd_child_no_keys(nodes_list);
+		edepth--;
+	}
+
+	if (nodes_list)
+		show_subtree(nodes_list, 0, DIFF_OP_NONE, opts, BOOL_FALSE);
+
+
+
+	sr_val_t *vals = NULL;
+	size_t val_num = 0;
+	size_t i = 0;
+
+	if (!xpath)
+		return BOOL_FALSE;
+	sr_get_items(sess, xpath, 0, 0, &vals, &val_num);
+	for (i = 0; i < val_num; i++) {
+		// Engine can find defaults for entries but not entries themself
+		if (!vals[i].dflt) {
+			found = BOOL_TRUE;
+			break;
+		}
+	}
+	sr_free_values(vals, val_num);
+
+	return found;
+*/
+
+}
 
 
 void pline_print_completions(const pline_t *pline, bool_t help,
@@ -1376,7 +1473,8 @@ void pline_print_completions(const pline_t *pline, bool_t help,
 
 			// Check node for existing if necessary
 			if (existing_nodes_only &&
-				!pline_node_exists(pline->sess, pcompl->xpath)) {
+				!pline_node_exists(pline->sess, pcompl->xpath,
+				node)) {
 					continue;
 			}
 
@@ -1439,7 +1537,8 @@ void pline_print_completions(const pline_t *pline, bool_t help,
 
 			// Existing entries
 			if (existing_nodes_only &&
-				!pline_node_exists(pline->sess, pcompl->xpath)) {
+				!pline_node_exists(pline->sess, pcompl->xpath,
+				node)) {
 					continue;
 			}