Serj Kalichev 10 éve
szülő
commit
a3e6117923

+ 11 - 1
bin/module.am

@@ -3,7 +3,8 @@ bin_PROGRAMS += \
 	bin/clish \
 	bin/konfd \
 	bin/konf \
-	bin/sigexec
+	bin/sigexec \
+	bin/xml2c
 
 bin_clish_SOURCES = bin/clish.c
 bin_clish_LDADD = \
@@ -29,3 +30,12 @@ bin_konf_LDADD = \
 bin_sigexec_SOURCES = bin/sigexec.c
 bin_sigexec_LDADD = \
 	$(LIBOBJS)
+
+bin_xml2c_SOURCES = bin/xml2c.c
+bin_xml2c_LDADD = \
+	libclish.la \
+	libkonf.la \
+	libtinyrl.la \
+	liblub.la \
+	$(LIBOBJS) \
+	@CLISH_PLUGIN_BUILTIN_LIBS@

+ 131 - 0
bin/xml2c.c

@@ -0,0 +1,131 @@
+/*
+ * --------------------------------------
+ * clish.c
+ *
+ * A console client for libclish
+ * --------------------------------------
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif /* HAVE_CONFIG_H */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+
+#if WITH_INTERNAL_GETOPT
+#include "libc/getopt.h"
+#else
+#ifdef HAVE_GETOPT_H
+#include <getopt.h>
+#endif
+#endif
+
+#include "lub/list.h"
+#include "clish/shell.h"
+
+#define QUOTE(t) #t
+#define version(v) printf("%s\n", v)
+
+/*--------------------------------------------------------- */
+/* Print help message */
+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("Usage: %s [options] [script_file] [script_file] ...\n", name);
+		printf("CLI utility. Command line shell."
+			"The part of the klish project.\n");
+		printf("Options:\n");
+		printf("\t-v, --version\tPrint version.\n");
+		printf("\t-h, --help\tPrint this help.\n");
+		printf("\t-x <path>, --xml-path=<path>\tPath to XML scheme files.\n");
+	}
+}
+
+/*--------------------------------------------------------- */
+int main(int argc, char **argv)
+{
+	int result = -1;
+	clish_shell_t *shell = NULL;
+
+	/* Command line options */
+	const char *xml_path = getenv("CLISH_PATH");
+	FILE *outfd = stdout;
+
+	static const char *shortopts = "hvx:";
+#ifdef HAVE_GETOPT_LONG
+	static const struct option longopts[] = {
+		{"help",	0, NULL, 'h'},
+		{"version",	0, NULL, 'v'},
+		{"xml-path",	1, NULL, 'x'},
+		{NULL,		0, NULL, 0}
+	};
+#endif
+
+	/* Parse command line options */
+	while(1) {
+		int opt;
+#ifdef HAVE_GETOPT_LONG
+		opt = getopt_long(argc, argv, shortopts, longopts, NULL);
+#else
+		opt = getopt(argc, argv, shortopts);
+#endif
+		if (-1 == opt)
+			break;
+		switch (opt) {
+		case 'x':
+			xml_path = optarg;
+			break;
+		case 'h':
+			help(0, argv[0]);
+			exit(0);
+			break;
+		case 'v':
+			version(VERSION);
+			exit(0);
+			break;
+		default:
+			help(-1, argv[0]);
+			goto end;
+			break;
+		}
+	}
+
+	/* Create shell instance */
+	shell = clish_shell_new(NULL, outfd, BOOL_FALSE);
+	if (!shell) {
+		fprintf(stderr, "Error: Can't create shell instance.\n");
+		goto end;
+	}
+	/* Load the XML files */
+	if (clish_shell_load_scheme(shell, xml_path))
+		goto end;
+
+	clish_shell_xml2c(shell);
+
+end:
+	/* Cleanup */
+	if (shell)
+		clish_shell_delete(shell);
+
+	return result;
+}

+ 36 - 2
clish/ptype/ptype_dump.c

@@ -1,9 +1,11 @@
-#ifdef DEBUG
-
 /*
  * ptype_dump.c
  */
 #include "private.h"
+#include "lub/xml2c.h"
+
+#ifdef DEBUG
+
 #include "lub/dump.h"
 
 /*--------------------------------------------------------- */
@@ -24,3 +26,35 @@ void clish_ptype_dump(clish_ptype_t * this)
 /*--------------------------------------------------------- */
 
 #endif /* DEBUG */
+
+static const char *method_macros[] = {
+	"CLISH_PTYPE_REGEXP",
+	"CLISH_PTYPE_INTEGER",
+	"CLISH_PTYPE_UNSIGNEDINTEGER",
+	"CLISH_PTYPE_SELECT"
+};
+
+static const char *preprocess_macros[] = {
+	"CLISH_PTYPE_NONE",
+	"CLISH_PTYPE_TOUPPER",
+	"CLISH_PTYPE_TOLOWER"
+};
+
+void clish_ptype_xml2c(clish_ptype_t *this)
+{
+	char *esc_name = xml2c_esc(clish_ptype__get_name(this));
+	char *esc_help = xml2c_esc(clish_ptype__get_text(this));
+	char *esc_pattern = xml2c_esc(this->pattern);
+
+	printf("clish_shell_find_create_ptype(shell,\n");
+	printf("\t\"%s\",\n", XML2C_STR(esc_name)); /* name */
+	printf("\t\"%s\",\n", XML2C_STR(esc_help)); /* help */
+	printf("\t\"%s\",\n", XML2C_STR(esc_pattern)); /* pattern */
+	printf("\t%s,\n", xml2c_enum(this->method, method_macros)); /* method */
+	printf("\t%s\n", xml2c_enum(this->preprocess, preprocess_macros)); /* preprocess */
+	printf(");\n\n");
+
+	lub_string_free(esc_name);
+	lub_string_free(esc_help);
+	lub_string_free(esc_pattern);
+}

