1264790Sbapt# vile: awkmode
2264790Sbaptfunction noident(given) {
3264790Sbapt	gsub(/\$/,"@", given);
4264790Sbapt	return given;
5264790Sbapt}
6264790SbaptBEGIN { havesection = 0;
7264790Sbapt	version = "$Id: skel2c,v 1.3 2014/04/06 19:48:04 tom Exp $";
8264790Sbapt	nsec = 0;
9264790Sbapt	ifdef = "";
10264790Sbapt	printf  "/* This file generated automatically using\n * %s\n */\n\n",
11264790Sbapt		noident(version);
12264790Sbapt      }
13264790Sbapt/[$]Id[:][^$]*[$]/ {
14264790Sbapt	printf "%s\n", noident($0);
15264790Sbapt	next;
16264790Sbapt      }
17264790Sbapt/^%% *insert *VERSION *here/ {
18264790Sbapt	printf "    CONCAT1(\"#define YYMAJOR \", YYMAJOR),\n";
19264790Sbapt	printf "    CONCAT1(\"#define YYMINOR \", YYMINOR),\n";
20264790Sbapt	printf "#ifdef YYPATCH\n";
21264790Sbapt	printf "    CONCAT1(\"#define YYPATCH \", YYPATCH),\n";
22264790Sbapt	printf "#endif\n";
23264790Sbapt	next;
24264790Sbapt      }
25264790Sbapt/^%%ifdef/ {
26264790Sbapt	if (NF >= 2) {
27264790Sbapt	    printf "#if defined(%s)\n", $2;
28264790Sbapt	    printf "    \"#if %s\",\n", $2;
29264790Sbapt	} else {
30264790Sbapt	    _abort_exit = 1;
31264790Sbapt	    printf "skel2c: ill-formed %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr";
32264790Sbapt	    exit 2;
33264790Sbapt	}
34264790Sbapt	if (ifdef != "") {
35264790Sbapt	    printf "skel2c: nested %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr";
36264790Sbapt	    exit 2;
37264790Sbapt	}
38264790Sbapt	ifdef = $2;
39264790Sbapt	next;
40264790Sbapt      }
41264790Sbapt/^%%endif/ {
42264790Sbapt	if (ifdef != "") {
43264790Sbapt	    if (NF >= 2) {
44264790Sbapt		printf "    \"#endif /* %s */\",\n", $2;
45264790Sbapt		printf "#endif /* defined(%s) */\n", $2;
46264790Sbapt	    } else {
47264790Sbapt		printf "    \"#endif /* %s */\",\n", ifdef;
48264790Sbapt		printf "#endif /* defined(%s) */\n", ifdef;
49264790Sbapt	    }
50264790Sbapt	    ifdef = "";
51264790Sbapt	} else {
52264790Sbapt	    printf "    \"#endif\",\n";
53264790Sbapt	    printf "#endif\n";
54264790Sbapt	    printf "skel2c: unmatched %endif in skeleton file on line %d\n", FNR > "/dev/stderr";
55264790Sbapt	    exit 2;
56264790Sbapt	}
57264790Sbapt	next;
58264790Sbapt      }
59264790Sbapt/^%%/ { if (havesection) {
60264790Sbapt	    printf "    0\n};\n\n";
61264790Sbapt	}
62264790Sbapt	if (NF >= 2) {
63264790Sbapt	    havesection = 1;
64264790Sbapt	    section = $2;
65264790Sbapt	    seclist[nsec] = section;
66264790Sbapt	    nsec = nsec + 1;
67264790Sbapt	    printf "const char *const %s[] =\n{\n", $2;
68264790Sbapt	} else {
69264790Sbapt	    havesection = 0;
70264790Sbapt	}
71264790Sbapt	next;
72264790Sbapt      }
73264790Sbapt      { if (havesection) {
74264790Sbapt	    # Could use 'gsub(/\\/, "\\\\")' instead of the following
75264790Sbapt	    # two lines, but there's a bug in mawk and the original
76264790Sbapt	    # awk (not in gawk) which is triggered by that.
77264790Sbapt	    gsub(/\\/, "\\\1");
78264790Sbapt	    gsub(/\1/, "\\");
79264790Sbapt#	    gsub(/\t/, "\\t"); # change '\t' to "\\t"
80264790Sbapt	    gsub(/\"/, "\\\"");
81264790Sbapt	    printf "    \"%s\",\n", $0;
82264790Sbapt	} else {
83264790Sbapt	    print $0;
84264790Sbapt	}
85264790Sbapt      }
86264790SbaptEND   { if (_abort_exit)
87264790Sbapt	    exit 2;
88264790Sbapt	if (havesection) {
89264790Sbapt	    print "    0\n};\n";
90264790Sbapt	}
91264790Sbapt	if (nsec > 0) {
92264790Sbapt	    print "void";
93264790Sbapt	    print "write_section(FILE * fp, const char *const section[])";
94264790Sbapt	    print "{";
95264790Sbapt	    print "    int i;";
96264790Sbapt	    print "    const char *s;\n";
97264790Sbapt	    print "    for (i = 0; (s = section[i]) != 0; ++i)";
98264790Sbapt	    print "    {";
99264790Sbapt	    print "\tif (fp == code_file)";
100264790Sbapt	    print "\t    ++outline;";
101264790Sbapt	    print "\tfprintf(fp, \"%s\\n\", s);";
102264790Sbapt	    print "    }";
103264790Sbapt	    print "}";
104264790Sbapt	} else {
105264790Sbapt	    print "skel2c: no sections defined in skeleton file" > "/dev/stderr";
106264790Sbapt	    exit 2;
107264790Sbapt	}
108264790Sbapt	if (ifdef != "") {
109264790Sbapt	    printf "skel2c: unmatched %%ifdef %s at end of skeleton file\n", $ifdef > "/dev/stderr";
110264790Sbapt	    exit 2;
111264790Sbapt	}
112264790Sbapt      }
113