blob: 2572367ee18433aa609970993be85b2522dc7621 [file] [log] [blame]
Lorenz Brun40025ff2021-08-31 13:06:02 +02001--- /dev/null
2+++ b/makenames.go
3@@ -0,0 +1,73 @@
4+// makenames replaces the built-in array generation consisting of Perl incantations being
5+// consumed by C code with a single implementation that's shorter anyways.
6+package main
7+
8+import (
9+ "io/ioutil"
10+ "log"
11+ "os"
12+ "regexp"
13+ "strconv"
14+ "strings"
15+ "text/template"
16+)
17+
18+type tmplParams struct {
19+ Bits int
20+ NameSize int
21+ Caps []string
22+}
23+
24+var capNamesTmpl = template.Must(template.New("cap_names").Parse(`/*
25+ * DO NOT EDIT: this file is generated automatically from
26+ *
27+ * <uapi/linux/capability.h>
28+ */
29+
30+#define __CAP_BITS {{ .Bits }}
31+#define __CAP_NAME_SIZE {{ .NameSize }}
32+
33+#ifdef LIBCAP_PLEASE_INCLUDE_ARRAY
34+#define LIBCAP_CAP_NAMES { \
35+{{ range $i, $name := .Caps }} /* {{ $i }} */ {{ if $name }}"{{ $name }}"{{else}}NULL, /* - presently unused */{{end}}, \
36+{{end}} }
37+#endif /* LIBCAP_PLEASE_INCLUDE_ARRAY */
38+
39+/* END OF FILE */
40+`))
41+
42+var capRe = regexp.MustCompile(`(?m)^#define[ \t](CAP[_A-Z]+)[ \t]+([0-9]+)\s+$`)
43+
44+func main() {
45+ sourceFile, err := ioutil.ReadFile(os.Args[1])
46+ if err != nil {
47+ log.Fatalln(err)
48+ }
49+
50+ matches := capRe.FindAllStringSubmatch(string(sourceFile), -1)
51+ out := tmplParams{
52+ Caps: make([]string, 1024),
53+ }
54+ for _, m := range matches {
55+ i, err := strconv.Atoi(m[2])
56+ if err != nil {
57+ log.Fatalln(err)
58+ }
59+ if i+1 > out.Bits {
60+ out.Bits = i + 1
61+ }
62+ if len(m[1])+1 > out.NameSize {
63+ out.NameSize = len(m[1]) + 1
64+ }
65+ out.Caps[i] = strings.ToLower(m[1])
66+ }
67+ out.Caps = out.Caps[:out.Bits]
68+ outFile, err := os.Create(os.Args[2])
69+ if err != nil {
70+ log.Fatalln(err)
71+ }
72+ if err := capNamesTmpl.ExecuteTemplate(outFile, "cap_names", &out); err != nil {
73+ log.Fatalln(err)
74+ }
75+ outFile.Close()
76+}
77--
782.25.1