Browse Source

Implement --xslt option

Serj Kalichev 8 years ago
parent
commit
8463f7cbd2
7 changed files with 83 additions and 13 deletions
  1. 19 2
      bin/clish.c
  2. 7 0
      clish/shell.h
  3. 11 0
      clish/shell/shell_expat.c
  4. 25 8
      clish/shell/shell_libxml2.c
  5. 11 0
      clish/shell/shell_roxml.c
  6. 2 2
      clish/shell/shell_xml.c
  7. 8 1
      clish/shell/xmlapi.h

+ 19 - 2
bin/clish.c

@@ -67,6 +67,7 @@ int main(int argc, char **argv)
 	const char *xml_path = getenv("CLISH_PATH");
 	const char *view = getenv("CLISH_VIEW");
 	const char *viewid = getenv("CLISH_VIEWID");
+	const char *xslt_file = NULL;
 
 	FILE *outfd = stdout;
 	bool_t istimeout = BOOL_FALSE;
@@ -83,7 +84,7 @@ int main(int argc, char **argv)
 	struct sigaction sigpipe_act;
 	sigset_t sigpipe_set;
 
-	static const char *shortopts = "hvs:ledx:w:i:bqu8oO:kt:c:f:z:";
+	static const char *shortopts = "hvs:ledx:w:i:bqu8oO:kt:c:f:z:p:";
 #ifdef HAVE_GETOPT_LONG
 	static const struct option longopts[] = {
 		{"help",	0, NULL, 'h'},
@@ -106,6 +107,7 @@ int main(int argc, char **argv)
 		{"command",	1, NULL, 'c'},
 		{"histfile",	1, NULL, 'f'},
 		{"histsize",	1, NULL, 'z'},
+		{"xslt",	1, NULL, 'p'},
 		{NULL,		0, NULL, 0}
 	};
 #endif
@@ -214,6 +216,14 @@ int main(int argc, char **argv)
 				histsize = itmp;
 			}
 			break;
+		case 'p':
+#ifdef HAVE_LIB_LIBXSLT
+			xslt_file = optarg;
+#else
+			fprintf(stderr, "Error: The klish was built without XSLT support.\n");
+			goto end;
+#endif
+			break;
 		case 'h':
 			help(0, argv[0]);
 			exit(0);
@@ -247,7 +257,8 @@ int main(int argc, char **argv)
 		goto end;
 	}
 	/* Load the XML files */
-	if (clish_shell_load_scheme(shell, xml_path, NULL))
+	clish_xmldoc_start();
+	if (clish_shell_load_scheme(shell, xml_path, xslt_file))
 		goto end;
 	/* Set communication to the konfd */
 	clish_shell__set_socket(shell, socket_path);
@@ -363,6 +374,9 @@ end:
 	}
 	lub_list_free(cmds);
 
+	/* Stop XML engine */
+	clish_xmldoc_stop();
+
 	return result;
 }
 
@@ -400,6 +414,9 @@ static void help(int status, const char *argv0)
 		printf("\t-q, --quiet\tDisable echo while executing commands\n\t\tfrom the file stream.\n");
 		printf("\t-d, --dry-run\tDon't actually execute ACTION scripts.\n");
 		printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
+#ifdef HAVE_LIB_LIBXSLT
+		printf("\t-p <path>, --xslt=<path>\tProcess XML with specified XSLT stylesheet.\n");
+#endif
 		printf("\t-w <view_name>, --view=<view_name>\tSet the startup view.\n");
 		printf("\t-i <vars>, --viewid=<vars>\tSet the startup viewid variables.\n");
 		printf("\t-u, --utf8\tForce UTF-8 encoding.\n");

+ 7 - 0
clish/shell.h

