Browse Source

Use API for context

Serj Kalichev 11 years ago
parent
commit
c3002dbecd
3 changed files with 19 additions and 8 deletions
  1. 1 0
      clish/shell.h
  2. 12 2
      clish/shell/context.c
  3. 6 6
      clish/shell/shell_wdog.c

+ 1 - 0
clish/shell.h

@@ -35,6 +35,7 @@ typedef struct clish_context_s clish_context_t;
 /* Context functions */
 _BEGIN_C_DECL
 clish_context_t *clish_context_new(clish_shell_t *shell);
+int clish_context_init(clish_context_t *instance, 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);

+ 12 - 2
clish/shell/context.c

@@ -8,6 +8,17 @@
 
 #include "private.h"
 
+/*--------------------------------------------------------- */
+int clish_context_init(clish_context_t *this, clish_shell_t *shell)
+{
+	if (!this)
+		return -1;
+	memset(this, 0, sizeof(*this));
+	this->shell = shell;
+
+	return 0;
+}
+
 /*--------------------------------------------------------- */
 clish_context_t *clish_context_new(clish_shell_t *shell)
 {
@@ -17,8 +28,7 @@ clish_context_t *clish_context_new(clish_shell_t *shell)
 		return NULL;
 	if (!(this = malloc(sizeof(*this))))
 		return NULL;
-	memset(this, 0, sizeof(*this));
-	this->shell = shell;
+	clish_context_init(this, shell);
 
 	return this;
 }

+ 6 - 6
clish/shell/shell_wdog.c

@@ -11,7 +11,7 @@
 int clish_shell_timeout_fn(tinyrl_t *tinyrl)
 {
 	clish_context_t *context = tinyrl__get_context(tinyrl);
-	clish_shell_t *this = context->shell;
+	clish_shell_t *this = clish_context__get_shell(context);
 
 	/* Idle timeout */
 	if (!this->wdog_active) {
@@ -33,7 +33,7 @@ int clish_shell_timeout_fn(tinyrl_t *tinyrl)
 int clish_shell_keypress_fn(tinyrl_t *tinyrl, int key)
 {
 	clish_context_t *context = tinyrl__get_context(tinyrl);
-	clish_shell_t *this = context->shell;
+	clish_shell_t *this = clish_context__get_shell(context);
 
 	if (this->wdog_active) {
 		this->wdog_active = BOOL_FALSE;
@@ -51,10 +51,10 @@ int clish_shell_wdog(clish_shell_t *this)
 	assert(this->wdog);
 
 	/* Prepare context */
-	context.shell = this;
-	context.cmd = this->wdog;
-	context.action = clish_command__get_action(this->wdog);
-	context.pargv = NULL;
+	clish_context_init(&context, this);
+	clish_context__set_cmd(&context, this->wdog);
+	clish_context__set_action(&context,
+		clish_command__get_action(this->wdog));
 
 	/* Call watchdog script */
 	return clish_shell_execute(&context, NULL);