forked from binpash/pash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.py
123 lines (102 loc) · 2.93 KB
/
wrapper.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
orig_commands = []
cid = 0
WRAPPER_CMD = "wrap"
def get_results():
global orig_commands, cid, WRAPPER_CMD
return (cid, orig_commands, WRAPPER_CMD)
def get_id():
global cid
sid = str(cid)
cid = cid + 1
return sid
def get_wrapper():
# ord / chr() for ascii conversion
global WRAPPER_CMD
io = [ ['C', ord(c)] for c in WRAPPER_CMD]
id = [ ['C', ord(c)] for c in get_id()]
return (io, id)
def wrap_command_value(value):
global orig_commands
orig_commands.append(value)
w = get_wrapper()
value[2].insert(0, w[1])
value[2].insert(0, w[0])
def try_wrap(ast_node):
# FIXME: deprecated!
if ast_node[0] == "Command":
value = ast_node[1]
# For commands, `value` is of type:
# [ lineno, [assgn], [arg], [redir] ]
wrap_command_value(value)
def rewrite_ast(asts):
for i, node in enumerate(asts):
# print("Rewrite AST {}".format(i))
rewrite_node(node)
# Fixme: Pythonic conversion for in-place rewrite? e.g., `rewrite!` ?
def rewrite_node(node):
rewrite = {
"Command": rewrite_command,
"Pipe": rewrite_pipe,
"Redir": rewrite_redir,
"Background": rewrite_background,
"Subshell": rewrite_subshell,
"And": rewrite_and,
"Or": rewrite_or,
"Not": rewrite_not,
"Semi": rewrite_semi,
"If": rewrite_if,
"While": rewrite_while,
"For": rewrite_for,
"Case": rewrite_case,
"Defun": rewrite_defun
}
rewrite[node[0]](node[1])
def rewrite_command(value):
# Command of (linno * assign list * args * redirection list)
wrap_command_value(value)
def rewrite_pipe(value):
# Pipe of (bool * t list)
for i, term in enumerate(value[1]):
rewrite_node(term)
def rewrite_redir(value):
# Redir of (linno * t * redirection list)
rewrite_node(value[1])
def rewrite_background(value):
# Background of (linno * t * redirection list)
rewrite_node(value[1])
def rewrite_subshell(value):
# Subshell of (linno * t * redirection list)
rewrite_node(value[1])
def rewrite_and(value):
# And of (t * t)
rewrite_node(value[0])
rewrite_node(value[1])
def rewrite_or(value):
# Or of (t * t)
rewrite_node(value[0])
rewrite_node(value[1])
def rewrite_not(value):
# Not of t
rewrite_node(value[0])
def rewrite_semi(value):
# Semi of (t * t)
rewrite_node(value[0])
rewrite_node(value[1])
def rewrite_if(value):
# If of (t * t * t)
rewrite_node(value[0])
rewrite_node(value[1])
rewrite_node(value[2])
def rewrite_while(value):
# While of (t * t)
rewrite_node(value[0])
rewrite_node(value[1])
def rewrite_for(value):
# For of (linno * arg * t * string)
rewrite_node(value[2])
def rewrite_case(value):
# Case of (linno * arg * case list)
(lambda x: x)(value) #no-op
def rewrite_defun(value):
# Defun of (linno * string * t)
rewrite_node(value[2])