Browse Source

Move plugin conf parsing to separate function

Serj Kalichev 5 months ago
parent
commit
b8c9452e67
3 changed files with 102 additions and 101 deletions
  1. 100 0
      src/pline.c
  2. 1 0
      src/pline.h
  3. 1 101
      src/plugin.c

+ 100 - 0
src/pline.c

@@ -13,6 +13,8 @@
 #include <faux/str.h>
 #include <faux/list.h>
 #include <faux/argv.h>
+#include <faux/ini.h>
+#include <faux/conv.h>
 
 #include <sysrepo.h>
 #include <sysrepo/xpath.h>
@@ -1362,3 +1364,101 @@ void pline_print_completions(const pline_t *pline, bool_t help, pt_e enabled_typ
 	if (current_ds != SRP_REPO_EDIT)
 		sr_session_switch_ds(pline->sess, SRP_REPO_EDIT);
 }
+
+
+int pline_parse_conf(const char *conf, pline_opts_t *opts)
+{
+	faux_ini_t *ini = NULL;
+	const char *val = NULL;
+
+	if (!opts)
+		return -1;
+	if (!conf)
+		return 0; // Use defaults
+
+	ini = faux_ini_new();
+	if (!faux_ini_parse_str(ini, conf)) {
+		faux_ini_free(ini);
+		return -1;
+	}
+
+	if ((val = faux_ini_find(ini, "ShowBrackets"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->show_brackets = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->show_brackets = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->show_semicolons = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->show_semicolons = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->first_key_w_stmt = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->first_key_w_stmt = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->keys_w_stmt = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->keys_w_stmt = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "Colorize"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->colorize = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->colorize = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "Indent"))) {
+		unsigned char indent = 0;
+		if (faux_conv_atouc(val, &indent, 10))
+			opts->indent = indent;
+	}
+
+	if ((val = faux_ini_find(ini, "DefaultKeys"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->default_keys = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->default_keys = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->show_default_keys = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->show_default_keys = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "HidePasswords"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->hide_passwords = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->hide_passwords = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "EnableNACM"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->enable_nacm = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->enable_nacm = BOOL_FALSE;
+	}
+
+	if ((val = faux_ini_find(ini, "Oneliners"))) {
+		if (faux_str_cmp(val, "y") == 0)
+			opts->oneliners = BOOL_TRUE;
+		else if (faux_str_cmp(val, "n") == 0)
+			opts->oneliners = BOOL_FALSE;
+	}
+
+	faux_ini_free(ini);
+
+	return 0;
+}

+ 1 - 0
src/pline.h

@@ -182,6 +182,7 @@ 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);
 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);
 

+ 1 - 101
src/plugin.c

@@ -22,8 +22,6 @@
 const uint8_t kplugin_sysrepo_major = KPLUGIN_MAJOR;
 const uint8_t kplugin_sysrepo_minor = KPLUGIN_MINOR;
 
-static int parse_plugin_conf(const char *conf, pline_opts_t *opts);
-
 static int kplugin_sysrepo_init_session(kcontext_t *context);
 static int kplugin_sysrepo_fini_session(kcontext_t *context);
 
@@ -142,7 +140,7 @@ int kplugin_sysrepo_init(kcontext_t *context)
 	udata->opts.hide_passwords = BOOL_TRUE;
 	udata->opts.enable_nacm = BOOL_FALSE;
 	udata->opts.oneliners = BOOL_TRUE;
-	parse_plugin_conf(kplugin_conf(plugin), &udata->opts);
+	pline_parse_conf(kplugin_conf(plugin), &udata->opts);
 
 	kplugin_set_udata(plugin, udata);
 
@@ -223,104 +221,6 @@ sr_session_ctx_t *srp_udata_sr_sess(kcontext_t *context)
 }
 
 
-static int parse_plugin_conf(const char *conf, pline_opts_t *opts)
-{
-	faux_ini_t *ini = NULL;
-	const char *val = NULL;
-
-	if (!opts)
-		return -1;
-	if (!conf)
-		return 0; // Use defaults
-
-	ini = faux_ini_new();
-	if (!faux_ini_parse_str(ini, conf)) {
-		faux_ini_free(ini);
-		return -1;
-	}
-
-	if ((val = faux_ini_find(ini, "ShowBrackets"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->show_brackets = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->show_brackets = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "ShowSemicolons"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->show_semicolons = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->show_semicolons = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "FirstKeyWithStatement"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->first_key_w_stmt = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->first_key_w_stmt = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "KeysWithStatement"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->keys_w_stmt = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->keys_w_stmt = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "Colorize"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->colorize = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->colorize = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "Indent"))) {
-		unsigned char indent = 0;
-		if (faux_conv_atouc(val, &indent, 10))
-			opts->indent = indent;
-	}
-
-	if ((val = faux_ini_find(ini, "DefaultKeys"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->default_keys = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->default_keys = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "ShowDefaultKeys"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->show_default_keys = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->show_default_keys = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "HidePasswords"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->hide_passwords = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->hide_passwords = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "EnableNACM"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->enable_nacm = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->enable_nacm = BOOL_FALSE;
-	}
-
-	if ((val = faux_ini_find(ini, "Oneliners"))) {
-		if (faux_str_cmp(val, "y") == 0)
-			opts->oneliners = BOOL_TRUE;
-		else if (faux_str_cmp(val, "n") == 0)
-			opts->oneliners = BOOL_FALSE;
-	}
-
-	faux_ini_free(ini);
-
-	return 0;
-}
-
-
 static int kplugin_sysrepo_init_session(kcontext_t *context)
 {
 	srp_udata_t *udata = NULL;