Browse Source

context: Add scheme field and ustore funcs

Serj Kalichev 3 months ago
parent
commit
bd6c878d57

+ 2 - 0
bin/klishd/klishd.c

@@ -393,6 +393,7 @@ static kscheme_t *load_all_dbs(const char *dbs,
 
 	// Prepare scheme
 	context = kcontext_new(KCONTEXT_PLUGIN_INIT);
+	kcontext_set_scheme(context, scheme);
 	retcode = kscheme_prepare(scheme, context, error);
 	kcontext_free(context);
 	if (!retcode) {
@@ -428,6 +429,7 @@ static bool_t clear_scheme(kscheme_t *scheme, faux_error_t *error)
 		return BOOL_TRUE; // It's not an error
 
 	context = kcontext_new(KCONTEXT_PLUGIN_FINI);
+	kcontext_set_scheme(context, scheme);
 	kscheme_fini(scheme, context, error);
 	kcontext_free(context);
 	kscheme_free(scheme);

+ 7 - 0
klish/kcontext.h

@@ -12,6 +12,7 @@
 #include <klish/kscheme.h>
 #include <klish/ksession.h>
 #include <klish/kaction.h>
+#include <klish/kudata.h>
 
 
 C_DECL_BEGIN
@@ -19,6 +20,9 @@ C_DECL_BEGIN
 // Type
 kcontext_type_e kcontext_type(const kcontext_t *context);
 FAUX_HIDDEN bool_t kcontext_set_type(kcontext_t *context, kcontext_type_e type);
+// Scheme
+kscheme_t *kcontext_scheme(const kcontext_t *context);
+bool_t kcontext_set_scheme(kcontext_t *context, kscheme_t *scheme);
 // RetCode
 int kcontext_retcode(const kcontext_t *context);
 FAUX_HIDDEN bool_t kcontext_set_retcode(kcontext_t *context, int retcode);
@@ -63,6 +67,9 @@ const kentry_t *kcontext_candidate_entry(const kcontext_t *context);
 const char *kcontext_candidate_value(const kcontext_t *context);
 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);
 
 
 C_DECL_END

+ 0 - 1
klish/kcontext_base.h

@@ -8,7 +8,6 @@
 
 #include <faux/faux.h>
 
-
 typedef struct kcontext_s kcontext_t;
 
 typedef enum {

+ 7 - 0
klish/kscheme.h

@@ -10,6 +10,7 @@
 #include <klish/kplugin.h>
 #include <klish/kentry.h>
 #include <klish/kcontext_base.h>
+#include <klish/kudata.h>
 
 
 typedef struct kscheme_s kscheme_t;
@@ -43,6 +44,12 @@ ssize_t kscheme_entrys_len(const kscheme_t *scheme);
 kscheme_entrys_node_t *kscheme_entrys_iter(const kscheme_t *scheme);
 kentry_t *kscheme_entrys_each(kscheme_entrys_node_t **iter);
 
+// User data store
+bool_t kscheme_named_udata_new(kscheme_t *scheme,
+	const char *name, void *data, kudata_data_free_fn free_fn);
+void *kscheme_named_udata(kscheme_t *scheme, const char *name);
+
+
 C_DECL_END
 
 #endif // _klish_kscheme_h

+ 36 - 0
klish/kscheme/kscheme.c

@@ -11,11 +11,13 @@
 #include <klish/kentry.h>
 #include <klish/kscheme.h>
 #include <klish/kcontext.h>
+#include <klish/kustore.h>
 
 
 struct kscheme_s {
 	faux_list_t *plugins;
 	faux_list_t *entrys;
+	kustore_t *ustore;
 };
 
 // Simple methods
@@ -63,6 +65,10 @@ kscheme_t *kscheme_new(void)
 		(void (*)(void *))kentry_free);
 	assert(scheme->entrys);
 
+	// User store
+	scheme->ustore = kustore_new();
+	assert(scheme->ustore);
+
 	return scheme;
 }
 
@@ -74,6 +80,7 @@ void kscheme_free(kscheme_t *scheme)
 
 	faux_list_free(scheme->plugins);
 	faux_list_free(scheme->entrys);
+	kustore_free(scheme->ustore);
 
 	faux_free(scheme);
 }
@@ -383,3 +390,32 @@ bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *err
 
 	return BOOL_TRUE;
 }
+
+
+bool_t kscheme_named_udata_new(kscheme_t *scheme,
+	const char *name, void *data, kudata_data_free_fn free_fn)
+{
+	kudata_t *udata = NULL;
+
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	assert(scheme->ustore);
+
+	udata = kustore_slot_new(scheme->ustore, name, data, free_fn);
+	if (!udata)
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
+
+void *kscheme_named_udata(kscheme_t *scheme, const char *name)
+{
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	assert(scheme->ustore);
+
+	return kustore_slot_data(scheme->ustore, name);
+}

+ 2 - 2
klish/ksession.h

@@ -16,10 +16,10 @@ typedef struct ksession_s ksession_t;
 
 C_DECL_BEGIN
 
-ksession_t *ksession_new(const kscheme_t *scheme, const char *start_entry);
+ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry);
 void ksession_free(ksession_t *session);
 
