Browse Source

kscheme: Add knspace_t

Serj Kalichev 2 years ago
parent
commit
ee9cf365b7

+ 1 - 0
klish/Makefile.am

@@ -31,6 +31,7 @@ nobase_include_HEADERS += \
 	klish/kaction.h \
 	klish/kptype.h \
 	klish/ksym.h \
+	klish/knspace.h \
 	klish/kdb.h
 
 # Session

+ 3 - 0
klish/kcontext.h

@@ -39,6 +39,9 @@ FAUX_HIDDEN bool_t kcontext_set_stdout(kcontext_t *context, int stdout);
 // STDERR
 int kcontext_stderr(const kcontext_t *context);
 FAUX_HIDDEN bool_t kcontext_set_stderr(kcontext_t *context, int stderr);
+// PID
+pid_t kcontext_pid(const kcontext_t *context);
+FAUX_HIDDEN bool_t kcontext_set_pid(kcontext_t *context, pid_t pid);
 
 C_DECL_END
 

+ 36 - 0
klish/knspace.h

@@ -0,0 +1,36 @@
+/** @file nspace.h
+ *
+ * @brief Klish scheme's "nspace" entry
+ */
+
+#ifndef _klish_knspace_h
+#define _klish_knspace_h
+
+#include <faux/faux.h>
+#include <faux/list.h>
+#include <klish/kview.h>
+
+
+typedef struct knspace_s knspace_t;
+
+// kview_t and knspace_t have a loop of references
+typedef struct kview_s kview_t;
+
+
+C_DECL_BEGIN
+
+knspace_t *knspace_new(const char *view_ref);
+void knspace_free(knspace_t *nspace);
+
+// View_ref
+const char *knspace_view_ref(const knspace_t *nspace);
+// View
+kview_t *knspace_view(const knspace_t *nspace);
+bool_t knspace_set_view(knspace_t *nspace, kview_t *view);
+// View
+const char *knspace_prefix(const knspace_t *nspace);
+bool_t knspace_set_prefix(knspace_t *nspace, const char *prefix);
+
+C_DECL_END
+
+#endif // _klish_knspace_h

+ 1 - 0
klish/kscheme/Makefile.am

@@ -8,4 +8,5 @@ libklish_la_SOURCES += \
 	klish/kscheme/kcommand.c \
 	klish/kscheme/kview.c \
 	klish/kscheme/kscheme.c \
+	klish/kscheme/knspace.c \
 	klish/kscheme/kdb.c

+ 64 - 0
klish/kscheme/knspace.c

@@ -0,0 +1,64 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/list.h>
+#include <faux/error.h>
+#include <klish/khelper.h>
+#include <klish/kview.h>
+#include <klish/knspace.h>
+
+struct knspace_s {
+	char *view_ref;
+	kview_t *view;
+	char *prefix;
+};
+
+
+// Simple attributes
+
+// Name
+KGET_STR(nspace, view_ref);
+
+// View
+KGET(nspace, kview_t *, view);
+KSET(nspace, kview_t *, view);
+
+// Prefix
+KGET_STR(nspace, prefix);
+KSET_STR(nspace, prefix);
+
+
+knspace_t *knspace_new(const char *view_ref)
+{
+	knspace_t *nspace = NULL;
+
+	if (faux_str_is_empty(view_ref))
+		return NULL;
+
+	nspace = faux_zmalloc(sizeof(*nspace));
+	assert(nspace);
+	if (!nspace)
+		return NULL;
+
+	// Initialize
+	nspace->view_ref = faux_str_dup(view_ref);
+	nspace->view = NULL;
+	nspace->prefix = NULL;
+
+	return nspace;
+}
+
+
+void knspace_free(knspace_t *nspace)
+{
+	if (!nspace)
+		return;
+
+	faux_str_free(nspace->view_ref);
+	faux_str_free(nspace->prefix);
+
+	faux_free(nspace);
+}

+ 0 - 1
klish/kscheme/kscheme.c

@@ -349,4 +349,3 @@ bool_t kscheme_prepare(kscheme_t *scheme, kcontext_t *context, faux_error_t *err
 
 	return BOOL_TRUE;
 }
