Browse Source

Add hotkey class

Serj Kalichev 12 years ago
parent
commit
6b374ef169
6 changed files with 232 additions and 0 deletions
  1. 17 0
      clish/hotkey.h
  2. 159 0
      clish/hotkey/hotkey.c
  3. 30 0
      clish/hotkey/hotkey_dump.c
  4. 4 0
      clish/hotkey/module.am
  5. 19 0
      clish/hotkey/private.h
  6. 3 0
      clish/module.am

+ 17 - 0
clish/hotkey.h

@@ -0,0 +1,17 @@
+/*
+ * hotkey.h
+ */
+#ifndef _clish_hotkey_h
+#define _clish_hotkey_h
+
+typedef struct clish_hotkey_s clish_hotkey_t;
+typedef struct clish_hotkeyv_s clish_hotkeyv_t;
+
+const char *clish_hotkeyv_cmd_by_code(clish_hotkeyv_t *instance, int code);
+int clish_hotkeyv_insert(clish_hotkeyv_t *instance,
+	const char *key, const char *cmd);
+clish_hotkeyv_t *clish_hotkeyv_new(void);
+void clish_hotkeyv_delete(clish_hotkeyv_t *instance);
+
+#endif				/* _clish_hotkey_h */
+/** @} clish_hotkey */

+ 159 - 0
clish/hotkey/hotkey.c

@@ -0,0 +1,159 @@
+/*
+ * hotkey.c
+ */
+#include "private.h"
+#include "lub/string.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
+	};
+
+/*--------------------------------------------------------- */
+/* Search for the specified hotkey and return its hotkey structure */
+static clish_hotkey_t *find_hotkey(clish_hotkeyv_t *this, int code)
+{
+	unsigned int i;
+	clish_hotkey_t *result = NULL;
+
+	if (!this)
+		return NULL;
+
+	/* 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;
+}
+
+/*--------------------------------------------------------- */
+const char *clish_hotkeyv_cmd_by_code(clish_hotkeyv_t *this, int code)
+{
+	clish_hotkey_t *hk;
+	if (!this)
+		return NULL;
+	hk = find_hotkey(this, code);
+	if (!hk)
+		return NULL;
+	return hk->cmd;
+}
+
+/*--------------------------------------------------------- */
+int clish_hotkeyv_insert(clish_hotkeyv_t *this,
+	const char *key, const char *cmd)
+{
+	int code = -1;
+	int i;
+	if (!this)
+		return -1;
+
+	/* 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;
+
+	/* 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;
+	}
+	hk->cmd = NULL;
+	if (cmd)
+		hk->cmd = lub_string_dup(cmd);
+
+	return 0;
+}
+
+/*--------------------------------------------------------- */
+clish_hotkeyv_t *clish_hotkeyv_new(void)
+{
+	clish_hotkeyv_t *this;
+
+	this = malloc(sizeof(clish_hotkeyv_t));
+	this->num = 0;
+	this->hotkeyv = NULL;
+
+	return this;
+}
+
+/*--------------------------------------------------------- */
+static void clish_hotkeyv_fini(clish_hotkeyv_t *this)
+{
+	unsigned int i;
+
+	for (i = 0; i < this->num; i++) {
+		lub_string_free(this->hotkeyv[i]->cmd);
+		free(this->hotkeyv[i]);
+	}
+	free(this->hotkeyv);
+}
+
+/*--------------------------------------------------------- */
+void clish_hotkeyv_delete(clish_hotkeyv_t *this)
+{
+	if (!this)
+		return;
+
+	clish_hotkeyv_fini(this);
+	free(this);
+}
+
+/*--------------------------------------------------------- */

+ 30 - 0
clish/hotkey/hotkey_dump.c

@@ -0,0 +1,30 @@
+/*
+ * hotkey_dump.c
+ */
+#include "private.h"
+#include "lub/dump.h"
+
+/*--------------------------------------------------------- */
+void clish_hotkey_dump(const clish_hotkey_t *this)
+{
+	lub_dump_printf("hotkey(%p)\n", this);
+
+	lub_dump_indent();
+	lub_dump_printf("key : %d\n", this->code);
+	lub_dump_printf("cmd : %s\n", this->cmd);
+	lub_dump_undent();
+}
+
+/*--------------------------------------------------------- */
+void clish_hotkeyv_dump(const clish_hotkeyv_t *this)
+{
+	unsigned int i;
+
+	lub_dump_printf("hotkeyv(%p)\n", this);
+	lub_dump_indent();
+	for (i = 0; i < this->num; i++)
+		clish_hotkey_dump(this->hotkeyv[i]);
+	lub_dump_undent();
+}
+
+/*--------------------------------------------------------- */

+ 4 - 0
clish/hotkey/module.am

@@ -0,0 +1,4 @@
+libclish_la_SOURCES += \
+	clish/hotkey/hotkey.c \
+	clish/hotkey/hotkey_dump.c \
+	clish/hotkey/private.h

+ 19 - 0
clish/hotkey/private.h

@@ -0,0 +1,19 @@
+/*
+ * hotkey private.h
+ */
+
+#include "clish/hotkey.h"
+
+/*---------------------------------------------------------
+ * PRIVATE TYPES
+ *--------------------------------------------------------- */
+
+struct clish_hotkey_s {
+	int code; /* Hotkey code */
+	char *cmd; /* Command to execute on this hotkey */
+};
+
+struct clish_hotkeyv_s {
+	unsigned int num;
+	clish_hotkey_t **hotkeyv;
+};

+ 3 - 0
clish/module.am

@@ -31,6 +31,7 @@ nobase_include_HEADERS += \
 	clish/var.h \
 	clish/action.h \
 	clish/config.h \
+	clish/hotkey.h \
 	clish/internal.h
 
 EXTRA_DIST += \
@@ -44,6 +45,7 @@ EXTRA_DIST += \
 	clish/var/module.am \
 	clish/action/module.am \
 	clish/config/module.am \
+	clish/hotkey/module.am \
 	clish/README
 
 include $(top_srcdir)/clish/command/module.am
@@ -56,3 +58,4 @@ include $(top_srcdir)/clish/nspace/module.am
 include $(top_srcdir)/clish/var/module.am
 include $(top_srcdir)/clish/action/module.am
 include $(top_srcdir)/clish/config/module.am
+include $(top_srcdir)/clish/hotkey/module.am