blob: 2572367ee18433aa609970993be85b2522dc7621 [file] [log] [blame]
--- /dev/null
+++ b/makenames.go
@@ -0,0 +1,73 @@
+// makenames replaces the built-in array generation consisting of Perl incantations being
+// consumed by C code with a single implementation that's shorter anyways.
+package main
+
+import (
+ "io/ioutil"
+ "log"
+ "os"
+ "regexp"
+ "strconv"
+ "strings"
+ "text/template"
+)
+
+type tmplParams struct {
+ Bits int
+ NameSize int
+ Caps []string
+}
+
+var capNamesTmpl = template.Must(template.New("cap_names").Parse(`/*
+ * DO NOT EDIT: this file is generated automatically from
+ *
+ * <uapi/linux/capability.h>
+ */
+
+#define __CAP_BITS {{ .Bits }}
+#define __CAP_NAME_SIZE {{ .NameSize }}
+
+#ifdef LIBCAP_PLEASE_INCLUDE_ARRAY
+#define LIBCAP_CAP_NAMES { \
+{{ range $i, $name := .Caps }} /* {{ $i }} */ {{ if $name }}"{{ $name }}"{{else}}NULL, /* - presently unused */{{end}}, \
+{{end}} }
+#endif /* LIBCAP_PLEASE_INCLUDE_ARRAY */
+
+/* END OF FILE */
+`))
+
+var capRe = regexp.MustCompile(`(?m)^#define[ \t](CAP[_A-Z]+)[ \t]+([0-9]+)\s+$`)
+
+func main() {
+ sourceFile, err := ioutil.ReadFile(os.Args[1])
+ if err != nil {
+ log.Fatalln(err)
+ }
+
+ matches := capRe.FindAllStringSubmatch(string(sourceFile), -1)
+ out := tmplParams{
+ Caps: make([]string, 1024),
+ }
+ for _, m := range matches {
+ i, err := strconv.Atoi(m[2])
+ if err != nil {
+ log.Fatalln(err)
+ }
+ if i+1 > out.Bits {
+ out.Bits = i + 1
+ }
+ if len(m[1])+1 > out.NameSize {
+ out.NameSize = len(m[1]) + 1
+ }
+ out.Caps[i] = strings.ToLower(m[1])
+ }
+ out.Caps = out.Caps[:out.Bits]
+ outFile, err := os.Create(os.Args[2])
+ if err != nil {
+ log.Fatalln(err)
+ }
+ if err := capNamesTmpl.ExecuteTemplate(outFile, "cap_names", &out); err != nil {
+ log.Fatalln(err)
+ }
+ outFile.Close()
+}
--
2.25.1