Browse Source

Merge branch 'master' of https://src.libcode.org/pkun/klish

Serj Kalichev 3 years ago
parent
commit
dc2efa243c

+ 1 - 0
Makefile.am

@@ -15,6 +15,7 @@ TESTC_CFLAGS = -DTESTC
 endif
 
 AM_CFLAGS = -Wall $(DEBUG_CFLAGS) $(TESTC_CFLAGS)
+AM_LDFLAGS = -z relro -z now -z defs
 
 bin_PROGRAMS =
 lib_LTLIBRARIES =

+ 0 - 2
bin/klishd/sch.c

@@ -6,14 +6,12 @@ ischeme_t sch = {
    .name = "plugin1",
    .id = "id1",
    .file = "file1",
-   .global = "true",
   },
 
   PLUGIN {
    .name = "plugin2",
    .id = "id2",
    .file = "file2",
-   .global = "false",
   },
 
  END_PLUGIN_LIST,

+ 0 - 3
klish.xsd

@@ -483,8 +483,6 @@
 * [id] - Internal plugin name. Can be the same as "name".
 * [file] - File name if standard autogenerated filename (using "name" field)
 *	is not appropriate.
-* [global] - A boolean RTLD_GLOBAL flag for dlopen()
-*	while plugin loading. Default is "false".
 ********************************************************
 -->
 	<xs:complexType name="plugin_t">
@@ -493,7 +491,6 @@
 				<xs:attribute name="name" type="xs:string" use="required"/>
 				<xs:attribute name="id" type="xs:string" use="optional"/>
 				<xs:attribute name="file" type="xs:string" use="optional"/>
-				<xs:attribute name="global" type="xs:boolean" use="optional" default="false"/>
 			</xs:extension>
 		</xs:simpleContent>
 	</xs:complexType>

+ 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
 
 # XML-helper
 nobase_include_HEADERS += \

+ 0 - 1
klish/iplugin.h

@@ -10,7 +10,6 @@ typedef struct iplugin_s {
 	char *name;
 	char *id;
 	char *file;
-	char *global;
 	char *conf;
 } iplugin_t;
 

+ 0 - 11
klish/ischeme/iplugin.c

@@ -43,16 +43,6 @@ bool_t iplugin_parse(const iplugin_t *info, kplugin_t *plugin,
 		}
 	}
 
-	// Global
-	if (!faux_str_is_empty(info->global)) {
-		bool_t b = BOOL_FALSE;
-		if (!faux_conv_str2bool(info->global, &b) ||
-			!kplugin_set_global(plugin, b)) {
-			faux_error_add(error, TAG": Illegal 'global' attribute");
-			retcode = BOOL_FALSE;
-		}
-	}
-
 	// Conf
 	if (!faux_str_is_empty(info->conf)) {
 		if (!kplugin_set_conf(plugin, info->conf)) {
@@ -107,7 +97,6 @@ char *iplugin_deploy(const kplugin_t *kplugin, int level)
 	attr2ctext(&str, "name", kplugin_name(kplugin), level + 1);
 	attr2ctext(&str, "id", kplugin_id(kplugin), level + 1);
 	attr2ctext(&str, "file", kplugin_file(kplugin), level + 1);
-	attr2ctext(&str, "global", faux_conv_bool2str(kplugin_global(kplugin)), level + 1);
 	attr2ctext(&str, "conf", kplugin_conf(kplugin), level + 1);
 
 	tmp = faux_str_sprintf("%*c},\n\n", level, ' ');

+ 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

+ 4 - 2
klish/kplugin.h

@@ -34,6 +34,10 @@ typedef struct kplugin_s kplugin_t;
 
 typedef faux_list_node_t kplugin_syms_node_t;
 
+// Callback function prototype
+typedef struct kcontext_s kcontext_t;
+typedef int (*kplugin_sym_f)(kcontext_t *context);
+
 
 C_DECL_BEGIN
 
@@ -45,8 +49,6 @@ const char *kplugin_id(const kplugin_t *plugin);
 bool_t kplugin_set_id(kplugin_t *plugin, const char *id);
 const char *kplugin_file(const kplugin_t *plugin);
 bool_t kplugin_set_file(kplugin_t *plugin, const char *file);
-bool_t kplugin_global(const kplugin_t *plugin);
-bool_t kplugin_set_global(kplugin_t *plugin, bool_t global);
 const char *kplugin_conf(const kplugin_t *plugin);
 bool_t kplugin_set_conf(kplugin_t *plugin, const char *conf);
 uint8_t kplugin_major(const kplugin_t *plugin);

+ 1 - 13
klish/kscheme/kplugin.c

@@ -18,7 +18,6 @@ struct kplugin_s {
 	char *name;
 	char *id;
 	char *file;
-	bool_t global;
 	char *conf;
 	uint8_t major;
 	uint8_t minor;
@@ -43,10 +42,6 @@ KSET_STR(plugin, id);
 KGET_STR(plugin, file);
 KSET_STR(plugin, file);
 
-// Global
-KGET_BOOL(plugin, global);
-KSET_BOOL(plugin, global);
-
 // Conf
 KGET_STR(plugin, conf);
 KSET_STR(plugin, conf);
@@ -89,7 +84,6 @@ kplugin_t *kplugin_new(const char *name)
 	plugin->name = faux_str_dup(name);
 	plugin->id = NULL;
 	plugin->file = NULL;
-	plugin->global = BOOL_FALSE;
 	plugin->conf = NULL;
 	plugin->major = 0;
 	plugin->minor = 0;
@@ -133,7 +127,7 @@ bool_t kplugin_load(kplugin_t *plugin)
 	char *fini_name = NULL;
 	char *major_name = NULL;
 	char *minor_name = NULL;
-	int flag = RTLD_NOW;
+	int flag = RTLD_NOW | RTLD_LOCAL;
 	const char *id = NULL;
 	bool_t retcode = BOOL_FALSE;
 	uint8_t *ver = NULL;
@@ -159,12 +153,6 @@ bool_t kplugin_load(kplugin_t *plugin)
 	init_name = faux_str_sprintf(KPLUGIN_INIT_FMT, id);
 	fini_name = faux_str_sprintf(KPLUGIN_FINI_FMT, id);
 
-	// SO flags
-	if (kplugin_global(plugin))
-		flag |= RTLD_GLOBAL;
-	else
-		flag |= RTLD_LOCAL;
-
 	// Open shared object
 	plugin->dlhan = dlopen(file_name, flag);
 	if (!plugin->dlhan) {

+ 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);
+}

+ 1 - 3
klish/xml-helper/load.c

@@ -46,7 +46,7 @@ typedef enum {
 	KTAG_MAX
 } ktags_e;
 
-static const char *kxml_tags[] = {
+static const char * const kxml_tags[] = {
 	NULL,
 	"ACTION",
 	"PARAM",
@@ -334,7 +334,6 @@ static bool_t process_plugin(const kxml_node_t *element, void *parent,
 	iplugin.name = kxml_node_attr(element, "name");
 	iplugin.id = kxml_node_attr(element, "id");
 	iplugin.file = kxml_node_attr(element, "file");
-	iplugin.global = kxml_node_attr(element, "global");
 	iplugin.conf = kxml_node_content(element);
 
 	plugin = iplugin_load(&iplugin, error);
@@ -361,7 +360,6 @@ err:
 	kxml_node_attr_free(iplugin.name);
 	kxml_node_attr_free(iplugin.id);
 	kxml_node_attr_free(iplugin.file);
-	kxml_node_attr_free(iplugin.global);
 	kxml_node_content_free(iplugin.conf);
 
 	return res;