@@ -213,6 +213,13 @@ int clish_shell__set_udata(clish_shell_t *instance,
 /* Access functions */
 int clish_shell_prepare(clish_shell_t *instance);
 
+/*
+ * Non shell specific functions.
+ * Start and Stop XML parser engine.
+ */
+int clish_xmldoc_start(void);
+int clish_xmldoc_stop(void);
+
 _END_C_DECL
 
 #endif				/* _clish_shell_h */

+ 11 - 0
clish/shell/shell_expat.c

@@ -307,6 +307,17 @@ static void clish_expat_free_node(clish_xmlnode_t *cur)
 /*
  * Public interface
  */
+
+int clish_xmldoc_start(void)
+{
+	return 0;
+}
+
+int clish_xmldoc_stop(void)
+{
+	return 0;
+}
+
 clish_xmldoc_t *clish_xmldoc_read(const char *filename)
 {
 	clish_xmldoc_t *doc;

+ 25 - 8
clish/shell/shell_libxml2.c

@@ -65,6 +65,29 @@ static inline clish_xmlnode_t *node_to_xmlnode(xmlNode *node)
 /*
  * public interface
  */
+
+int clish_xmldoc_start(void)
+{
+#ifdef HAVE_LIB_LIBXSLT
+	/* The XSLT example contain these settings but I doubt 
+	 * it's really necessary.
+	 */
+/*	xmlSubstituteEntitiesDefault(1);
+	xmlLoadExtDtdDefaultValue = 1;
+*/
+#endif
+	return 0;
+}
+
+int clish_xmldoc_stop(void)
+{
+#ifdef HAVE_LIB_LIBXSLT
+	xsltCleanupGlobals();
+#endif
+	xmlCleanupParser();
+	return 0;
+}
+
 clish_xmldoc_t *clish_xmldoc_read(const char *filename)
 {
 	xmlDoc *doc;
@@ -72,13 +95,10 @@ clish_xmldoc_t *clish_xmldoc_read(const char *filename)
 	return doc_to_xmldoc(doc);
 }
 
-
 void clish_xmldoc_release(clish_xmldoc_t *doc)
 {
-	if (doc) {
+	if (doc)
 		xmlFreeDoc(xmldoc_to_doc(doc));
-		xmlCleanupParser();
-	}
 }
 
 int clish_xmldoc_is_valid(clish_xmldoc_t *doc)
@@ -294,7 +314,7 @@ void clish_xml_release(void *p)
 
 static inline xsltStylesheet *xslt_to_xsltStylesheet(clish_xslt_t *xslt)
 {
-	return (xsltStylesheet*)doc;
+	return (xsltStylesheet*)xslt;
 }
 
 static inline clish_xslt_t *xsltStylesheet_to_xslt(xsltStylesheet *xslt)
@@ -324,8 +344,6 @@ clish_xslt_t *clish_xslt_read(const char *filename)
 {
 	xsltStylesheet* cur = NULL;
 
-	xmlSubstituteEntitiesDefault(1);
-	xmlLoadExtDtdDefaultValue = 1;
 	cur = xsltParseStylesheetFile((const xmlChar *)filename);
 
 	return xsltStylesheet_to_xslt(cur);
@@ -338,7 +356,6 @@ void clish_xslt_release(clish_xslt_t *stylesheet)
 	if (!cur)
 		return;
 	xsltFreeStylesheet(cur);
-	xsltCleanupGlobals();
 }
 
 #endif /* HAVE_LIB_LIBXSLT */

+ 11 - 0
clish/shell/shell_roxml.c

@@ -50,6 +50,17 @@ static inline clish_xmlnode_t *node_to_xmlnode(node_t *node)
 /*
  * public interface
  */
+
+int clish_xmldoc_start(void)
+{
+	return 0;
+}
+
+int clish_xmldoc_stop(void)
+{
+	return 0;
+}
+
 clish_xmldoc_t *clish_xmldoc_read(const char *filename)
 {
 	node_t *doc = roxml_load_doc((char*)filename);

+ 2 - 2
clish/shell/shell_xml.c

@@ -139,9 +139,9 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 				doc = clish_xmldoc_read(filename);
 				if (clish_xmldoc_is_valid(doc)) {
 					clish_xmlnode_t *root;
-#ifdef HAVEL_LIB_LIBXSLT
+#ifdef HAVE_LIB_LIBXSLT
 					if (xslt_path) {
-						xmlDoc *tmp;
+						clish_xmldoc_t *tmp;
 						tmp = clish_xslt_apply(doc, xslt);
 						if (!clish_xmldoc_is_valid(tmp)) {
 							fprintf(stderr, CLISH_XML_ERROR_STR"Can't load XSLT file %s\n", xslt_path);

+ 8 - 1
clish/shell/xmlapi.h

@@ -27,6 +27,13 @@ typedef struct clish_xmldoc_s clish_xmldoc_t;
  */
 typedef struct clish_xmlnode_s clish_xmlnode_t;
 
+/*
+ * Start and Stop XML parser engine.
+ * Some parsers need a global cleanup at the end of the programm.
+ */
+int clish_xmldoc_start(void);
+int clish_xmldoc_stop(void);
+
 /*
  * read an XML document
  */
@@ -197,7 +204,7 @@ void clish_xml_release(void *p);
  */
 void clish_xmlnode_print(clish_xmlnode_t *node, FILE *out);
 
-#ifdef HAVE_LIB_XSLT
+#ifdef HAVE_LIB_LIBXSLT
 
 /*
  * XSLT stylesheet (opaque type)