Browse Source

XML engine can parse NSPACE tag

Serj Kalichev 3 years ago
parent
commit
8c6bd7497f
5 changed files with 96 additions and 1 deletions
  1. 0 1
      klish/inspace.h
  2. 5 0
      klish/ischeme.h
  3. 44 0
      klish/ischeme/iview.c
  4. 2 0
      klish/iview.h
  5. 45 0
      klish/xml-helper/load.c

+ 0 - 1
klish/inspace.h

@@ -7,7 +7,6 @@
 #define _klish_inspace_h
 
 #include <faux/error.h>
-#include <klish/icommand.h>
 #include <klish/knspace.h>
 
 typedef struct inspace_s {

+ 5 - 0
klish/ischeme.h

@@ -10,6 +10,7 @@
 #include <klish/iptype.h>
 #include <klish/iplugin.h>
 #include <klish/iview.h>
+#include <klish/inspace.h>
 #include <klish/kscheme.h>
 
 #define VIEW_LIST .views = &(iview_t * []) {
@@ -36,6 +37,10 @@
 #define END_PLUGIN_LIST NULL }
 #define PLUGIN &(iplugin_t)
 
+#define NSPACE_LIST .nspaces = &(inspace_t * []) {
+#define END_NSPACE_LIST NULL }
+#define NSPACE &(inspace_t)
+
 
 typedef struct ischeme_s {
 	char *name;

+ 44 - 0
klish/ischeme/iview.c

@@ -8,6 +8,7 @@
 #include <faux/error.h>
 #include <klish/khelper.h>
 #include <klish/kcommand.h>
+#include <klish/knspace.h>
 #include <klish/kview.h>
 #include <klish/iview.h>
 
@@ -41,6 +42,28 @@ bool_t iview_parse_nested(const iview_t *iview, kview_t *kview,
 		return BOOL_FALSE;
 	}
 
+	// NSPACE list
+	if (iview->nspaces) {
+		inspace_t **p_inspace = NULL;
+		for (p_inspace = *iview->nspaces; *p_inspace; p_inspace++) {
+			knspace_t *knspace = NULL;
+			inspace_t *inspace = *p_inspace;
+			knspace = inspace_load(inspace, error);
+			if (!knspace) {
+				retval = BOOL_FALSE;
+				continue;
+			}
+			if (!kview_add_nspace(kview, knspace)) {
+				faux_error_sprintf(error,
+					TAG": Can't add NSPACE \"%s\"",
+					knspace_view_ref(knspace));
+				knspace_free(knspace);
+				retval = BOOL_FALSE;
+				continue;
+			}
+		}
+	}
+
 	// COMMAND list
 	if (iview->commands) {
 		icommand_t **p_icommand = NULL;
@@ -119,6 +142,7 @@ char *iview_deploy(const kview_t *kview, int level)
 	char *str = NULL;
 	char *tmp = NULL;
 	kview_commands_node_t *commands_iter = NULL;
+	kview_nspaces_node_t *nspaces_iter = NULL;
 
 	if (!kview)
 		return NULL;
@@ -129,6 +153,26 @@ char *iview_deploy(const kview_t *kview, int level)
 
 	attr2ctext(&str, "name", kview_name(kview), level + 1);
 
+	// NSPACE list
+	nspaces_iter = kview_nspaces_iter(kview);
+	if (nspaces_iter) {
+		knspace_t *nspace = NULL;
+
+		tmp = faux_str_sprintf("\n%*cNSPACE_LIST\n\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+
+		while ((nspace = kview_nspaces_each(&nspaces_iter))) {
+			tmp = inspace_deploy(nspace, level + 2);
+			faux_str_cat(&str, tmp);
+			faux_str_free(tmp);
+		}
+
+		tmp = faux_str_sprintf("%*cEND_NSPACE_LIST,\n", level + 1, ' ');
+		faux_str_cat(&str, tmp);
+		faux_str_free(tmp);
+	}
+
 	// COMMAND list
 	commands_iter = kview_commands_iter(kview);
 	if (commands_iter) {

+ 2 - 0
klish/iview.h

@@ -8,10 +8,12 @@
 
 #include <faux/error.h>
 #include <klish/icommand.h>
+#include <klish/inspace.h>
 #include <klish/kview.h>
 
 typedef struct iview_s {
 	char *name;
+	inspace_t * (*nspaces)[];
 	icommand_t * (*commands)[];
 } iview_t;
 

+ 45 - 0
klish/xml-helper/load.c

@@ -36,6 +36,7 @@ static kxml_process_fn
 	process_view,
 	process_ptype,
 	process_plugin,
+	process_nspace,
 	process_klish;
 
 // Different TAGs types
@@ -47,6 +48,7 @@ typedef enum {
 	KTAG_VIEW,
 	KTAG_PTYPE,
 	KTAG_PLUGIN,
+	KTAG_NSPACE,
 	KTAG_KLISH,
 	KTAG_MAX
 } ktags_e;
@@ -59,6 +61,7 @@ static const char * const kxml_tags[] = {
 	"VIEW",
 	"PTYPE",
 	"PLUGIN",
+	"NSPACE",
 	"KLISH"
 };
 
@@ -70,6 +73,7 @@ static kxml_process_fn *kxml_handlers[] = {
 	process_view,
 	process_ptype,
 	process_plugin,
+	process_nspace,
 	process_klish
 };
 
@@ -604,3 +608,44 @@ err:
 
 	return res;
 }
+
+
+static bool_t process_nspace(const kxml_node_t *element, void *parent,
+	faux_error_t *error)
+{
+	inspace_t inspace = {};
+	knspace_t *nspace = NULL;
+	bool_t res = BOOL_FALSE;
+	ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
+
+	if (parent_tag != KTAG_VIEW) {
+		faux_error_sprintf(error,
+			TAG": Tag \"%s\" can't contain NSPACE tag",
+			kxml_tag_name(parent_tag));
+		return BOOL_FALSE;
+	}
+
+	inspace.ref = kxml_node_attr(element, "ref");
+	inspace.prefix = kxml_node_attr(element, "prefix");
+
+	nspace = inspace_load(&inspace, error);
+	if (!nspace)
+		goto err;
+
+	if (!kview_add_nspace((kview_t *)parent, nspace)) {
+		faux_error_sprintf(error, TAG": Can't add NSPACE \"%s\". ",
+			knspace_view_ref(nspace));
+		knspace_free(nspace);
+		goto err;
+	}
+
+	if (!process_children(element, nspace, error))
+		goto err;
+
+	res = BOOL_TRUE;
+err:
+	kxml_node_attr_free(inspace.ref);
+	kxml_node_attr_free(inspace.prefix);
+
+	return res;
+}