Browse Source

Workaround for escaped user typed symbols

git-svn-id: https://klish.googlecode.com/svn/trunk@453 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
150c317cb5
5 changed files with 29 additions and 25 deletions
  1. 1 2
      clish/callback_config.c
  2. 2 3
      clish/shell.h
  3. 1 2
      clish/shell/shell_execute.c
  4. 24 17
      clish/shell/shell_var.c
  5. 1 1
      lub/string/string_escape.c

+ 1 - 2
clish/callback_config.c

@@ -49,7 +49,6 @@ bool_t clish_config_callback(clish_context_t *context)
 {
 	clish_shell_t *this = context->shell;
 	const clish_command_t *cmd = context->cmd;
-	clish_pargv_t *pargv = context->pargv;
 	clish_config_t *config;
 	char *command = NULL;
 	konf_client_t *client;
@@ -79,7 +78,7 @@ bool_t clish_config_callback(clish_context_t *context)
 		lub_string_cat(&command, "-s");
 
 		/* Add entered line */
-		str = clish_shell__get_line(cmd, pargv);
+		str = clish_shell__get_line(context);
 		lub_string_cat(&command, " -l \"");
 		lub_string_cat(&command, str);
 		lub_string_cat(&command, "\"");

+ 2 - 3
clish/shell.h

@@ -359,7 +359,6 @@ clish_view_t *clish_shell__get_pwd_view(const clish_shell_t * instance,
 				 unsigned index);
 char *clish_shell__get_pwd_viewid(const clish_shell_t * instance,
 				 unsigned index);
-char *clish_shell__get_line(const clish_command_t * cmd, clish_pargv_t * pargv);
 konf_client_t *clish_shell__get_client(const clish_shell_t * instance);
 FILE *clish_shell__get_istream(const clish_shell_t * instance);
 FILE *clish_shell__get_ostream(const clish_shell_t * instance);
@@ -385,8 +384,8 @@ void clish_shell__set_interactive(clish_shell_t * instance, bool_t interactive);
 bool_t clish_shell__get_interactive(const clish_shell_t * instance);
 bool_t clish_shell__get_utf8(const clish_shell_t * instance);
 void clish_shell__set_utf8(clish_shell_t * instance, bool_t utf8);
-char *clish_shell__get_line(const clish_command_t * cmd, clish_pargv_t * pargv);
-char *clish_shell__get_params(const clish_command_t * cmd, clish_pargv_t * pargv);
+char *clish_shell__get_line(clish_context_t *context);
+char *clish_shell__get_params(clish_context_t *context);
 
 _END_C_DECL
 

+ 1 - 2
clish/shell/shell_execute.c

@@ -182,7 +182,6 @@ bool_t clish_shell_execute(clish_context_t *context, char **out)
 {
 	clish_shell_t *this = context->shell;
 	const clish_command_t *cmd = context->cmd;
-	clish_pargv_t *pargv = context->pargv;
 	clish_action_t *action;
 	bool_t result = BOOL_TRUE;
 	char *lock_path = clish_shell__get_lockfile(this);
@@ -300,7 +299,7 @@ bool_t clish_shell_execute(clish_context_t *context, char **out)
 			clish_command__get_viewid(cmd), SHELL_VAR_ACTION, context);
 		if (view) {
 			/* Save the current config PWD */
-			char *line = clish_shell__get_line(cmd, pargv);
+			char *line = clish_shell__get_line(context);
 			clish_shell__set_pwd(this,
 				clish_command__get_depth(cmd),
 				line, this->view, this->viewid);

+ 24 - 17
clish/shell/shell_var.c

@@ -88,10 +88,10 @@ static char *find_context_var(const char *name, clish_context_t *this)
 			clish_command__get_orig(this->cmd)));
 	} else if (!lub_string_nocasecmp(name, "__line")) {
 		if (this->pargv)
-			result = clish_shell__get_line(this->cmd, this->pargv);
+			result = clish_shell__get_line(this);
 	} else if (!lub_string_nocasecmp(name, "__params")) {
 		if (this->pargv)
-			result = clish_shell__get_params(this->cmd, this->pargv);
+			result = clish_shell__get_params(this);
 	} else if (lub_string_nocasestr(name, "__prefix") == name) {
 		int idx = 0;
 		int pnum = 0;
@@ -256,8 +256,9 @@ char *clish_shell_expand(const char *str, clish_shell_var_t vtype, void *context
 }
 
 /*--------------------------------------------------------- */
-char *clish_shell__get_params(const clish_command_t *cmd, clish_pargv_t *pargv)
+char *clish_shell__get_params(clish_context_t *context)
 {
+	clish_pargv_t *pargv = context->pargv;
 	char *line = NULL;
 	unsigned i, cnt;
 	const clish_param_t *param;
@@ -268,29 +269,22 @@ char *clish_shell__get_params(const clish_command_t *cmd, clish_pargv_t *pargv)
 
 	cnt = clish_pargv__get_count(pargv);
 	for (i = 0; i < cnt; i++) {
-		const char *tmp;
-		char *space = NULL;
 		param = clish_pargv__get_param(pargv, i);
 		if (clish_param__get_hidden(param))
 			continue;
 		parg = clish_pargv__get_parg(pargv, i);
-		tmp = clish_parg__get_value(parg);
-		space = strchr(tmp, ' ');
-		if (line)
-			lub_string_cat(&line, " ");
-		if (space)
-			lub_string_cat(&line, "\\\"");
-		lub_string_cat(&line, tmp);
-		if (space)
-			lub_string_cat(&line, "\\\"");
+		line = clish_shell_expand_var(clish_parg__get_name(parg),
+			SHELL_VAR_ACTION, context);
 	}
 
 	return line;
 }
 
 /*--------------------------------------------------------- */
-char *clish_shell__get_line(const clish_command_t *cmd, clish_pargv_t *pargv)
+char *clish_shell__get_line(clish_context_t *context)
 {
+	const clish_command_t *cmd = context->cmd;
+	clish_pargv_t *pargv = context->pargv;
 	char *line = NULL;
 	char *params = NULL;
 
@@ -300,7 +294,7 @@ char *clish_shell__get_line(const clish_command_t *cmd, clish_pargv_t *pargv)
 	if (!pargv)
 		return line;
 
-	params = clish_shell__get_params(cmd, pargv);
+	params = clish_shell__get_params(context);
 	if (params) {
 		lub_string_cat(&line, " ");
 		lub_string_cat(&line, params);
@@ -333,8 +327,21 @@ char *clish_shell_expand_var(const char *name, clish_shell_var_t vtype, void *co
 	if (pargv) {
 		const clish_parg_t *parg = clish_pargv_find_arg(pargv, name);
 		/* substitute the command line value */
-		if (parg)
+		if (parg) {
+			char *space = NULL;
 			tmp = clish_parg__get_value(parg);
+			space = strchr(tmp, ' ');
+			if (space) {
+				char *q = NULL;
+				char *tstr;
+				tstr = lub_string_encode(tmp, "\"\\");
+				lub_string_cat(&q, "\"");
+				lub_string_cat(&q, tstr);
+				lub_string_free(tstr);
+				lub_string_cat(&q, "\"");
+				tmp = string = q;
+			}
+		}
 	}
 	/* try and substitute the param's default */
 	if (!tmp && cmd)

+ 1 - 1
lub/string/string_escape.c

@@ -6,7 +6,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-const char *lub_string_esc_default = "`|$<>&()#;\\\"";
+const char *lub_string_esc_default = "`|$<>&()#;\"\\";
 const char *lub_string_esc_regex = "^$.*+[](){}";
 
 /*--------------------------------------------------------- */