-

+ 19 - 0
klish/kscheme/kview.c

@@ -8,11 +8,13 @@
 #include <faux/error.h>
 #include <klish/khelper.h>
 #include <klish/kcommand.h>
+#include <klish/knspace.h>
 #include <klish/kview.h>
 
 struct kview_s {
 	char *name;
 	faux_list_t *commands;
+	faux_list_t *nspaces;
 };
 
 
@@ -31,6 +33,13 @@ KNESTED_LEN(view, command);
 KNESTED_ITER(view, command);
 KNESTED_EACH(view, command);
 
+// NSPACE list
+KGET(view, faux_list_t *, nspaces);
+KADD_NESTED(view, nspace);
+KNESTED_LEN(view, nspace);
+KNESTED_ITER(view, nspace);
+KNESTED_EACH(view, nspace);
+
 
 kview_t *kview_new(const char *name)
 {
@@ -47,11 +56,20 @@ kview_t *kview_new(const char *name)
 	// Initialize
 	view->name = faux_str_dup(name);
 
+	// COMMANDs
 	view->commands = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
 		kview_command_compare, kview_command_kcompare,
 		(void (*)(void *))kcommand_free);
 	assert(view->commands);
 
+	// NSPACEs
+	// The order of NSPACEs is the same as order of NSPACE tags.
+	// The NSPACE need not to be unique because each VIEW can be
+	// NSPACEd with prefix or without prefix.
+	view->nspaces = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))knspace_free);
+	assert(view->nspaces);
+
 	return view;
 }
 
@@ -63,6 +81,7 @@ void kview_free(kview_t *view)
 
 	faux_str_free(view->name);
 	faux_list_free(view->commands);
+	faux_list_free(view->nspaces);
 
 	faux_free(view);
 }

+ 10 - 0
klish/ksession/kcontext.c

@@ -2,6 +2,8 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include <faux/str.h>
 #include <faux/conv.h>
@@ -21,6 +23,7 @@ struct kcontext_s {
 	int stdin;
 	int stdout;
 	int stderr;
+	pid_t pid;
 };
 
 
@@ -62,6 +65,10 @@ FAUX_HIDDEN KSET(context, int, stdout);
 KGET(context, int, stderr);
 FAUX_HIDDEN KSET(context, int, stderr);
 
+// STDERR
+KGET(context, pid_t, pid);
+FAUX_HIDDEN KSET(context, pid_t, pid);
+
 
 kcontext_t *kcontext_new(kcontext_type_e type)
 {
@@ -84,6 +91,9 @@ kcontext_t *kcontext_new(kcontext_type_e type)
 	context->stdout = -1;
 	context->stderr = -1;
 
+	// PID
+	context->pid = 0; // PID of currently executed ACTION
+
 	return context;
 }
 

+ 14 - 0
klish/kview.h

@@ -9,11 +9,17 @@
 #include <faux/faux.h>
 #include <faux/list.h>
 #include <klish/kcommand.h>
+#include <klish/knspace.h>
 
 
 typedef struct kview_s kview_t;
 
+// kview_t and knspace_t have a loop of references
+typedef struct knspace_s knspace_t;
+
+
 typedef faux_list_node_t kview_commands_node_t;
+typedef faux_list_node_t kview_nspaces_node_t;
 
 
 C_DECL_BEGIN
@@ -31,6 +37,14 @@ ssize_t kview_commands_len(const kview_t *view);
 kview_commands_node_t *kview_commands_iter(const kview_t *view);
 kcommand_t *kview_commands_each(kview_commands_node_t **iter);
 
+// NAMESPACEs
+faux_list_t *kview_nspaces(const kview_t *view);
+bool_t kview_add_nspace(kview_t *view, knspace_t *nspace);
+ssize_t kview_nspaces_len(const kview_t *view);
+kview_nspaces_node_t *kview_nspaces_iter(const kview_t *view);
+knspace_t *kview_nspaces_each(kview_nspaces_node_t **iter);
+
+
 C_DECL_END
 
 #endif // _klish_kview_h