-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
102 lines (82 loc) · 2.05 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
package main
import (
"flag"
"fmt"
"go/format"
"go/token"
"io"
"io/ioutil"
"os"
)
var inputFile = flag.String("i", "", "Input file (.x)")
var outputFile = flag.String("o", "", "Output file (.go)")
var outputPackage = flag.String("p", "main", "Output package name")
var typesFile = flag.String("t", "", "Output file for separate type definitions (optional")
var debugFlag = flag.Bool("d", false, "Debug parsing")
var unsignedEnumFlag = flag.Bool("unsigned-enum", false, "Unsigned integer enum types")
var constTypeFlag = flag.String("const-type", "", "Optional type for const definitions")
var out io.Writer
var tout io.Writer
func main() {
flag.Parse()
if *inputFile == "" {
fmt.Fprintf(os.Stderr, "Must specify input file (-i)\n")
os.Exit(1)
}
if *outputFile == "" {
fmt.Fprintf(os.Stderr, "Must specify output file (-o)\n")
os.Exit(1)
}
src, err := ioutil.ReadFile(*inputFile)
if err != nil {
panic(err)
}
fset := token.NewFileSet()
f := fset.AddFile(*inputFile, -1, len(src))
var l lexer
l.s.Init(f, src, nil, 0)
outTmp := *outputFile + ".tmp"
outf, err := os.OpenFile(outTmp, os.O_WRONLY|os.O_EXCL|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
defer os.Remove(outTmp)
out = outf
fmt.Fprintf(out, "package %s\n", *outputPackage)
fmt.Fprintf(out, "import \"github.com/zeldovich/go-rpcgen/xdr\"\n")
var toutTmp string
var toutf *os.File
if *typesFile != "" {
toutTmp = *typesFile + ".tmp"
toutf, err = os.OpenFile(toutTmp, os.O_WRONLY|os.O_EXCL|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
panic(err)
}
defer os.Remove(toutTmp)
tout = toutf
fmt.Fprintf(tout, "package %s\n", *outputPackage)
} else {
tout = outf
}
xdrParse(&l)
outf.Close()
refmt(outTmp, *outputFile)
if *typesFile != "" {
toutf.Close()
refmt(toutTmp, *typesFile)
}
}
func refmt(infile string, outfile string) {
buf, err := ioutil.ReadFile(infile)
if err != nil {
panic(err)
}
buf, err = format.Source(buf)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(outfile, buf, 0666)
if err != nil {
panic(err)
}
}