1 # -*- coding: utf-8 -*-
2 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 # See https://llvm.org/LICENSE.txt for license information.
4 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 """ This module implements basic shell escaping/unescaping methods. """
10 __all__
= ["encode", "decode"]
14 """Takes a command as list and returns a string."""
16 def needs_quote(word
):
17 """Returns true if arguments needs to be protected by quotes.
19 Previous implementation was shlex.split method, but that's not good
20 for this job. Currently is running through the string with a basic
44 if state
== 0 and current
in reserved
:
46 elif state
== 0 and current
== "\\":
48 elif state
== 1 and current
in reserved |
{"\\"}:
50 elif state
== 0 and current
== '"':
52 elif state
== 2 and current
== '"':
54 elif state
== 0 and current
== "'":
56 elif state
== 3 and current
== "'":
61 """Do protect argument if that's needed."""
63 table
= {"\\": "\\\\", '"': '\\"'}
64 escaped
= "".join([table
.get(c
, c
) for c
in word
])
66 return '"' + escaped
+ '"' if needs_quote(word
) else escaped
68 return " ".join([escape(arg
) for arg
in command
])
72 """Takes a command string and returns as a list."""
75 """Gets rid of the escaping characters."""
77 if len(arg
) >= 2 and arg
[0] == arg
[-1] and arg
[0] == '"':
79 return re
.sub(r
'\\(["\\])', r
"\1", arg
)
80 return re
.sub(r
"\\([\\ $%&\(\)\[\]\{\}\*|<>@?!])", r
"\1", arg
)
82 return [unescape(arg
) for arg
in shlex
.split(string
)]