forked from raspi/heksa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
219 lines (179 loc) · 5.8 KB
/
main.go
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"fmt"
"github.com/DavidGamba/go-getoptions"
"github.com/raspi/heksa/pkg/color"
"github.com/raspi/heksa/pkg/iface"
"github.com/raspi/heksa/pkg/reader"
"github.com/raspi/heksa/pkg/units"
"io"
"os"
"os/signal"
"strings"
)
var VERSION = `v0.0.0`
var BUILD = `dev`
var BUILDDATE = `0000-00-00T00:00:00+00:00`
const AUTHOR = `Pekka Järvinen`
const HOMEPAGE = `https://github.com/raspi/heksa`
// Parse command line arguments
func getParams() (source iface.ReadSeekerCloser, displays []reader.ByteFormatter, offsetViewer []reader.OffsetFormatter, limit uint64, palette [256]color.AnsiColor, filesize int64) {
opt := getoptions.New()
opt.HelpSynopsisArgs(`<filename> or STDIN`)
opt.Bool(`help`, false,
opt.Alias("h", "?"),
opt.Description(`Show this help`),
)
opt.Bool(`version`, false,
opt.Description(`Show version information`),
)
argOffset := opt.StringOptional(`offset-format`, `hex`,
opt.Alias(`o`),
opt.ArgName(`fmt1[,fmt2]`),
opt.Description(
`One or two of: `+strings.Join(reader.GetOffsetViewerList(), `, `)+`, no, ''.`+
"\n"+
`First one is displayed on the left side and second one on right side after formatters.`,
),
)
argFormat := opt.StringOptional(`format`, `hex,asc`,
opt.Alias(`f`),
opt.ArgName(`fmt1,fmt2,..`),
opt.Description(`One or multiple of: `+strings.Join(reader.GetViewerList(), `, `)),
)
argLimit := opt.StringOptional(`limit`, `0`,
opt.Alias("l"),
opt.ArgName(`[prefix]bytes[unit]`),
opt.Description(`Read only N bytes (0 = no limit). See NOTES.`),
)
argSeek := opt.StringOptional(`seek`, `0`,
opt.Alias("s"),
opt.ArgName(`[prefix]offset[unit]`),
opt.Description(`Start reading from certain offset. See NOTES.`),
)
remainingArgs, err := opt.Parse(os.Args[1:])
if opt.Called("help") {
_, _ = fmt.Fprintf(os.Stdout, `heksa - hex file dumper %v - (%v)`+"\n", VERSION, BUILDDATE)
_, _ = fmt.Fprintf(os.Stdout, `(c) %v 2019- [ %v ]`+"\n", AUTHOR, HOMEPAGE)
_, _ = fmt.Fprintln(os.Stdout, opt.Help())
_, _ = fmt.Fprintln(os.Stdout, `NOTES:`)
_, _ = fmt.Fprintln(os.Stdout, ` - You can use prefixes for seek and limit. 0x = hex, 0b = binary, 0o = octal`)
_, _ = fmt.Fprintln(os.Stdout, ` - Use 'no' or '' for offset formatter for disabling offset output`)
_, _ = fmt.Fprintln(os.Stdout, ` - Use '--seek \-1234' for seeking from end of file`)
_, _ = fmt.Fprintln(os.Stdout, ` - Limit and seek parameters supports units (KB, KiB, MB, MiB, GB, GiB, TB, TiB)`)
_, _ = fmt.Fprintln(os.Stdout)
_, _ = fmt.Fprintln(os.Stdout, `EXAMPLES:`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -f hex,asc,bit foo.dat`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -o hex,per -f hex,asc foo.dat`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -o hex -f hex,asc,bit foo.dat`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -o no -f bit foo.dat`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -l 0x1024 foo.dat`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -s 0b1010 foo.dat`)
_, _ = fmt.Fprintln(os.Stdout, ` heksa -s 4321KiB foo.dat`)
os.Exit(0)
} else if opt.Called("version") {
_, _ = fmt.Fprintf(os.Stdout, `%v build %v on %v`+"\n", VERSION, BUILD, BUILDDATE)
os.Exit(0)
}
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
_, _ = fmt.Fprintln(os.Stderr, opt.Help(getoptions.HelpSynopsis))
os.Exit(1)
}
limitTmp, err := units.Parse(*argLimit)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error parsing limit: %v`, err)
os.Exit(1)
}
limit = uint64(limitTmp)
startOffset, err := units.Parse(strings.Replace(*argSeek, `\`, ``, -1))
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error parsing seek: %v`, err)
os.Exit(1)
}
offsetViewer, err = reader.GetOffsetFormatters(strings.Split(*argOffset, `,`))
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error getting offset formatter: %v`, err)
os.Exit(1)
}
displays, err = reader.GetViewers(strings.Split(*argFormat, `,`))
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error getting formatter: %v`, err)
os.Exit(1)
}
palette = defaultCharacterColors
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// Stdin has data
source = os.Stdin
filesize = -1
} else {
// Read file
if len(remainingArgs) != 1 {
_, _ = fmt.Fprintln(os.Stderr, `error: no file given as argument, see --help`)
os.Exit(1)
}
fpath := remainingArgs[0]
fhandle, err := os.Open(fpath)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error opening file: %v`, err)
os.Exit(1)
}
fi, err := fhandle.Stat()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `error stat'ing file: %v`, err)
os.Exit(1)
}
if fi.IsDir() {
_, _ = fmt.Fprintf(os.Stderr, `error: %v is directory`, fpath)
os.Exit(1)
}
// Seek to given offset
if startOffset > 0 {
_, err = fhandle.Seek(startOffset, io.SeekCurrent)
} else if startOffset < 0 {
_, err = fhandle.Seek(startOffset, io.SeekEnd)
}
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, `couldn't seek to %v: %v`, startOffset, err)
os.Exit(1)
}
filesize = fi.Size()
source = fhandle
}
return source, displays, offsetViewer, limit, palette, filesize
}
func main() {
source, displays, offViewer, limit, palette, filesize := getParams()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
r := reader.New(source, offViewer, displays, palette, filesize)
isEven := false
// Dump hex
for {
select {
case <-stop: // Kill or ctrl-C
break
default:
}
s, err := r.Read()
if err != nil {
if err == io.EOF {
break
}
_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintf(`error while reading file: %v`, err))
os.Exit(1)
}
color := LineEven
if isEven {
color = LineOdd
}
isEven = !isEven
_, _ = fmt.Printf(`%s%s%s`+"\n", color, s, clear)
if limit > 0 && r.ReadBytes >= limit {
// Limit is set and found
break
}
}
source.Close()
}