diff options
Diffstat (limited to 'tools/mc_command.py')
| -rw-r--r-- | tools/mc_command.py | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/tools/mc_command.py b/tools/mc_command.py index 79e6e459c..0a7119e6e 100644 --- a/tools/mc_command.py +++ b/tools/mc_command.py | |||
| @@ -16,6 +16,44 @@ import shutil | |||
| 16 | import subprocess | 16 | import subprocess |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | def split_windows_command(raw: str) -> list[str]: | ||
| 20 | """Decode an MSVC-style argument list, rejecting unmatched quotes.""" | ||
| 21 | args = [] | ||
| 22 | pos = 0 | ||
| 23 | while pos < len(raw): | ||
| 24 | if raw[pos] in " \t": | ||
| 25 | pos += 1 | ||
| 26 | continue | ||
| 27 | arg = [] | ||
| 28 | quoted = False | ||
| 29 | while pos < len(raw) and (quoted or raw[pos] not in " \t"): | ||
| 30 | start = pos | ||
| 31 | while pos < len(raw) and raw[pos] == "\\": | ||
| 32 | pos += 1 | ||
| 33 | backslashes = pos - start | ||
| 34 | if pos < len(raw) and raw[pos] == '"': | ||
| 35 | # Backslashes are special only before a quote. An odd final slash escapes that quote. | ||
| 36 | arg.append("\\" * (backslashes // 2)) | ||
| 37 | if backslashes % 2: | ||
| 38 | arg.append('"') | ||
| 39 | elif quoted and raw[pos + 1:pos + 2] == '"': | ||
| 40 | arg.append('"') | ||
| 41 | pos += 1 | ||
| 42 | else: | ||
| 43 | quoted = not quoted | ||
| 44 | pos += 1 | ||
| 45 | else: | ||
| 46 | arg.append("\\" * backslashes) | ||
| 47 | if pos == len(raw) or (not quoted and raw[pos] in " \t"): | ||
| 48 | break | ||
| 49 | arg.append(raw[pos]) | ||
| 50 | pos += 1 | ||
| 51 | if quoted: | ||
| 52 | raise ValueError("No closing quotation") | ||
| 53 | args.append("".join(arg)) | ||
| 54 | return args | ||
| 55 | |||
| 56 | |||
| 19 | def split_command(raw: str) -> list[str]: | 57 | def split_command(raw: str) -> list[str]: |
| 20 | """Split a command line without eating the backslashes of a Windows path (POSIX shlex would).""" | 58 | """Split a command line without eating the backslashes of a Windows path (POSIX shlex would).""" |
| 21 | raw = raw.strip() | 59 | raw = raw.strip() |
| @@ -28,20 +66,11 @@ def split_command(raw: str) -> list[str]: | |||
| 28 | # for it, but it is no file under that exact name, so it used to be split at its spaces. | 66 | # for it, but it is no file under that exact name, so it used to be split at its spaces. |
| 29 | return [raw] | 67 | return [raw] |
| 30 | try: | 68 | try: |
| 31 | if os.name == "nt": | 69 | return split_windows_command(raw) if os.name == "nt" else shlex.split(raw) |
| 32 | # Quotes can start inside an argument, as in -o="path with spaces". | ||
| 33 | lexer = shlex.shlex(raw, posix=True) | ||
| 34 | lexer.quotes = '"' | ||
| 35 | lexer.escape = "" | ||
| 36 | lexer.commenters = "" | ||
| 37 | lexer.whitespace_split = True | ||
| 38 | return list(lexer) | ||
| 39 | parts = shlex.split(raw) | ||
| 40 | except ValueError as e: | 70 | except ValueError as e: |
| 41 | # shader_disasm.py calls mc_command() at import time: a bad LLVM_MC must give one clear line, | 71 | # shader_disasm.py calls mc_command() at import time: a bad LLVM_MC must give one clear line, |
| 42 | # not a ValueError traceback before any tool's own argument parsing runs. | 72 | # not a ValueError traceback before any tool's own argument parsing runs. |
| 43 | raise SystemExit(f"LLVM_MC: invalid command line {raw!r}: {e}") from None | 73 | raise SystemExit(f"LLVM_MC: invalid command line {raw!r}: {e}") from None |
| 44 | return [p[1:-1] if len(p) > 1 and p[0] == p[-1] == '"' else p for p in parts] | ||
| 45 | 74 | ||
| 46 | 75 | ||
| 47 | def mc_command(raw: str | None = None) -> list: | 76 | def mc_command(raw: str | None = None) -> list: |