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