Browse Source

Move some service functions to kly.c

Serj Kalichev 1 year ago
parent
commit
436a27dfa0
7 changed files with 141 additions and 127 deletions
  1. 1 5
      bin/Makefile.am
  2. 2 1
      src/Makefile.am
  3. 128 0
      src/kly.c
  4. 0 4
      src/plugin.c
  5. 6 0
      src/private.h
  6. 3 112
      src/show.c
  7. 1 5
      src/syms.c

+ 1 - 5
bin/Makefile.am

@@ -5,8 +5,4 @@ bin_ytree_SOURCES = \
 	bin/ytree.c \
 	src/sr_copypaste.c \
 	src/pline.c \
-	src/show.c
-
-#bin_klish_klish_LDADD = \
-#	libklish.la \
-#	libtinyrl.la
+	src/kly.c

+ 2 - 1
src/Makefile.am

@@ -15,4 +15,5 @@ kplugin_sysrepo_la_SOURCES += \
 	src/sr_copypaste.c \
 	src/show.c \
 	src/pline.h \
-	src/pline.c
+	src/pline.c \
+	src/kly.c

+ 128 - 0
src/kly.c

@@ -0,0 +1,128 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <syslog.h>
+
+#include <faux/faux.h>
+#include <faux/str.h>
+#include <faux/list.h>
+#include <faux/argv.h>
+
+#include <sysrepo.h>
+#include <sysrepo/xpath.h>
+#include <sysrepo/values.h>
+#include <libyang/tree_edit.h>
+
+#include "private.h"
+#include "pline.h"
+
+
+// Get extension by name from schema node
+static bool_t klysc_ext(const struct lysc_ext_instance *exts,
+	const char *module, const char *name, const char **argument)
+{
+	LY_ARRAY_COUNT_TYPE u = 0;
+
+	if (!exts)
+		return BOOL_FALSE;
+
+	LY_ARRAY_FOR(exts, u) {
+		const struct lysc_ext_instance *ext = &exts[u];
+		//syslog(LOG_ERR, "mod: %s, ext: %s", ext->def->module->name, ext->def->name);
+		if (faux_str_cmp(ext->def->module->name, module) != 0)
+			continue;
+		if (faux_str_cmp(ext->def->name, name) != 0)
+			continue;
+		if (argument)
+			*argument = ext->argument;
+		return BOOL_TRUE;
+	}
+
+	return BOOL_FALSE;
+}
+
+
+// Get extension by name
+bool_t klysc_node_ext(const struct lysc_node *node,
+	const char *module, const char *name, const char **argument)
+{
+	if (!node)
+		return BOOL_FALSE;
+	if (klysc_ext(node->exts, module, name, argument))
+		return BOOL_TRUE;
+
+	return BOOL_FALSE;
+}
+
+
+bool_t klysc_node_ext_is_password(const struct lysc_node *node)
+{
+	return klysc_node_ext(node, "klish", "password", NULL);
+}
+
+
+const char *klysc_node_ext_completion(const struct lysc_node *node)
+{
+	const char *xpath = NULL;
+
+	klysc_node_ext(node, "klish", "completion", &xpath);
+
+	return xpath;
+}
+
+
+const char *klysc_node_ext_default(const struct lysc_node *node)
+{
+	const char *dflt = NULL;
+
+	klysc_node_ext(node, "klish", "default", &dflt);
+
+	return dflt;
+}
+
+
+// Get value from data lyd node
+char *klyd_node_value(const struct lyd_node *node)
+{
+	const struct lysc_node *schema = NULL;
+	const struct lysc_type *type = NULL;
+	const char *origin_value = NULL;
+	char *space = NULL;
+	char *escaped = NULL;
+	char *result = NULL;
+
+	if (!node)
+		return NULL;
+
+	schema = node->schema;
+	if (!(schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
+		return NULL;
+
+	if (schema->nodetype & LYS_LEAF)
+		type = ((const struct lysc_node_leaf *)schema)->type;
+	else
+		type = ((const struct lysc_node_leaflist *)schema)->type;
+
+	if (type->basetype != LY_TYPE_IDENT) {
+		origin_value = lyd_get_value(node);
+	} else {
+		// Identity
+		const struct lyd_value *value = NULL;
+		value = &((const struct lyd_node_term *)node)->value;
+		origin_value = value->ident->name;
+	}
+
+	escaped = faux_str_c_esc(origin_value);
+	// String with space must have quotes
+	space = strchr(origin_value, ' ');
+	if (space) {
+		result = faux_str_sprintf("\"%s\"", escaped);
+		faux_str_free(escaped);
+	} else {
+		result = escaped;
+	}
+
+	return result;
+}

+ 0 - 4
src/plugin.c

@@ -25,10 +25,6 @@ static int parse_plugin_conf(const char *conf, pline_opts_t *opts);
 int kplugin_sysrepo_init(kcontext_t *context)
 {
 	kplugin_t *plugin = NULL;
-	ksym_t *sym = NULL;
-	int err = SR_ERR_OK;
-	sr_conn_ctx_t *conn = NULL;
-	sr_session_ctx_t *sess = NULL;
 	srp_udata_t *udata = NULL;
 
 	assert(context);

+ 6 - 0
src/private.h

@@ -79,7 +79,13 @@ void show_subtree(const struct lyd_node *nodes_list, size_t level,
 // Sysrepo copy-paste
 int sr_module_is_internal(const struct lys_module *ly_mod);
 
+// kly helper library
+bool_t klysc_node_ext(const struct lysc_node *node,
+	const char *module, const char *name, const char **argument);
+bool_t klysc_node_ext_is_password(const struct lysc_node *node);
+const char *klysc_node_ext_completion(const struct lysc_node *node);
 const char *klysc_node_ext_default(const struct lysc_node *node);
+char *klyd_node_value(const struct lyd_node *node);
 
 C_DECL_END
 

+ 3 - 112
src/show.c

@@ -32,114 +32,6 @@ static void show_node(const struct lyd_node *node, size_t level,
 static enum diff_op str2diff_op(const char *str);
 
 
-// Get extension by name
-static bool_t klysc_ext(const struct lysc_ext_instance *exts,
-	const char *module, const char *name, const char **argument)
-{
-	LY_ARRAY_COUNT_TYPE u = 0;
-
-	if (!exts)
-		return BOOL_FALSE;
-
-	LY_ARRAY_FOR(exts, u) {
-		const struct lysc_ext_instance *ext = &exts[u];
-		//syslog(LOG_ERR, "mod: %s, ext: %s", ext->def->module->name, ext->def->name);
-		if (faux_str_cmp(ext->def->module->name, module) != 0)
-			continue;
-		if (faux_str_cmp(ext->def->name, name) != 0)
-			continue;
-		if (argument)
-			*argument = ext->argument;
-		return BOOL_TRUE;
-	}
-
-	return BOOL_FALSE;
-}
-
-
-// Get extension by name
-bool_t klysc_node_ext(const struct lysc_node *node,
-	const char *module, const char *name, const char **argument)
-{
-	if (!node)
-		return BOOL_FALSE;
-	if (klysc_ext(node->exts, module, name, argument))
-		return BOOL_TRUE;
-
-	return BOOL_FALSE;
-}
-
-
-bool_t klysc_node_ext_is_password(const struct lysc_node *node)
-{
-	return klysc_node_ext(node, "klish", "password", NULL);
-}
-
-
-const char *klysc_node_ext_completion(const struct lysc_node *node)
-{
-	const char *xpath = NULL;
-
-	klysc_node_ext(node, "klish", "completion", &xpath);
-
-	return xpath;
-}
-
-
-const char *klysc_node_ext_default(const struct lysc_node *node)
-{
-	const char *dflt = NULL;
-
-	klysc_node_ext(node, "klish", "default", &dflt);
-
-	return dflt;
-}
-
-
-static char *get_value(const struct lyd_node *node)
-{
-	const struct lysc_node *schema = NULL;
-	const struct lysc_type *type = NULL;
-	const char *origin_value = NULL;
-	char *space = NULL;
-	char *escaped = NULL;
-	char *result = NULL;
-
-	if (!node)
-		return NULL;
-
-	schema = node->schema;
-	if (!(schema->nodetype & (LYS_LEAF | LYS_LEAFLIST)))
-		return NULL;
-
-	if (schema->nodetype & LYS_LEAF)
-		type = ((const struct lysc_node_leaf *)schema)->type;
-	else
-		type = ((const struct lysc_node_leaflist *)schema)->type;
-
-	if (type->basetype != LY_TYPE_IDENT) {
-		origin_value = lyd_get_value(node);
-	} else {
-		// Identity
-		const struct lyd_value *value = NULL;
-		value = &((const struct lyd_node_term *)node)->value;
-		origin_value = value->ident->name;
-	}
-
-	escaped = faux_str_c_esc(origin_value);
-	// String with space must have quotes
-	space = strchr(origin_value, ' ');
-	if (space) {
-		result = faux_str_sprintf("\"%s\"", escaped);
-		faux_str_free(escaped);
-	} else {
-		result = escaped;
-	}
-
-	return result;
-}
-
-
 static const char *diff_prefix(enum diff_op op, pline_opts_t *opts)
 {
 	bool_t c = opts->colorize;
@@ -193,7 +85,6 @@ static void show_list(const struct lyd_node *node, size_t level,
 	enum diff_op op, pline_opts_t *opts)
 {
 	char begin_bracket[3] = {' ', opts->begin_bracket, '\0'};
-	size_t keys_num = 0;
 	const struct lyd_node *iter = NULL;
 	bool_t first_key = BOOL_TRUE;
 
@@ -216,7 +107,7 @@ static void show_list(const struct lyd_node *node, size_t level,
 			(opts->first_key_w_stmt ||
 			(opts->default_keys && klysc_node_ext_default(iter->schema))))))
 			printf(" %s", iter->schema->name);
-		value = get_value(iter);
+		value = klyd_node_value(iter);
 		printf(" %s", value);
 		faux_str_free(value);
 		first_key = BOOL_FALSE;
@@ -256,7 +147,7 @@ static void show_leaf(const struct lyd_node *node, size_t level,
 			klysc_node_ext_is_password(node->schema)) {
 			printf(" <hidden>");
 		} else {
-			char *value = get_value(node);
+			char *value = klyd_node_value(node);
 			printf(" %s", value);
 			faux_str_free(value);
 		}
@@ -276,7 +167,7 @@ static void show_leaflist(const struct lyd_node *node, size_t level,
 	if (!node)
 		return;
 
-	value = get_value(node);
+	value = klyd_node_value(node);
 	printf("%s%*s%s %s%s%s\n",
 		diff_prefix(op, opts),
 		(int)(level * opts->indent), "",

+ 1 - 5
src/syms.c

@@ -29,6 +29,7 @@
 static void _log(LY_LOG_LEVEL level, const char *msg, const char *path)
 {
 	fprintf(stderr, ERRORMSG "%s %s\n", msg, path ? path : "");
+	level = level;
 }
 
 
@@ -403,7 +404,6 @@ int srp_del(kcontext_t *context)
 	sr_conn_ctx_t *conn = NULL;
 	sr_session_ctx_t *sess = NULL;
 	pexpr_t *expr = NULL;
-	size_t err_num = 0;
 	faux_argv_t *cur_path = NULL;
 
 	assert(context);
@@ -461,7 +461,6 @@ int srp_edit(kcontext_t *context)
 	sr_conn_ctx_t *conn = NULL;
 	sr_session_ctx_t *sess = NULL;
 	pexpr_t *expr = NULL;
-	size_t err_num = 0;
 	faux_argv_t *cur_path = NULL;
 
 	assert(context);
@@ -589,7 +588,6 @@ int srp_insert(kcontext_t *context)
 	pline_t *pline_to = NULL;
 	sr_conn_ctx_t *conn = NULL;
 	sr_session_ctx_t *sess = NULL;
-	faux_list_node_t *iter = NULL;
 	pexpr_t *expr = NULL;
 	pexpr_t *expr_to = NULL;
 	faux_argv_t *cur_path = NULL;
@@ -815,7 +813,6 @@ int srp_show_xml(kcontext_t *context)
 	faux_argv_t *cur_path = NULL;
 	sr_data_t *data = NULL;
 	struct ly_out *out = NULL;
-	struct lyd_node *child = NULL;
 
 	assert(context);
 
@@ -975,7 +972,6 @@ int srp_deactivate(kcontext_t *context)
 	pexpr_t *expr = NULL;
 	faux_argv_t *cur_path = NULL;
 	sr_data_t *data = NULL;
-	const struct ly_ctx *ctx = NULL;
 
 	assert(context);