Browse Source

Kscheme fini function

Serj Kalichev 3 years ago
parent
commit
2ce1d62f2f
4 changed files with 38 additions and 14 deletions
  1. 19 9
      bin/klishd/klishd.c
  2. 1 0
      klish/kscheme.h
  3. 16 3
      klish/kscheme/kscheme.c
  4. 2 2
      plugins/klish/plugin_init.c

+ 19 - 9
bin/klishd/klishd.c

@@ -73,6 +73,7 @@ int main(int argc, char **argv)
 	int listen_unix_sock = -1;
 	ktpd_clients_t *clients = NULL;
 	kscheme_t *scheme = NULL;
+	faux_error_t *error = faux_error_new();
 
 	struct timespec delayed = { .tv_sec = 10, .tv_nsec = 0 };
 	struct timespec period = { .tv_sec = 3, .tv_nsec = 0 };
@@ -134,29 +135,29 @@ int main(int argc, char **argv)
 		}
 	}
 
-	// Load scheme
+	// Scheme
 	{
 	char *txt = NULL;
-	faux_error_t *error = faux_error_new();
-	kcontext_t *context = kcontext_new(KCONTEXT_PLUGIN_INIT);
+	kcontext_t *context = NULL;
+	bool_t prepare_retcode = BOOL_FALSE;
+
+	// Load scheme
 	scheme = ischeme_load(&sch, error);
 	if (!scheme) {
 		fprintf(stderr, "Scheme errors:\n");
-		faux_error_show(error);
-		faux_error_free(error);
 		goto err;
 	}
 	// Prepare scheme
-	if (!kscheme_prepare(scheme, context, error)) {
+	context = kcontext_new(KCONTEXT_PLUGIN_INIT);
+	prepare_retcode = kscheme_prepare(scheme, context, error);
+	kcontext_free(context);
+	if (!prepare_retcode) {
 		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);
-	faux_error_free(error);
 	}
 
 	// Listen socket
@@ -215,12 +216,21 @@ err:
 	}
 
 	// Free scheme
+	{
+	kcontext_t *context = kcontext_new(KCONTEXT_PLUGIN_FINI);
+	kscheme_fini(scheme, context, error);
+	kcontext_free(context);
 	kscheme_free(scheme);
+	}
 
 	// Free command line options
 	opts_free(opts);
 	syslog(LOG_INFO, "Stop daemon.\n");
 
+	if (faux_error_len(error) > 0)
+		faux_error_show(error);
+	faux_error_free(error);
+
 	return retval;
 }
 

+ 1 - 0
klish/kscheme.h

@@ -45,6 +45,7 @@ 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);
+bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error);
 
 
 C_DECL_END

+ 16 - 3
klish/kscheme/kscheme.c

@@ -157,6 +157,21 @@ bool_t kscheme_fini_plugins(kscheme_t *scheme, kcontext_t *context,
 }
 
 
+bool_t kscheme_fini(kscheme_t *scheme, kcontext_t *context, faux_error_t *error)
+{
+
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	if (!context)
+		return BOOL_FALSE;
+
+	if (!kscheme_fini_plugins(scheme, context, error))
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
 
 /** @brief Prepares schema for execution.
  *
@@ -173,10 +188,8 @@ bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *err
 	if (!context)
 		return BOOL_FALSE;
 
-	if (!kscheme_load_plugins(scheme, context, error)) {
-		kscheme_fini_plugins(scheme, context, error);
+	if (!kscheme_load_plugins(scheme, context, error))
 		return BOOL_FALSE;
-	}
 
 #if 0
 	clish_command_t *cmd;

+ 2 - 2
plugins/klish/plugin_init.c

@@ -43,7 +43,7 @@ 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");
+//	fprintf(stderr, "Plugin 'klish' init\n");
 	context = context; // Happy compiler
 
 	return 0;
@@ -52,7 +52,7 @@ int kplugin_klish_init(kcontext_t *context)
 
 int kplugin_klish_fini(kcontext_t *context)
 {
-	//fprintf(stderr, "Plugin 'klish' fini\n");
+//	fprintf(stderr, "Plugin 'klish' fini\n");
 	context = context;
 
 	return 0;