Browse Source

Execute init hook on startup

Serj Kalichev 11 years ago
parent
commit
aa1eca0762
4 changed files with 28 additions and 21 deletions
  1. 6 1
      clish/plugin/plugin.c
  2. 1 7
      clish/shell/shell_new.c
  3. 5 4
      clish/shell/shell_plugin.c
  4. 16 9
      clish/shell/shell_startup.c

+ 6 - 1
clish/plugin/plugin.c

@@ -254,10 +254,15 @@ clish_plugin_init_t *clish_plugin_load(clish_plugin_t *this)
 	if (!this)
 		return NULL;
 
-	if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_LOCAL)))
+	if (!(this->dlhan = dlopen(this->file, RTLD_NOW | RTLD_LOCAL))) {
+		fprintf(stderr, "Error: Can't open plugin %s: %s\n",
+			this->file, dlerror());
 		return NULL;
+	}
 	plugin_init = (clish_plugin_init_t *)dlsym(this->dlhan, CLISH_PLUGIN_INIT_NAME);
 	if (!plugin_init) {
+		fprintf(stderr, "Error: Can't get plugin %s init function: %s\n",
+			this->file, dlerror());
 		dlclose(this->dlhan);
 		this->dlhan = NULL;
 	}

+ 1 - 7
clish/shell/shell_new.c

@@ -219,15 +219,9 @@ clish_shell_t *clish_shell_new(
 {
 	clish_shell_t *this = malloc(sizeof(clish_shell_t));
 
-	if (this) {
+	if (this)
 		clish_shell_init(this, cookie,
 			istream, ostream, stop_on_error);
-//		if (hooks->init_fn) {
-			/* now call the client initialisation */
-//			if (BOOL_TRUE != hooks->init_fn(this))
-//				this->state = SHELL_STATE_CLOSING;
-//		}
-	}
 
 	return this;
 }

+ 5 - 4
clish/shell/shell_plugin.c

@@ -4,6 +4,7 @@
 #include "private.h"
 #include <assert.h>
 #include <string.h>
+#include <dlfcn.h>
 
 #include "lub/string.h"
 #include "lub/list.h"
@@ -29,12 +30,12 @@ int clish_shell_load_plugins(clish_shell_t *this)
 	for(iter = lub_list__get_head(this->plugins);
 		iter; iter = lub_list_node__get_next(iter)) {
 		plugin = (clish_plugin_t *)lub_list_node__get_data(iter);
-		if (!(plugin_init = clish_plugin_load(plugin))) {
-			fprintf(stderr, "Error: Can't load plugin %s.\n",
-				clish_plugin__get_file(plugin));
+		if (!(plugin_init = clish_plugin_load(plugin)))
 			return -1;
-		}
 		plugin_init(this, plugin);
+		/* TODO: Check plugin_init() retval. If < 0 then
+		   destroy current plugin or exit on error.
+		*/
 #ifdef DEBUG
 		clish_plugin_dump(plugin);
 #endif

+ 16 - 9
clish/shell/shell_startup.c

@@ -11,26 +11,33 @@ int clish_shell_startup(clish_shell_t *this)
 {
 	const char *banner;
 	clish_context_t context;
-	int res = 0;
 
-	assert(this->startup);
-	banner = clish_command__get_detail(this->startup);
-	if (banner)
-		tinyrl_printf(this->tinyrl, "%s\n", banner);
+	if (!this->startup) {
+		fprintf(stderr, "Error: Can't get valid STARTUP tag.\n");
+		return -1;
+	}
 
 	/* Prepare context */
 	context.shell = this;
 	context.cmd = this->startup;
 	context.action = clish_command__get_action(this->startup);
 	context.pargv = NULL;
-	
+
+	/* If an init hook exists - call it. */
+	if (clish_shell_exec_init(&context)) {
+		fprintf(stderr, "Error: Init hook returned error code.\n");
+		return -1;
+	}
+
+	banner = clish_command__get_detail(this->startup);
+	if (banner)
+		tinyrl_printf(this->tinyrl, "%s\n", banner);
+
 	/* Call log initialize */
 	if (clish_shell__get_log(this))
 		clish_shell_exec_log(&context, NULL, 0);
 	/* Call startup script */
-	res = clish_shell_execute(&context, NULL);
-
-	return res;
+	return clish_shell_execute(&context, NULL);
 }
 
 /*----------------------------------------------------------- */