Serj Kalichev 1 éve
szülő
commit
3c2e686d69
4 módosított fájl, 116 hozzáadás és 13 törlés
  1. 5 1
      src/plugin.c
  2. 86 6
      src/syms.c
  3. 2 0
      src/syms.h
  4. 23 6
      xml/sysrepo.xml

+ 5 - 1
src/plugin.c

@@ -53,9 +53,13 @@ int kplugin_sysrepo_init(kcontext_t *context)
 	// Operations
 	kplugin_add_syms(plugin, ksym_new("srp_set", srp_set));
 	kplugin_add_syms(plugin, ksym_new("srp_del", srp_del));
-	// Note: srp_edit() must be sync to set current path
+	// Note: 'edit', 'top', 'up'  must be sync to set current path
 	kplugin_add_syms(plugin, ksym_new_ext("srp_edit", srp_edit,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("srp_top", srp_top,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
+		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 
 	return 0;
 }

+ 86 - 6
src/syms.c

@@ -99,6 +99,25 @@ int srp_help(kcontext_t *context)
 }
 
 
+int srp_prompt_edit_path(kcontext_t *context)
+{
+	faux_argv_t *cur_path = NULL;
+	kplugin_t *plugin = NULL;
+	char *path = NULL;
+
+	assert(context);
+
+	plugin = kcontext_plugin(context);
+	cur_path = (faux_argv_t *)kplugin_udata(plugin);
+	if (cur_path)
+		path = faux_argv_line(cur_path);
+	printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
+	faux_str_free(path);
+
+	return 0;
+}
+
+
 static int srp_check_type(kcontext_t *context,
 	pt_e not_accepted_nodes,
 	size_t max_expr_num)
@@ -367,20 +386,81 @@ cleanup:
 }
 
 
-int srp_prompt_edit_path(kcontext_t *context)
+int srp_top(kcontext_t *context)
 {
 	faux_argv_t *cur_path = NULL;
 	kplugin_t *plugin = NULL;
-	char *path = NULL;
 
 	assert(context);
 
 	plugin = kcontext_plugin(context);
 	cur_path = (faux_argv_t *)kplugin_udata(plugin);
-	if (cur_path)
-		path = faux_argv_line(cur_path);
-	printf("[edit%s%s]\n", path ? " " : "", path ? path : "");
-	faux_str_free(path);
+	faux_argv_free(cur_path);
+	kplugin_set_udata(plugin, NULL);
 
 	return 0;
 }
+
+
+int srp_up(kcontext_t *context)
+{
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	faux_argv_t *cur_path = NULL;
+	kplugin_t *plugin = NULL;
+	faux_argv_node_t *iter = NULL;
+
+	assert(context);
+
+	plugin = kcontext_plugin(context);
+	cur_path = (faux_argv_t *)kplugin_udata(plugin);
+	if (!cur_path)
+		return -1; // It's top level and can't level up
+
+	if (sr_connect(SR_CONN_DEFAULT, &conn))
+		return -1;
+	if (sr_session_start(conn, SR_DS_RUNNING, &sess)) {
+		sr_disconnect(conn);
+		return -1;
+	}
+
+	// Remove last arguments one by one and wait for legal edit-like pline
+	while (faux_argv_len(cur_path) > 0) {
+		pline_t *pline = NULL;
+		pexpr_t *expr = NULL;
+		size_t len = 0;
+
+		iter = faux_argv_iterr(cur_path);
+		faux_argv_del(cur_path, iter);
+		pline = pline_parse(sess, cur_path, 0);
+		if (pline->invalid) {
+			pline_free(pline);
+			continue;
+		}
+		len = faux_list_len(pline->exprs);
+		if (len != 1) {
+			pline_free(pline);
+			continue;
+		}
+		expr = (pexpr_t *)faux_list_data(faux_list_head(pline->exprs));
+		if (!(expr->pat & PT_EDIT)) {
+			pline_free(pline);
+			continue;
+		}
+		// Here new path is ok
+		pline_free(pline);
+		break;
+	}
+
+	// Don't store empty path
+	while (faux_argv_len(cur_path) == 0) {
+		faux_argv_free(cur_path);
+		kplugin_set_udata(plugin, NULL);
+	}
+
+	sr_disconnect(conn);
+
+	return 0;
+}
+
+

+ 2 - 0
src/syms.h

@@ -25,6 +25,8 @@ int srp_prompt_edit_path(kcontext_t *context);
 int srp_set(kcontext_t *context);
 int srp_del(kcontext_t *context);
 int srp_edit(kcontext_t *context);
+int srp_top(kcontext_t *context);
+int srp_up(kcontext_t *context);
 
 C_DECL_END
 

+ 23 - 6
xml/sysrepo.xml

@@ -41,16 +41,15 @@
 <ENTRY name="sysrepo" mode="switch" container="true">
 
 	<ENTRY name="prompt" purpose="prompt">
-		<ACTION sym="print">#</ACTION>
+		<ACTION sym="srp_prompt_edit_path@sysrepo"/>
+		<ACTION sym="print"># </ACTION>
 	</ENTRY>
 
-<!--
-	<ENTRY name="exit" help="Exit view">
+	<ENTRY name="exit" help="Exit configuration mode or level up">
 		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
-		<ACTION sym="nav">pop</ACTION>
-		<ACTION sym="print">Exiting klish session</ACTION>
+		<ACTION sym="srp_up@sysrepo"/>
+		<ACTION sym="nav" exec_on="fail">pop</ACTION>
 	</ENTRY>
--->
 
 	<ENTRY name="set" help="Set data to database" mode="sequence">
 		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
@@ -68,6 +67,24 @@
 		<ACTION sym="srp_del@sysrepo"/>
 	</ENTRY>
 
+	<ENTRY name="edit" help="Edit data hierarchy" mode="sequence">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ENTRY name="path" max="100">
+			<ENTRY name="PLINE_EDIT" purpose="ptype" ref="/PLINE_EDIT"/>
+		</ENTRY>
+		<ACTION sym="srp_edit@sysrepo"/>
+	</ENTRY>
+
+	<ENTRY name="top" help="Edit top data level" mode="sequence">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ACTION sym="srp_top@sysrepo"/>
+	</ENTRY>
+
+	<ENTRY name="up" help="Up data level" mode="sequence">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ACTION sym="srp_up@sysrepo"/>
+	</ENTRY>
+
 </ENTRY>
 
 </KLISH>