+ 41 - 2
clish/shell/shell_dump.c

@@ -1,9 +1,10 @@
-#ifdef DEBUG
-
 /*
  * shell_dump.c
  */
 #include "private.h"
+
+#ifdef DEBUG
+
 #include "lub/dump.h"
 
 /*--------------------------------------------------------- */
@@ -46,3 +47,41 @@ void clish_shell_dump(clish_shell_t * this)
 /*--------------------------------------------------------- */
 
 #endif /* DEBUG */
+
+/*--------------------------------------------------------- */
+void clish_shell_xml2c(clish_shell_t *this)
+{
+	clish_view_t *v;
+	clish_ptype_t *t;
+	clish_var_t *var;
+	lub_bintree_iterator_t iter;
+
+	printf("int clish_shell_load_scheme(clish_shell_t *shell, const char *xml_path)\n"
+		"{\n\n");
+
+	/* Iterate the tree of types */
+	printf("/*########## PTYPE ##########*/\n\n");
+	t = lub_bintree_findfirst(&this->ptype_tree);
+	for (lub_bintree_iterator_init(&iter, &this->ptype_tree, t);
+		t; t = lub_bintree_iterator_next(&iter)) {
+		clish_ptype_xml2c(t);
+	}
+#if 0
+	v = lub_bintree_findfirst(&this->view_tree);
+	/* iterate the tree of views */
+	for (lub_bintree_iterator_init(&iter, &this->view_tree, v);
+		v; v = lub_bintree_iterator_next(&iter)) {
+		clish_view_dump(v);
+	}
+
+	/* iterate the tree of vars */
+	var = lub_bintree_findfirst(&this->var_tree);
+	for (lub_bintree_iterator_init(&iter, &this->var_tree, var);
+		var; var = lub_bintree_iterator_next(&iter)) {
+		clish_var_dump(var);
+	}
+#endif
+
+	printf("\n}\n");
+}
+

+ 1 - 1
clish/shell/shell_startup.c

@@ -10,7 +10,7 @@
 const char* clish_plugin_default_hook[] = {
 	NULL,
 	"clish_script@clish",
-	NULL,
+	NULL, /* Access */
 	"clish_hook_config@clish",
 	"clish_hook_log@clish"
 };

+ 4 - 1
lub/module.am

@@ -15,7 +15,8 @@ nobase_include_HEADERS += \
     lub/system.h \
     lub/db.h \
     lub/ini.h \
-    lub/log.h
+    lub/log.h \
+    lub/xml2c.h
 
 EXTRA_DIST +=   \
     lub/argv/module.am \
@@ -28,6 +29,7 @@ EXTRA_DIST +=   \
     lub/db/module.am \
     lub/ini/module.am \
     lub/log/module.am \
+    lub/xml2c/module.am \
     lub/README
 
 include $(top_srcdir)/lub/argv/module.am
@@ -40,3 +42,4 @@ include $(top_srcdir)/lub/system/module.am
 include $(top_srcdir)/lub/db/module.am
 include $(top_srcdir)/lub/ini/module.am
 include $(top_srcdir)/lub/log/module.am
+include $(top_srcdir)/lub/xml2c/module.am

+ 10 - 0
lub/xml2c.h

@@ -0,0 +1,10 @@
+#ifndef _lub_xml2c_h
+#define _lub_xml2c_h
+
+#define XML2C_NULL ""
+#define XML2C_STR(str) ( str ? str : XML2C_NULL )
+#define XML2C_BOOL(val) ( val ? "BOOL_TRUE" : "BOOL_FALSE" )
+
+const char *xml2c_enum(int value, const char *array[]);
+
+#endif

+ 2 - 0
lub/xml2c/module.am

@@ -0,0 +1,2 @@
+liblub_la_SOURCES +=	lub/xml2c/xml2c.c \
+			lub/xml2c/private.h

+ 4 - 0
lub/xml2c/private.h

@@ -0,0 +1,4 @@
+/*
+ * private.h
+ */
+#include "lub/xml2c.h"

+ 63 - 0
lub/xml2c/xml2c.c

@@ -0,0 +1,63 @@
+/*
+ * xml2c.c
+ */
+#include "private.h"
+
+#include <stdio.h>
+#include <stdarg.h>
+
+static int indent = 0;
+
+/*--------------------------------------------------------- */
+const char *xml2c_enum(int value, const char *array[])
+{
+	return array[value];
+}
+
+char *xml2c_esc(const char *str)
+{
+/*	char *tmp, *d, *s;
+	if (!str)
+		return NULL;
+	tmp = malloc(strlen(str) * 2);
+	s = str;
+	d = tmp;
+	while (*s) {
+		switch(*s) {
+		case '\"':
+		case '\\':
+			*d = '\\';
+			d++;
+			*d = *s;
+			break;
+		case '%':
+			*d = '\\';
+			d++;
+			*d = *s;
+			break;
+		default:
+			*d = *s;
+			break;
+		}
+		d++;
+	}
+*/
+	return lub_string_encode(str, "\\\"");
+}
+
+/*--------------------------------------------------------- */
+/*int lub_dump_printf(const char *fmt, ...)
+{
+	va_list args;
+	int len;
+
+	va_start(args, fmt);
+	fprintf(stderr, "%*s", indent, "");
+	len = vfprintf(stderr, fmt, args);
+	va_end(args);
+
+	return len;
+}
+*/
+
+/*--------------------------------------------------------- */