-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
221 lines (205 loc) · 4.73 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
220
221
package main
import (
"bufio"
"fmt"
"github.com/pborman/getopt/v2"
"io"
"net"
"os"
"strings"
)
var VERSION = "SNAPSHOT"
func doClose(c io.Closer) {
if err := c.Close(); err != nil {
panic(err)
}
}
//noinspection SpellCheckingInspection
func fprintln(w io.Writer, a ...interface{}) {
if _, err := fmt.Fprintln(w, a...); err != nil {
panic(err)
}
}
func parseIp(str string) net.IP {
for _, b := range str {
switch b {
case '.':
return net.ParseIP(str).To4()
case ':':
return net.ParseIP(str).To16()
}
}
return nil
}
// maybe IpWrapper, Range or IpNetWrapper is returned
func parse(text string) (IRange, error) {
if index := strings.IndexByte(text, '/'); index != -1 {
if _, network, err := net.ParseCIDR(text); err == nil {
return IpNetWrapper{network}, nil
} else {
return nil, err
}
}
if ip := parseIp(text); ip != nil {
return IpWrapper{ip}, nil
}
if index := strings.IndexByte(text, '-'); index != -1 {
if start, end := parseIp(text[:index]), parseIp(text[index+1:]); start != nil && end != nil {
if len(start) == len(end) && !lessThan(end, start) {
return &Range{start: start, end: end}, nil
}
}
return nil, &net.ParseError{Type: "range", Text: text}
}
return nil, &net.ParseError{Type: "ip/CIDR address/range", Text: text}
}
func read(input *bufio.Scanner) []IRange {
var arr []IRange
for input.Scan() {
if text := input.Text(); text != "" {
if maybe, err := parse(text); err != nil {
fprintln(os.Stderr, err)
} else {
arr = append(arr, maybe)
}
}
if err := input.Err(); err != nil {
panic(err)
}
}
return arr
}
func readFile(inputFile string) []IRange {
var input *bufio.Scanner
if inputFile == "-" {
input = bufio.NewScanner(os.Stdin)
} else {
in, err := os.Open(inputFile)
if err != nil {
panic(err)
}
defer doClose(in)
input = bufio.NewScanner(in)
}
input.Split(bufio.ScanWords)
return read(input)
}
func readAll(inputFiles ...string) []IRange {
var result []IRange
for _, inputFile := range inputFiles {
result = append(result, readFile(inputFile)...)
}
return result
}
func printAsIpNets(writer io.Writer, r IRange, simpler func(IRange) IRange) {
for _, cidr := range r.ToIpNets() {
fprintln(writer, simpler(IpNetWrapper{cidr}))
}
}
func mainConsole(option *Option) {
outputType := option.outputType
simpler := option.simpler
var printer func(writer io.Writer, r IRange)
switch outputType {
case OutputTypeRange:
printer = func(writer io.Writer, r IRange) {
fprintln(writer, simpler(r.ToRange()))
}
case OutputTypeCidr:
printer = func(writer io.Writer, r IRange) {
printAsIpNets(writer, r, simpler)
}
default:
printer = func(writer io.Writer, r IRange) {
switch r.(type) {
case IpWrapper:
fprintln(writer, IpNetWrapper{r.ToIpNets()[0]})
case IpNetWrapper:
fprintln(writer, simpler(r.ToRange()))
case *Range:
printAsIpNets(writer, r, simpler)
default:
assert(false, "unreachable")
}
}
}
input := bufio.NewScanner(os.Stdin)
input.Split(bufio.ScanWords)
for input.Scan() {
if text := input.Text(); text != "" {
if r, err := parse(text); err != nil {
fprintln(os.Stderr, err)
} else {
printer(os.Stdout, r)
}
}
}
}
func process(option *Option, outputFile string, inputFiles ...string) {
result := readAll(inputFiles...)
if len(result) == 0 {
switch option.emptyPolicy {
case "error":
panic("no data")
case "skip":
return
default:
// empty string if not specified, or "ignore"
}
}
if !option.originalOrder {
result = sortAndMerge(result)
}
result = convertBatch(result, option.outputType)
var target *os.File
if outputFile == "-" {
target = os.Stdout
} else if file, err := os.Create(outputFile); err != nil {
panic(err)
} else {
defer doClose(file)
target = file
}
writer := bufio.NewWriter(target)
for _, r := range result {
fprintln(writer, option.simpler(r))
}
if err := writer.Flush(); err != nil {
panic(err)
}
}
func mainNormal(option *Option) {
if outputSize := len(option.outputFiles); outputSize <= 1 {
var outputFile string
if outputSize == 1 {
outputFile = option.outputFiles[0]
} else {
outputFile = "-"
}
if len(option.inputFiles) == 0 {
process(option, outputFile, "-")
} else {
process(option, outputFile, option.inputFiles...)
}
} else if len(option.inputFiles) == outputSize {
for i := 0; i < outputSize; i++ {
process(option, option.outputFiles[i], option.inputFiles[i])
}
} else {
panic("Input files' size doesn't match output files' size")
}
}
func main() {
defer func() {
if err := recover(); err != nil {
fprintln(os.Stderr, err)
os.Exit(2)
}
}()
getopt.HelpColumn = 28
if option := parseOptions(); option.consoleMode {
mainConsole(option)
} else {
mainNormal(option)
}
}