Browse Source

Move internal plugin to plugins/default

Serj Kalichev 11 years ago
parent
commit
fd67d0784a

+ 2 - 0
Makefile.am

@@ -23,6 +23,7 @@ EXTRA_DIST = \
 	lub/module.am \
 	tinyrl/module.am \
 	konf/module.am \
+	plugins/module.am \
 	contrib \
 	xml-examples \
 	debian \
@@ -37,3 +38,4 @@ include $(top_srcdir)/tinyrl/module.am
 include $(top_srcdir)/konf/module.am
 include $(top_srcdir)/clish/module.am
 include $(top_srcdir)/bin/module.am
+include $(top_srcdir)/plugins/module.am

+ 4 - 0
clish.xsd

@@ -190,6 +190,9 @@
 * [lock] - The same as lock for COMMAND tag.
 *
 * [interrupt] - The same as interrupt for COMMAND tag.
+*
+* [default_plugin] - Use (or don't use) default plugin.
+*                    It can be true or false.
 ********************************************************
 -->
     <xs:complexType name="startup_t">
@@ -204,6 +207,7 @@
         <xs:attribute name="timeout" type="xs:string" use="optional"/>
         <xs:attribute name="lock" type="bool_t" use="optional" default="true"/>
         <xs:attribute name="interrupt" type="bool_t" use="optional" default="false"/>
+        <xs:attribute name="default_plugin" type="bool_t" use="optional" default="true"/>
     </xs:complexType>
     <!--
 *******************************************************

+ 0 - 8
clish/builtin/module.am

@@ -1,8 +0,0 @@
-libclish_la_SOURCES += \
-	clish/builtin/builtin_init.c \
-	clish/builtin/hook_access.c \
-	clish/builtin/hook_config.c \
-	clish/builtin/hook_log.c \
-	clish/builtin/sym_misc.c \
-	clish/builtin/sym_script.c
-

+ 1 - 3
clish/module.am

@@ -44,8 +44,7 @@ EXTRA_DIST += \
 	clish/config/module.am \
 	clish/hotkey/module.am \
 	clish/plugin/module.am \
-	clish/builtin/module.am \
-	clish/udata/module.am	\
+	clish/udata/module.am \
 	clish/README
 
 include $(top_srcdir)/clish/command/module.am
@@ -60,5 +59,4 @@ include $(top_srcdir)/clish/action/module.am
 include $(top_srcdir)/clish/config/module.am
 include $(top_srcdir)/clish/hotkey/module.am
 include $(top_srcdir)/clish/plugin/module.am
-include $(top_srcdir)/clish/builtin/module.am
 include $(top_srcdir)/clish/udata/module.am

+ 0 - 6
clish/plugin.h

@@ -45,12 +45,6 @@ typedef struct clish_plugin_s clish_plugin_t;
 #define CLISH_HOOK_CONFIG(name) int name(void *clish_context)
 #define CLISH_HOOK_LOG(name) int name(void *clish_context, const char *line, int retcode)
 
-/* Default syms */
-#define CLISH_DEFAULT_SYM "clish_script@clish" /* Builtin symbol to use by default */
-#define CLISH_DEFAULT_ACCESS "clish_hook_access@clish"
-#define CLISH_DEFAULT_CONFIG "clish_hook_config@clish"
-#define CLISH_DEFAULT_LOG "clish_hook_log@clish"
-
 /* typedef void clish_shell_cmd_line_fn_t(clish_context_t *context, const char *cmd_line); */
 
 typedef CLISH_PLUGIN_INIT_FUNC(clish_plugin_init_t);

+ 2 - 6
clish/shell/private.h

@@ -48,12 +48,7 @@ struct clish_shell_s {
 
 	/* Hooks */
 	clish_sym_t *hooks[CLISH_SYM_TYPE_MAX]; /* Callback hooks */
-#define hooks_fn hooks[CLISH_SYM_TYPE_ACTION]
-#define hooks_init hooks[CLISH_SYM_TYPE_INIT]
-#define hooks_fini hooks[CLISH_SYM_TYPE_FINI]
-#define hooks_access hooks[CLISH_SYM_TYPE_ACCESS]
-#define hooks_config hooks[CLISH_SYM_TYPE_CONFIG]
-#define hooks_log hooks[CLISH_SYM_TYPE_LOG]
+	bool_t hooks_use[CLISH_SYM_TYPE_MAX]; /* Is hook defined */
 
 	void *client_cookie; /* Client callback cookie */
 	clish_view_t *global; /* Reference to the global view. */
@@ -82,6 +77,7 @@ struct clish_shell_s {
 	bool_t interactive; /* Is shell interactive. */
 	bool_t log; /* If command logging is enabled */
 	bool_t dryrun; /* Is this a dry-running */
+	bool_t default_plugin; /* Use or not default plugin */
 
 	/* Plugins and symbols */
 	lub_list_t *plugins; /* List of plugins */

+ 6 - 4
clish/shell/shell_execute.c

@@ -29,7 +29,7 @@ static int clish_shell_lock(const char *lock_path)
 		return -1;
 	lock_fd = open(lock_path, O_WRONLY | O_CREAT, 00644);
 	if (-1 == lock_fd) {
-		fprintf(stderr, "Can't open lockfile %s.\n", lock_path);
+		fprintf(stderr, "Warning: Can't open lockfile %s.\n", lock_path);
 		return -1;
 	}
 	lock.l_type = F_WRLCK;
@@ -45,14 +45,14 @@ static int clish_shell_lock(const char *lock_path)
 		if ((EAGAIN == errno) || (EACCES == errno)) {
 			if (0 == i)
 				fprintf(stderr,
-					"Try to get lock. Please wait...\n");
+					"Warning: Try to get lock. Please wait...\n");
 			sleep(1);
 			continue;
 		}
 		break;
 	}
 	if (res == -1) {
-		fprintf(stderr, "Can't get lock.\n");
+		fprintf(stderr, "Error: Can't get lock.\n");
 		close(lock_fd);
 		return -1;
 	}
@@ -210,8 +210,10 @@ int clish_shell_exec_action(clish_context_t *context, char **out)
 		return 0;
 	if (shell->dryrun && !clish_sym__get_permanent(sym))
 		return 0;
-	if (!(func = clish_sym__get_func(sym)))
+	if (!(func = clish_sym__get_func(sym))) {
+		fprintf(stderr, "Error: Default ACTION symbol is not specified.\n");
 		return -1;
+	}
 	script = clish_shell_expand(clish_action__get_script(action), SHELL_VAR_ACTION, context);
 	result = func(context, script, out);
 	lub_string_free(script);

+ 16 - 18
clish/shell/shell_new.c

@@ -20,7 +20,7 @@ static void clish_shell_init(clish_shell_t * this,
 	bool_t stop_on_error)
 {
 	clish_ptype_t *tmp_ptype = NULL;
-	clish_plugin_t *plugin = NULL;
+	int i;
 
 	/* initialise the tree of views */
 	lub_bintree_init(&this->view_tree,
@@ -39,9 +39,6 @@ static void clish_shell_init(clish_shell_t * this,
 
 	/* Initialize plugin list */
 	this->plugins = lub_list_new(NULL);
-	/* Create internal plugin "clish" */
-	plugin = clish_plugin_new(NULL, "clish");
-	lub_list_add(this->plugins, plugin);
 
 	/* Initialise the list of unresolved (yet) symbols */
 	this->syms = lub_list_new(clish_sym_compare);
@@ -50,20 +47,13 @@ static void clish_shell_init(clish_shell_t * this,
 	this->udata = lub_list_new(clish_udata_compare);
 	assert(this->udata);
 
-	/* Default syms and hooks */
-	this->hooks[CLISH_SYM_TYPE_NONE] = NULL;
-	this->hooks[CLISH_SYM_TYPE_ACTION] = clish_sym_new(
-		CLISH_DEFAULT_SYM, NULL, CLISH_SYM_TYPE_ACTION);
-	this->hooks[CLISH_SYM_TYPE_INIT] = NULL;
-	this->hooks[CLISH_SYM_TYPE_FINI] = NULL;
-	this->hooks[CLISH_SYM_TYPE_ACCESS] = clish_sym_new(
-		CLISH_DEFAULT_ACCESS, NULL, CLISH_SYM_TYPE_ACCESS);
-	this->hooks[CLISH_SYM_TYPE_CONFIG] = clish_sym_new(
-		CLISH_DEFAULT_CONFIG, NULL, CLISH_SYM_TYPE_CONFIG);
-	this->hooks[CLISH_SYM_TYPE_LOG] = clish_sym_new(
-		CLISH_DEFAULT_LOG, NULL, CLISH_SYM_TYPE_LOG);
-
-	/* set up defaults */
+	/* Hooks */
+	for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
+		this->hooks[i] = clish_sym_new(NULL, NULL, i);
+		this->hooks_use[i] = BOOL_FALSE;
+	}
+
+	/* Set up defaults */
 	this->client_cookie = cookie;
 	this->global = NULL;
 	this->startup = NULL;
@@ -86,6 +76,7 @@ static void clish_shell_init(clish_shell_t * this,
 	this->log = BOOL_FALSE; /* Disable logging by default */
 	this->dryrun = BOOL_FALSE; /* Disable dry-run by default */
 	this->user = lub_db_getpwuid(getuid()); /* Get user information */
+	this->default_plugin = BOOL_TRUE; /* Load default plugin by default */
 
 	/* Create internal ptypes and params */
 	/* Current depth */
@@ -155,6 +146,13 @@ static void clish_shell_fini(clish_shell_t *this)
 		clish_var_delete(var);
 	}
 
+	/* Free empty hooks */
+	for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
+		if (clish_sym__get_name(this->hooks[i]))
+			continue;
+		clish_sym_free(this->hooks[i]);
+	}
+
 	/* Free symbol list */
 	while ((iter = lub_list__get_head(this->syms))) {
 		/* Remove the symbol from the list */

+ 45 - 17
clish/shell/shell_xml.c

@@ -18,6 +18,18 @@
 #include <sys/types.h>
 #include <dirent.h>
 
+/* Default hooks */
+#define CLISH_PLUGIN_DEFAULT "clish_plugin_default.so"
+const char* clish_plugin_default_hook[] = {
+	NULL,
+	"clish_script@clish",
+	NULL,
+	NULL,
+	"clish_hook_access@clish",
+	"clish_hook_config@clish",
+	"clish_hook_log@clish"
+	};
+
 #define CLISH_XML_ERROR_STR "Error parsing XML: "
 #define CLISH_XML_ERROR_ATTR(attr) CLISH_XML_ERROR_STR"The \""attr"\" attribute is required.\n"
 
@@ -142,9 +154,25 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
 	/* tidy up */
 	lub_string_free(buffer);
 
+	/* Load default plugin */
+	if (this->default_plugin) {
+		clish_plugin_t *plugin;
+		plugin = clish_plugin_new(CLISH_PLUGIN_DEFAULT, "clish");
+		lub_list_add(this->plugins, plugin);
+		/* Default hooks */
+		for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
+			if (this->hooks_use[i])
+				continue;
+			if (!clish_plugin_default_hook[i])
+				continue;
+			clish_sym__set_name(this->hooks[i],
+				clish_plugin_default_hook[i]);
+		}
+	}
+
 	/* Add default syms to unresolved table */
 	for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
-		if (this->hooks[i])
+		if (clish_sym__get_name(this->hooks[i]))
 			lub_list_add(this->syms, this->hooks[i]);
 	}
 
@@ -576,11 +604,11 @@ process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
 	char *default_shebang =
 		clish_xmlnode_fetch_attr(element, "default_shebang");
-	char *default_builtin =
-		clish_xmlnode_fetch_attr(element, "default_builtin");
 	char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
 	char *lock = clish_xmlnode_fetch_attr(element, "lock");
 	char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
+	char *default_plugin = clish_xmlnode_fetch_attr(element,
+		"default_plugin");
 
 	/* Check syntax */
 	if (!view) {
@@ -606,10 +634,6 @@ process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	if (default_shebang)
 		clish_shell__set_default_shebang(shell, default_shebang);
 
-	if (default_builtin)
-		clish_sym__set_name(shell->hooks[CLISH_SYM_TYPE_ACTION],
-			default_builtin);
-
 	if (timeout)
 		clish_shell__set_timeout(shell, atoi(timeout));
 
@@ -625,6 +649,10 @@ process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
 	else
 		clish_command__set_interrupt(cmd, BOOL_FALSE);
 
+	/* If we need the default plugin */
+	if (default_plugin && (0 == strcmp(default_plugin, "false")))
+		shell->default_plugin = BOOL_FALSE;
+
 	/* remember this command */
 	shell->startup = cmd;
 
@@ -633,7 +661,6 @@ error:
 	clish_xml_release(view);
 	clish_xml_release(viewid);
 	clish_xml_release(default_shebang);
-	clish_xml_release(default_builtin);
 	clish_xml_release(timeout);
 	clish_xml_release(lock);
 	clish_xml_release(interrupt);
@@ -848,6 +875,7 @@ process_action(clish_shell_t *shell, clish_xmlnode_t *element, void *parent)
 			CLISH_SYM_TYPE_ACTION);
 	else
 		sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
+
 	clish_action__set_builtin(action, sym);
 	if (shebang)
 		clish_action__set_shebang(action, shebang);
@@ -1209,6 +1237,8 @@ process_hook(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
 		type = CLISH_SYM_TYPE_INIT;
 	else if (!strcmp(name, "fini"))
 		type = CLISH_SYM_TYPE_FINI;
+	else if (!strcmp(name, "action"))
+		type = CLISH_SYM_TYPE_ACTION;
 	else if (!strcmp(name, "access"))
 		type = CLISH_SYM_TYPE_ACCESS;
 	else if (!strcmp(name, "config"))
@@ -1220,16 +1250,14 @@ process_hook(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
 		goto error;
 	}
 
-	if (builtin) {
-		if (shell->hooks[type])
-			clish_sym__set_name(shell->hooks[type], builtin);
-		else
-			shell->hooks[type] = clish_sym_new(builtin, NULL, type);
-	} else {
-		if (shell->hooks[type])
-			clish_sym_free(shell->hooks[type]);
-		shell->hooks[type] = NULL;
+	/* Check duplicate HOOK tag */
+	if (shell->hooks_use[type]) {
+		fprintf(stderr,
+			CLISH_XML_ERROR_STR"HOOK %s duplication.\n", name);
+		goto error;
 	}
+	shell->hooks_use[type] = BOOL_TRUE;
+	clish_sym__set_name(shell->hooks[type], builtin);
 
 	res = 0;
 error:

+ 0 - 0
clish/builtin/builtin_init.c → plugins/default/builtin_init.c


+ 0 - 0
clish/builtin/hook_access.c → plugins/default/hook_access.c


+ 0 - 0
clish/builtin/hook_config.c → plugins/default/hook_config.c


+ 0 - 0
clish/builtin/hook_log.c → plugins/default/hook_log.c


+ 24 - 0
plugins/default/module.am

@@ -0,0 +1,24 @@
+lib_LTLIBRARIES+= clish_plugin_default.la
+clish_plugin_default_la_SOURCES =
+clish_plugin_default_la_LIBADD = \
+	libclish.la \
+	libkonf.la \
+	liblub.la
+clish_plugin_default_la_DEPENDENCIES = \
+	libclish.la \
+	libkonf.la \
+	liblub.la
+
+clish_plugin_default_la_LDFLAGS	 = -avoid-version -module -shared
+#clish_plugin_default_la_LDFLAGS	+= -export-symbols-regex "^clish_plugin_lua_"
+
+nobase_include_HEADERS += \
+	plugins/default/private.h
+
+clish_plugin_default_la_SOURCES += \
+	plugins/default/builtin_init.c \
+	plugins/default/hook_access.c \
+	plugins/default/hook_config.c \
+	plugins/default/hook_log.c \
+	plugins/default/sym_misc.c \
+	plugins/default/sym_script.c

+ 0 - 0
clish/builtin/private.h → plugins/default/private.h


+ 0 - 0
clish/builtin/sym_misc.c → plugins/default/sym_misc.c


+ 0 - 0
clish/builtin/sym_script.c → plugins/default/sym_script.c


+ 6 - 0
plugins/module.am

@@ -0,0 +1,6 @@
+## Process this file with automake to produce Makefile.in
+
+EXTRA_DIST += \
+	plugins/default/module.am
+
+include $(top_srcdir)/plugins/default/module.am