forked from charithe/terminfo
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gen.go
280 lines (266 loc) · 6.93 KB
/
gen.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//go:build ignore
package main
import (
"archive/tar"
"bufio"
"bytes"
"compress/gzip"
"errors"
"flag"
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"unicode"
"github.com/kenshaw/snaker"
)
func main() {
out := flag.String("out", "capvals.go", "out file")
cache := flag.String("cache", ".cache", "cache directory")
ver := flag.String("ver", "", "version")
flag.Parse()
if err := run(*out, *cache, *ver); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func run(dest, cache, ver string) error {
// get version
ver, err := getVer(ver)
if err != nil {
return err
}
// get archive
buf, err := get(cache, ver)
if err != nil {
return err
}
// load caps file
caps, err := load(buf, ver)
if err != nil {
return err
}
// process caps
buf, err = processCaps(caps)
if err != nil {
return err
}
// write
buf, err = format.Source(buf)
if err != nil {
return err
}
return ioutil.WriteFile(dest, buf, 0o644)
}
func getVer(ver string) (string, error) {
if ver != "" {
return ver, nil
}
res, err := http.Get("https://ftp.gnu.org/pub/gnu/ncurses/")
if err != nil {
return "", err
}
defer res.Body.Close()
buf, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
m := verRE.FindAllStringSubmatch(string(buf), -1)
sort.Slice(m, func(i, j int) bool {
va, _ := strconv.Atoi(m[i][1])
vb, _ := strconv.Atoi(m[j][1])
if va == vb {
va, _ = strconv.Atoi(m[i][2])
vb, _ = strconv.Atoi(m[j][2])
return va > vb
}
return va > vb
})
return m[0][1] + "." + m[0][2], nil
}
var verRE = regexp.MustCompile(`href="ncurses-([0-9]+)\.([0-9]+)\.tar\.gz"`)
// get retrieves a file either from the the http path, or from disk.
func get(cache, ver string) ([]byte, error) {
file := fmt.Sprintf("https://ftp.gnu.org/pub/gnu/ncurses/ncurses-%s.tar.gz", ver)
if err := os.MkdirAll(cache, 0o755); err != nil {
return nil, err
}
// check if the file exists
cacheFile := filepath.Join(cache, filepath.Base(file))
fi, err := os.Stat(cacheFile)
if err == nil && !fi.IsDir() {
log.Printf("loading %s", cacheFile)
return ioutil.ReadFile(cacheFile)
}
// retrieve
log.Printf("retrieving %s", file)
res, err := http.Get(file)
if err != nil {
return nil, err
}
defer res.Body.Close()
// read
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
// cache
log.Printf("saving %s", cacheFile)
if err := ioutil.WriteFile(cacheFile, buf, 0o644); err != nil {
return nil, err
}
return buf, nil
}
// load extracts a file from a tar.gz.
func load(buf []byte, ver string) ([]byte, error) {
file := fmt.Sprintf("ncurses-%s/include/Caps", ver)
// create gzip reader
gr, err := gzip.NewReader(bytes.NewReader(buf))
if err != nil {
return nil, err
}
defer gr.Close()
// walk files in tar
tr := tar.NewReader(gr)
for {
h, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
// found file, read contents
if h.Name == file {
b := bytes.NewBuffer(make([]byte, h.Size))
var n int64
n, err = io.Copy(b, tr)
if err != nil {
return nil, err
}
// check that all bytes were copied
if n != h.Size {
return nil, errors.New("could not read entire file")
}
return b.Bytes(), nil
}
}
return nil, fmt.Errorf("could not load file %s", file)
}
// processCaps processes the data in the Caps file.
func processCaps(capsBuf []byte) ([]byte, error) {
// create scanner
s := bufio.NewScanner(bytes.NewReader(capsBuf))
s.Buffer(make([]byte, 1024*1024), 1024*1024)
// storage
bools, nums, strs := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer)
var boolCount, numCount, stringCount int
var lastBool, lastNum, lastString string
var boolNames, numNames, stringNames []string
// process caps
var n int
for s.Scan() {
// read line
line := strings.TrimSpace(commentRE.ReplaceAllString(strings.Trim(s.Text(), "\x00"), ""))
if len(line) == 0 || strings.HasPrefix(line, "capalias") || strings.HasPrefix(line, "infoalias") {
continue
}
// split line's columns
row := make([]string, 8)
for i := 0; i < 7; i++ {
start := strings.IndexFunc(line, unicode.IsSpace)
end := strings.IndexFunc(line[start:], notSpace)
row[i] = strings.TrimSpace(line[:start+end])
line = line[start+end:]
}
row[7] = strings.TrimSpace(line)
// manipulation
var buf *bytes.Buffer
var names *[]string
var typ, isFirst, prefix, suffix string
// format variable name
name := snaker.SnakeToCamel(row[0])
switch row[2] {
case "bool":
if boolCount == 0 {
isFirst = " = iota"
}
buf, names, lastBool, prefix, suffix = bools, &boolNames, name, "indicates", ""
typ = "bool"
boolCount++
case "num":
if numCount == 0 {
isFirst = " = iota"
}
buf, names, lastNum, prefix, suffix = nums, &numNames, name, "is", ""
typ = "num"
numCount++
case "str":
if stringCount == 0 {
isFirst = " = iota"
}
buf, names, lastString, prefix, suffix = strs, &stringNames, name, "is the", ""
typ = "string"
stringCount++
default:
return nil, fmt.Errorf("line %d is invalid, has type: %s", n, row[2])
}
if isFirst == "" {
buf.WriteString("\n")
}
fmt.Fprintf(buf, "// The %s [%s, %s] %s capability %s\n%s%s", name, row[0], row[1], typ, formatComment(row[7], prefix, suffix), name, isFirst)
*names = append(*names, row[0], row[1])
n++
}
if err := s.Err(); err != nil {
return nil, err
}
f := new(bytes.Buffer)
f.WriteString(hdr)
// add consts
typs := []string{"Bool", "Num", "String"}
for i, b := range []*bytes.Buffer{bools, nums, strs} {
f.WriteString(fmt.Sprintf("// %s capabilities.\nconst (\n", typs[i]))
b.WriteTo(f)
f.WriteString(")\n")
}
// add counts
f.WriteString("const (\n")
f.WriteString(fmt.Sprintf("// CapCountBool is the count of bool capabilities.\nCapCountBool = %s+1\n", lastBool))
f.WriteString(fmt.Sprintf("// CapCountNum is the count of num capabilities.\nCapCountNum = %s+1\n", lastNum))
f.WriteString(fmt.Sprintf("// CapCountString is the count of string capabilities.\nCapCountString = %s+1\n", lastString))
f.WriteString(")\n")
// add names
z := []string{"bool", "num", "string"}
for n, s := range [][]string{boolNames, numNames, stringNames} {
y := z[n]
f.WriteString(fmt.Sprintf("// %sCapNames are the %s term cap names.\n", y, y))
f.WriteString(fmt.Sprintf("var %sCapNames = [...]string{\n", y))
for i := 0; i < len(s); i += 2 {
f.WriteString(fmt.Sprintf(`"%s", "%s",`+"\n", s[i], s[i+1]))
}
f.WriteString("}\n")
}
return f.Bytes(), nil
}
// formatComment formats comments with prefix and suffix.
func formatComment(s, prefix, suffix string) string {
s = strings.TrimPrefix(s, prefix)
s = strings.TrimSuffix(s, ".")
s = strings.TrimSuffix(s, suffix)
return strings.TrimSpace(prefix+" "+s+" "+suffix) + "."
}
func notSpace(r rune) bool {
return !unicode.IsSpace(r)
}
var commentRE = regexp.MustCompile(`^#.*`)
const hdr = `package terminfo
// Code generated by gen.go. DO NOT EDIT.
`