diff options
Diffstat (limited to 'usage2c.awk')
-rw-r--r-- | usage2c.awk | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/usage2c.awk b/usage2c.awk new file mode 100644 index 0000000..8d812fe --- /dev/null +++ b/usage2c.awk @@ -0,0 +1,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}"); +} + |