Browse Source

Beta version of escaping

git-svn-id: https://klish.googlecode.com/svn/trunk@456 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
9bfb97d2c9
3 changed files with 66 additions and 29 deletions
  1. 9 3
      clish/callback_config.c
  2. 55 24
      clish/shell/shell_var.c
  3. 2 2
      lub/string/string_escape.c

+ 9 - 3
clish/callback_config.c

@@ -54,9 +54,11 @@ bool_t clish_config_callback(clish_context_t *context)
 	konf_client_t *client;
 	konf_buf_t *buf = NULL;
 	char *str = NULL;
+	char *tstr;
 	char tmp[PATH_MAX + 100];
 	clish_config_op_t op;
 	unsigned int num;
+	const char *escape_chars = lub_string_esc_quoted;
 
 	if (!this)
 		return BOOL_TRUE;
@@ -78,7 +80,9 @@ bool_t clish_config_callback(clish_context_t *context)
 		lub_string_cat(&command, "-s");
 
 		/* Add entered line */
-		str = clish_shell__get_line(context);
+		tstr = clish_shell__get_line(context);
+		str = lub_string_encode(tstr, escape_chars);
+		lub_string_free(tstr);
 		lub_string_cat(&command, " -l \"");
 		lub_string_cat(&command, str);
 		lub_string_cat(&command, "\"");
@@ -122,11 +126,13 @@ bool_t clish_config_callback(clish_context_t *context)
 
 	/* Add pattern */
 	if ((CLISH_CONFIG_SET == op) || (CLISH_CONFIG_UNSET == op)) {
-		str = clish_shell_expand(clish_config__get_pattern(config), SHELL_VAR_REGEX, context);
-		if (!str) {
+		tstr = clish_shell_expand(clish_config__get_pattern(config), SHELL_VAR_REGEX, context);
+		if (!tstr) {
 			lub_string_free(command);
 			return BOOL_FALSE;
 		}
+		str = lub_string_encode(tstr, escape_chars);
+		lub_string_free(tstr);
 		lub_string_cat(&command, " -r \"");
 		lub_string_cat(&command, str);
 		lub_string_cat(&command, "\"");

+ 55 - 24
clish/shell/shell_var.c

@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <assert.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "lub/string.h"
 #include "private.h"
@@ -201,37 +202,59 @@ static char *expand_nextsegment(const char **string, const char *escape_chars,
 			 */
 			for (q = strtok_r(text, ":", &saveptr);
 				q; q = strtok_r(NULL, ":", &saveptr)) {
-				char *var = clish_shell_expand_var(q, this);
+				char *var;
+				int mod_quote = 0; /* quoute modifier */
+				int mod_esc = 0; /* escape modifier */
+				char *space;
+
+				/* Search for modifiers */
+				while (*q && !isalpha(*q)) {
+					if ('#' == *q) {
+						mod_quote = 1;
+						mod_esc = 1;
+					} else if ('\\' == *q)
+						mod_esc = 1;
+					else
+						break;
+					q++;
+				}
+
+				/* Get clean variable value */
+				var = clish_shell_expand_var(q, this);
+				if (!var) {
+					lub_string_cat(&result, q);
+					continue;
+				}
+				valid = BOOL_TRUE;
+
+				/* Quoting */
+				if (mod_quote)
+					space = strchr(var, ' ');
+				if (mod_quote && space)
+					lub_string_cat(&result, "\"");
+
+				/* Internal escaping */
+				if (mod_esc) {
+					char *tstr = lub_string_encode(var,
+						lub_string_esc_quoted);
+					lub_string_free(var);
+					var = tstr;
+				}
 
 				/* Escape special chars */
-				if (var && escape_chars) {
+				if (escape_chars) {
 					char *tstr = lub_string_encode(var, escape_chars);
 					lub_string_free(var);
 					var = tstr;
 				}
-		/* substitute the command line value */
-/*		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_esc_quoted);
-				lub_string_cat(&q, "\"");
-				lub_string_cat(&q, tstr);
-				lub_string_free(tstr);
-				lub_string_cat(&q, "\"");
-				tmp = string = q;
-			}
-		}
-*/
+
 				/* copy the expansion or the raw word */
-				lub_string_cat(&result, var ? var : q);
+				lub_string_cat(&result, var);
+
+				/* Quoting */
+				if (mod_quote && space)
+					lub_string_cat(&result, "\"");
 
-				/* record any expansions */
-				if (var)
-					valid = BOOL_TRUE;
 				lub_string_free(var);
 			}
 
@@ -303,6 +326,7 @@ char *clish_shell__get_params(clish_context_t *context)
 	unsigned i, cnt;
 	const clish_param_t *param;
 	const clish_parg_t *parg;
+	char *request = NULL;
 
 	if (!pargv)
 		return NULL;
@@ -313,9 +337,16 @@ char *clish_shell__get_params(clish_context_t *context)
 		if (clish_param__get_hidden(param))
 			continue;
 		parg = clish_pargv__get_parg(pargv, i);
-		line = clish_shell_expand_var(clish_parg__get_name(parg), context);
+		if (request)
+			lub_string_cat(&request, " ");
+		lub_string_cat(&request, "${#");
+		lub_string_cat(&request, clish_parg__get_name(parg));
+		lub_string_cat(&request, "}");
 	}
 
+	line = clish_shell_expand(request, SHELL_VAR_NONE, context);
+	lub_string_free(request);
+
 	return line;
 }
 

+ 2 - 2
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 = "^$.*+[](){}";
 const char *lub_string_esc_quoted = "\\\"";
 
@@ -70,7 +70,7 @@ char *lub_string_encode(const char *string, const char *escape_chars)
 	const char *p;
 
 	if (!escape_chars)
-		escape_chars = lub_string_esc_default;
+		lub_string_dup(string);
 
 	for (p = string; p && *p; p++) {
 		/* find any special characters and prefix them with '\' */