Browse Source

kentry_t: Initial version

Serj Kalichev 2 years ago
parent
commit
a5fe975b2a
6 changed files with 255 additions and 4 deletions
  1. 0 1
      klish.xsd
  2. 1 0
      klish/Makefile.am
  3. 92 0
      klish/kentry.h
  4. 2 1
      klish/kscheme/Makefile.am
  5. 158 0
      klish/kscheme/kentry.c
  6. 2 2
      klish/ksession/ksession.c

+ 0 - 1
klish.xsd

@@ -171,7 +171,6 @@
 		<xs:attribute name="ref" type="xs:string" use="optional"/>
 		<xs:attribute name="value" type="xs:string" use="optional"/>
 		<xs:attribute name="restore" type="xs:boolean" use="optional" default="false"/>
-		<xs:attribute name="completion" type="xs:string" use="optional"/>
 	</xs:complexType>
 
 

+ 1 - 0
klish/Makefile.am

@@ -24,6 +24,7 @@ nobase_include_HEADERS += \
 # Scheme
 nobase_include_HEADERS += \
 	klish/kscheme.h \
+	klish/kentry.h \
 	klish/kview.h \
 	klish/kcommand.h \
 	klish/kparam.h \

+ 92 - 0
klish/kentry.h

@@ -0,0 +1,92 @@
+/** @file kentry.h
+ *
+ * @brief Klish scheme's "ENTRY" entry
+ */
+
+#ifndef _klish_kentry_h
+#define _klish_kentry_h
+
+#include <faux/list.h>
+#include <klish/kaction.h>
+
+typedef struct kentry_s kentry_t;
+
+typedef faux_list_node_t kentry_entrys_node_t;
+typedef faux_list_node_t kentry_actions_node_t;
+
+// Mode of nested entrys list
+typedef enum {
+	KENTRY_MODE_NONE, // Illegal
+	KENTRY_MODE_SEQUENCE, // Sequence of entrys
+	KENTRY_MODE_SWITCH, // Switch of entrys
+	KENTRY_MODE_EMPTY, // Entry must not have a nested entrys
+} kentry_mode_e;
+
+
+// Number of max occurs
+typedef enum {
+	KENTRY_OCCURS_UNBOUNDED = (size_t)(-1),
+} kentry_occurs_e;
+
+
+C_DECL_BEGIN
+
+kentry_t *kentry_new(const char *name);
+void kentry_free(kentry_t *entry);
+
+// Name
+const char *kentry_name(const kentry_t *entry);
+// Help
+const char *kentry_help(const kentry_t *entry);
+bool_t kentry_set_help(kentry_t *entry, const char *help);
+// Parent
+kentry_t *kentry_parent(const kentry_t *entry);
+bool_t kentry_set_parent(kentry_t *entry, kentry_t *parent);
+// Container
+bool_t kentry_container(const kentry_t *entry);
+bool_t kentry_set_container(kentry_t *entry, bool_t container);
+// Mode
+kentry_mode_e kentry_mode(const kentry_t *entry);
+bool_t kentry_set_mode(kentry_t *entry, kentry_mode_e mode);
+// Min occurs
+size_t kentry_min(const kentry_t *entry);
+bool_t kentry_set_min(kentry_t *entry, size_t min);
+// Max occurs
+size_t kentry_max(const kentry_t *entry);
+bool_t kentry_set_max(kentry_t *entry, size_t max);
+// Ptype
+const char *kentry_ptype_str(const kentry_t *entry);
+bool_t kentry_set_ptype_str(kentry_t *entry, const char *ptype_str);
+kentry_t *kentry_ptype(const kentry_t *entry);
+bool_t kentry_set_ptype(kentry_t *entry, kentry_t *ptype);
+// Ref
+const char *kentry_ref_str(const kentry_t *entry);
+bool_t kentry_set_ref_str(kentry_t *entry, const char *ref_str);
+kentry_t *kentry_ref(const kentry_t *entry);
+bool_t kentry_set_ref(kentry_t *entry, kentry_t *ptype);
+// Value
+const char *kentry_value(const kentry_t *entry);
+bool_t kentry_set_value(kentry_t *entry, const char *value);
+// Restore
+bool_t kentry_restore(const kentry_t *entry);
+bool_t kentry_set_restore(kentry_t *entry, bool_t restore);
+
+// Nested ENTRY list
+faux_list_t *kentry_entrys(const kentry_t *entry);
+bool_t kentry_add_entry(kentry_t *entry, kentry_t *nested_entry);
+kentry_t *kentry_find_entry(const kentry_t *entry, const char *name);
+ssize_t kentry_entrys_len(const kentry_t *entry);
+kentry_entrys_node_t *kentry_entrys_iter(const kentry_t *entry);
+kentry_t *kentry_entrys_each(kentry_entrys_node_t **iter);
+
+// ACTIONs
+faux_list_t *kentry_actions(const kentry_t *entry);
+bool_t kentry_add_action(kentry_t *entry, kaction_t *action);
+ssize_t kentry_actions_len(const kentry_t *entry);
+kentry_actions_node_t *kentry_actions_iter(const kentry_t *entry);
+kaction_t *kentry_actions_each(kentry_actions_node_t **iter);
+
+
+C_DECL_END
+
+#endif // _klish_kentry_h

