Przeglądaj źródła

Start of 'insert' development. Unfinished

Serj Kalichev 1 rok temu
rodzic
commit
9b9c0d68f0
5 zmienionych plików z 152 dodań i 0 usunięć
  1. 8 0
      src/pline.h
  2. 5 0
      src/plugin.c
  3. 77 0
      src/syms.c
  4. 3 0
      src/syms.h
  5. 59 0
      xml/sysrepo.xml

+ 8 - 0
src/pline.h

@@ -62,6 +62,14 @@ typedef enum {
 		PAT_LEAFLIST |
 		PAT_LEAFLIST_VALUE,
 
+	PT_INSERT =
+		PAT_LIST_KEY |
+		PAT_LEAFLIST_VALUE,
+
+	PT_NOT_INSERT =
+		PAT_LEAF |
+		PAT_LEAF_VALUE,
+
 } pt_e;
 
 

+ 5 - 0
src/plugin.c

@@ -41,6 +41,10 @@ int kplugin_sysrepo_init(kcontext_t *context)
 		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("PLINE_EDIT", srp_PLINE_EDIT,
 		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_FROM", srp_PLINE_INSERT_FROM,
+		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
+	kplugin_add_syms(plugin, ksym_new_ext("PLINE_INSERT_TO", srp_PLINE_INSERT_TO,
+		KSYM_USERDEFINED_PERMANENT, KSYM_UNSYNC));
 
 	// Completion/Help/Prompt
 	kplugin_add_syms(plugin, ksym_new_ext("srp_compl", srp_compl,
@@ -60,6 +64,7 @@ int kplugin_sysrepo_init(kcontext_t *context)
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
 	kplugin_add_syms(plugin, ksym_new_ext("srp_up", srp_up,
 		KSYM_USERDEFINED_PERMANENT, KSYM_SYNC));
+	kplugin_add_syms(plugin, ksym_new("srp_insert", srp_insert));
 
 	return 0;
 }

+ 77 - 0
src/syms.c

@@ -190,6 +190,18 @@ int srp_PLINE_EDIT(kcontext_t *context)
 }
 
 
+int srp_PLINE_INSERT_FROM(kcontext_t *context)
+{
+	return srp_check_type(context, PT_NOT_INSERT, 1);
+}
+
+
+int srp_PLINE_INSERT_TO(kcontext_t *context)
+{
+	return srp_check_type(context, PT_NOT_INSERT, 1);
+}
+
+
 int srp_set(kcontext_t *context)
 {
 	int ret = 0;
@@ -465,3 +477,68 @@ int srp_up(kcontext_t *context)
 }
 
 
+int srp_insert(kcontext_t *context)
+{
+	int ret = 0;
+	faux_argv_t *args = NULL;
+	pline_t *pline = NULL;
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	faux_list_node_t *iter = NULL;
+	pexpr_t *expr = NULL;
+	size_t err_num = 0;
+	faux_argv_t *cur_path = NULL;
+	kplugin_t *plugin = NULL;
+
+	assert(context);
+
+	if (sr_connect(SR_CONN_DEFAULT, &conn))
+		return -1;
+	if (sr_session_start(conn, SRP_REPO_EDIT, &sess)) {
+		sr_disconnect(conn);
+		return -1;
+	}
+
+	plugin = kcontext_plugin(context);
+	cur_path = (faux_argv_t *)kplugin_udata(plugin);
+	args = param2argv(cur_path, kcontext_pargv(context), "path");
+	pline = pline_parse(sess, args, 0);
+	faux_argv_free(args);
+
+	if (pline->invalid) {
+		fprintf(stderr, "Invalid set request\n");
+		ret = -1;
+		goto cleanup;
+	}
+
+	iter = faux_list_head(pline->exprs);
+	while ((expr = (pexpr_t *)faux_list_each(&iter))) {
+		if (!(expr->pat & PT_SET)) {
+			err_num++;
+			fprintf(stderr, "Illegal expression for set operation\n");
+			break;
+		}
+		if (sr_set_item_str(sess, expr->xpath, expr->value, NULL, 0) !=
+			SR_ERR_OK) {
+			err_num++;
+			fprintf(stderr, "Can't set data\n");
+			break;
+		}
+	}
+
+	if (sr_has_changes(sess)) {
+		if (err_num > 0)
+			sr_discard_changes(sess);
+		else
+			sr_apply_changes(sess, 0);
+	}
+
+	if (err_num > 0)
+		ret = -1;
+
+cleanup:
+	pline_free(pline);
+	sr_disconnect(conn);
+
+	return ret;
+}

+ 3 - 0
src/syms.h

@@ -17,6 +17,8 @@ C_DECL_BEGIN
 int srp_PLINE_SET(kcontext_t *context);
 int srp_PLINE_DEL(kcontext_t *context);
 int srp_PLINE_EDIT(kcontext_t *context);
+int srp_PLINE_INSERT_FROM(kcontext_t *context);
+int srp_PLINE_INSERT_TO(kcontext_t *context);
 
 // Completion/Help/Prompt
 int srp_compl(kcontext_t *context);
@@ -29,6 +31,7 @@ int srp_del(kcontext_t *context);
 int srp_edit(kcontext_t *context);
 int srp_top(kcontext_t *context);
 int srp_up(kcontext_t *context);
+int srp_insert(kcontext_t *context);
 
 C_DECL_END
 

+ 59 - 0
xml/sysrepo.xml

@@ -37,6 +37,36 @@
 	<ACTION sym="PLINE_EDIT@sysrepo"/>
 </ENTRY>
 
+<ENTRY name="PLINE_INSERT_FROM" purpose="ptype">
+	<ENTRY name="completion" purpose="completion">
+		<ACTION sym="srp_compl@sysrepo"/>
+	</ENTRY>
+	<ENTRY name="help" purpose="help">
+		<ACTION sym="srp_help@sysrepo"/>
+	</ENTRY>
+	<ACTION sym="PLINE_INSERT_FROM@sysrepo"/>
+</ENTRY>
+
+<ENTRY name="PLINE_INSERT_TO" purpose="ptype">
+	<ENTRY name="completion" purpose="completion">
+		<ACTION sym="srp_compl@sysrepo"/>
+	</ENTRY>
+	<ENTRY name="help" purpose="help">
+		<ACTION sym="srp_help@sysrepo"/>
+	</ENTRY>
+	<ACTION sym="PLINE_INSERT_TO@sysrepo"/>
+</ENTRY>
+
+
+<ENTRY name="main" container="true">
+
+	<ENTRY name="configure" help="Configure system">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ACTION sym="nav">push sysrepo</ACTION>
+	</ENTRY>
+
+</ENTRY>
+
 
 <ENTRY name="sysrepo" mode="switch" container="true">
 
@@ -85,6 +115,35 @@
 		<ACTION sym="srp_up@sysrepo"/>
 	</ENTRY>
 
+	<ENTRY name="insert" help="Insert statement to specified position" mode="sequence">
+		<ENTRY name="COMMAND" purpose="ptype" ref="/COMMAND"/>
+		<ENTRY name="from_path" max="100">
+			<ENTRY name="PLINE_INSERT_FROM" purpose="ptype" ref="/PLINE_INSERT_FROM"/>
+		</ENTRY>
+		<ENTRY name="position" mode="switch" container="true">
+			<ENTRY name="first" help="First position">
+				<ENTRY name="SUBCOMMAND" purpose="ptype" ref="/SUBCOMMAND"/>
+			</ENTRY>
+			<ENTRY name="last" help="Last position">
+				<ENTRY name="SUBCOMMAND" purpose="ptype" ref="/SUBCOMMAND"/>
+			</ENTRY>
+			<ENTRY name="relative_block" mode="sequence" container="true">
+				<ENTRY name="position" mode="switch" container="true">
+					<ENTRY name="before" help="Before specified element">
+						<ENTRY name="SUBCOMMAND" purpose="ptype" ref="/SUBCOMMAND"/>
+					</ENTRY>
+					<ENTRY name="after" help="After specified element">
+						<ENTRY name="SUBCOMMAND" purpose="ptype" ref="/SUBCOMMAND"/>
+					</ENTRY>
+				</ENTRY>
+				<ENTRY name="to_path" max="100">
+					<ENTRY name="PLINE_INSERT_TO" purpose="ptype" ref="/PLINE_INSERT_TO"/>
+				</ENTRY>
+			</ENTRY>
+		</ENTRY>
+		<ACTION sym="srp_insert@sysrepo"/>
+	</ENTRY>
+
 </ENTRY>
 
 </KLISH>