Commit bd815cb7 authored by Mikio Hara's avatar Mikio Hara

go.net/ipv4: make use of go generate to create system adaptation files

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/160800043
parent cd2d3ece
...@@ -6,8 +6,9 @@ ...@@ -6,8 +6,9 @@
//go:generate go run gen.go //go:generate go run gen.go
// This program generates internet protocol constants and tables by // This program generates system adaptation constants and types,
// reading IANA protocol registries. // internet protocol constants and tables by reading template files
// and IANA protocol registries.
package main package main
import ( import (
...@@ -19,10 +20,55 @@ import ( ...@@ -19,10 +20,55 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"os/exec"
"runtime"
"strconv" "strconv"
"strings" "strings"
) )
func main() {
if err := genzsys(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if err := geniana(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func genzsys() error {
defs := "defs_" + runtime.GOOS + ".go"
f, err := os.Open(defs)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
f.Close()
cmd := exec.Command("go", "tool", "cgo", "-godefs", defs)
b, err := cmd.Output()
if err != nil {
return err
}
switch runtime.GOOS {
case "dragonfly", "solaris":
// The ipv4 pacakge still supports go1.2, and so we
// need to take care of additional platforms in go1.3
// and above for working with go1.2.
b = bytes.Replace(b, []byte("package ipv4\n"), []byte("// +build "+runtime.GOOS+"\n\npackage ipv4\n"), 1)
}
b, err = format.Source(b)
if err != nil {
return err
}
if err := ioutil.WriteFile("zsys_"+runtime.GOOS+".go", b, 0644); err != nil {
return err
}
return nil
}
var registries = []struct { var registries = []struct {
url string url string
parse func(io.Writer, io.Reader) error parse func(io.Writer, io.Reader) error
...@@ -33,7 +79,7 @@ var registries = []struct { ...@@ -33,7 +79,7 @@ var registries = []struct {
}, },
} }
func main() { func geniana() error {
var bb bytes.Buffer var bb bytes.Buffer
fmt.Fprintf(&bb, "// go generate gen.go\n") fmt.Fprintf(&bb, "// go generate gen.go\n")
fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n") fmt.Fprintf(&bb, "// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT\n\n")
...@@ -41,29 +87,25 @@ func main() { ...@@ -41,29 +87,25 @@ func main() {
for _, r := range registries { for _, r := range registries {
resp, err := http.Get(r.url) resp, err := http.Get(r.url)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
fmt.Fprintf(os.Stderr, "got HTTP status code %v for %v\n", resp.StatusCode, r.url) return fmt.Errorf("got HTTP status code %v for %v\n", resp.StatusCode, r.url)
os.Exit(1)
} }
if err := r.parse(&bb, resp.Body); err != nil { if err := r.parse(&bb, resp.Body); err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
fmt.Fprintf(&bb, "\n") fmt.Fprintf(&bb, "\n")
} }
b, err := format.Source(bb.Bytes()) b, err := format.Source(bb.Bytes())
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
if err := ioutil.WriteFile("iana.go", b, 0644); err != nil { if err := ioutil.WriteFile("iana.go", b, 0644); err != nil {
fmt.Fprintln(os.Stderr, err) return err
os.Exit(1)
} }
return nil
} }
func parseICMPv4Parameters(w io.Writer, r io.Reader) error { func parseICMPv4Parameters(w io.Writer, r io.Reader) error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment