updated: [161164c] Reworked shell_unescape function in order to make use of glib.

Patrick Winnertz winnie at debian.org
Tue Feb 10 11:49:57 UTC 2009


The following commit has been merged in the master branch:
commit 161164c55640a6e034c8e00ebd1fb504e5357c1d
Author: Patrick Winnertz <winnie at debian.org>
Date:   Fri Feb 6 14:50:22 2009 +0100

    Reworked shell_unescape function in order to make use of glib.
    
    This commit will replace some *ptr = c; stuff with g_string_append_c(str,c);
    in order to use glib here.
    
    Signed-off-by: Patrick Winnertz <winnie at debian.org>

diff --git a/src/util.c b/src/util.c
index 96396c0..2046f10 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1562,7 +1562,7 @@ shell_escape(const char* src)
 		return strdup("");
 
 	str = g_string_new("");
-
+	
 	/* look for the first char to escape */
 	while (1)
 	{
@@ -1594,21 +1594,28 @@ shell_escape(const char* src)
  string for unescaping
 
  \returns
- return unescaped string
+ return unescaped string (which needs to be freed)
  */
 char*
-shell_unescape(char* text)
+shell_unescape(const char* text)
 {
+	GString *str;
+	char *result = NULL;
+	
 	if (!text)
 		return NULL;
 
+
 	/* look for the first \ - that's quick skipover if there's nothing to escape */
-	char* readptr = text;
+	const char* readptr = text;
 	while ((*readptr) && ((*readptr)!='\\'))	readptr++;
-	if (!(*readptr)) return text;
+	if (!(*readptr)) {
+		result = g_strdup(text);
+		return result;
+	}
+	str = g_string_new("");
 
 	/* if we're here, we're standing on the first '\' */
-	char* writeptr = readptr;
 	char c;
 	while ((c = *readptr))
 	{
@@ -1617,9 +1624,9 @@ shell_unescape(char* text)
 			readptr++;
 			switch ((c = *readptr))
 			{
-				case 'n':	(*writeptr) = '\n'; writeptr++;	break;
-				case 'r':	(*writeptr) = '\r'; writeptr++;	break;
-				case 't':	(*writeptr) = '\t'; writeptr++;	break;
+				case 'n':	g_string_append_c(str,'\n');	break;
+				case 'r':	g_string_append_c(str,'\r');	break;
+				case 't':	g_string_append_c(str,'\t');	break;
 
 				case ' ':
 				case '\\':
@@ -1644,20 +1651,21 @@ shell_unescape(char* text)
 					case '\0': /* end of string! malformed escape string */
 						goto out;
 				default:
-					(*writeptr) = c; writeptr++; break;
+					g_string_append_c(str,c); break;
 			}
 		}
 		else	/* got a normal character */
 		{
-			(*writeptr) = *readptr;
-			writeptr++;
+			g_string_append_c(str,c);
 		}
 		readptr++;
 	}
 out:
-		*writeptr = 0;
+	g_string_append_c(str,'\0');
 
-    return text;
+	result = str->str;
+	g_string_free(str,FALSE);
+    return result;
 }
 
 /** Check if char in pointer contain escape'd chars
diff --git a/src/util.h b/src/util.h
index e5daccb..1ac88dd 100644
--- a/src/util.h
+++ b/src/util.h
@@ -259,7 +259,7 @@ const char *Q_ (const char *s);
 
 
 gboolean shell_is_char_escaped ( const char * );
-char *shell_unescape( char * );
+char *shell_unescape( const char * );
 char *shell_escape( const char * );
 
 #define str_dup_range(s_start, s_bound) (g_strndup(s_start, s_bound - s_start))

-- 
Midnight Commander Development



More information about the mc-devel mailing list