+ 2 - 1
klish/kscheme/Makefile.am

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

+ 158 - 0
klish/kscheme/kentry.c

@@ -0,0 +1,158 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/faux.h>
+#include <faux/str.h>
+#include <faux/list.h>
+#include <klish/khelper.h>
+#include <klish/kaction.h>
+#include <klish/kentry.h>
+
+
+struct kentry_s {
+	char *name; // Mandatory name (identifier within entries tree)
+	char *help; // Help for the entry
+	kentry_t *parent; // Parent kentry_t element
+	bool_t container; // Is entry container (element with hidden path)
+	kentry_mode_e mode; // Mode of nested ENTRYs list
+	size_t min; // Min occurs of entry
+	size_t max; // Max occurs of entry
+	char *ptype_str; // Text reference to PTYPE
+	kentry_t *ptype; // Resolved entry's PTYPE
+	char *ref_str; // Text reference to aliased ENTRY
+	kentry_t *ref; // Resolved aliased ENTRY
+	char *value; // Additional info
+	bool_t restore; // Should entry restore its depth while execution
+	faux_list_t *entrys; // Nested ENTRYs
+	faux_list_t *actions; // Nested ACTIONs
+};
+
+
+// Simple methods
+
+// Name
+KGET_STR(entry, name);
+
+// Help
+KGET_STR(entry, help);
+KSET_STR(entry, help);
+
+// Parent
+KGET(entry, kentry_t *, parent);
+KSET(entry, kentry_t *, parent);
+
+// Container
+KGET_BOOL(entry, container);
+KSET_BOOL(entry, container);
+
+// Mode
+KGET(entry, kentry_mode_e, mode);
+KSET(entry, kentry_mode_e, mode);
+
+// Min occurs
+KGET(entry, size_t, min);
+KSET(entry, size_t, min);
+
+// Max occurs
+KGET(entry, size_t, max);
+KSET(entry, size_t, max);
+
+// PTYPE string (must be resolved later)
+KGET_STR(entry, ptype_str);
+KSET_STR(entry, ptype_str);
+// PTYPE (resolved)
+KGET(entry, kentry_t *, ptype);
+KSET(entry, kentry_t *, ptype);
+
+// Ref string (must be resolved later)
+KGET_STR(entry, ref_str);
+KSET_STR(entry, ref_str);
+// Ref (resolved)
+KGET(entry, kentry_t *, ref);
+KSET(entry, kentry_t *, ref);
+
+// Value
+KGET_STR(entry, value);
+KSET_STR(entry, value);
+
+// Restore
+KGET_BOOL(entry, restore);
+KSET_BOOL(entry, restore);
+
+// Nested ENTRYs list
+KGET(entry, faux_list_t *, entrys);
+static KCMP_NESTED(entry, entry, name);
+static KCMP_NESTED_BY_KEY(entry, entry, name);
+KADD_NESTED(entry, entry);
+KFIND_NESTED(entry, entry);
+KNESTED_ITER(entry, entry);
+KNESTED_EACH(entry, entry);
+
+// ACTION list
+KGET(entry, faux_list_t *, actions);
+KADD_NESTED(entry, action);
+KNESTED_LEN(entry, action);
+KNESTED_ITER(entry, action);
+KNESTED_EACH(entry, action);
+
+
+kentry_t *kentry_new(const char *name)
+{
+	kentry_t *entry = NULL;
+
+	if (faux_str_is_empty(name))
+		return NULL;
+
+	entry = faux_zmalloc(sizeof(*entry));
+	assert(entry);
+	if (!entry)
+		return NULL;
+
+	// Initialize
+	entry->name = faux_str_dup(name);
+	entry->help = NULL;
+	entry->parent = NULL;
+	entry->container = BOOL_FALSE;
+	entry->mode = KENTRY_MODE_SWITCH;
+	entry->min = 1;
+	entry->max = 1;
+	entry->ptype_str = NULL;
+	entry->ptype = NULL;
+	entry->ref_str = NULL;
+	entry->ref = NULL;
+	entry->value = NULL;
+	entry->restore = BOOL_FALSE;
+
+	// ENTRY list
+	entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
+		kentry_entry_compare, kentry_entry_kcompare,
+		(void (*)(void *))kentry_free);
+	assert(entry->entrys);
+
+	// ACTION list
+	entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
+		NULL, NULL, (void (*)(void *))kaction_free);
+	assert(entry->actions);
+
+	return entry;
+}
+
+
+void kentry_free(kentry_t *entry)
+{
+	if (!entry)
+		return;
+
+	faux_str_free(entry->name);
+	faux_str_free(entry->help);
+	faux_str_free(entry->ptype_str);
+	faux_str_free(entry->ref_str);
+	faux_str_free(entry->value);
+
+	faux_list_free(entry->entrys);
+	faux_list_free(entry->actions);
+
+	faux_free(entry);
+}

+ 2 - 2
klish/ksession/ksession.c

@@ -80,11 +80,11 @@ void ksession_free(ksession_t *session)
 	free(session);
 }
 
-
+/*
 kpargv_t *ksession_parse_line(ksession_t session, const faux_argv_t *argv)
 {
 
 
 
 }
-
+*/