Browse Source

Context public API

Serj Kalichev 11 years ago
parent
commit
94fc319ee2

+ 19 - 13
clish/shell.h

@@ -30,16 +30,22 @@
 #define CLISH_LOCK_WAIT 20
 
 typedef struct clish_shell_s clish_shell_t;
-
-/* This is used to hold context during callbacks */
-struct clish_context_s {
-	clish_shell_t *shell;
-	const clish_command_t *cmd;
-	clish_pargv_t *pargv;
-	const clish_action_t *action;
-};
 typedef struct clish_context_s clish_context_t;
 
+/* Context functions */
+_BEGIN_C_DECL
+clish_context_t *clish_context_new(clish_shell_t *shell);
+void clish_context_free(clish_context_t *instance);
+clish_shell_t *clish_context__get_shell(const void *instance);
+void clish_context__set_cmd(void *instance, clish_command_t *cmd);
+const clish_command_t *clish_context__get_cmd(const void *instance);
+void clish_context__set_pargv(void *instance, clish_pargv_t *pargv);
+clish_pargv_t *clish_context__get_pargv(const void *instance);
+void clish_context__set_action(void *instance, clish_action_t *action);
+const clish_action_t *clish_context__get_action(const void *instance);
+_END_C_DECL
+
+/* Shell */
 typedef enum {
 	SHELL_STATE_OK = 0,
 	SHELL_STATE_UNKNOWN = 1,
@@ -65,9 +71,7 @@ _BEGIN_C_DECL
  * meta functions
  *----------------- */
 
-clish_shell_t *clish_shell_new(
-	FILE * istream,
-	FILE * ostream,
+clish_shell_t *clish_shell_new(FILE * istream, FILE * ostream,
 	bool_t stop_on_error);
 /*-----------------
  * methods
@@ -118,7 +122,8 @@ char *clish_shell_expand(const char *str, clish_shell_var_t vtype, clish_context
  * attributes
  *----------------- */
 clish_view_t *clish_shell__get_view(const clish_shell_t * instance);
-unsigned clish_shell__get_depth(const clish_shell_t * instance);
+unsigned int clish_shell__get_depth(const clish_shell_t * instance);
+void clish_shell__set_depth(clish_shell_t *instance, unsigned int depth);
 const char *clish_shell__get_viewid(const clish_shell_t * instance);
 const char *clish_shell__get_overview(const clish_shell_t * instance);
 tinyrl_t *clish_shell__get_tinyrl(const clish_shell_t * instance);
@@ -126,7 +131,8 @@ void clish_shell__set_pwd(clish_shell_t *instance, const char * line,
 	clish_view_t * view, char * viewid, clish_context_t *context);
 char *clish_shell__get_pwd_line(const clish_shell_t * instance,
 	 unsigned int index);
-char *clish_shell__get_pwd_full(const clish_shell_t * instance, unsigned depth);
+char *clish_shell__get_pwd_full(const clish_shell_t * instance,
+	unsigned int depth);
 clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
 	unsigned int index);
 konf_client_t *clish_shell__get_client(const clish_shell_t * instance);

+ 89 - 0
clish/shell/context.c

@@ -0,0 +1,89 @@
+/*
+ * context.c
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+#include <string.h>
+
+#include "private.h"
+
+/*--------------------------------------------------------- */
+clish_context_t *clish_context_new(clish_shell_t *shell)
+{
+	clish_context_t *this;
+
+	if (!shell)
+		return NULL;
+	if (!(this = malloc(sizeof(*this))))
+		return NULL;
+	memset(this, 0, sizeof(*this));
+	this->shell = shell;
+
+	return this;
+}
+
+/*--------------------------------------------------------- */
+/* Note it will not free all content because it's a
+ * container only.
+ */
+void clish_context_free(clish_context_t *this)
+{
+	if (!this)
+		return;
+	free(this);
+}
+
+/*--------------------------------------------------------- */
+clish_shell_t *clish_context__get_shell(const void *this)
+{
+	const clish_context_t *context = (const clish_context_t *)this;
+	return context->shell;
+}
+
+/*--------------------------------------------------------- */
+void clish_context__set_cmd(void *this, clish_command_t *cmd)
+{
+	clish_context_t *context = (clish_context_t *)this;
+	assert(context);
+	context->cmd = cmd;
+}
+
+/*--------------------------------------------------------- */
+const clish_command_t *clish_context__get_cmd(const void *this)
+{
+	const clish_context_t *context = (const clish_context_t *)this;
+	return context->cmd;
+}
+
+/*--------------------------------------------------------- */
+void clish_context__set_pargv(void *this, clish_pargv_t *pargv)
+{
+	clish_context_t *context = (clish_context_t *)this;
+	assert(context);
+	context->pargv = pargv;
+}
+
+/*--------------------------------------------------------- */
+clish_pargv_t *clish_context__get_pargv(const void *this)
+{
+	const clish_context_t *context = (const clish_context_t *)this;
+	return context->pargv;
+}
+
+/*--------------------------------------------------------- */
+void clish_context__set_action(void *this, clish_action_t *action)
+{
+	clish_context_t *context = (clish_context_t *)this;
+	assert(context);
+	context->action = action;
+}
+
+/*--------------------------------------------------------- */
+const clish_action_t *clish_context__get_action(const void *this)
+{
+	const clish_context_t *context = (const clish_context_t *)this;
+	return context->action;
+}
+
+/*--------------------------------------------------------- */

+ 4 - 1
clish/shell/module.am

@@ -21,5 +21,8 @@ libclish_la_SOURCES += \
 	clish/shell/shell_roxml.c \
 	clish/shell/shell_libxml2.c \
 	clish/shell/shell_expat.c \
-	clish/shell/shell_udata.c
+	clish/shell/shell_udata.c \
+	clish/shell/shell_misc.c \
+	clish/shell/context.c
+
 

+ 10 - 4
clish/shell/private.h

@@ -1,5 +1,5 @@
 /*
- * shell.h - private interface to the shell class
+ * shell/private.h - private interface to the shell class
  */
 #include "lub/bintree.h"
 #include "lub/list.h"
@@ -41,6 +41,15 @@ typedef struct {
 	lub_bintree_t viewid;
 } clish_shell_pwd_t;
 
+/* Context structure */
+struct clish_context_s {
+	clish_shell_t *shell;
+	const clish_command_t *cmd;
+	clish_pargv_t *pargv;
+	const clish_action_t *action;
+};
+
+/* Shell structure */
 struct clish_shell_s {
 	lub_bintree_t view_tree; /* Tree of views */
 	lub_bintree_t ptype_tree; /* Tree of ptypes */
@@ -144,6 +153,3 @@ void clish_shell__init_pwd(clish_shell_pwd_t *pwd);
 void clish_shell__fini_pwd(clish_shell_pwd_t *pwd);
 int clish_shell_timeout_fn(tinyrl_t *tinyrl);
 int clish_shell_keypress_fn(tinyrl_t *tinyrl, int key);
-
-/* Internal plugin symbols */
-CLISH_PLUGIN_SYM(clish_script);

+ 17 - 0
clish/shell/shell_misc.c

@@ -0,0 +1,17 @@
+/*
+ * shell_misc.c
+ */
+
+#include <stdlib.h>
+#include <assert.h>
+
+#include "private.h"
+
+/*--------------------------------------------------------- */
+const char *clish_shell__get_overview(const clish_shell_t *this)
+{
+	assert(this);
+	return this->overview;
+}
+
+/*--------------------------------------------------------- */

+ 1 - 1
clish/shell/shell_pwd.c

@@ -83,7 +83,7 @@ char *clish_shell__get_pwd_line(const clish_shell_t *this, unsigned int index)
 }
 
 /*--------------------------------------------------------- */
-char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned depth)
+char *clish_shell__get_pwd_full(const clish_shell_t * this, unsigned int depth)
 {
 	char *pwd = NULL;
 	unsigned i;

+ 8 - 1
clish/shell/shell_view.c

@@ -47,10 +47,17 @@ clish_view_t *clish_shell__get_view(const clish_shell_t * this)
 }
 
 /*--------------------------------------------------------- */
