Browse Source

Universal function kcontext_plugin() that can return plugin from ACTION

Serj Kalichev 3 months ago
parent
commit
801aa0304b
2 changed files with 25 additions and 3 deletions
  1. 1 1
      klish/kcontext.h
  2. 24 2
      klish/ksession/kcontext.c

+ 1 - 1
klish/kcontext.h

@@ -69,7 +69,7 @@ const kaction_t *kcontext_action(const kcontext_t *context);
 const char *kcontext_script(const kcontext_t *context);
 bool_t kcontext_named_udata_new(kcontext_t *context,
 	const char *name, void *data, kudata_data_free_fn free_fn);
-void *kcontext_named_udata(kcontext_t *context, const char *name);
+void *kcontext_named_udata(const kcontext_t *context, const char *name);
 
 
 C_DECL_END

+ 24 - 2
klish/ksession/kcontext.c

@@ -50,7 +50,6 @@ KGET(context, int, retcode);
 FAUX_HIDDEN KSET(context, int, retcode);
 
 // Plugin
-KGET(context, kplugin_t *, plugin);
 FAUX_HIDDEN KSET(context, kplugin_t *, plugin);
 
 // Sym
@@ -214,7 +213,7 @@ bool_t kcontext_named_udata_new(kcontext_t *context,
 }
 
 
-void *kcontext_named_udata(kcontext_t *context, const char *name)
+void *kcontext_named_udata(const kcontext_t *context, const char *name)
 {
 	assert(context);
 	if (!context)
@@ -222,3 +221,26 @@ void *kcontext_named_udata(kcontext_t *context, const char *name)
 
 	return kscheme_named_udata(context->scheme, name);
 }
+
+
+kplugin_t *kcontext_plugin(const kcontext_t *context)
+{
+	const kaction_t *action = NULL;
+
+	assert(context);
+	if (!context)
+		return NULL;
+
+	// If plugin field is specified then return it. It is specified for
+	// plugin's init() and fini() functions.
+	if (context->plugin)
+		return context->plugin;
+
+	// If plugin is not explicitly cpecified then return parent plugin for
+	// currently executed sym (ACTION structure contains it).
+	action = kcontext_action(context);
+	if (!action)
+		return NULL;
+
+	return kaction_plugin(action);
+}