xref: /netbsd-src/external/bsd/byacc/dist/skel2c (revision b05df594cf91d0d4ed5b47fb559d5346ce1e09ad)
1*b05df594Schristos#!/usr/bin/awk -f
2428aa3b9Schristos# vile: awkmode
3428aa3b9Schristosfunction noident(given) {
4428aa3b9Schristos	gsub(/\$/,"@", given);
5428aa3b9Schristos	return given;
6428aa3b9Schristos}
7428aa3b9SchristosBEGIN { havesection = 0;
8da17a642Schristos	version = "Id: skel2c,v 1.4 2016/06/07 00:26:09 tom Exp ";
9428aa3b9Schristos	nsec = 0;
10428aa3b9Schristos	ifdef = "";
11428aa3b9Schristos	printf  "/* This file generated automatically using\n * %s\n */\n\n",
12428aa3b9Schristos		noident(version);
13428aa3b9Schristos      }
14428aa3b9Schristos/[$]Id[:][^$]*[$]/ {
15428aa3b9Schristos	printf "%s\n", noident($0);
16428aa3b9Schristos	next;
17428aa3b9Schristos      }
18428aa3b9Schristos/^%% *insert *VERSION *here/ {
19428aa3b9Schristos	printf "    CONCAT1(\"#define YYMAJOR \", YYMAJOR),\n";
20428aa3b9Schristos	printf "    CONCAT1(\"#define YYMINOR \", YYMINOR),\n";
21428aa3b9Schristos	printf "#ifdef YYPATCH\n";
22428aa3b9Schristos	printf "    CONCAT1(\"#define YYPATCH \", YYPATCH),\n";
23428aa3b9Schristos	printf "#endif\n";
24428aa3b9Schristos	next;
25428aa3b9Schristos      }
26428aa3b9Schristos/^%%ifdef/ {
27428aa3b9Schristos	if (NF >= 2) {
28428aa3b9Schristos	    printf "#if defined(%s)\n", $2;
29428aa3b9Schristos	    printf "    \"#if %s\",\n", $2;
30428aa3b9Schristos	} else {
31428aa3b9Schristos	    _abort_exit = 1;
32428aa3b9Schristos	    printf "skel2c: ill-formed %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr";
33428aa3b9Schristos	    exit 2;
34428aa3b9Schristos	}
35428aa3b9Schristos	if (ifdef != "") {
36428aa3b9Schristos	    printf "skel2c: nested %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr";
37428aa3b9Schristos	    exit 2;
38428aa3b9Schristos	}
39428aa3b9Schristos	ifdef = $2;
40428aa3b9Schristos	next;
41428aa3b9Schristos      }
42428aa3b9Schristos/^%%endif/ {
43428aa3b9Schristos	if (ifdef != "") {
44428aa3b9Schristos	    if (NF >= 2) {
45428aa3b9Schristos		printf "    \"#endif /* %s */\",\n", $2;
46da17a642Schristos		printf "#endif\t\t\t/* defined(%s) */\n", $2;
47428aa3b9Schristos	    } else {
48428aa3b9Schristos		printf "    \"#endif /* %s */\",\n", ifdef;
49da17a642Schristos		printf "#endif\t\t\t/* defined(%s) */\n", ifdef;
50428aa3b9Schristos	    }
51428aa3b9Schristos	    ifdef = "";
52428aa3b9Schristos	} else {
53428aa3b9Schristos	    printf "    \"#endif\",\n";
54428aa3b9Schristos	    printf "#endif\n";
55428aa3b9Schristos	    printf "skel2c: unmatched %endif in skeleton file on line %d\n", FNR > "/dev/stderr";
56428aa3b9Schristos	    exit 2;
57428aa3b9Schristos	}
58428aa3b9Schristos	next;
59428aa3b9Schristos      }
60428aa3b9Schristos/^%%/ { if (havesection) {
61428aa3b9Schristos	    printf "    0\n};\n\n";
62428aa3b9Schristos	}
63428aa3b9Schristos	if (NF >= 2) {
64428aa3b9Schristos	    havesection = 1;
65428aa3b9Schristos	    section = $2;
66428aa3b9Schristos	    seclist[nsec] = section;
67428aa3b9Schristos	    nsec = nsec + 1;
68428aa3b9Schristos	    printf "const char *const %s[] =\n{\n", $2;
69428aa3b9Schristos	} else {
70428aa3b9Schristos	    havesection = 0;
71428aa3b9Schristos	}
72428aa3b9Schristos	next;
73428aa3b9Schristos      }
74428aa3b9Schristos      { if (havesection) {
75428aa3b9Schristos	    # Could use 'gsub(/\\/, "\\\\")' instead of the following
76428aa3b9Schristos	    # two lines, but there's a bug in mawk and the original
77428aa3b9Schristos	    # awk (not in gawk) which is triggered by that.
78428aa3b9Schristos	    gsub(/\\/, "\\\1");
79428aa3b9Schristos	    gsub(/\1/, "\\");
80428aa3b9Schristos#	    gsub(/\t/, "\\t"); # change '\t' to "\\t"
81428aa3b9Schristos	    gsub(/\"/, "\\\"");
82428aa3b9Schristos	    printf "    \"%s\",\n", $0;
83428aa3b9Schristos	} else {
84428aa3b9Schristos	    print $0;
85428aa3b9Schristos	}
86428aa3b9Schristos      }
87428aa3b9SchristosEND   { if (_abort_exit)
88428aa3b9Schristos	    exit 2;
89428aa3b9Schristos	if (havesection) {
90428aa3b9Schristos	    print "    0\n};\n";
91428aa3b9Schristos	}
92428aa3b9Schristos	if (nsec > 0) {
93428aa3b9Schristos	    print "void";
94428aa3b9Schristos	    print "write_section(FILE * fp, const char *const section[])";
95428aa3b9Schristos	    print "{";
96428aa3b9Schristos	    print "    int i;";
97428aa3b9Schristos	    print "    const char *s;\n";
98428aa3b9Schristos	    print "    for (i = 0; (s = section[i]) != 0; ++i)";
99428aa3b9Schristos	    print "    {";
100428aa3b9Schristos	    print "\tif (fp == code_file)";
101428aa3b9Schristos	    print "\t    ++outline;";
102428aa3b9Schristos	    print "\tfprintf(fp, \"%s\\n\", s);";
103428aa3b9Schristos	    print "    }";
104428aa3b9Schristos	    print "}";
105428aa3b9Schristos	} else {
106428aa3b9Schristos	    print "skel2c: no sections defined in skeleton file" > "/dev/stderr";
107428aa3b9Schristos	    exit 2;
108428aa3b9Schristos	}
109428aa3b9Schristos	if (ifdef != "") {
110428aa3b9Schristos	    printf "skel2c: unmatched %%ifdef %s at end of skeleton file\n", $ifdef > "/dev/stderr";
111428aa3b9Schristos	    exit 2;
112428aa3b9Schristos	}
113428aa3b9Schristos      }
114