Browse Source

Add klish_plugin_sysrepo.h public header

Serj Kalichev 4 months ago
parent
commit
bf73c518f2
11 changed files with 370 additions and 228 deletions
  1. 3 0
      Makefile.am
  2. 168 9
      bin/sr_load.c
  3. 1 1
      bin/ytree.c
  4. 3 2
      src/Makefile.am
  5. 190 8
      src/klish_plugin_sysrepo.h
  6. 1 2
      src/kly.c
  7. 1 2
      src/pline.c
  8. 0 199
      src/pline.h
  9. 1 1
      src/plugin.c
  10. 1 2
      src/show.c
  11. 1 2
      src/syms.c

+ 3 - 0
Makefile.am

@@ -19,6 +19,9 @@ lib_LIBRARIES =
 nobase_include_HEADERS =
 noinst_HEADERS =
 
+include_klishdir = ${includedir}/klish
+include_klish_HEADERS =
+
 EXTRA_DIST = \
 	bin/Makefile.am \
 	src/Makefile.am \

+ 168 - 9
bin/sr_load.c

@@ -1,5 +1,7 @@
 #include <stdlib.h>
 #include <stdio.h>
+#include <assert.h>
+#include <getopt.h>
 
 #include <faux/faux.h>
 #include <faux/str.h>
@@ -10,30 +12,60 @@
 #include <sysrepo/xpath.h>
 #include <sysrepo/netconf_acm.h>
 
-#include "pline.h"
+#include "klish_plugin_sysrepo.h"
 
+#ifndef VERSION
+#define VERSION "1.0.0"
+#endif
 
-int srp_mass_set(int fd, pline_opts_t *opts, const char *user, bool_t stop_on_error);
+#define DEFAULT_USER "root"
+#define DEFAULT_DATASTORE SR_DS_CANDIDATE
+
+typedef struct cmd_opts_s {
+	bool_t verbose;
+	char *cfg;
+	char *file;
+	char *user;
+	bool_t stop_on_error;
+	sr_datastore_t datastore;
+} cmd_opts_t;
+
+
+int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
+	const char *user, bool_t stop_on_error);
+
+// Command line options
+static cmd_opts_t *cmd_opts_init(void);
+static void cmd_opts_free(cmd_opts_t *opts);
+static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts);
+static void help(int status, const char *argv0);
 
 
 int main(int argc, char **argv)
 {
 	int ret = -1;
 	pline_opts_t opts;
-	bool_t stop_on_error = BOOL_TRUE;
-	const char *user = "root";
+	cmd_opts_t *cmd_opts = NULL;
+
+	cmd_opts = cmd_opts_init();
+	if (cmd_opts_parse(argc, argv, cmd_opts) < 0) {
+		fprintf(stderr, "Error: Illegal command line options\n");
+		goto out;
+	}
 
 	pline_opts_init(&opts);
-	ret = srp_mass_set(STDIN_FILENO, &opts, user, stop_on_error);
+	ret = srp_mass_set(STDIN_FILENO, cmd_opts->datastore, &opts,
+		cmd_opts->user, cmd_opts->stop_on_error);
 
-	argc = argc;
-	argv = argv;
+out:
+	cmd_opts_free(cmd_opts);
 
 	return ret;
 }
 
 
