Browse Source

scheme: Add ksym_t

Serj Kalichev 3 years ago
parent
commit
d8e3114399
5 changed files with 144 additions and 4 deletions
  1. 5 3
      klish/Makefile.am
  2. 5 0
      klish/kplugin.h
  3. 5 1
      klish/kscheme/Makefile.am
  4. 95 0
      klish/kscheme/ksym.c
  5. 34 0
      klish/ksym.h

+ 5 - 3
klish/Makefile.am

@@ -7,6 +7,7 @@ libklish_la_LDFLAGS = $(AM_LDFLAGS) $(VERSION_INFO)
 #endif
 
 nobase_include_HEADERS += \
+	klish/khelper.h \
 	klish/ktp.h \
 	klish/ktp_session.h \
 	klish/kscheme.h \
@@ -15,10 +16,11 @@ nobase_include_HEADERS += \
 	klish/kparam.h \
 	klish/kplugin.h \
 	klish/kaction.h \
-	klish/kptype.h
+	klish/kptype.h \
+	klish/ksym.h
 
-noinst_HEADERS += \
-	klish/khelper.h
+#noinst_HEADERS += \
+#	klish/khelper.h
 
 EXTRA_DIST += \
 	klish/ktp/Makefile.am \

+ 5 - 0
klish/kplugin.h

@@ -8,6 +8,11 @@
 
 #include <faux/error.h>
 
+// Plugin API version
+#define KPLUGIN_MAJOR 1
+#define KPLUGIN_MINOR 1
+
+
 typedef struct kplugin_s kplugin_t;
 
 typedef struct iplugin_s {

+ 5 - 1
klish/kscheme/Makefile.am

@@ -1,12 +1,15 @@
 libklish_la_SOURCES += \
 	klish/kscheme/khelper.c \
+	klish/kscheme/ksym.c \
 	klish/kscheme/kplugin.c \
 	klish/kscheme/kptype.c \
 	klish/kscheme/kaction.c \
 	klish/kscheme/kparam.c \
 	klish/kscheme/kcommand.c \
 	klish/kscheme/kview.c \
-	klish/kscheme/kscheme.c \
+	klish/kscheme/kscheme.c
+
+libklish_la_SOURCES += \
 	klish/kscheme/ischeme.c \
 	klish/kscheme/iptype.c \
 	klish/kscheme/iaction.c \
@@ -14,3 +17,4 @@ libklish_la_SOURCES += \
 	klish/kscheme/icommand.c \
 	klish/kscheme/iparam.c \
 	klish/kscheme/iplugin.c
+

+ 95 - 0
klish/kscheme/ksym.c

@@ -0,0 +1,95 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include <faux/str.h>
+#include <faux/list.h>
+#include <faux/conv.h>
+#include <faux/error.h>
+#include <klish/khelper.h>
+#include <klish/ksym.h>
+
+
+struct ksym_s {
+	char *name;
+	const void *fn;
+};
+
+
+// Simple methods
+
+// Name
+KGET_STR(sym, name);
+KSET_STR_ONCE(sym, name);
+
+// Fn (function)
+KGET(sym, const void *, fn);
+KSET(sym, const void *, fn);
+
+
+static ksym_t *ksym_new_empty(void)
+{
+	ksym_t *sym = NULL;
+
+	sym = faux_zmalloc(sizeof(*sym));
+	assert(sym);
+	if (!sym)
+		return NULL;
+
+	// Initialize
+	sym->name = NULL;
+	sym->fn = NULL;
+
+	return sym;
+}
+
+
+ksym_t *ksym_new(ksym_error_e *error)
+{
+	ksym_t *sym = NULL;
+
+	sym = ksym_new_empty();
+	assert(sym);
+	if (!sym) {
+		if (error)
+			*error = KSYM_ERROR_ALLOC;
+		return NULL;
+	}
+
+	return sym;
+}
+
+
+void ksym_free(ksym_t *sym)
+{
+	if (!sym)
+		return;
+
+	faux_str_free(sym->name);
+
+	faux_free(sym);
+}
+
+
+const char *ksym_strerror(ksym_error_e error)
+{
+	const char *str = NULL;
+
+	switch (error) {
+	case KSYM_ERROR_OK:
+		str = "Ok";
+		break;
+	case KSYM_ERROR_INTERNAL:
+		str = "Internal error";
+		break;
+	case KSYM_ERROR_ALLOC:
+		str = "Memory allocation error";
+		break;
+	default:
+		str = "Unknown error";
+		break;
+	}
+
+	return str;
+}

+ 34 - 0
klish/ksym.h

@@ -0,0 +1,34 @@
+/** @file ksym.h
+ *
+ * @brief Klish symbol
+ */
+
+#ifndef _klish_ksym_h
+#define _klish_ksym_h
+
+#include <faux/error.h>
+
+typedef struct ksym_s ksym_t;
+
+typedef enum {
+	KSYM_ERROR_OK,
+	KSYM_ERROR_INTERNAL,
+	KSYM_ERROR_ALLOC,
+} ksym_error_e;
+
+
+C_DECL_BEGIN
+
+// ksym_t
+ksym_t *ksym_new(ksym_error_e *error);
+void ksym_free(ksym_t *sym);
+const char *ksym_strerror(ksym_error_e error);
+
+const char *ksym_name(const ksym_t *sym);
+bool_t ksym_set_name(ksym_t *sym, const char *name);
+const void *ksym_fn(const ksym_t *sym);
+bool_t ksym_set_fn(ksym_t *sym, const void *fn);
+
+C_DECL_END
+
+#endif // _klish_ksym_h