Browse Source

kpargv: Initial version of kparg_t and kpargv_t

Serj Kalichev 2 years ago
parent
commit
33bea193e2
5 changed files with 212 additions and 2 deletions
  1. 2 1
      klish/Makefile.am
  2. 46 0
      klish/kpargv.h
  3. 3 1
      klish/ksession/Makefile.am
  4. 58 0
      klish/ksession/kparg.c
  5. 103 0
      klish/ksession/kpargv.c

+ 2 - 1
klish/Makefile.am

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

+ 46 - 0
klish/kpargv.h

@@ -0,0 +1,46 @@
+/** @file kpargv.h
+ *
+ * @brief Klish pargv
+ */
+
+#ifndef _klish_kpargv_h
+#define _klish_kpargv_h
+
+#include <faux/list.h>
+#include <klish/kparam.h>
+#include <klish/kcommand.h>
+
+typedef struct kpargv_s kpargv_t;
+typedef struct kparg_s kparg_t;
+
+typedef faux_list_node_t kpargv_pargs_node_t;
+
+
+C_DECL_BEGIN
+
+// Parg
+
+kparg_t *kparg_new(kparam_t *param, const char *value);
+void kparg_free(kparg_t *parg);
+
+kparam_t *kparg_param(const kparg_t *parg);
+bool_t kparg_set_value(kparg_t *parg, const char *value);
+const char *kparg_value(const kparg_t *parg);
+
+// Pargv
+
+kpargv_t *kpargv_new();
+void kpargv_free(kpargv_t *pargv);
+
+kcommand_t *kpargv_command(const kpargv_t *pargv);
+bool_t kpargv_set_command(kpargv_t *pargv, kcommand_t *command);
+faux_list_t *kpargv_pargs(const kpargv_t *pargv);
+
+size_t kpargv_len(const kpargv_t *pargv);
+size_t kpargv_is_empty(const kpargv_t *pargv);
+bool_t kpargv_add(kpargv_t *pargv, kparg_t *parg);
+kparg_t *kpargv_last(const kpargv_t *pargv);
+
+C_DECL_END
+
+#endif // _klish_kpargv_h

+ 3 - 1
klish/ksession/Makefile.am

@@ -4,4 +4,6 @@ libklish_la_SOURCES += \
 	klish/ksession/kcontext.c \
 	klish/ksession/klevel.c \
 	klish/ksession/kpath.c \
-	klish/ksession/kexec.c
+	klish/ksession/kexec.c \
+	klish/ksession/kparg.c \
+	klish/ksession/kpargv.c

+ 58 - 0
klish/ksession/kparg.c

@@ -0,0 +1,58 @@
+/** @file kparg.c
+ */
+
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <faux/str.h>
+#include <klish/khelper.h>
+#include <klish/kparam.h>
+#include <klish/kpargv.h> // Contains parg and pargv
+
+
+struct kparg_s {
+	kparam_t *param;
+	char *value;
+};
+
+
+// Param
+KGET(parg, kparam_t *, param);
+
+// Value
+KSET_STR(parg, value);
+KGET_STR(parg, value);
+
+
+kparg_t *kparg_new(kparam_t *param, const char *value)
+{
+	kparg_t *parg = NULL;
+
+	if (!param)
+		return NULL;
+
+	parg = faux_zmalloc(sizeof(*parg));
+	assert(parg);
+	if (!parg)
+		return NULL;
+
+	// Initialize
+	parg->param = param;
+	kparg_set_value(parg, value);
+
+	return parg;
+}
+
+
+void kparg_free(kparg_t *parg)
+{
+	if (!parg)
+		return;
+
+	faux_str_free(parg->value);
+
+	faux_free(parg);
+}

+ 103 - 0
klish/ksession/kpargv.c

@@ -0,0 +1,103 @@
+/** @file kpargv.c
+ */
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <faux/list.h>
+#include <klish/khelper.h>
+#include <klish/kcommand.h>
+#include <klish/kparam.h>
+#include <klish/kpargv.h>
+
+
+struct kpargv_s {
+	kcommand_t *command;
+	faux_list_t *pargs;
+};
+
+
+// Command
+KGET(pargv, kcommand_t *, command);
+KSET(pargv, kcommand_t *, command);
+
+// Pargs
+KGET(pargv, faux_list_t *, pargs);
+
+
+kpargv_t *kpargv_new()
+{
+	kpargv_t *pargv = NULL;
+
+	pargv = faux_zmalloc(sizeof(*pargv));
+	assert(pargv);
+	if (!pargv)
+		return NULL;
+
+	// Parsed arguments list
+	pargv->pargs = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))kparg_free);
+	assert(pargv->pargs);
+
+	return pargv;
+}
+
+
+void kpargv_free(kpargv_t *pargv)
+{
+	if (!pargv)
+		return;
+
+	faux_list_free(pargv->pargs);
+
+	free(pargv);
+}
+
+
+size_t kpargv_len(const kpargv_t *pargv)
+{
+	assert(pargv);
+	if (!pargv)
+		return 0;
+
+	return faux_list_len(pargv->pargs);
+}
+
+
+size_t kpargv_is_empty(const kpargv_t *pargv)
+{
+	assert(pargv);
+	if (!pargv)
+		return 0;
+
+	return faux_list_is_empty(pargv->pargs);
+}
+
+
+bool_t kpargv_add(kpargv_t *pargv, kparg_t *parg)
+{
+	assert(pargv);
+	assert(parg);
+	if (!pargv)
+		return BOOL_FALSE;
+	if (!parg)
+		return BOOL_FALSE;
+
+	if (!faux_list_add(pargv->pargs, parg))
+		return BOOL_FALSE;
+
+	return BOOL_TRUE;
+}
+
+
+kparg_t *kpargv_last(const kpargv_t *pargv)
+{
+	assert(pargv);
+	if (!pargv)
+		return NULL;
+	if (kpargv_is_empty(pargv))
+		return NULL;
+
+	return (kparg_t *)faux_list_data(faux_list_tail(pargv->pargs));
+}