-unsigned clish_shell__get_depth(const clish_shell_t * this)
+unsigned int clish_shell__get_depth(const clish_shell_t *this)
 {
 	assert(this);
 	return this->depth;
 }
 
 /*--------------------------------------------------------- */
+void clish_shell__set_depth(clish_shell_t *this, unsigned int depth)
+{
+	assert(this);
+	this->depth = depth;
+}
+
+/*--------------------------------------------------------- */

+ 3 - 0
plugins/default/builtin_init.c

@@ -11,6 +11,9 @@
 /* Initialize internal pseudo-plugin */
 CLISH_PLUGIN_INIT
 {
+	/* Set plugin name */
+	clish_plugin__set_name(plugin, "clish");
+
 	/* Add hooks */
 	clish_plugin_add_phook(plugin, clish_hook_access,
 		"clish_hook_access", CLISH_SYM_TYPE_ACCESS);

+ 8 - 8
plugins/default/hook_config.c

@@ -22,6 +22,7 @@
 
 static int send_request(konf_client_t * client, char *command);
 
+/*--------------------------------------------------------- */
 static unsigned short str2ushort(const char *str)
 {
 	unsigned short num = 0;
@@ -47,9 +48,8 @@ static unsigned short str2ushort(const char *str)
 /*--------------------------------------------------------- */
 CLISH_HOOK_CONFIG(clish_hook_config)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = context->shell;
-	const clish_command_t *cmd = context->cmd;
+	clish_shell_t *this = clish_context__get_shell(clish_context);
+	const clish_command_t *cmd = clish_context__get_cmd(clish_context);
 	clish_config_t *config;
 	char *command = NULL;
 	konf_client_t *client;
@@ -81,7 +81,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 		lub_string_cat(&command, "-s");
 
 		/* Add entered line */
-		tstr = clish_shell__get_line(context);
+		tstr = clish_shell__get_line(clish_context);
 		str = lub_string_encode(tstr, escape_chars);
 		lub_string_free(tstr);
 		lub_string_cat(&command, " -l \"");
@@ -109,7 +109,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 		lub_string_cat(&command, "-d");
 
 		/* Add filename */
-		str = clish_shell_expand(clish_config__get_file(config), SHELL_VAR_ACTION, context);
+		str = clish_shell_expand(clish_config__get_file(config), SHELL_VAR_ACTION, clish_context);
 		if (str) {
 			lub_string_cat(&command, " -f \"");
 			if (str[0] != '\0')
@@ -127,7 +127,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 
 	/* Add pattern */
 	if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
-		tstr = clish_shell_expand(clish_config__get_pattern(config), SHELL_VAR_REGEX, context);
+		tstr = clish_shell_expand(clish_config__get_pattern(config), SHELL_VAR_REGEX, clish_context);
 		if (!tstr) {
 			lub_string_free(command);
 			return BOOL_FALSE;
@@ -150,7 +150,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 
 	/* Add sequence */
 	if (clish_config__get_seq(config)) {
-		str = clish_shell_expand(clish_config__get_seq(config), SHELL_VAR_ACTION, context);
+		str = clish_shell_expand(clish_config__get_seq(config), SHELL_VAR_ACTION, clish_context);
 		snprintf(tmp, sizeof(tmp) - 1, " -q %u", str2ushort(str));
 		tmp[sizeof(tmp) - 1] = '\0';
 		lub_string_cat(&command, tmp);
@@ -159,7 +159,7 @@ CLISH_HOOK_CONFIG(clish_hook_config)
 
 	/* Add pwd */
 	if (clish_config__get_depth(config)) {
-		str = clish_shell_expand(clish_config__get_depth(config), SHELL_VAR_ACTION, context);
+		str = clish_shell_expand(clish_config__get_depth(config), SHELL_VAR_ACTION, clish_context);
 		num = str2ushort(str);
 		lub_string_free(str);
 	} else {

+ 1 - 2
plugins/default/hook_log.c

@@ -17,8 +17,7 @@
 /*--------------------------------------------------------- */
 CLISH_HOOK_LOG(clish_hook_log)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = context->shell;
+	clish_shell_t *this = clish_context__get_shell(clish_context);
 	struct passwd *user = NULL;
 	char *uname = "unknown";
 

+ 0 - 1
plugins/default/private.h

@@ -4,7 +4,6 @@
 
 #include "clish/plugin.h"
 #include "clish/shell.h"
-#include "clish/shell/private.h"
 
 /* Hooks */
 CLISH_HOOK_ACCESS(clish_hook_access);

+ 20 - 25
plugins/default/sym_misc.c

@@ -21,11 +21,8 @@
 /* Terminate the current shell session */
 CLISH_PLUGIN_SYM(clish_close)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = (clish_shell_t *)context->shell;
-
-	this->state = SHELL_STATE_CLOSING;
-
+	clish_shell_t *this = clish_context__get_shell(clish_context);
+	clish_shell__set_state(this, SHELL_STATE_CLOSING);
 	return 0;
 }
 
@@ -43,7 +40,7 @@ static int clish_source_internal(clish_context_t *context,
 	struct stat fileStat;
 
 	/* the exception proves the rule... */
-	clish_shell_t *this = (clish_shell_t *)context->shell;
+	clish_shell_t *this = clish_context__get_shell(context);
 
 	/*
 	 * Check file specified is not a directory 
@@ -92,20 +89,18 @@ CLISH_PLUGIN_SYM(clish_source_nostop)
 */
 CLISH_PLUGIN_SYM(clish_overview)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = context->shell;
-
-	tinyrl_printf(this->tinyrl, "%s\n", context->shell->overview);
-
+	clish_shell_t *this = clish_context__get_shell(clish_context);
+	tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
+	tinyrl_printf(tinyrl, "%s\n", clish_shell__get_overview(this));
 	return 0;
 }
 
 /*----------------------------------------------------------- */
 CLISH_PLUGIN_SYM(clish_history)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = context->shell;
-	tinyrl_history_t *history = tinyrl__get_history(this->tinyrl);
+	clish_shell_t *this = clish_context__get_shell(clish_context);
+	tinyrl_t *tinyrl = clish_shell__get_tinyrl(this);
+	tinyrl_history_t *history = tinyrl__get_history(tinyrl);
 	tinyrl_history_iterator_t iter;
 	const tinyrl_history_entry_t *entry;
 	unsigned limit = 0;
@@ -124,7 +119,7 @@ CLISH_PLUGIN_SYM(clish_history)
 	for (entry = tinyrl_history_getfirst(history, &iter);
 		entry; entry = tinyrl_history_getnext(&iter)) {
 		/* dump the details of this entry */
-		tinyrl_printf(this->tinyrl,
+		tinyrl_printf(tinyrl,
 			"%5d  %s\n",
 			tinyrl_history_entry__get_index(entry),
 			tinyrl_history_entry__get_line(entry));
@@ -138,17 +133,18 @@ CLISH_PLUGIN_SYM(clish_history)
  */
 CLISH_PLUGIN_SYM(clish_nested_up)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = context->shell;
+	clish_shell_t *this = clish_context__get_shell(clish_context);
+	unsigned int depth;
+
 	if (!this)
 		return -1;
-
 	/* If depth=0 than exit */
-	if (0 == this->depth) {
-		this->state = SHELL_STATE_CLOSING;
+	if ((depth = clish_shell__get_depth(this)) == 0) {
+		clish_shell__set_state(this, SHELL_STATE_CLOSING);
 		return 0;
 	}
-	this->depth--;
+	depth--;
+	clish_shell__set_depth(this, depth);
 
 	return 0;
 }
@@ -168,17 +164,16 @@ CLISH_PLUGIN_SYM(clish_nop)
  */
 CLISH_PLUGIN_SYM(clish_wdog)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
 	const char *arg = script;
-	clish_shell_t *this = context->shell;
+	clish_shell_t *this = clish_context__get_shell(clish_context);
 
 	/* Turn off watchdog if no args */
 	if (!arg || ('\0' == *arg)) {
-		this->wdog_timeout = 0;
+		clish_shell__set_wdog_timeout(this, 0);
 		return 0;
 	}
 
-	this->wdog_timeout = (unsigned int)atoi(arg);
+	clish_shell__set_wdog_timeout(this, (unsigned int)atoi(arg));
 
 	return 0;
 }

+ 3 - 4
plugins/default/sym_script.c

@@ -23,10 +23,9 @@
 /*--------------------------------------------------------- */
 CLISH_PLUGIN_SYM(clish_script)
 {
-	clish_context_t *context = (clish_context_t *)clish_context;
-	clish_shell_t *this = context->shell;
-	const clish_action_t *action = context->action;
-	const char * shebang = NULL;
+	clish_shell_t *this = clish_context__get_shell(clish_context);
+	const clish_action_t *action = clish_context__get_action(clish_context);
+	const char *shebang = NULL;
 	pid_t cpid = -1;
 	int res;
 	const char *fifo_name;