Laszlo Nagy | bc68758 | 2016-01-12 22:38:41 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # The LLVM Compiler Infrastructure |
| 3 | # |
| 4 | # This file is distributed under the University of Illinois Open Source |
| 5 | # License. See LICENSE.TXT for details. |
| 6 | """ This module implements basic shell escaping/unescaping methods. """ |
| 7 | |
| 8 | import re |
| 9 | import shlex |
| 10 | |
| 11 | __all__ = ['encode', 'decode'] |
| 12 | |
| 13 | |
| 14 | def encode(command): |
| 15 | """ Takes a command as list and returns a string. """ |
| 16 | |
| 17 | def needs_quote(word): |
| 18 | """ Returns true if arguments needs to be protected by quotes. |
| 19 | |
| 20 | Previous implementation was shlex.split method, but that's not good |
| 21 | for this job. Currently is running through the string with a basic |
| 22 | state checking. """ |
| 23 | |
| 24 | reserved = {' ', '$', '%', '&', '(', ')', '[', ']', '{', '}', '*', '|', |
| 25 | '<', '>', '@', '?', '!'} |
| 26 | state = 0 |
| 27 | for current in word: |
| 28 | if state == 0 and current in reserved: |
| 29 | return True |
| 30 | elif state == 0 and current == '\\': |
| 31 | state = 1 |
| 32 | elif state == 1 and current in reserved | {'\\'}: |
| 33 | state = 0 |
| 34 | elif state == 0 and current == '"': |
| 35 | state = 2 |
| 36 | elif state == 2 and current == '"': |
| 37 | state = 0 |
| 38 | elif state == 0 and current == "'": |
| 39 | state = 3 |
| 40 | elif state == 3 and current == "'": |
| 41 | state = 0 |
| 42 | return state != 0 |
| 43 | |
| 44 | def escape(word): |
| 45 | """ Do protect argument if that's needed. """ |
| 46 | |
| 47 | table = {'\\': '\\\\', '"': '\\"'} |
| 48 | escaped = ''.join([table.get(c, c) for c in word]) |
| 49 | |
| 50 | return '"' + escaped + '"' if needs_quote(word) else escaped |
| 51 | |
| 52 | return " ".join([escape(arg) for arg in command]) |
| 53 | |
| 54 | |
| 55 | def decode(string): |
| 56 | """ Takes a command string and returns as a list. """ |
| 57 | |
| 58 | def unescape(arg): |
| 59 | """ Gets rid of the escaping characters. """ |
| 60 | |
| 61 | if len(arg) >= 2 and arg[0] == arg[-1] and arg[0] == '"': |
| 62 | arg = arg[1:-1] |
| 63 | return re.sub(r'\\(["\\])', r'\1', arg) |
| 64 | return re.sub(r'\\([\\ $%&\(\)\[\]\{\}\*|<>@?!])', r'\1', arg) |
| 65 | |
| 66 | return [unescape(arg) for arg in shlex.split(string)] |