[kupfer] commands: Fix commandline quoting to be more faithful



commit a2b3d24a6be63aac0641f6002a4c832ac86e6542
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Wed Mar 2 13:52:52 2011 +0100

    commands: Fix commandline quoting to be more faithful
    
    Use shlex but pass posix=False so that it preserves backslashes etc.
    For example the string "grep -E ^\+" we want to be split into
    ['grep', '-E', '^\+']

 kupfer/plugin/commands.py |   15 +++++++++++++--
 1 files changed, 13 insertions(+), 2 deletions(-)
---
diff --git a/kupfer/plugin/commands.py b/kupfer/plugin/commands.py
index 7df73da..9900dd7 100644
--- a/kupfer/plugin/commands.py
+++ b/kupfer/plugin/commands.py
@@ -27,10 +27,21 @@ def unicode_shlex_split(ustr, **kwargs):
 	s_str = ustr.encode("UTF-8")
 	return [kupferstring.tounicode(t) for t in shlex.split(s_str, **kwargs)]
 
+def rm_quotes(us):
+	"""Remove "quotes" -> quotes if wrapped in " or ' quotes"""
+	if len(us) > 1 and us[0] in ("'", '"') and us[0] == us[-1]:
+		return us[1:-1]
+	return us
+
+def custom_shlex_split(ustr):
+	# Use posix=False so that we don't swallow backslashes "\"
+	# After that we have to remove quotes ourselves
+	return map(rm_quotes, unicode_shlex_split(ustr, posix=False))
+
 def get_commandline_argv(commandline):
-	# use shlex to allow simple quoting
+	""" Use shlex to allow simple quoting in the commandline """
 	try:
-		argv = unicode_shlex_split(commandline)
+		argv = custom_shlex_split(commandline)
 	except ValueError:
 		# Exception raised on unpaired quotation marks
 		argv = commandline.split(None, 1)



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