diff --git a/scp.py b/scp.py index dc929de..6cc094a 100644 --- a/scp.py +++ b/scp.py @@ -14,21 +14,25 @@ import types -# this is quote from the shlex module, added in py3.3 -_find_unsafe = re.compile(br'[^\w@%+=:,./~-]').search +safe_shell_chars = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "-+=/:.,%_") def _sh_quote(s): - """Return a shell-escaped version of the string `s`.""" - if not s: - return b"" - if _find_unsafe(s) is None: + r"""Given bl"a, returns "bl\\"a". + """ + if isinstance(s, bytes): + s = s.decode('utf-8') + if not s or any(c not in safe_shell_chars for c in s): + return '"%s"' % (s.replace('\\', '\\\\') + .replace('"', '\\"') + .replace('`', '\\`') + .replace('$', '\\$')) + else: return s - # use single quotes, and put single quotes into double quotes - # the string $'b is then quoted as '$'"'"'b' - return b"'" + s.replace(b"'", b"'\"'\"'") + b"'" - # Unicode conversion functions; assume UTF-8