Ver código fonte

srp_load: Parses pline options file (-f option)

Serj Kalichev 11 meses atrás
pai
commit
63bbfe0e6f
3 arquivos alterados com 55 adições e 11 exclusões
  1. 8 0
      bin/srp_load.c
  2. 1 0
      src/klish_plugin_sysrepo.h
  3. 46 11
      src/pline.c

+ 8 - 0
bin/srp_load.c

@@ -46,12 +46,14 @@ int main(int argc, char **argv)
 	int fd = STDIN_FILENO;
 	faux_argv_t *cur_path = NULL;
 
+	// Command line options parsing
 	cmd_opts = cmd_opts_init();
 	if (cmd_opts_parse(argc, argv, cmd_opts) < 0) {
 		fprintf(stderr, "Error: Illegal command line options\n");
 		goto out;
 	}
 
+	// Open input file
 	if (cmd_opts->file) {
 		fd = open(cmd_opts->file, O_RDONLY, 0);
 		if (fd < 0) {
@@ -59,7 +61,13 @@ int main(int argc, char **argv)
 			goto out;
 		}
 	}
+
+	// Get pline options
 	pline_opts_init(&opts);
+	if (cmd_opts->cfg)
+		pline_opts_parse_file(cmd_opts->cfg, &opts);
+
+	// Prepare argv structure for current sysrepo path
 	if (cmd_opts->current_path) {
 		cur_path = faux_argv_new();
 		faux_argv_parse(cur_path, cmd_opts->current_path);

+ 1 - 0
src/klish_plugin_sysrepo.h

@@ -182,6 +182,7 @@ C_DECL_BEGIN
 pline_t *pline_new(sr_session_ctx_t *sess);
 void pline_opts_init(pline_opts_t *opts);
 int pline_opts_parse(const char *conf, pline_opts_t *opts);
+int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts);
 pline_t *pline_parse(sr_session_ctx_t *sess, const faux_argv_t *argv,
 	const pline_opts_t *opts);
 pexpr_t *pline_current_expr(pline_t *pline);

+ 46 - 11
src/pline.c

@@ -1384,21 +1384,14 @@ void pline_opts_init(pline_opts_t *opts)
 }
 
 
-int pline_opts_parse(const char *conf, pline_opts_t *opts)
+static int pline_opts_parse_ini(const faux_ini_t *ini, 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 (!ini)
+		return 0; // Nothing to parse
 
 	if ((val = faux_ini_find(ini, "ShowBrackets"))) {
 		if (faux_str_cmp(val, "y") == 0)
@@ -1476,7 +1469,49 @@ int pline_opts_parse(const char *conf, pline_opts_t *opts)
 			opts->oneliners = BOOL_FALSE;
 	}
 
+	return 0;
+}
+
+
+int pline_opts_parse(const char *conf, pline_opts_t *opts)
+{
+	faux_ini_t *ini = NULL;
+	int rc = -1;
+
+	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;
+	}
+	rc = pline_opts_parse_ini(ini, opts);
 	faux_ini_free(ini);
 
-	return 0;
+	return rc;
+}
+
+
+int pline_opts_parse_file(const char *conf_name, pline_opts_t *opts)
+{
+	faux_ini_t *ini = NULL;
+	int rc = -1;
+
+	if (!opts)
+		return -1;
+	if (!conf_name)
+		return 0; // Use defaults
+
+	ini = faux_ini_new();
+	if (!faux_ini_parse_file(ini, conf_name)) {
+		faux_ini_free(ini);
+		return -1;
+	}
+	rc = pline_opts_parse_ini(ini, opts);
+	faux_ini_free(ini);
+
+	return rc;
 }