Browse Source

Early add static plugins

Serj Kalichev 9 years ago
parent
commit
9dc9bd8b96
4 changed files with 43 additions and 27 deletions
  1. 7 23
      clish/plugin/plugin.c
  2. 4 1
      clish/shell.h
  3. 19 0
      clish/shell/shell_plugin.c
  4. 13 3
      clish/shell/shell_startup.c

+ 7 - 23
clish/plugin/plugin.c

@@ -331,24 +331,6 @@ static int clish_plugin_load_shared(clish_plugin_t *this)
 #endif /* HAVE_DLFCN_H */
 }
 
-/*--------------------------------------------------------- */
-static int clish_plugin_load_builtin(clish_plugin_t *this)
-{
-	int i = 0;
-
-	/* Search plugin in the list of builtin plugins */
-	while (clish_plugin_builtin_list[i].name) {
-		if (strcmp(clish_plugin_builtin_list[i].name, this->name))
-			continue;
-		this->init = clish_plugin_builtin_list[i].init;
-		break;
-	}
-	if (this->init)
-		return 0;
-
-	return -1;
-}
-
 /*--------------------------------------------------------- */
 int clish_plugin_load(clish_plugin_t *this, void *userdata)
 {
@@ -358,14 +340,16 @@ int clish_plugin_load(clish_plugin_t *this, void *userdata)
 		return -1;
 	assert(this->name);
 
-	/* Firstly try to find builtin plugin and if there is
-	   no such builtin plugin then try to find plugin 
-	   shared object */
-	if (clish_plugin_load_builtin(this) < 0) {
+	/* Builtin plugins already have init function. */
+	if (!this->builtin_flag) {
 		if (clish_plugin_load_shared(this) < 0)
 			return -1;
 	}
-
+	if (!this->init) {
+		fprintf(stderr, "Error: PLUGIN %s has no init function\n",
+			this->name);
+		return -1;
+	}
 	/* Execute init function */
 	if ((res = this->init(userdata, this)))
 		fprintf(stderr, "Error: Plugin %s init retcode: %d\n",

+ 4 - 1
clish/shell.h

@@ -182,7 +182,10 @@ void clish_shell__set_dryrun(clish_shell_t *instance, bool_t dryrun);
 bool_t clish_shell__get_dryrun(const clish_shell_t *instance);
 
 /* Plugin functions */
-clish_plugin_t * clish_shell_find_plugin(clish_shell_t *instance, const char *name);
+clish_plugin_t * clish_shell_find_plugin(clish_shell_t *instance,
+	const char *name);
+clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *instance,
+	const char *name);
 int clish_shell_load_plugins(clish_shell_t *instance);
 int clish_shell_link_plugins(clish_shell_t *instance);
 

+ 19 - 0
clish/shell/shell_plugin.c

@@ -33,6 +33,25 @@ clish_plugin_t * clish_shell_find_plugin(clish_shell_t *this, const char *name)
 	return NULL;
 }
 
+/*----------------------------------------------------------------------- */
+clish_plugin_t * clish_shell_find_create_plugin(clish_shell_t *this,
+	const char *name)
+{
+	clish_plugin_t *plugin;
+	assert(this);
+
+	if (!name || !name[0])
+		return NULL;
+
+	plugin = clish_shell_find_plugin(this, name);
+	if (plugin)
+		return plugin;
+	plugin = clish_plugin_new(name);
+	lub_list_add(this->plugins, plugin);
+
+	return plugin;
+}
+
 /*----------------------------------------------------------------------- */
 /* For all plugins:
  *  * dlopen(plugin)

+ 13 - 3
clish/shell/shell_startup.c

@@ -146,13 +146,23 @@ int clish_shell_prepare(clish_shell_t *this)
 	lub_list_node_t *nspace_iter;
 	clish_hook_access_fn_t *access_fn = NULL;
 	clish_paramv_t *paramv;
-	int i;
+	int i = 0;
+
+	/* Add statically linked plugins */
+	while (clish_plugin_builtin_list[i].name) {
+		clish_plugin_t *plugin;
+		plugin = clish_shell_find_create_plugin(this,
+			clish_plugin_builtin_list[i].name);
+		clish_plugin_add_init(plugin,
+			clish_plugin_builtin_list[i].init);
+		clish_plugin__set_builtin_flag(plugin, BOOL_TRUE);
+		i++;
+	}
 
 	/* Add default plugin to the list of plugins */
 	if (this->default_plugin) {
 		clish_plugin_t *plugin;
-		plugin = clish_plugin_new("clish");
-		lub_list_add(this->plugins, plugin);
+		plugin = clish_shell_find_create_plugin(this, "clish");
 		/* Default hooks */
 		for (i = 0; i < CLISH_SYM_TYPE_MAX; i++) {
 			if (this->hooks_use[i])