summaryrefslogtreecommitdiff
path: root/usage2c.awk
blob: 8d812fe81a52a933a262cd71935a4bdf06b83456 (plain)
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
#!/usr/bin/awk -f

BEGIN {
    print("#include <stdio.h>");
    print("#include <libxml/xmlversion.h>");
}

length(command_name) == 0 {
    command_name = FILENAME;
    sub(/\.txt$/, "", command_name);
    sub(/^([^\/]+\/)*/, "", command_name);
    gsub(/-/, "_", command_name);
    printf("static const char %s[] = {\n", command_name);
    progs = 0;
}

/PROG/ {
    for (i = 1; i <= NF; i++) {
        if ($i == "PROG") {
            progs++;
            $i = "%s";
        }
    }
}

/^#/
!/^#/ {
    gsub(/./, "'&',");
    gsub(/'\\'/, "'\\\\'");     # '\' --> '\\'
    gsub(/'''/, "'\\''");       # ''' --> '\''
    print($0 "'\\n',");
}

END {
    print("0 };");
    printf("void fprint_%s(FILE* out, const char* argv0) {\n", command_name);
    printf("  fprintf(out, %s", command_name);
    for (i = 1; i <= progs; i++)
        printf(", argv0");
    print(");\n}");
}