]>
Commit | Line | Data |
---|---|---|
d78792f5 | 1 | -- The getopt-functionality is loaded from pm3/getopt.lua |
2 | -- Have a look there for further details | |
3 | getopt = require('getopt') | |
4 | bin = require('bin') | |
5 | ||
6 | example = "script run 14araw -x 6000F57b" | |
7 | author = "Martin Holst Swende" | |
8 | usage = "script run htmldump [-f <file>]" | |
9 | desc =[[ | |
10 | This script takes a dumpfile and produces a html based dump, which is a | |
11 | bit more easily analyzed. | |
12 | ||
13 | Arguments: | |
14 | -h This help | |
15 | -i <file> Specifies the dump-file (input). If omitted, 'dumpdata.bin' is used | |
16 | -o <filename> Speciies the output file. If omitted, <curtime>.html is used. | |
17 | ||
18 | ]] | |
19 | ||
20 | ------------------------------- | |
21 | -- Some utilities | |
22 | ------------------------------- | |
23 | ||
24 | --- | |
25 | -- A debug printout-function | |
26 | function dbg(args) | |
27 | if DEBUG then | |
28 | print("###", args) | |
29 | end | |
30 | end | |
31 | --- | |
32 | -- This is only meant to be used when errors occur | |
33 | function oops(err) | |
34 | print("ERROR: ",err) | |
35 | end | |
36 | ||
37 | ||
38 | --- | |
39 | -- Usage help | |
40 | function help() | |
41 | print(desc) | |
42 | print("Example usage") | |
43 | print(example) | |
44 | end | |
45 | ||
46 | local function readdump(infile) | |
47 | t = infile:read("*all") | |
48 | --print(string.len(t)) | |
49 | len = string.len(t) | |
50 | local len,hex = bin.unpack(("H%d"):format(len),t) | |
51 | --print(len,hex) | |
52 | return hex | |
53 | end | |
54 | local function convert_to_js(hexdata) | |
55 | if string.len(hexdata) % 32 ~= 0 then | |
56 | return oops(("Bad data, length should be a multiple of 32 (was %d)"):format(string.len(hexdata))) | |
57 | end | |
58 | local js,i = "["; | |
59 | for i = 1, string.len(hexdata),32 do | |
60 | js = js .."'" ..string.sub(hexdata,i,i+31).."',\n" | |
61 | end | |
62 | js = js .. "]" | |
63 | return js | |
64 | end | |
65 | ||
66 | local function main(args) | |
67 | ||
68 | print(desc) | |
69 | local input = "dumpdata.bin" | |
70 | local output = os.date("%Y-%m-%d_%H%M%S.html"); | |
71 | for o, a in getopt.getopt(args, 'i:o:h') do | |
72 | if o == "h" then return help() end | |
73 | if o == "i" then input = a end | |
74 | if o == "o" then output = a end | |
75 | end | |
76 | -- Validate the parameters | |
77 | ||
78 | local infile = io.open(input, "r") | |
79 | if infile == nil then | |
80 | return ("Could not read file ", input) | |
81 | end | |
82 | --lokal skel = require("skel") | |
83 | local dumpdata = readdump(infile) | |
84 | io.close(infile) | |
85 | ||
86 | local js_code = convert_to_js(dumpdata) | |
87 | --print(js_code) | |
88 | local skel = require("htmlskel") | |
89 | html = skel.getHTML(js_code); | |
90 | ||
91 | local outfile = io.open(output, "w") | |
92 | if outfile == nil then | |
93 | return oops("Could not write to file ", output) | |
94 | end | |
95 | outfile:write(html) | |
96 | io.close(outfile) | |
97 | print(("Wrote a HTML dump to the file %s"):format(output)) | |
98 | end | |
99 | ||
100 | ||
101 | --[[ | |
102 | In the future, we may implement so that scripts are invoked directly | |
103 | into a 'main' function, instead of being executed blindly. For future | |
104 | compatibility, I have done so, but I invoke my main from here. | |
105 | --]] | |
106 | main(args) |