-const kscheme_t *ksession_scheme(const ksession_t *session);
+kscheme_t *ksession_scheme(const ksession_t *session);
 kpath_t *ksession_path(const ksession_t *session);
 
 // Done

+ 28 - 0
klish/ksession/kcontext.c

@@ -14,10 +14,12 @@
 #include <klish/kscheme.h>
 #include <klish/ksession.h>
 #include <klish/kaction.h>
+#include <klish/kscheme.h>
 
 
 struct kcontext_s {
 	kcontext_type_e type;
+	kscheme_t *scheme;
 	int retcode;
 	ksession_t *session;
 	kplugin_t *plugin;
@@ -39,6 +41,10 @@ struct kcontext_s {
 KGET(context, kcontext_type_e, type);
 FAUX_HIDDEN KSET(context, kcontext_type_e, type);
 
+// Scheme
+KGET(context, kscheme_t *, scheme);
+KSET(context, kscheme_t *, scheme);
+
 // RetCode
 KGET(context, int, retcode);
 FAUX_HIDDEN KSET(context, int, retcode);
@@ -99,6 +105,7 @@ kcontext_t *kcontext_new(kcontext_type_e type)
 
 	// Initialize
 	context->type = type;
+	context->scheme = NULL;
 	context->retcode = 0;
 	context->plugin = NULL;
 	context->pargv = NULL;
@@ -194,3 +201,24 @@ const char *kcontext_script(const kcontext_t *context)
 
 	return kaction_script(action);
 }
+
+
+bool_t kcontext_named_udata_new(kcontext_t *context,
+	const char *name, void *data, kudata_data_free_fn free_fn)
+{
+	assert(context);
+	if (!context)
+		return BOOL_FALSE;
+
+	return kscheme_named_udata_new(context->scheme, name, data, free_fn);
+}
+
+
+void *kcontext_named_udata(kcontext_t *context, const char *name)
+{
+	assert(context);
+	if (!context)
+		return NULL;
+
+	return kscheme_named_udata(context->scheme, name);
+}

+ 3 - 3
klish/ksession/ksession.c

@@ -12,14 +12,14 @@
 
 
 struct ksession_s {
-	const kscheme_t *scheme;
+	kscheme_t *scheme;
 	kpath_t *path;
 	bool_t done; // Indicates that session is over and must be closed
 };
 
 
 // Scheme
-KGET(session, const kscheme_t *, scheme);
+KGET(session, kscheme_t *, scheme);
 
 // Path
 KGET(session, kpath_t *, path);
@@ -29,7 +29,7 @@ KGET_BOOL(session, done);
 KSET_BOOL(session, done);
 
 
-ksession_t *ksession_new(const kscheme_t *scheme, const char *start_entry)
+ksession_t *ksession_new(kscheme_t *scheme, const char *start_entry)
 {
 	ksession_t *session = NULL;
 	const kentry_t *entry = NULL;

+ 2 - 0
klish/ksession/ksession_parse.c

@@ -530,6 +530,7 @@ kexec_t *ksession_parse_for_exec(ksession_t *session, const char *raw_line,
 		// Fill the kexec_t
 		context = kcontext_new(KCONTEXT_ACTION);
 		assert(context);
+		kcontext_set_scheme(context, ksession_scheme(session));
 		kcontext_set_pargv(context, pargv);
 		// Context for ACTION execution contains session
 		kcontext_set_session(context, session);
@@ -585,6 +586,7 @@ kexec_t *ksession_parse_for_local_exec(ksession_t *session,
 
 	context = kcontext_new(KCONTEXT_SERVICE_ACTION);
 	assert(context);
+	kcontext_set_scheme(context, ksession_scheme(session));
 	kcontext_set_pargv(context, pargv);
 	kcontext_set_parent_pargv(context, parent_pargv);
 	// Service ACTIONs like PTYPE, CONDitions etc. doesn't need session

+ 1 - 1
klish/ktp/ktpd_session.c

@@ -53,7 +53,7 @@ static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
 	int *retcode, faux_error_t *error, bool_t dry_run);
 
 
-ktpd_session_t *ktpd_session_new(int sock, const kscheme_t *scheme,
+ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
 	const char *start_entry, faux_eloop_t *eloop)
 {
 	ktpd_session_t *ktpd = NULL;

+ 1 - 1
klish/ktp_session.h

@@ -57,7 +57,7 @@ bool_t ktp_session_req_cmd(ktp_session_t *ktp, const char *line,
 typedef bool_t (*ktpd_session_stall_cb_fn)(ktpd_session_t *session,
 	void *user_data);
 
-ktpd_session_t *ktpd_session_new(int sock, const kscheme_t *scheme,
+ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
 	const char *start_entry, faux_eloop_t *eloop);
 void ktpd_session_free(ktpd_session_t *session);
 bool_t ktpd_session_connected(ktpd_session_t *session);