Browse Source

Add initial version of kexec_t

Serj Kalichev 2 years ago
parent
commit
c8f92a74c6
4 changed files with 143 additions and 2 deletions
  1. 2 1
      klish/Makefile.am
  2. 38 0
      klish/kexec.h
  3. 2 1
      klish/ksession/Makefile.am
  4. 101 0
      klish/ksession/kexec.c

+ 2 - 1
klish/Makefile.am

@@ -38,7 +38,8 @@ nobase_include_HEADERS += \
 	klish/kudata.h \
 	klish/kustore.h \
 	klish/kcontext.h \
-	klish/kpath.h
+	klish/kpath.h \
+	klish/kexec.h
 
 # XML-helper
 nobase_include_HEADERS += \

+ 38 - 0
klish/kexec.h

@@ -0,0 +1,38 @@
+/** @file kexec.h
+ *
+ * @brief Klish exec
+ */
+
+#ifndef _klish_kexec_h
+#define _klish_kexec_h
+
+#include <faux/list.h>
+#include <klish/kcontext.h>
+
+typedef struct kexec_s kexec_t;
+
+typedef faux_list_node_t kexec_contexts_node_t;
+
+
+C_DECL_BEGIN
+
+kexec_t *kexec_new(void);
+void kexec_free(kexec_t *exec);
+
+size_t kexec_len(const kexec_t *exec);
+size_t kexec_is_empty(const kexec_t *exec);
+bool_t kexec_add(kexec_t *exec, kcontext_t *context);
+
+// STDIN
+int kexec_stdin(const kexec_t *exec);
+bool_t kexec_set_stdin(kexec_t *exec, int stdin);
+// STDOUT
+int kexec_stdout(const kexec_t *exec);
+bool_t kexec_set_stdout(kexec_t *exec, int stdout);
+// STDERR
+int kexec_stderr(const kexec_t *exec);
+bool_t kexec_set_stderr(kexec_t *exec, int stderr);
+
+C_DECL_END
+
+#endif // _klish_kexec_h

+ 2 - 1
klish/ksession/Makefile.am

@@ -3,4 +3,5 @@ libklish_la_SOURCES += \
 	klish/ksession/kustore.c \
 	klish/ksession/kcontext.c \
 	klish/ksession/klevel.c \
-	klish/ksession/kpath.c
+	klish/ksession/kpath.c \
+	klish/ksession/kexec.c

+ 101 - 0
klish/ksession/kexec.c

@@ -0,0 +1,101 @@
+/** @file kexec.c
+ */
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <faux/list.h>
+#include <klish/khelper.h>
+#include <klish/kcontext.h>
+#include <klish/kexec.h>
+
+struct kexec_s {
+	faux_list_t *contexts;
+	int stdin;
+	int stdout;
+	int stderr;
+};
+
+
+// STDIN
+KGET(exec, int, stdin);
+KSET(exec, int, stdin);
+
+// STDOUT
+KGET(exec, int, stdout);
+KSET(exec, int, stdout);
+
+// STDERR
+KGET(exec, int, stderr);
+KSET(exec, int, stderr);
+
+
+kexec_t *kexec_new()
+{
+	kexec_t *exec = NULL;
+
+	exec = faux_zmalloc(sizeof(*exec));
+	assert(exec);
+	if (!exec)
+		return NULL;
+
+	// List of execute contexts
+	exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))kcontext_free);
+	assert(exec->contexts);
+
+	// I/O
+	exec->stdin = -1;
+	exec->stdout = -1;
+	exec->stderr = -1;
+
+	return exec;
+}
+
+
+void kexec_free(kexec_t *exec)
+{
+	if (!exec)
+		return;
+
+	faux_list_free(exec->contexts);
+
+	free(exec);
+}
+
+
+size_t kexec_len(const kexec_t *exec)
+{
+	assert(exec);
+	if (!exec)
+		return 0;
+
+	return faux_list_len(exec->contexts);
+}
+
+
+size_t kexec_is_empty(const kexec_t *exec)
+{
+	assert(exec);
+	if (!exec)
+		return 0;
+
+	return faux_list_is_empty(exec->contexts);
+}
+
+
+bool_t kexec_add(kexec_t *exec, kcontext_t *context)
+{
+	assert(exec);
+	assert(context);
+	if (!exec)
+		return BOOL_FALSE;
+	if (!context)
+		return BOOL_FALSE;
+
+	if (!faux_list_add(exec->contexts, context))
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}