File: usage2c.awk

package info (click to toggle)
xmlstarlet 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,412 kB
  • ctags: 656
  • sloc: sh: 4,735; ansic: 4,183; xml: 1,936; makefile: 70; awk: 43
file content (62 lines) | stat: -rwxr-xr-x 1,579 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/awk -f

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

# text in src/foo-bar.txt results in
#   static const char foo_text[] = {
#     't', 'h', 'e', ' ', 't', 'e', 'x', 't', ...
#   }
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;
}

# escape percent signs: we are going to use printf(). I don't think there are
# any %s currently, but it's bound to happen eventually.
{ gsub(/%/, "%%"); }

# white space separted instances of PROG will be replaced with the final name of
# the xmlstarlet executable
/PROG/ {
    for (i = 1; i <= NF; i++) {
        if ($i == "PROG") {
            progs++;
            $i = "%s";
        }
    }
}

# C-preprocessor directives are unchanged
/^#/

# convert plain text lines into equivalent char array literal
!/^#/ {
    for (i = 1; i <= length($0); i++) {
        c = substr($0, i, 1);
        if (c == "\\")          # \ --> '\\'
            printf("'\\\\'");
        else if (c == "'")      # ' --> '\''
            printf("'\\''");
        else                    # X --> 'X'
            printf("'%c'", c);
        printf(",");
    }
    print("'\\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}");
}