Browse Source

Move pline opts init to separate function

Serj Kalichev 5 months ago
parent
commit
5d12e02867
5 changed files with 106 additions and 16 deletions
  1. 1 0
      bin/.gitignore
  2. 82 0
      bin/sr_load.c
  3. 19 1
      src/pline.c
  4. 2 1
      src/pline.h
  5. 2 14
      src/plugin.c

+ 1 - 0
bin/.gitignore

@@ -1 +1,2 @@
 /ytree
+/sr_load

+ 82 - 0
bin/sr_load.c

@@ -0,0 +1,82 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <faux/faux.h>
+#include <faux/str.h>
+#include <faux/file.h>
+#include <faux/argv.h>
+
+#include <sysrepo.h>
+#include <sysrepo/xpath.h>
+
+#include "pline.h"
+
+
+int main(int argc, char **argv)
+{
+	int ret = -1;
+	int err = SR_ERR_OK;
+	sr_conn_ctx_t *conn = NULL;
+	sr_session_ctx_t *sess = NULL;
+	faux_file_t *file = NULL;
+	char *line = NULL;
+	pline_opts_t opts;
+
+	err = sr_connect(SR_CONN_DEFAULT, &conn);
+	if (err) {
+		fprintf(stderr, "Error: Can't connect to sysrepo\n");
+		goto out;
+	}
+	err = sr_session_start(conn, SR_DS_RUNNING, &sess);
+	if (err) {
+		fprintf(stderr, "Error: Can't start session\n");
+		goto out;
+	}
+
+	file = faux_file_fdopen(STDIN_FILENO);
+	if (!file) {
+		fprintf(stderr, "Error: Can't open stdin\n");
+		goto out;
+	}
+
+	pline_opts_init(&opts);
+
+	while ((line = faux_file_getline(file))) {
+		pline_t *pline = NULL;
+		faux_argv_t *args = NULL;
+
+		args = faux_argv_new();
+		faux_argv_parse(args, line);
+		pline = pline_parse(sess, args, &opts);
+		faux_argv_free(args);
+		if (!pline || pline->invalid) {
+			fprintf(stderr, "Invalid line: %s\n", line);
+		} else {
+			pline_debug(pline);
+			printf("pline\n");
+//			pline_print_completions(pline, BOOL_TRUE, PT_COMPL_ALL);
+		}
+		pline_free(pline);
+		faux_str_free(line);
+	}
+
+/*
+	faux_argv_t *args = faux_argv_new();
+	faux_argv_parse(args, argv[1]);
+	faux_argv_del_continuable(args);
+	pline = pline_parse(sess, args, 0);
+	faux_argv_free(args);
+	pline_debug(pline);
+	pline_print_completions(pline, BOOL_TRUE, PT_COMPL_ALL);
+	pline_free(pline);
+*/
+	ret = 0;
+out:
+	faux_file_close(file);
+	sr_disconnect(conn);
+
+	argc = argc;
+	argv = argv;
+
+	return ret;
+}

+ 19 - 1
src/pline.c

@@ -1366,7 +1366,25 @@ void pline_print_completions(const pline_t *pline, bool_t help, pt_e enabled_typ
 }
 
 
-int pline_parse_conf(const char *conf, pline_opts_t *opts)
+void pline_opts_init(pline_opts_t *opts)
+{
+	opts->begin_bracket = '{';
+	opts->end_bracket = '}';
+	opts->show_brackets = BOOL_TRUE;
+	opts->show_semicolons = BOOL_TRUE;
+	opts->first_key_w_stmt = BOOL_FALSE;
+	opts->keys_w_stmt = BOOL_TRUE;
+	opts->colorize = BOOL_TRUE;
+	opts->indent = 2;
+	opts->default_keys = BOOL_FALSE;
+	opts->show_default_keys = BOOL_FALSE;
+	opts->hide_passwords = BOOL_TRUE;
+	opts->enable_nacm = BOOL_FALSE;
+	opts->oneliners = BOOL_TRUE;
+}
+
+
+int pline_opts_parse(const char *conf, pline_opts_t *opts)
 {
 	faux_ini_t *ini = NULL;
 	const char *val = NULL;

+ 2 - 1
src/pline.h

@@ -182,7 +182,8 @@ typedef struct {
 C_DECL_BEGIN
 
 pline_t *pline_new(sr_session_ctx_t *sess);
-int pline_parse_conf(const char *conf, pline_opts_t *opts);
+void pline_opts_init(pline_opts_t *opts);
+int pline_opts_parse(const char *conf, pline_opts_t *opts);
 pline_t *pline_parse(sr_session_ctx_t *sess, faux_argv_t *argv, pline_opts_t *opts);
 pexpr_t *pline_current_expr(pline_t *pline);
 

+ 2 - 14
src/plugin.c

@@ -127,20 +127,8 @@ int kplugin_sysrepo_init(kcontext_t *context)
 	udata->nacm_sub = NULL;
 
 	// Settings
-	udata->opts.begin_bracket = '{';
-	udata->opts.end_bracket = '}';
-	udata->opts.show_brackets = BOOL_TRUE;
-	udata->opts.show_semicolons = BOOL_TRUE;
-	udata->opts.first_key_w_stmt = BOOL_FALSE;
-	udata->opts.keys_w_stmt = BOOL_TRUE;
-	udata->opts.colorize = BOOL_TRUE;
-	udata->opts.indent = 2;
-	udata->opts.default_keys = BOOL_FALSE;
-	udata->opts.show_default_keys = BOOL_FALSE;
-	udata->opts.hide_passwords = BOOL_TRUE;
-	udata->opts.enable_nacm = BOOL_FALSE;
-	udata->opts.oneliners = BOOL_TRUE;
-	pline_parse_conf(kplugin_conf(plugin), &udata->opts);
+	pline_opts_init(&udata->opts);
+	pline_opts_parse(kplugin_conf(plugin), &udata->opts);
 
 	kplugin_set_udata(plugin, udata);