Explorar o código

Implement sym and plugin new/free

Serj Kalichev %!s(int64=11) %!d(string=hai) anos
pai
achega
208d1a8bdf
Modificáronse 3 ficheiros con 82 adicións e 115 borrados
  1. 3 2
      clish/plugin.h
  2. 1 3
      clish/plugin/module.am
  3. 78 110
      clish/plugin/plugin.c

+ 3 - 2
clish/plugin.h

@@ -15,10 +15,11 @@ typedef int clish_plugin_init_t(clish_plugin_t *plugin);
 /* Name of init function within plugin */
 #define CLISH_PLUGIN_INIT "clish_plugin_init"
 
-clish_plugin_t *clish_plugin_new(const char *file);
+clish_plugin_t *clish_plugin_new(const char *name, const char *file);
 void clish_plugin_free(clish_plugin_t *instance);
 int clish_plugin_load(clish_plugin_t *instance);
-
+clish_plugin_fn_t *clish_plugin_resolve(clish_plugin_t *instance,
+	const char *name);
 int clish_plugin_sym(clish_plugin_t *instance,
 	clish_plugin_fn_t *func, const char *name);
 

+ 1 - 3
clish/plugin/module.am

@@ -1,6 +1,4 @@
 libclish_la_SOURCES += \
+	clish/plugin/plugin.c \
 	clish/plugin/plugin_dump.c \
 	clish/plugin/private.h
-
-#	clish/plugin/plugin.c \
-#

+ 78 - 110
clish/plugin/plugin.c

@@ -1,159 +1,127 @@
 /*
- * hotkey.c
+ * plugin.c
  */
 #include "private.h"
 #include "lub/string.h"
+#include "lub/list.h"
 
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <assert.h>
 
-/* Symbolic key codes */
-const char *clish_hotkey_list[] = {
-	"^@", /* 0 Null character */
-	"^A", /* 1 Start of heading, = console interrupt */
-	"^B", /* 2 Start of text, maintenance mode on HP console */
-	"^C", /* 3 End of text */
-	"^D", /* 4 End of transmission, not the same as ETB */
-	"^E", /* 5 Enquiry, goes with ACK; old HP flow control */
-	"^F", /* 6 Acknowledge, clears ENQ logon hand */
-	"^G", /* 7 Bell, rings the bell... */
-	"^H", /* 8 Backspace, works on HP terminals/computers */
-	"^I", /* 9 Horizontal tab, move to next tab stop */
-	"^J", /* 10 Line Feed */
-	"^K", /* 11 Vertical tab */
-	"^L", /* 12 Form Feed, page eject */
-	"^M", /* 13 Carriage Return*/
-	"^N", /* 14 Shift Out, alternate character set */
-	"^O", /* 15 Shift In, resume defaultn character set */
-	"^P", /* 16 Data link escape */
-	"^Q", /* 17 XON, with XOFF to pause listings; "okay to send". */
-	"^R", /* 18 Device control 2, block-mode flow control */
-	"^S", /* 19 XOFF, with XON is TERM=18 flow control */
-	"^T", /* 20 Device control 4 */
-	"^U", /* 21 Negative acknowledge */
-	"^V", /* 22 Synchronous idle */
-	"^W", /* 23 End transmission block, not the same as EOT */
-	"^X", /* 24 Cancel line, MPE echoes !!! */
-	"^Y", /* 25 End of medium, Control-Y interrupt */
-	"^Z", /* 26 Substitute */
-	"^[", /* 27 Escape, next character is not echoed */
-	"^\\", /* 28 File separator */
-	"^]", /* 29 Group separator */
-	"^^", /* 30 Record separator, block-mode terminator */
-	"^_",  /* 31 Unit separator */
-	NULL
-	};
+/**********************************************************
+ * SYM functions                                          *
+ **********************************************************/
 
 /*--------------------------------------------------------- */
-/* Search for the specified hotkey and return its hotkey structure */
-static clish_hotkey_t *find_hotkey(clish_hotkeyv_t *this, int code)
+static int clish_sym_compare(const void *first, const void *second)
 {
-	unsigned int i;
-	clish_hotkey_t *result = NULL;
+	const clish_sym_t *f = (const clish_sym_t *)first;
+	const clish_sym_t *s = (const clish_sym_t *)second;
 
-	if (!this)
-		return NULL;
+	return strcmp(f->name, s->name);
+}
 
-	/* Scan the hotkey entries in this instance */
-	for (i = 0; i < this->num; i++) {
-		clish_hotkey_t *hk = this->hotkeyv[i];
-		if (code == hk->code) {
-			result = hk;
-			break;
-		}
-	}
-	return result;
+/*--------------------------------------------------------- */
+static clish_sym_t *clish_sym_new(const char *name, clish_plugin_fn_t *func)
+{
+	clish_sym_t *this;
+
+	this = malloc(sizeof(*this));
+	this->name = lub_string_dup(name);
+	this->func = func;
+
+	return this;
 }
 
 /*--------------------------------------------------------- */
