Browse Source

Move escape_special_chars() to lub_string with the name lub_string_encode().

git-svn-id: https://klish.googlecode.com/svn/trunk@213 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 14 years ago
parent
commit
b42edab2ac
3 changed files with 59 additions and 42 deletions
  1. 1 39
      clish/variable/variable_expand.c
  2. 18 3
      lub/string.h
  3. 40 0
      lub/string/string_escape.c

+ 1 - 39
clish/variable/variable_expand.c

@@ -10,15 +10,6 @@
 
 #include <sys/types.h>
 #include <regex.h>
-/*
- * These are the escape characters which are used by default when 
- * expanding variables. These characters will be backslash escaped
- * to prevent them from being interpreted in a script.
- *
- * This is a security feature to prevent users from arbitarily setting
- * parameters to contain special sequences.
- */
-static const char *default_escape_chars = "`|$<>&()#";
 
 /*----------------------------------------------------------- */
 /*
@@ -103,35 +94,6 @@ static char *find_context_var(const context_t * this, const char *name)
 	return result;
 }
 
-/*----------------------------------------------------------- */
-/*
- * This needs to escape any dangerous characters within the command line
- * to prevent gaining access to the underlying system shell.
- */
-static char *escape_special_chars(const char *string, const char *escape_chars)
-{
-	char *result = NULL;
-	const char *p;
-
-	if (NULL == escape_chars) {
-		escape_chars = default_escape_chars;
-	}
-	for (p = string; p && *p; p++)
-	{
-		/* find any special characters and prefix them with '\' */
-		size_t len = strcspn(p, escape_chars);
-		lub_string_catn(&result, p, len);
-		p += len;
-		if (*p) {
-			lub_string_catn(&result, "\\", 1);
-			lub_string_catn(&result, p, 1);
-		} else {
-			break;
-		}
-	}
-	return result;
-}
-
 /*--------------------------------------------------------- */
 static char *context_retrieve(const context_t * this, const char *name)
 {
@@ -171,7 +133,7 @@ static char *context_retrieve(const context_t * this, const char *name)
 		/* override the escape characters */
 		escape_chars = clish_command__get_escape_chars(this->cmd);
 	}
-	result = escape_special_chars(tmp, escape_chars);
+	result = lub_string_encode(tmp, escape_chars);
 	if (NULL != string) {
 		/* free the dynamic memory */
 		lub_string_free(string);

+ 18 - 3
lub/string.h

@@ -239,18 +239,33 @@ void
 /**
  * This operation decode the escaped string.
  *
- * \pre 
+ * \pre
  * - none
- * 
+ *
  * \return
  * - The allocated string without escapes.
  *
- * \post 
+ * \post
  * - The result string must be freed after using.
  */
 char *
     lub_string_decode(const char *string);
 
+/**
+ * This operation encode the string using escape.
+ *
+ * \pre
+ * - none
+ *
+ * \return
+ * - The allocated string with escapes.
+ *
+ * \post
+ * - The result string must be freed after using.
+ */
+char *
+    lub_string_encode(const char *string, const char *escape_chars);
+
 
  _END_C_DECL
 

+ 40 - 0
lub/string/string_escape.c

@@ -5,6 +5,17 @@
 
 #include <stdlib.h>
 #include <string.h>
+
+/*
+ * These are the escape characters which are used by default when 
+ * expanding variables. These characters will be backslash escaped
+ * to prevent them from being interpreted in a script.
+ *
+ * This is a security feature to prevent users from arbitarily setting
+ * parameters to contain special sequences.
+ */
+static const char *default_escape_chars = "`|$<>&()#";
+
 /*--------------------------------------------------------- */
 char *
 lub_string_decode(const char *string)
@@ -53,4 +64,33 @@ lub_string_decode(const char *string)
     return p;
 }
 
+/*----------------------------------------------------------- */
+/*
+ * This needs to escape any dangerous characters within the command line
+ * to prevent gaining access to the underlying system shell.
+ */
+char *lub_string_encode(const char *string, const char *escape_chars)
+{
+	char *result = NULL;
+	const char *p;
+
+	if (NULL == escape_chars) {
+		escape_chars = default_escape_chars;
+	}
+	for (p = string; p && *p; p++)
+	{
+		/* find any special characters and prefix them with '\' */
+		size_t len = strcspn(p, escape_chars);
+		lub_string_catn(&result, p, len);
+		p += len;
+		if (*p) {
+			lub_string_catn(&result, "\\", 1);
+			lub_string_catn(&result, p, 1);
+		} else {
+			break;
+		}
+	}
+	return result;
+}
+
 /*--------------------------------------------------------- */