ソースを参照

Working on scheme

Serj Kalichev 3 年 前
コミット
3870841c3f

+ 9 - 0
bin/klishd/klishd.c

@@ -33,6 +33,8 @@
 #include <klish/ktp.h>
 #include <klish/ktp_session.h>
 
+#include <klish/kparam.h>
+
 #include "private.h"
 
 
@@ -127,6 +129,13 @@ int main(int argc, char **argv)
 		}
 	}
 
+	// Load scheme
+	{
+	kparam_t *param = NULL;
+	param = kparam_new_static((kparam_info_t){.name="PARAM", .help="This is param", .ptype = "STRING" });
+	param = param;
+	}
+
 	// Listen socket
 	syslog(LOG_DEBUG, "Create listen UNIX socket: %s\n", opts->unix_socket_path);
 	listen_unix_sock = create_listen_unix_sock(opts->unix_socket_path);

+ 3 - 0
klish/kcommand.h

@@ -16,6 +16,9 @@ typedef struct kcommand_info_s {
 
 C_DECL_BEGIN
 
+kcommand_t *kcommand_new(kcommand_info_t info);
+kcommand_t *kcommand_new_static(kcommand_info_t info);
+void kcommand_free(kcommand_t *command);
 
 C_DECL_END
 

+ 3 - 0
klish/kparam.h

@@ -17,6 +17,9 @@ typedef struct kparam_info_s {
 
 C_DECL_BEGIN
 
+kparam_t *kparam_new(kparam_info_t info);
+kparam_t *kparam_new_static(kparam_info_t info);
+void kparam_free(kparam_t *param);
 
 C_DECL_END
 

+ 5 - 0
klish/kscheme.h

@@ -11,9 +11,14 @@
 #include <klish/kcommand.h>
 #include <klish/kparam.h>
 
+typedef struct kscheme_s kscheme_t;
 
 C_DECL_BEGIN
 
+kscheme_t *kscheme_new(void);
+void kscheme_free(kscheme_t *scheme);
+bool_t kscheme_add_view(kscheme_t *scheme, kview_t *view);
+kview_t *kscheme_find_view(const kscheme_t *scheme, const char *name);
 
 C_DECL_END
 

+ 49 - 0
klish/kscheme/kcommand.c

@@ -5,3 +5,52 @@
 
 #include <faux/str.h>
 #include <klish/kcommand.h>
+
+
+struct kcommand_s {
+	bool_t is_static;
+	kcommand_info_t info;
+};
+
+
+static kcommand_t *kcommand_new_internal(kcommand_info_t info, bool_t is_static)
+{
+	kcommand_t *command = NULL;
+
+	command = faux_zmalloc(sizeof(*command));
+	assert(command);
+	if (!command)
+		return NULL;
+
+	// Initialize
+	command->is_static = is_static;
+	command->info = info;
+
+	return command;
+}
+
+
+kcommand_t *kcommand_new(kcommand_info_t info)
+{
+	return kcommand_new_internal(info, BOOL_FALSE);
+}
+
+
+kcommand_t *kcommand_new_static(kcommand_info_t info)
+{
+	return kcommand_new_internal(info, BOOL_TRUE);
+}
+
+
+void kcommand_free(kcommand_t *command)
+{
+	if (!command)
+		return;
+
+	if (!command->is_static) {
+		faux_str_free(command->info.name);
+		faux_str_free(command->info.help);
+	}
+
+	faux_free(command);
+}

+ 47 - 2
klish/kscheme/kparam.c

@@ -7,5 +7,50 @@
 #include <klish/kparam.h>
 
 struct kparam_s {
-	
-};
+	bool_t is_static;
+	kparam_info_t info;
+};
+
+
+static kparam_t *kparam_new_internal(kparam_info_t info, bool_t is_static)
+{
+	kparam_t *param = NULL;
+
+	param = faux_zmalloc(sizeof(*param));
+	assert(param);
+	if (!param)
+		return NULL;
+
+	// Initialize
+	param->is_static = is_static;
+	param->info = info;
+
+	return param;
+}
+
+
+kparam_t *kparam_new(kparam_info_t info)
+{
+	return kparam_new_internal(info, BOOL_FALSE);
+}
+
+
+kparam_t *kparam_new_static(kparam_info_t info)
+{
+	return kparam_new_internal(info, BOOL_TRUE);
+}
+
+
+void kparam_free(kparam_t *param)
+{
+	if (!param)
+		return;
+
+	if (!param->is_static) {
+		faux_str_free(param->info.name);
+		faux_str_free(param->info.help);
+		faux_str_free(param->info.ptype);
+	}
+
+	faux_free(param);
+}

+ 83 - 0
klish/kscheme/kscheme.c

@@ -4,4 +4,87 @@
 #include <assert.h>
 
 #include <faux/str.h>
+#include <faux/list.h>
+#include <klish/kview.h>
 #include <klish/kscheme.h>
+
+
+struct kscheme_s {
+	faux_list_t *views;
+};
+
+
+static int kscheme_view_compare(const void *first, const void *second)
+{
+	const kview_t *f = (const kview_t *)first;
+	const kview_t *s = (const kview_t *)second;
+
+	return strcmp(kview_name(f), kview_name(s));
+}
+
+
+static int kscheme_view_kcompare(const void *key, const void *list_item)
+{
+	const char *f = (const char *)key;
+	const kview_t *s = (const kview_t *)list_item;
+
+	return strcmp(f, kview_name(s));
+}
+
+
+kscheme_t *kscheme_new(void)
+{
+	kscheme_t *scheme = NULL;
+
+	scheme = faux_zmalloc(sizeof(*scheme));
+	assert(scheme);
+	if (!scheme)
+		return NULL;
+
+	// Initialize
+	scheme->views = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
+		kscheme_view_compare, kscheme_view_kcompare,
+		(void (*)(void *))kview_free);
+	assert(scheme->views);
+
+	return scheme;
+}
+
+
+void kscheme_free(kscheme_t *scheme)
+{
+	if (!scheme)
+		return;
+
+	faux_list_free(scheme->views);
+	faux_free(scheme);
+}
+
+
+bool_t kscheme_add_view(kscheme_t *scheme, kview_t *view)
+{
+	assert(scheme);
+	if (!scheme)
+		return BOOL_FALSE;
+	assert(view);
+	if (!view)
+		return BOOL_FALSE;
+
+	if (!faux_list_add(scheme->views, view))
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
+
+kview_t *kscheme_find_view(const kscheme_t *scheme, const char *name)
+{
+	assert(scheme);
+	if (!scheme)
+		return NULL;
+	assert(name);
+	if (!name)
+		return NULL;
+
+	return (kview_t *)faux_list_kfind(scheme->views, name);
+}

+ 58 - 0
klish/kscheme/kview.c

@@ -5,3 +5,61 @@
 
 #include <faux/str.h>
 #include <klish/kview.h>
+
+
+struct kview_s {
+	bool_t is_static;
+	kview_info_t info;
+};
+
+
+static kview_t *kview_new_internal(kview_info_t info, bool_t is_static)
+{
+	kview_t *view = NULL;
+
+	view = faux_zmalloc(sizeof(*view));
+	assert(view);
+	if (!view)
+		return NULL;
+
+	// Initialize
+	view->is_static = is_static;
+	view->info = info;
+
+	return view;
+}
+
+
+kview_t *kview_new(kview_info_t info)
+{
+	return kview_new_internal(info, BOOL_FALSE);
+}
+
+
+kview_t *kview_new_static(kview_info_t info)
+{
+	return kview_new_internal(info, BOOL_TRUE);
+}
+
+
+void kview_free(kview_t *view)
+{
+	if (!view)
+		return;
+
+	if (!view->is_static) {
+		faux_str_free(view->info.name);
+	}
+
+	faux_free(view);
+}
+
+
+const char *kview_name(const kview_t *view)
+{
+	assert(view);
+	if (!view)
+		return NULL;
+
+	return view->info.name;
+}

+ 6 - 0
klish/kview.h

@@ -15,6 +15,12 @@ typedef struct kview_info_s {
 
 C_DECL_BEGIN
 
+kview_t *kview_new(kview_info_t info);
+kview_t *kview_new_static(kview_info_t info);
+void kview_free(kview_t *view);
+
+const char *kview_name(const kview_t *view);
+
 
 C_DECL_END