-const char *clish_hotkeyv_cmd_by_code(clish_hotkeyv_t *this, int code)
+static void clish_sym_free(clish_sym_t *this)
 {
-	clish_hotkey_t *hk;
 	if (!this)
+		return;
+	lub_string_free(this->name);
+	free(this);
+}
+
+/**********************************************************
+ * PLUGIN functions                                       *
+ **********************************************************/
+
+/*--------------------------------------------------------- */
+clish_plugin_t *clish_plugin_new(const char *name, const char *file)
+{
+	clish_plugin_t *this;
+
+	if (!file)
 		return NULL;
-	hk = find_hotkey(this, code);
-	if (!hk)
-		return NULL;
-	return hk->cmd;
+
+	this = malloc(sizeof(*this));
+
+	this->file = lub_string_dup(file);
+	if (name)
+		this->name = lub_string_dup(name);
+	else
+		this->name = NULL;
+	/* Initialise the list of symbols */
+	this->syms = lub_list_new(clish_sym_compare);
+
+	return this;
 }
 
 /*--------------------------------------------------------- */
-int clish_hotkeyv_insert(clish_hotkeyv_t *this,
-	const char *key, const char *cmd)
+void clish_plugin_free(clish_plugin_t *this)
 {
-	int code = -1;
-	int i;
+	lub_list_node_t *iter;
+
 	if (!this)
-		return -1;
+		return;
 
-	/* Find out key code */
-	i = 0;
-	while (clish_hotkey_list[i]) {
-		if (!strcmp(clish_hotkey_list[i], key))
-			code = i;
-		i++;
-	}
-	if (code < 0)
-		return -1;
+	lub_string_free(this->file);
+	lub_string_free(this->name);
 
-	/* Search for existance of such hotkey */
-	clish_hotkey_t *hk = find_hotkey(this, code);
-	if (hk) {
-		/* release the current value */
-		lub_string_free(hk->cmd);
-	} else {
-		size_t new_size = ((this->num + 1) * sizeof(clish_hotkey_t *));
-		clish_hotkey_t **tmp;
-		/* resize the hotkeys vector */
-		tmp = realloc(this->hotkeyv, new_size);
-	
-		this->hotkeyv = tmp;
-		/* insert reference to the parameter */
-		hk = malloc(sizeof(*hk));
-		this->hotkeyv[this->num++] = hk;
-		hk->code = code;
+	/* Free symbol list */
+	while ((iter = lub_list__get_head(this->syms))) {
+		/* Remove the symbol from the list */
+		lub_list_del(this->syms, iter);
+		/* Free the instance */
+		clish_sym_free((clish_sym_t *)lub_list_node__get_data(iter));
+		lub_list_node_free(iter);
 	}
-	hk->cmd = NULL;
-	if (cmd)
-		hk->cmd = lub_string_dup(cmd);
+	lub_list_free(this->syms);
 
-	return 0;
+	free(this);
 }
 
 /*--------------------------------------------------------- */
-clish_hotkeyv_t *clish_hotkeyv_new(void)
+int clish_plugin_sym(clish_plugin_t *this,
+	clish_plugin_fn_t *func, const char *name)
 {
-	clish_hotkeyv_t *this;
+	clish_sym_t *sym;
+
+	if (!name || !func)
+		return -1;
 
-	this = malloc(sizeof(clish_hotkeyv_t));
-	this->num = 0;
-	this->hotkeyv = NULL;
+	if (!(sym = clish_sym_new(name, func)))
+		return -1;
+	lub_list_add(this->syms, sym);
 
-	return this;
+	return 0;
 }
 
 /*--------------------------------------------------------- */
-static void clish_hotkeyv_fini(clish_hotkeyv_t *this)
+clish_plugin_fn_t *clish_plugin_resolve(clish_plugin_t *this, const char *name)
 {
-	unsigned int i;
 
-	for (i = 0; i < this->num; i++) {
-		lub_string_free(this->hotkeyv[i]->cmd);
-		free(this->hotkeyv[i]);
-	}
-	free(this->hotkeyv);
+	return NULL;
 }
 
 /*--------------------------------------------------------- */
-void clish_hotkeyv_delete(clish_hotkeyv_t *this)
+int clish_plugin_load(clish_plugin_t *this)
 {
-	if (!this)
-		return;
 
-	clish_hotkeyv_fini(this);
-	free(this);
+
+	return 0;
 }
 
 /*--------------------------------------------------------- */