aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/main.go
blob: 69579a4b54dfa404ef164ab4b46bbcec27819c80 (plain)
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
package main

import (
	"flag"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"strings"
)

var (
	protoPath = flag.String("proto-path",
		"/usr/share/xcb", "path to directory of X protocol XML files")
	gofmt = flag.Bool("gofmt", true,
		"When disabled, gofmt will not be run before outputting Go code")
)

func usage() {
	basename := os.Args[0]
	if lastSlash := strings.LastIndex(basename, "/"); lastSlash > -1 {
		basename = basename[lastSlash+1:]
	}
	log.Printf("Usage: %s [flags] xml-file", basename)
	flag.PrintDefaults()
	os.Exit(1)
}

func init() {
	log.SetFlags(0)
}

func main() {
	flag.Usage = usage
	flag.Parse()

	if flag.NArg() != 1 {
		log.Printf("A single XML protocol file can be processed at once.")
		flag.Usage()
	}

	// Read the single XML file into []byte
	xmlBytes, err := ioutil.ReadFile(flag.Arg(0))
	if err != nil {
		log.Fatal(err)
	}

	// Initialize the buffer, parse it, and filter it through gofmt.
	c := newContext()
	c.Translate(xmlBytes)

	if !*gofmt {
		c.out.WriteTo(os.Stdout)
	} else {
		cmdGofmt := exec.Command("gofmt")
		cmdGofmt.Stdin = c.out
		cmdGofmt.Stdout = os.Stdout
		err = cmdGofmt.Run()
		if err != nil {
			log.Fatal(err)
		}
	}
}