-int srp_mass_set(int fd, pline_opts_t *opts, const char *user, bool_t stop_on_error)
+int srp_mass_set(int fd, sr_datastore_t ds, pline_opts_t *opts,
+	const char *user, bool_t stop_on_error)
 {
 	int ret = -1;
 	int err = SR_ERR_OK;
@@ -49,7 +81,7 @@ int srp_mass_set(int fd, pline_opts_t *opts, const char *user, bool_t stop_on_er
 		fprintf(stderr, "Error: Can't connect to sysrepo\n");
 		goto out;
 	}
-	err = sr_session_start(conn, SR_DS_CANDIDATE, &sess);
+	err = sr_session_start(conn, ds, &sess);
 	if (err) {
 		fprintf(stderr, "Error: Can't start session\n");
 		goto out;
@@ -128,3 +160,130 @@ out:
 
 	return ret;
 }
+
+
+static cmd_opts_t *cmd_opts_init(void)
+{
+	cmd_opts_t *opts = NULL;
+
+	opts = faux_zmalloc(sizeof(*opts));
+	assert(opts);
+
+	// Initialize
+	opts->verbose = BOOL_FALSE;
+	opts->stop_on_error = BOOL_FALSE;
+	opts->cfg = NULL;
+	opts->file = NULL;
+	opts->user = NULL;
+	opts->datastore = DEFAULT_DATASTORE;
+
+	return opts;
+}
+
+
+static void cmd_opts_free(cmd_opts_t *opts)
+{
+	if (!opts)
+		return;
+	faux_str_free(opts->cfg);
+	faux_str_free(opts->file);
+	faux_str_free(opts->user);
+
+	faux_free(opts);
+}
+
+
+static int cmd_opts_parse(int argc, char *argv[], cmd_opts_t *opts)
+{
+	static const char *shortopts = "hf:veu:d:";
+	static const struct option longopts[] = {
+		{"conf",		1, NULL, 'f'},
+		{"help",		0, NULL, 'h'},
+		{"verbose",		0, NULL, 'v'},
+		{"user",		1, NULL, 'u'},
+		{"stop-on-error",	0, NULL, 'e'},
+		{"datastore",		1, NULL, 'd'},
+		{NULL,			0, NULL, 0}
+	};
+
+	optind = 1;
+	while(1) {
+		int opt = 0;
+
+		opt = getopt_long(argc, argv, shortopts, longopts, NULL);
+		if (-1 == opt)
+			break;
+		switch (opt) {
+		case 'v':
+			opts->verbose = BOOL_TRUE;
+			break;
+		case 'e':
+			opts->stop_on_error = BOOL_TRUE;
+			break;
+		case 'h':
+			help(0, argv[0]);
+			_exit(0);
+			break;
+		case 'u':
+			faux_str_free(opts->user);
+			opts->user = faux_str_dup(optarg);
+			break;
+		case 'f':
+			faux_str_free(opts->cfg);
+			opts->cfg = faux_str_dup(optarg);
+			break;
+		case 'd':
+			if (!kly_str2ds(optarg, strlen(optarg), &opts->datastore))
+				return BOOL_FALSE;
+			break;
+		default:
+			help(-1, argv[0]);
+			_exit(-1);
+			break;
+		}
+	}
+
+	// Input file
+	if(optind < argc) {
+		faux_str_free(opts->file);
+		opts->file = faux_str_dup(argv[optind]);
+	}
+
+	// Validate options
+	if (!opts->user)
+		opts->user = faux_str_dup(DEFAULT_USER);
+
+	return 0;
+}
+
+
+static void help(int status, const char *argv0)
+{
+	const char *name = NULL;
+
+	if (!argv0)
+		return;
+
+	// Find the basename
+	name = strrchr(argv0, '/');
+	if (name)
+		name++;
+	else
+		name = argv0;
+
+	if (status != 0) {
+		fprintf(stderr, "Try `%s -h' for more information.\n",
+			name);
+	} else {
+		printf("Version : %s\n", VERSION);
+		printf("Usage   : %s [options] [filename]\n", name);
+		printf("Load mass of config strings to Sysrepo repository\n");
+		printf("Options :\n");
+		printf("\t-h, --help Print this help.\n");
+		printf("\t-v, --verbose Be verbose.\n");
+		printf("\t-e, --stop-on-error Stop script execution on error.\n");
+		printf("\t-u <name>, --user=<name> NACM user.\n");
+		printf("\t-f <path>, --conf=<path> Config file.\n");
+		printf("\t-d <ds>, --datastore=<ds> Datastore.\n");
+	}
+}

+ 1 - 1
bin/ytree.c

@@ -7,7 +7,7 @@
 #include <sysrepo.h>
 #include <sysrepo/xpath.h>
 
-#include "pline.h"
+#include "klish_plugin_sysrepo.h"
 
 
 int main(int argc, char **argv)

+ 3 - 2
src/Makefile.am

@@ -7,9 +7,10 @@ libklish_plugin_sysrepo_la_CFLAGS = $(AM_CFLAGS)
 
 libklish_plugin_sysrepo_la_SOURCES += \
 	src/plugin.c \
-	src/private.h \
 	src/syms.c \
 	src/show.c \
-	src/pline.h \
 	src/pline.c \
 	src/kly.c
+
+include_klish_HEADERS += \
+	src/klish_plugin_sysrepo.h

+ 190 - 8
src/private.h → src/klish_plugin_sysrepo.h

@@ -1,16 +1,198 @@
-/*
- * private.h
- */
-
-#ifndef _pligin_sysrepo_private_h
-#define _plugin_sysrepo_private_h
+#ifndef _klish_pligin_sysrepo_h
+#define _klish_plugin_sysrepo_h
 
 #include <sysrepo.h>
+#include <sysrepo/xpath.h>
 #include <faux/faux.h>
 #include <faux/argv.h>
+#include <faux/list.h>
 #include <klish/kcontext_base.h>
 
-#include "pline.h"
+
+// Type of positional pline argument
+// P(line) A(rg) T(ype)
+typedef enum {
+	PAT_NONE			= 0x0000,
+	PAT_CONTAINER			= 0x0001,
+	PAT_LIST			= 0x0002,
+	PAT_LIST_KEY			= 0x0004,
+	PAT_LIST_KEY_INCOMPLETED	= 0x0008,
+	PAT_LEAF			= 0x0010,
+	PAT_LEAF_VALUE			= 0x0020,
+	PAT_LEAF_EMPTY			= 0x0040,
+	PAT_LEAFLIST			= 0x0080,
+	PAT_LEAFLIST_VALUE		= 0x0100,
+} pat_e;
+
+
+// Type of pline expression
+// P(line) T(ype)
+typedef enum {
+
+	PT_COMPL_ALL =
+		PAT_CONTAINER |
+		PAT_LIST |
+		PAT_LIST_KEY |
+		PAT_LIST_KEY_INCOMPLETED |
+		PAT_LEAF |
+		PAT_LEAF_VALUE |
+		PAT_LEAF_EMPTY |
+		PAT_LEAFLIST |
+		PAT_LEAFLIST_VALUE,
+
+	PT_SET =
+		PAT_CONTAINER |
+		PAT_LIST_KEY |
+		PAT_LEAF_VALUE |
+		PAT_LEAF_EMPTY |
+		PAT_LEAFLIST_VALUE,
+
+	PT_NOT_SET =
+		0,
+
+	PT_COMPL_SET =
+		PAT_CONTAINER |
+		PAT_LIST |
+		PAT_LIST_KEY |
+		PAT_LIST_KEY_INCOMPLETED |
+		PAT_LEAF |
+		PAT_LEAF_VALUE |
+		PAT_LEAF_EMPTY |
+		PAT_LEAFLIST |
+		PAT_LEAFLIST_VALUE,
+
+	PT_DEL =
+		PAT_CONTAINER |
+		PAT_LIST_KEY |
+		PAT_LEAF |
+		PAT_LEAF_EMPTY |
+		PAT_LEAFLIST |
+		PAT_LEAFLIST_VALUE,
+
+	PT_NOT_DEL =
+		PAT_LEAF_VALUE,
+
+	PT_COMPL_DEL =
+		PAT_CONTAINER |
+		PAT_LIST |
+		PAT_LIST_KEY |
+		PAT_LIST_KEY_INCOMPLETED |
+		PAT_LEAF |
+		PAT_LEAF_EMPTY |
+		PAT_LEAFLIST |
+		PAT_LEAFLIST_VALUE,
+
+	PT_EDIT =
+		PAT_CONTAINER |
+		PAT_LIST_KEY,
+
+	PT_NOT_EDIT =
+		PAT_LEAF |
+		PAT_LEAF_VALUE |
+		PAT_LEAFLIST |
+		PAT_LEAFLIST_VALUE,
+
+	PT_COMPL_EDIT =
+		PAT_CONTAINER |
+		PAT_LIST |
+		PAT_LIST_KEY |
+		PAT_LIST_KEY_INCOMPLETED,
+
+	PT_INSERT =
+		PAT_LIST_KEY |
+		PAT_LEAFLIST_VALUE,
+
+	PT_NOT_INSERT =
+		PAT_LEAF |
+		PAT_LEAF_VALUE,
+
+	PT_COMPL_INSERT =
+		PAT_CONTAINER |
+		PAT_LIST |
+		PAT_LIST_KEY |
+		PAT_LIST_KEY_INCOMPLETED |
+		PAT_LEAFLIST |
+		PAT_LEAFLIST_VALUE,
+
+} pt_e;
+
+
+// Plain EXPRession
+typedef struct {
+	char *xpath;
+	char *value;
+	bool_t active;
+	pat_e pat;
+	size_t args_num;
+	size_t list_pos;
+	char *last_keys;
+} pexpr_t;
+
+
+// Possible types of completion source
+typedef enum {
+	PCOMPL_NODE = 0,
+	PCOMPL_TYPE = 1,
+} pcompl_type_e;
+
+
+// Plain COMPLetion
+typedef struct {
+	pcompl_type_e type;
+	const struct lysc_node *node;
+	char *xpath;
+	sr_datastore_t xpath_ds;
+	pat_e pat;
+} pcompl_t;
+
+
+// Plain LINE
+typedef struct pline_s {
+	sr_session_ctx_t *sess;
+	bool_t invalid;
+	faux_list_t *exprs;
+	faux_list_t *compls;
+} pline_t;
+
+
+// Parse/show settings
+typedef struct {
+	char begin_bracket;
+	char end_bracket;
+	bool_t show_brackets;
+	bool_t show_semicolons;
+	bool_t first_key_w_stmt;
+	bool_t keys_w_stmt;
+	bool_t colorize;
+	uint8_t indent;
+	bool_t default_keys;
+	bool_t show_default_keys;
+	bool_t hide_passwords;
+	bool_t enable_nacm;
+	bool_t oneliners;
+} pline_opts_t;
+
+
+#define SRP_NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE | LYS_CASE)
+
+
+C_DECL_BEGIN
+
+// PLine
+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);
+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);
+
+void pline_free(pline_t *pline);
+
+void pline_debug(pline_t *pline);
+void pline_print_completions(const pline_t *pline, bool_t help, pt_e enabled_types);
+
+size_t num_of_keys(const struct lysc_node *node);
+
+C_DECL_END
 
 
 // Plugin's user-data structure
@@ -117,4 +299,4 @@ bool_t kly_parse_ext_xpath(const char *xpath, const char **raw_xpath,
 C_DECL_END
 
 
-#endif // _plugin_sysrepo_private_h
+#endif // _klish_plugin_sysrepo_h

+ 1 - 2
src/kly.c

@@ -15,8 +15,7 @@
 #include <sysrepo/values.h>
 #include <libyang/tree_edit.h>
 
-#include "private.h"
-#include "pline.h"
+#include "klish_plugin_sysrepo.h"
 
 
 int klysc_key_compare(const void *first, const void *second)

+ 1 - 2
src/pline.c

@@ -21,8 +21,7 @@
 #include <sysrepo/values.h>
 #include <libyang/tree_edit.h>
 
-#include "private.h"
-#include "pline.h"
+#include "klish_plugin_sysrepo.h"
 
 
 static int sr_ly_module_is_internal(const struct lys_module *ly_mod)

+ 0 - 199
src/pline.h

@@ -1,199 +0,0 @@
-/** @file pline.h
- * @brief Plain line.
- */
-
-#ifndef _pline_h
-#define _pline_h
-
-#include <faux/list.h>
-#include <faux/argv.h>
-
-#include <sysrepo.h>
-#include <sysrepo/xpath.h>
-
-
-// Type of positional pline argument
-// P(line) A(rg) T(ype)
-typedef enum {
-	PAT_NONE			= 0x0000,
-	PAT_CONTAINER			= 0x0001,
-	PAT_LIST			= 0x0002,
-	PAT_LIST_KEY			= 0x0004,
-	PAT_LIST_KEY_INCOMPLETED	= 0x0008,
-	PAT_LEAF			= 0x0010,
-	PAT_LEAF_VALUE			= 0x0020,
-	PAT_LEAF_EMPTY			= 0x0040,
-	PAT_LEAFLIST			= 0x0080,
-	PAT_LEAFLIST_VALUE		= 0x0100,
-} pat_e;
-
-
-// Type of pline expression
-// P(line) T(ype)
-typedef enum {
-
-	PT_COMPL_ALL =
-		PAT_CONTAINER |
-		PAT_LIST |
-		PAT_LIST_KEY |
-		PAT_LIST_KEY_INCOMPLETED |
-		PAT_LEAF |
-		PAT_LEAF_VALUE |
-		PAT_LEAF_EMPTY |
-		PAT_LEAFLIST |
-		PAT_LEAFLIST_VALUE,
-
-	PT_SET =
-		PAT_CONTAINER |
-		PAT_LIST_KEY |
-		PAT_LEAF_VALUE |
-		PAT_LEAF_EMPTY |
-		PAT_LEAFLIST_VALUE,
-
-	PT_NOT_SET =
-		0,
-
-	PT_COMPL_SET =
-		PAT_CONTAINER |
-		PAT_LIST |
-		PAT_LIST_KEY |
-		PAT_LIST_KEY_INCOMPLETED |
-		PAT_LEAF |
-		PAT_LEAF_VALUE |
-		PAT_LEAF_EMPTY |
-		PAT_LEAFLIST |
-		PAT_LEAFLIST_VALUE,
-
-	PT_DEL =
-		PAT_CONTAINER |
-		PAT_LIST_KEY |
-		PAT_LEAF |
-		PAT_LEAF_EMPTY |
-		PAT_LEAFLIST |
-		PAT_LEAFLIST_VALUE,
-
-	PT_NOT_DEL =
-		PAT_LEAF_VALUE,
-
-	PT_COMPL_DEL =
-		PAT_CONTAINER |
-		PAT_LIST |
-		PAT_LIST_KEY |
-		PAT_LIST_KEY_INCOMPLETED |
-		PAT_LEAF |
-		PAT_LEAF_EMPTY |
-		PAT_LEAFLIST |
-		PAT_LEAFLIST_VALUE,
-
-	PT_EDIT =
-		PAT_CONTAINER |
-		PAT_LIST_KEY,
-
-	PT_NOT_EDIT =
-		PAT_LEAF |
-		PAT_LEAF_VALUE |
-		PAT_LEAFLIST |
-		PAT_LEAFLIST_VALUE,
-
-	PT_COMPL_EDIT =
-		PAT_CONTAINER |
-		PAT_LIST |
-		PAT_LIST_KEY |
-		PAT_LIST_KEY_INCOMPLETED,
-
-	PT_INSERT =
-		PAT_LIST_KEY |
-		PAT_LEAFLIST_VALUE,
-
-	PT_NOT_INSERT =
-		PAT_LEAF |
-		PAT_LEAF_VALUE,
-
-	PT_COMPL_INSERT =
-		PAT_CONTAINER |
-		PAT_LIST |
-		PAT_LIST_KEY |
-		PAT_LIST_KEY_INCOMPLETED |
-		PAT_LEAFLIST |
-		PAT_LEAFLIST_VALUE,
-
-} pt_e;
-
-
-// Plain EXPRession
-typedef struct {
-	char *xpath;
-	char *value;
-	bool_t active;
-	pat_e pat;
-	size_t args_num;
-	size_t list_pos;
-	char *last_keys;
-} pexpr_t;
-
-
-// Possible types of completion source
-typedef enum {
-	PCOMPL_NODE = 0,
-	PCOMPL_TYPE = 1,
-} pcompl_type_e;
-
-
-// Plain COMPLetion
-typedef struct {
-	pcompl_type_e type;
-	const struct lysc_node *node;
-	char *xpath;
-	sr_datastore_t xpath_ds;
-	pat_e pat;
-} pcompl_t;
-
-
-// Plain LINE
-typedef struct pline_s {
-	sr_session_ctx_t *sess;
-	bool_t invalid;
-	faux_list_t *exprs;
-	faux_list_t *compls;
-} pline_t;
-
-
-// Parse/show settings
-typedef struct {
-	char begin_bracket;
-	char end_bracket;
-	bool_t show_brackets;
-	bool_t show_semicolons;
-	bool_t first_key_w_stmt;
-	bool_t keys_w_stmt;
-	bool_t colorize;
-	uint8_t indent;
-	bool_t default_keys;
-	bool_t show_default_keys;
-	bool_t hide_passwords;
-	bool_t enable_nacm;
-	bool_t oneliners;
-} pline_opts_t;
-
-
-#define SRP_NODETYPE_CONF (LYS_CONTAINER | LYS_LIST | LYS_LEAF | LYS_LEAFLIST | LYS_CHOICE | LYS_CASE)
-
-
-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);
-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);
-
-void pline_free(pline_t *pline);
-
-void pline_debug(pline_t *pline);
-void pline_print_completions(const pline_t *pline, bool_t help, pt_e enabled_types);
-
-size_t num_of_keys(const struct lysc_node *node);
-
-C_DECL_END
-
-#endif				/* _pline_h */

+ 1 - 1
src/plugin.c

@@ -16,7 +16,7 @@
 #include <klish/kplugin.h>
 #include <klish/kcontext.h>
 
-#include "private.h"
+#include "klish_plugin_sysrepo.h"
 
 
 const uint8_t kplugin_sysrepo_major = KPLUGIN_MAJOR;

+ 1 - 2
src/show.c

@@ -15,8 +15,7 @@
 #include <sysrepo/values.h>
 #include <libyang/tree_edit.h>
 
-#include "private.h"
-#include "pline.h"
+#include "klish_plugin_sysrepo.h"
 
 
 static void show_container(const struct lyd_node *node, size_t level,

+ 1 - 2
src/syms.c

@@ -20,8 +20,7 @@
 #include <sysrepo/xpath.h>
 #include <sysrepo/values.h>
 
-#include "pline.h"
-#include "private.h"
+#include "klish_plugin_sysrepo.h"
 
 #define ERRORMSG "Error: "