File: gen.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (74 lines) | stat: -rw-r--r-- 1,523 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build ignore
// +build ignore

// This program generates the table keysymCodePoints from /usr/include/X11/keysymdef.h
package main

import (
	"bufio"
	"bytes"
	"fmt"
	"go/format"
	"log"
	"os"
	"regexp"
	"strings"
)

func main() {
	fh, err := os.Open("/usr/include/X11/keysymdef.h")
	if err != nil {
		log.Fatalf("opening keysymdef.h: %v", err)
	}

	defer fh.Close()

	seen := make(map[string]struct{})

	buf := &bytes.Buffer{}

	fmt.Fprintf(buf, `// generated by go generate; DO NOT EDIT.

package x11key

// keysymCodePoints maps xproto.Keysym values to their corresponding unicode code point.
var keysymCodePoints = map[rune]rune{
`)

	re := regexp.MustCompile(`^#define (XK_[^ ]*) *0x([[:xdigit:]]+) .*U\+([[:xdigit:]]+) (.+)(?: |\))\*/$`)

	s := bufio.NewScanner(fh)
	for s.Scan() {
		m := re.FindStringSubmatch(strings.TrimSpace(s.Text()))
		if m == nil {
			continue
		}

		if _, isSeen := seen[m[2]]; isSeen {
			continue
		}
		seen[m[2]] = struct{}{}

		fmt.Fprintf(buf, "0x%s: 0x%s, // %s:\t%s\n", m[2], m[3], m[1], m[4])

	}
	if err := s.Err(); err != nil {
		log.Fatalf("reading keysymdef.h: %v", err)
	}

	fmt.Fprintf(buf, "}\n")

	fmted, err := format.Source(buf.Bytes())
	if err != nil {
		log.Fatalf("formatting output: %v", err)
	}

	err = os.WriteFile("table.go", fmted, 0644)
	if err != nil {
		log.Fatalf("writing table.go: %v", err)
	}
}