-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand-reparse.py
executable file
·43 lines (37 loc) · 1.18 KB
/
command-reparse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python3
import sys
import os.path
def contents(filename):
result = ""
for line in open(filename):
result += line.translate(str.maketrans("\n", " "))
return result
def expand_at_symbol(s):
"""
GCC uses the following notation to include the contents of a file foo:
@foo
This function reconstructs the verbose command line, substituting the
contents of foo (with newlines replaced by spaces) for the reference.
If foo is not a file, the occurance of @foo is unchanged.
"""
words = s.split()
for i in range(len(words)):
w = words[i]
if w.startswith("@"):
maybe_file = w[1:]
if (os.path.isfile(maybe_file)):
words[i] = contents(maybe_file)
return " ".join(words)
while True:
try:
delchars = str.maketrans({i: None for i in '",][()\''})
at_expanded = expand_at_symbol(input().translate(delchars))
words = at_expanded.split()
# At this point words usually contain [<pid> command command <args...> ]
start_idx = 2 if words[1] == words[2] else 0
print(" ".join(words[start_idx:]))
except EOFError:
sys.exit()
except Exception as e:
print(e)
sys.exit(1)