Browse Source

kplugin: Load plugin

Serj Kalichev 3 years ago
parent
commit
63e18e8746

+ 9 - 0
bin/klishd/klishd.c

@@ -36,6 +36,7 @@
 
 #include <klish/kscheme.h>
 #include <klish/ischeme.h>
+#include <klish/kcontext.h>
 
 #include "private.h"
 
@@ -137,6 +138,7 @@ int main(int argc, char **argv)
 	{
 	char *txt = NULL;
 	faux_error_t *error = faux_error_new();
+	kcontext_t *context = kcontext_new(KCONTEXT_PLUGIN_INIT);
 	scheme = ischeme_load(&sch, error);
 	if (!scheme) {
 		fprintf(stderr, "Scheme errors:\n");
@@ -144,6 +146,13 @@ int main(int argc, char **argv)
 		faux_error_free(error);
 		goto err;
 	}
+	// Prepare scheme
+	if (!kscheme_prepare(scheme, context, error)) {
+		fprintf(stderr, "Scheme preparing errors:\n");
+		faux_error_show(error);
+		faux_error_free(error);
+		goto err;
+	}
 	txt = ischeme_deploy(scheme, 0);
 	printf("%s\n", txt);
 	faux_str_free(txt);

+ 4 - 6
bin/klishd/sch.c

@@ -3,15 +3,13 @@ ischeme_t sch = {
  PLUGIN_LIST
 
   PLUGIN {
-   .name = "plugin1",
-   .id = "id1",
-   .file = "file1",
+   .name = "klish",
   },
 
   PLUGIN {
-   .name = "plugin2",
-   .id = "id2",
-   .file = "file2",
+   .name = "base",
+   .id = "klish",
+   .file = "kplugin_klish.so",
   },
 
  END_PLUGIN_LIST,

+ 3 - 0
klish/kscheme.h

@@ -44,6 +44,9 @@ ssize_t kscheme_plugins_len(const kscheme_t *scheme);
 kscheme_plugins_node_t *kscheme_plugins_iter(const kscheme_t *scheme);
 kplugin_t *kscheme_plugins_each(kscheme_plugins_node_t **iter);
 
+bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error);
+
+
 C_DECL_END
 
 #endif // _klish_kscheme_h

+ 5 - 0
klish/kscheme/kplugin.c

@@ -12,6 +12,7 @@
 #include <klish/khelper.h>
 #include <klish/kplugin.h>
 #include <klish/ksym.h>
+#include <klish/kcontext_base.h>
 
 
 struct kplugin_s {
@@ -208,6 +209,8 @@ int kplugin_init(kplugin_t *plugin, kcontext_t *context)
 
 	if (!plugin->init_fn)
 		return -1;
+	// Be sure the context type is appropriate one
+	kcontext_set_type(context, KCONTEXT_PLUGIN_INIT);
 
 	return plugin->init_fn(context);
 }
@@ -224,6 +227,8 @@ int kplugin_fini(kplugin_t *plugin, kcontext_t *context)
 
 	if (!plugin->fini_fn)
 		return 0; // Fini function is not mandatory so it's ok
+	// Be sure the context type is appropriate one
+	kcontext_set_type(context, KCONTEXT_PLUGIN_FINI);
 
 	return plugin->fini_fn(context);
 }

+ 27 - 19
klish/kscheme/kscheme.c

@@ -104,6 +104,8 @@ bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
 	if (!scheme)
 		return BOOL_FALSE;
 	assert(scheme->plugins);
+	if (!context)
+		return BOOL_FALSE;
 
 	iter = kscheme_plugins_iter(scheme);
 	while ((plugin = kscheme_plugins_each(&iter))) {
@@ -131,7 +133,6 @@ bool_t kscheme_load_plugins(kscheme_t *scheme, kcontext_t *context,
 bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
 	faux_error_t *error)
 {
-	bool_t retcode = BOOL_TRUE;
 	kscheme_plugins_node_t *iter = NULL;
 	kplugin_t *plugin = NULL;
 
@@ -139,39 +140,45 @@ bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
 	if (!scheme)
 		return BOOL_FALSE;
 	assert(scheme->plugins);
+	if (!context)
+		return BOOL_FALSE;
 
 	iter = kscheme_plugins_iter(scheme);
 	while ((plugin = kscheme_plugins_each(&iter))) {
-		int init_retcode = 0;
-		if (!kplugin_load(plugin)) {
-			faux_error_sprintf(error,
-				TAG ": Can't load plugin \"%s\"",
-				kplugin_name(plugin));
-			retcode = BOOL_FALSE;
-			continue; // Try to load all plugins
-		}
-		if ((init_retcode = kplugin_init(plugin, context)) < 0) {
+		int fini_retcode = -1;
+		if ((fini_retcode = kplugin_fini(plugin, context)) < 0) {
 			faux_error_sprintf(error,
-				TAG ": Can't init plugin \"%s\" (%d)",
-				kplugin_name(plugin), init_retcode);
-			retcode = BOOL_FALSE;
-			continue;
+				TAG ": Can't fini plugin \"%s\" (%d)",
+				kplugin_name(plugin), fini_retcode);
 		}
 	}
 
-	return retcode;
+	return BOOL_TRUE;
 }
 
 
-#if 0
+
 /** @brief Prepares schema for execution.
  *
  * It loads plugins, link unresolved symbols, then iterates all the
  * objects and link them to each other, check access
  * permissions. Without this function the schema is not fully functional.
  */
-int kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
+bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
 {
+
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	if (!context)
+		return BOOL_FALSE;
+
+	if (!kscheme_load_plugins(scheme, context, error)) {
+		kscheme_fini_plugins(scheme, context, error);
+		return BOOL_FALSE;
+	}
+
+#if 0
 	clish_command_t *cmd;
 	clish_view_t *view;
 	clish_nspace_t *nspace;
@@ -364,7 +371,8 @@ int kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
 			}
 		}
 	}
+#endif
 
-	return 0;
+	return BOOL_TRUE;
 }
-#endif
+

+ 1 - 1
plugins/klish/Makefile.am

@@ -1,4 +1,4 @@
-plugin_LTLIBRARIES += kplugin-klish.la
+plugin_LTLIBRARIES += kplugin_klish.la
 kplugin_klish_la_SOURCES =
 kplugin_klish_la_LDFLAGS = $(AM_LDFLAGS) -avoid-version -module
 kplugin_klish_la_LIBS =

+ 11 - 0
plugins/klish/plugin_init.c

@@ -7,6 +7,7 @@
 
 #include <faux/faux.h>
 #include <klish/kplugin.h>
+#include <klish/kcontext.h>
 
 //#include "private.h"
 
@@ -42,7 +43,17 @@ int kplugin_klish_init(kcontext_t *context)
 	clish_plugin_add_psym(plugin, clish_print_script, "clish_print_script");
 	clish_plugin_add_psym(plugin, clish_print_var, "clish_print_var");
 */
+	//fprintf(stderr, "Plugin 'klish' init\n");
 	context = context; // Happy compiler
 
 	return 0;
 }
+
+
+int kplugin_klish_fini(kcontext_t *context)
+{
+	//fprintf(stderr, "Plugin 'klish' fini\n");
+	context = context;
+
+	return 0;
+}