[kupfer] desktop_parse: Unescape escaped sequences outside quotes



commit 36803c0f117399181d9ba4c88bc9d87a8e2145ae
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Mon Apr 4 16:14:36 2011 +0200

    desktop_parse: Unescape escaped sequences outside quotes
    
    Broken desktop files contain stuff like
    
        Exec=program Hi\\ There
    
    Which is broken, because ``\`` is not allowed when not inside quotes.
    
    Correct::
    
        Exec:program "Hi There"
    
    Not so hard. But we handle the first case too, because ``\`` outside
    quoted arguments is *always* wrong so we can do something about it.

 kupfer/desktop_parse.py |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/kupfer/desktop_parse.py b/kupfer/desktop_parse.py
index bb8f7a2..a3c07bb 100644
--- a/kupfer/desktop_parse.py
+++ b/kupfer/desktop_parse.py
@@ -83,9 +83,16 @@ def quote_scanner(s, reptable):
 		if is_quoted:
 			parts.append(two_part_unescaper(rmquotes(_ps), reptable))
 		elif '\\' in _ps:
+			## Here we handle out-of-spec things
 			warnings.warn(RuntimeWarning("Broken unquoted Exec= %s" % repr(s)))
+			## try to split by whitespace, ignore backslash-escaped spaces
+			## insert NUL instead of '\ ' and then split, then reverse
+			space_escaped = two_part_unescaper(_ps, {r'\ ': '\x00'})
+			space_esc_split = space_escaped.split()
+			ps_split = [x.replace('\x00', ' ') for x in space_esc_split]
 			parts.extend([two_part_unescaper(_ps_part, reptable) for _ps_part
-			              in _ps.split()])
+			              in ps_split])
+			## end out-of spec
 		else:
 			parts.extend(_ps.split())
 
@@ -175,6 +182,14 @@ def parse_argv(instr):
 	The following style is common but unspecified
 	>>> parse_argv('env VAR="is broken" ./program')
 	['env', 'VAR=is broken', './program']
+
+	The following is just completely broken
+	>>> parse_argv('./program unquoted\\\\argument')
+	['./program', 'unquoted\\argument']
+
+	The following is just completely broken
+	>>> parse_argv('./program No\\ Space')
+	['./program', 'No Space']
 	"""
 	return quote_scanner(instr, quoted_table)
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]