Browse Source

Use context API

Serj Kalichev 11 years ago
parent
commit
357c1f223d

+ 2 - 2
clish/shell.h

@@ -38,11 +38,11 @@ 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);
+void clish_context__set_cmd(void *instance, const 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);
+void clish_context__set_action(void *instance, const clish_action_t *action);
 const clish_action_t *clish_context__get_action(const void *instance);
 _END_C_DECL
 

+ 2 - 2
clish/shell/context.c

@@ -52,7 +52,7 @@ clish_shell_t *clish_context__get_shell(const void *this)
 }
 
 /*--------------------------------------------------------- */
-void clish_context__set_cmd(void *this, clish_command_t *cmd)
+void clish_context__set_cmd(void *this, const clish_command_t *cmd)
 {
 	clish_context_t *context = (clish_context_t *)this;
 	assert(context);
@@ -82,7 +82,7 @@ clish_pargv_t *clish_context__get_pargv(const void *this)
 }
 
 /*--------------------------------------------------------- */
-void clish_context__set_action(void *this, clish_action_t *action)
+void clish_context__set_action(void *this, const clish_action_t *action)
 {
 	clish_context_t *context = (clish_context_t *)this;
 	assert(context);

+ 3 - 4
clish/shell/shell_command.c

@@ -97,10 +97,9 @@ void clish_shell_param_generator(clish_shell_t *this, lub_argv_t *matches,
 
 		/* Parse command line to get completion pargv's */
 		/* Prepare context */
-		context.shell = this;
-		context.cmd = cmd;
-		context.action = NULL;
-		context.pargv = pargv;
+		clish_context_init(&context, this);
+		clish_context__set_cmd(&context, cmd);
+		clish_context__set_pargv(&context, pargv);
 
 		clish_shell_parse_pargv(pargv, cmd, &context,
 			clish_command__get_paramv(cmd),

+ 3 - 4
clish/shell/shell_help.c

@@ -65,10 +65,9 @@ static int available_params(clish_shell_t *this,
 	pargv = clish_pargv_new();
 
 	/* Prepare context */
-	context.shell = this;
-	context.cmd = cmd;
-	context.action = NULL;
-	context.pargv = pargv;
+	clish_context_init(&context, this);
+	clish_context__set_cmd(&context, cmd);
+	clish_context__set_pargv(&context, pargv);
 
 	status = clish_shell_parse_pargv(pargv, cmd, &context,
 		clish_command__get_paramv(cmd),

+ 3 - 4
clish/shell/shell_parse.c

@@ -27,10 +27,9 @@ clish_pargv_status_t clish_shell_parse(
 	/* Now construct the parameters for the command */
 	/* Prepare context */
 	*pargv = clish_pargv_new();
-	context.shell = this;
-	context.cmd = cmd;
-	context.action = NULL;
-	context.pargv = *pargv;
+	clish_context_init(&context, this);
+	clish_context__set_cmd(&context, cmd);
+	clish_context__set_pargv(&context, *pargv);
 
 	idx = lub_argv_wordcount(clish_command__get_name(cmd));
 	argv = lub_argv_new(line, 0);

+ 13 - 11
clish/shell/shell_var.c

@@ -140,7 +140,7 @@ static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *co
 	/* Try to execute ACTION */
 	if (!res) {
 		char *out = NULL;
-		context->action = clish_var__get_action(var);
+		clish_context__set_action(context, clish_var__get_action(var));
 		if (clish_shell_exec_action(context, &out)) {
 			lub_string_free(out);
 			return NULL;
@@ -158,16 +158,18 @@ static char *find_var(const char *name, lub_bintree_t *tree, clish_context_t *co
 /*--------------------------------------------------------- */
 static char *find_global_var(const char *name, clish_context_t *context)
 {
-	return find_var(name, &context->shell->var_tree, context);
+	clish_shell_t *shell = clish_context__get_shell(context);
+	return find_var(name, &shell->var_tree, context);
 }
 
 /*--------------------------------------------------------- */
 static char *find_viewid_var(const char *name, clish_context_t *context)
 {
-	int depth = clish_shell__get_depth(context->shell);
+	clish_shell_t *shell = clish_context__get_shell(context);
+	int depth = clish_shell__get_depth(shell);
 	if (depth < 0)
 		return NULL;
-	return find_var(name, &context->shell->pwdv[depth]->viewid, context);
+	return find_var(name, &shell->pwdv[depth]->viewid, context);
 }
 
 static char * chardiff(const char *syms, const char *minus)
@@ -342,7 +344,7 @@ char *clish_shell_expand(const char *str, clish_shell_var_t vtype, clish_context
 {
 	char *seg, *result = NULL;
 	const char *escape_chars = NULL;
-	const clish_command_t *cmd = context->cmd;
+	const clish_command_t *cmd = clish_context__get_cmd(context);
 
 	/* Escape special characters */
 	if (SHELL_VAR_REGEX == vtype) {
@@ -369,7 +371,7 @@ char *clish_shell_expand(const char *str, clish_shell_var_t vtype, clish_context
 /*--------------------------------------------------------- */
 char *clish_shell__get_params(clish_context_t *context)
 {
-	clish_pargv_t *pargv = context->pargv;
+	clish_pargv_t *pargv = clish_context__get_pargv(context);
 	char *line = NULL;
 	unsigned i, cnt;
 	const clish_param_t *param;
@@ -401,8 +403,8 @@ char *clish_shell__get_params(clish_context_t *context)
 /*--------------------------------------------------------- */
 static char *internal_get_line(clish_context_t *context, int cmd_type)
 {
-	const clish_command_t *cmd = context->cmd;
-	clish_pargv_t *pargv = context->pargv;
+	const clish_command_t *cmd = clish_context__get_cmd(context);
+	clish_pargv_t *pargv = clish_context__get_pargv(context);
 	char *line = NULL;
 	char *params = NULL;
 
@@ -449,9 +451,9 @@ char *clish_shell_expand_var(const char *name, clish_context_t *context)
 	assert(name);
 	if (!context)
 		return NULL;
-	this = context->shell;
-	cmd = context->cmd;
-	pargv = context->pargv;
+	this = clish_context__get_shell(context);
+	cmd = clish_context__get_cmd(context);
+	pargv = clish_context__get_pargv(context);
 
 	/* try and substitute a parameter value */
 	if (pargv) {