Browse Source

kcontext_base.h

Serj Kalichev 3 years ago
parent
commit
ed1c2f09b1
6 changed files with 106 additions and 21 deletions
  1. 1 17
      klish/kcontext.h
  2. 28 0
      klish/kcontext_base.h
  3. 1 2
      klish/kplugin.h
  4. 1 0
      klish/kscheme.h
  5. 74 1
      klish/kscheme/kscheme.c
  6. 1 1
      klish/ksym.h

+ 1 - 17
klish/kcontext.h

@@ -6,28 +6,12 @@
 #ifndef _klish_kcontext_h
 #define _klish_kcontext_h
 
-#include <faux/error.h>
-
+#include <klish/kcontext_base.h>
 #include <klish/kscheme.h>
 
 
-typedef struct kcontext_s kcontext_t;
-
-typedef enum {
-	KCONTEXT_NONE,
-	KCONTEXT_PLUGIN_INIT,
-	KCONTEXT_PLUGIN_FINI,
-	KCONTEXT_PLUGIN_ACTION
-} kcontext_type_e;
-
-
 C_DECL_BEGIN
 
-kcontext_t *kcontext_new(kcontext_type_e type);
-void kcontext_free(kcontext_t *context);
-
-kcontext_type_e kcontext_type(const kcontext_t *context);
-bool_t kcontext_set_type(kcontext_t *context, kcontext_type_e type);
 int kcontext_retcode(const kcontext_t *context);
 bool_t kcontext_set_retcode(kcontext_t *context, int retcode);
 const kplugin_t *kcontext_plugin(const kcontext_t *context);

+ 28 - 0
klish/kcontext_base.h

@@ -0,0 +1,28 @@
+/** @file kcontext_base.h
+ *
+ * @brief Klish base context to pass to plugin's functions
+ */
+
+#ifndef _klish_kcontext_base_h
+#define _klish_kcontext_base_h
+
+typedef struct kcontext_s kcontext_t;
+
+typedef enum {
+	KCONTEXT_NONE,
+	KCONTEXT_PLUGIN_INIT,
+	KCONTEXT_PLUGIN_FINI,
+	KCONTEXT_PLUGIN_ACTION
+} kcontext_type_e;
+
+
+C_DECL_BEGIN
+
+kcontext_t *kcontext_new(kcontext_type_e type);
+void kcontext_free(kcontext_t *context);
+kcontext_type_e kcontext_type(const kcontext_t *context);
+bool_t kcontext_set_type(kcontext_t *context, kcontext_type_e type);
+
+C_DECL_END
+
+#endif // _klish_kcontext_base_h

+ 1 - 2
klish/kplugin.h

@@ -10,6 +10,7 @@
 
 #include <faux/list.h>
 #include <klish/ksym.h>
+#include <klish/kcontext_base.h>
 
 // Current API version
 // Major and minor version numbers is uint8_t
@@ -31,8 +32,6 @@
 #define KPLUGIN_FINI_FMT "kplugin_%s_fini"
 
 
-typedef struct kcontext_s kcontext_t;
-
 typedef struct kplugin_s kplugin_t;
 
 typedef faux_list_node_t kplugin_syms_node_t;

+ 1 - 0
klish/kscheme.h

@@ -10,6 +10,7 @@
 #include <klish/kplugin.h>
 #include <klish/kptype.h>
 #include <klish/kview.h>
+#include <klish/kcontext_base.h>
 
 
 typedef struct kscheme_s kscheme_t;

+ 74 - 1
klish/kscheme/kscheme.c

@@ -5,6 +5,7 @@
 
 #include <faux/str.h>
 #include <faux/list.h>
+#include <faux/error.h>
 #include <klish/khelper.h>
 #include <klish/kplugin.h>
 #include <klish/kptype.h>
@@ -90,6 +91,78 @@ void kscheme_free(kscheme_t *scheme)
 	faux_free(scheme);
 }
 
+#define TAG "PLUGIN"
+
+bool_t kscheme_load_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;
+
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	assert(scheme->plugins);
+
+	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) {
+			faux_error_sprintf(error,
+				TAG ": Can't init plugin \"%s\" (%d)",
+				kplugin_name(plugin), init_retcode);
+			retcode = BOOL_FALSE;
+			continue;
+		}
+	}
+
+	return retcode;
+}
+
+
+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;
+
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	assert(scheme->plugins);
+
+	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) {
+			faux_error_sprintf(error,
+				TAG ": Can't init plugin \"%s\" (%d)",
+				kplugin_name(plugin), init_retcode);
+			retcode = BOOL_FALSE;
+			continue;
+		}
+	}
+
+	return retcode;
+}
+
+
 #if 0
 /** @brief Prepares schema for execution.
  *
@@ -97,7 +170,7 @@ void kscheme_free(kscheme_t *scheme)
  * 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)
+int kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
 {
 	clish_command_t *cmd;
 	clish_view_t *view;

+ 1 - 1
klish/ksym.h

@@ -6,11 +6,11 @@
 #ifndef _klish_ksym_h
 #define _klish_ksym_h
 
+#include <klish/kcontext_base.h>
 
 typedef struct ksym_s ksym_t;
 
 // Callback function prototype
-typedef struct kcontext_s kcontext_t; // Redeclaration to don't include kcontext.h
 typedef int (*ksym_fn)(kcontext_t *context);