-
Notifications
You must be signed in to change notification settings - Fork 12
/
dumpscript.py
51 lines (46 loc) · 1.11 KB
/
dumpscript.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
import sys
from construct import *
script_struct = GreedyRange(Struct("statement",
Anchor("start"),
ULInt16("length"),
Enum(ULInt16("type"),
RUN = 0,
PRINT = 3,
_default_ = Pass,
),
If(lambda ctx: ctx.length > 0,
Switch("args", lambda ctx: ctx.type,
{
"PRINT": Struct("print",
CString("text"),
Anchor("end"),
Padding(lambda ctx: ctx._.length - ctx.end + ctx._.start),
),
"RUN": Struct("run",
Padding(6),
ULInt8("argc"),
ULInt8("envc"),
CString("program"),
Array(lambda ctx: ctx.argc, CString("arg")),
Array(lambda ctx: ctx.envc, CString("env")),
Anchor("end"),
Padding(lambda ctx: ctx._.length - ctx.end + ctx._.start),
)
},
default = String("unknown", lambda ctx: ctx.length - 4)
),
)
))
input = open(sys.argv[1], "rb")
data = input.read()
input.close()
script = script_struct.parse(data)
for stmt in script:
if stmt.length == 0:
continue
elif stmt.type == 'PRINT':
print 'echo "%s"' % stmt.args.text
elif stmt.type == 'RUN':
print ' '.join(stmt.args.arg)
else:
print '# CMD%d %s' % (stmt.type, stmt.args)