File: config2c.awk

package info (click to toggle)
dte 1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,152 kB
  • sloc: ansic: 28,421; sh: 94; awk: 56; makefile: 13; sed: 1
file content (67 lines) | stat: -rwxr-xr-x 1,365 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
#!/usr/bin/awk -f

function escape_ident(s) {
    gsub(/[+\/.-]/, "_", s)
    return s
}

function escape_string(s) {
    gsub(/\\/, "\\134", s)
    gsub(/"/, "\\042", s)
    return s
}

function escape_syntax(s) {
    gsub(/^ +/, "", s)
    gsub(/\\/, "\\134", s)
    gsub(/"/, "\\042", s)
    return s
}

BEGIN {
    print "#ifdef __linux__"
    print "#define CONFIG_SECTION SECTION(\".dte.config\") ALIGNED(8)"
    print "#else"
    print "#define CONFIG_SECTION"
    print "#endif\n"
    print "IGNORE_WARNING(\"-Woverlength-strings\")\n"
}

FNR == 1 {
    if (NR != 1) {
        print ";\n"
    }
    name = FILENAME
    gsub(/^config\//, "", name)
    ident = "builtin_" escape_ident(name)
    print "static CONFIG_SECTION const char " ident "[] ="
    names[++nfiles] = name
    idents[nfiles] = ident
}

name ~ /syntax\// {
    if ($0 !~ /^ *#/) {
        print "\"" escape_syntax($0) "\\n\""
    }
    next
}

{
    print "\"" escape_string($0) "\\n\""
}

END {
    print ";\n\nUNIGNORE_WARNINGS\n"

    print \
        "#define cfg(n, t) { \\\n" \
        "    .name = n, \\\n" \
        "    .text = {.data = t, .length = sizeof(t) - 1} \\\n" \
        "}\n"

    print "static CONFIG_SECTION const BuiltinConfig builtin_configs[] = {"
    for (i = 1; i <= nfiles; i++) {
        print "    cfg(\"" names[i] "\", " idents[i] "),"
    }
    print "};"
}