Browse Source

Add kcontext class

Serj Kalichev 3 years ago
parent
commit
c1fd13e94d
4 changed files with 124 additions and 2 deletions
  1. 2 1
      klish/Makefile.am
  2. 44 0
      klish/kcontext.h
  3. 2 1
      klish/ksession/Makefile.am
  4. 76 0
      klish/ksession/kcontext.c

+ 2 - 1
klish/Makefile.am

@@ -29,7 +29,8 @@ nobase_include_HEADERS += \
 # Session
 nobase_include_HEADERS += \
 	klish/kudata.h \
-	klish/kustore.h
+	klish/kustore.h \
+	klish/kcontext.h
 
 #noinst_HEADERS += \
 #	klish/khelper.h

+ 44 - 0
klish/kcontext.h

@@ -0,0 +1,44 @@
+/** @file kcontext.h
+ *
+ * @brief Klish context to pass to plugin's functions
+ */
+
+#ifndef _klish_kcontext_h
+#define _klish_kcontext_h
+
+#include <faux/error.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);
+bool_t kcontext_set_plugin(kcontext_t *context, const kplugin_t *plugin);
+const ksym_t *kcontext_sym(const kcontext_t *context);
+bool_t kcontext_set_sym(kcontext_t *context, const ksym_t *sym);
+const kaction_t *kcontext_action(const kcontext_t *context);
+bool_t kcontext_set_action(kcontext_t *context, const kaction_t *action);
+const kcommand_t *kcontext_command(const kcontext_t *context);
+bool_t kcontext_set_command(kcontext_t *context, const kcommand_t *command);
+
+C_DECL_END
+
+#endif // _klish_kcontext_h

+ 2 - 1
klish/ksession/Makefile.am

@@ -1,3 +1,4 @@
 libklish_la_SOURCES += \
 	klish/ksession/kudata.c \
-	klish/ksession/kustore.c
+	klish/ksession/kustore.c \
+	klish/ksession/kcontext.c

+ 76 - 0
klish/ksession/kcontext.c

@@ -0,0 +1,76 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/conv.h>
+#include <faux/list.h>
+#include <klish/khelper.h>
+#include <klish/kcontext.h>
+#include <klish/kscheme.h>
+
+
+struct kcontext_s {
+	kcontext_type_e type;
+	int retcode;
+	const kplugin_t *plugin;
+	const ksym_t *sym;
+	const kaction_t *action;
+	const kcommand_t *command;
+};
+
+
+// Simple methods
+
+// Type
+KGET(context, kcontext_type_e, type);
+KSET(context, kcontext_type_e, type);
+
+// RetCode
+KGET(context, int, retcode);
+KSET(context, int, retcode);
+
+// Plugin
+KGET(context, const kplugin_t *, plugin);
+KSET(context, const kplugin_t *, plugin);
+
+// Sym
+KGET(context, const ksym_t *, sym);
+KSET(context, const ksym_t *, sym);
+
+// Action
+KGET(context, const kaction_t *, action);
+KSET(context, const kaction_t *, action);
+
+// Command
+KGET(context, const kcommand_t *, command);
+KSET(context, const kcommand_t *, command);
+
+
+kcontext_t *kcontext_new(kcontext_type_e type)
+{
+	kcontext_t *context = NULL;
+
+	context = faux_zmalloc(sizeof(*context));
+	assert(context);
+	if (!context)
+		return NULL;
+
+	// Initialize
+	context->type = type;
+	context->plugin = NULL;
+	context->sym = NULL;
+	context->action = NULL;
+
+	return context;
+}
+
+
+void kcontext_free(kcontext_t *context)
+{
+	if (!context)
+		return;
+
+	faux_free(context);
+}