-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_spec.go
175 lines (162 loc) · 3.84 KB
/
generate_spec.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
// +build ignore
package main
import (
"bufio"
"bytes"
"encoding/xml"
"fmt"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"unicode"
"unicode/utf8"
"github.com/CN-TU/go-ipfix"
)
type specType int
const (
ieSpec specType = iota
xmlSpec
)
func main() {
var input, output *os.File
var err error
var spec specType
if len(os.Args) != 3 {
log.Panicf("Usage: %s Funcname file\n", os.Args[0])
}
funcName := os.Args[1]
inputName := os.Args[2]
if r, _ := utf8.DecodeRuneInString(funcName); r == utf8.RuneError || !unicode.IsUpper(r) {
log.Panicln("Funcname must start with a legal UTF-8 uppercase letter")
}
ext := filepath.Ext(inputName)
switch ext {
case ".xml":
spec = xmlSpec
case ".iespec":
spec = ieSpec
default:
log.Panicf("Unknown file extension '%s'; I only know xml and iespec!\n", ext)
}
outputName := inputName[:len(inputName)-len(ext)] + ".go"
if output, err = os.Create(outputName); err != nil {
log.Panicln("Couldn't open output file", outputName, err)
}
if input, err = os.Open(inputName); err != nil {
log.Panicln("Couldn't open input file", inputName, err)
}
wr := bufio.NewWriter(output)
fmt.Fprintf(wr, `package ipfix
// GENERATED BY generate_spec; DO NOT CHANGE!
var loaded%s = false
// %s loads information elements from the given specification
func %s() {
if loaded%s {
return
}
loaded%s = true
`, funcName, funcName, funcName, funcName, funcName)
cb := func(ie ipfix.InformationElement) {
iev := reflect.ValueOf(ie)
iet := reflect.TypeOf(ie)
wr.WriteString(" RegisterInformationElement(InformationElement{")
for i := 0; i < iev.NumField()-1; i++ {
if i > 0 {
wr.WriteString(", ")
}
fmt.Fprintf(wr, "%s: %#v", iet.Field(i).Name, iev.Field(i))
}
wr.WriteString("})\n")
}
switch spec {
case xmlSpec:
xmlspec(input, cb)
case ieSpec:
iespec(input, cb)
}
wr.WriteString(`}
`)
wr.Flush()
input.Close()
output.Close()
}
func iespec(spec *os.File, cb func(ipfix.InformationElement)) {
rd := bufio.NewScanner(spec)
for rd.Scan() {
if ie, err := ipfix.MakeIEFromSpec(rd.Bytes()); err == nil {
cb(ie)
} else {
log.Panic(err)
}
}
}
func xmlspec(spec *os.File, cb func(ipfix.InformationElement)) {
type Record struct {
XMLName xml.Name `xml:"record"`
Name string `xml:"name"`
DataType []byte `xml:"dataType"`
ElementID int `xml:"elementId"`
}
dec := xml.NewDecoder(spec)
SEARCH_IES:
for {
if tok, err := dec.Token(); err != nil {
log.Panic(err)
} else {
if tok, ok := tok.(xml.StartElement); ok {
if tok.Name.Local == "registry" && len(tok.Attr) == 1 && tok.Attr[0].Name.Local == "id" && tok.Attr[0].Value == "ipfix-information-elements" {
break SEARCH_IES
}
}
}
}
var start xml.StartElement
SEARCH_FIRST_RECORD:
for {
if tok, err := dec.Token(); err != nil {
log.Panic(err)
} else {
if tok, ok := tok.(xml.StartElement); ok {
if tok.Name.Local == "record" {
start = tok
break SEARCH_FIRST_RECORD
}
}
}
}
NEXT_RECORD:
for {
var rec Record
dec.DecodeElement(&rec, &start)
rec.Name = strings.TrimSpace(rec.Name)
rec.DataType = bytes.TrimSpace(rec.DataType)
if rec.Name != "" && rec.ElementID != 0 && len(rec.DataType) != 0 &&
string(rec.DataType) != "basicList" && string(rec.DataType) != "subTemplateList" && string(rec.DataType) != "subTemplateMultiList" {
//SMELL: hardcoded iana stuff
cb(ipfix.NewInformationElement(rec.Name, 0, uint16(rec.ElementID), ipfix.NameToType(rec.DataType), 0))
}
for {
if tok, err := dec.Token(); err != nil {
log.Panic(err)
} else {
switch t := tok.(type) {
case xml.StartElement:
if t.Name.Local == "record" {
start = t
continue NEXT_RECORD
}
if t.Name.Local == "registry" {
goto FINISHED
}
case xml.EndElement:
if t.Name.Local == "registry" {
goto FINISHED
}
}
}
}
}
FINISHED:
}