1*0a6a1f1dSLionel Sambuc# vile: awkmode 2*0a6a1f1dSLionel Sambucfunction noident(given) { 3*0a6a1f1dSLionel Sambuc gsub(/\$/,"@", given); 4*0a6a1f1dSLionel Sambuc return given; 5*0a6a1f1dSLionel Sambuc} 6*0a6a1f1dSLionel SambucBEGIN { havesection = 0; 7*0a6a1f1dSLionel Sambuc version = "Id: skel2c,v 1.3 2014/04/06 19:48:04 tom Exp "; 8*0a6a1f1dSLionel Sambuc nsec = 0; 9*0a6a1f1dSLionel Sambuc ifdef = ""; 10*0a6a1f1dSLionel Sambuc printf "/* This file generated automatically using\n * %s\n */\n\n", 11*0a6a1f1dSLionel Sambuc noident(version); 12*0a6a1f1dSLionel Sambuc } 13*0a6a1f1dSLionel Sambuc/[$]Id[:][^$]*[$]/ { 14*0a6a1f1dSLionel Sambuc printf "%s\n", noident($0); 15*0a6a1f1dSLionel Sambuc next; 16*0a6a1f1dSLionel Sambuc } 17*0a6a1f1dSLionel Sambuc/^%% *insert *VERSION *here/ { 18*0a6a1f1dSLionel Sambuc printf " CONCAT1(\"#define YYMAJOR \", YYMAJOR),\n"; 19*0a6a1f1dSLionel Sambuc printf " CONCAT1(\"#define YYMINOR \", YYMINOR),\n"; 20*0a6a1f1dSLionel Sambuc printf "#ifdef YYPATCH\n"; 21*0a6a1f1dSLionel Sambuc printf " CONCAT1(\"#define YYPATCH \", YYPATCH),\n"; 22*0a6a1f1dSLionel Sambuc printf "#endif\n"; 23*0a6a1f1dSLionel Sambuc next; 24*0a6a1f1dSLionel Sambuc } 25*0a6a1f1dSLionel Sambuc/^%%ifdef/ { 26*0a6a1f1dSLionel Sambuc if (NF >= 2) { 27*0a6a1f1dSLionel Sambuc printf "#if defined(%s)\n", $2; 28*0a6a1f1dSLionel Sambuc printf " \"#if %s\",\n", $2; 29*0a6a1f1dSLionel Sambuc } else { 30*0a6a1f1dSLionel Sambuc _abort_exit = 1; 31*0a6a1f1dSLionel Sambuc printf "skel2c: ill-formed %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr"; 32*0a6a1f1dSLionel Sambuc exit 2; 33*0a6a1f1dSLionel Sambuc } 34*0a6a1f1dSLionel Sambuc if (ifdef != "") { 35*0a6a1f1dSLionel Sambuc printf "skel2c: nested %%ifdef in skeleton file on line %d\n", FNR > "/dev/stderr"; 36*0a6a1f1dSLionel Sambuc exit 2; 37*0a6a1f1dSLionel Sambuc } 38*0a6a1f1dSLionel Sambuc ifdef = $2; 39*0a6a1f1dSLionel Sambuc next; 40*0a6a1f1dSLionel Sambuc } 41*0a6a1f1dSLionel Sambuc/^%%endif/ { 42*0a6a1f1dSLionel Sambuc if (ifdef != "") { 43*0a6a1f1dSLionel Sambuc if (NF >= 2) { 44*0a6a1f1dSLionel Sambuc printf " \"#endif /* %s */\",\n", $2; 45*0a6a1f1dSLionel Sambuc printf "#endif /* defined(%s) */\n", $2; 46*0a6a1f1dSLionel Sambuc } else { 47*0a6a1f1dSLionel Sambuc printf " \"#endif /* %s */\",\n", ifdef; 48*0a6a1f1dSLionel Sambuc printf "#endif /* defined(%s) */\n", ifdef; 49*0a6a1f1dSLionel Sambuc } 50*0a6a1f1dSLionel Sambuc ifdef = ""; 51*0a6a1f1dSLionel Sambuc } else { 52*0a6a1f1dSLionel Sambuc printf " \"#endif\",\n"; 53*0a6a1f1dSLionel Sambuc printf "#endif\n"; 54*0a6a1f1dSLionel Sambuc printf "skel2c: unmatched %endif in skeleton file on line %d\n", FNR > "/dev/stderr"; 55*0a6a1f1dSLionel Sambuc exit 2; 56*0a6a1f1dSLionel Sambuc } 57*0a6a1f1dSLionel Sambuc next; 58*0a6a1f1dSLionel Sambuc } 59*0a6a1f1dSLionel Sambuc/^%%/ { if (havesection) { 60*0a6a1f1dSLionel Sambuc printf " 0\n};\n\n"; 61*0a6a1f1dSLionel Sambuc } 62*0a6a1f1dSLionel Sambuc if (NF >= 2) { 63*0a6a1f1dSLionel Sambuc havesection = 1; 64*0a6a1f1dSLionel Sambuc section = $2; 65*0a6a1f1dSLionel Sambuc seclist[nsec] = section; 66*0a6a1f1dSLionel Sambuc nsec = nsec + 1; 67*0a6a1f1dSLionel Sambuc printf "const char *const %s[] =\n{\n", $2; 68*0a6a1f1dSLionel Sambuc } else { 69*0a6a1f1dSLionel Sambuc havesection = 0; 70*0a6a1f1dSLionel Sambuc } 71*0a6a1f1dSLionel Sambuc next; 72*0a6a1f1dSLionel Sambuc } 73*0a6a1f1dSLionel Sambuc { if (havesection) { 74*0a6a1f1dSLionel Sambuc # Could use 'gsub(/\\/, "\\\\")' instead of the following 75*0a6a1f1dSLionel Sambuc # two lines, but there's a bug in mawk and the original 76*0a6a1f1dSLionel Sambuc # awk (not in gawk) which is triggered by that. 77*0a6a1f1dSLionel Sambuc gsub(/\\/, "\\\1"); 78*0a6a1f1dSLionel Sambuc gsub(/\1/, "\\"); 79*0a6a1f1dSLionel Sambuc# gsub(/\t/, "\\t"); # change '\t' to "\\t" 80*0a6a1f1dSLionel Sambuc gsub(/\"/, "\\\""); 81*0a6a1f1dSLionel Sambuc printf " \"%s\",\n", $0; 82*0a6a1f1dSLionel Sambuc } else { 83*0a6a1f1dSLionel Sambuc print $0; 84*0a6a1f1dSLionel Sambuc } 85*0a6a1f1dSLionel Sambuc } 86*0a6a1f1dSLionel SambucEND { if (_abort_exit) 87*0a6a1f1dSLionel Sambuc exit 2; 88*0a6a1f1dSLionel Sambuc if (havesection) { 89*0a6a1f1dSLionel Sambuc print " 0\n};\n"; 90*0a6a1f1dSLionel Sambuc } 91*0a6a1f1dSLionel Sambuc if (nsec > 0) { 92*0a6a1f1dSLionel Sambuc print "void"; 93*0a6a1f1dSLionel Sambuc print "write_section(FILE * fp, const char *const section[])"; 94*0a6a1f1dSLionel Sambuc print "{"; 95*0a6a1f1dSLionel Sambuc print " int i;"; 96*0a6a1f1dSLionel Sambuc print " const char *s;\n"; 97*0a6a1f1dSLionel Sambuc print " for (i = 0; (s = section[i]) != 0; ++i)"; 98*0a6a1f1dSLionel Sambuc print " {"; 99*0a6a1f1dSLionel Sambuc print "\tif (fp == code_file)"; 100*0a6a1f1dSLionel Sambuc print "\t ++outline;"; 101*0a6a1f1dSLionel Sambuc print "\tfprintf(fp, \"%s\\n\", s);"; 102*0a6a1f1dSLionel Sambuc print " }"; 103*0a6a1f1dSLionel Sambuc print "}"; 104*0a6a1f1dSLionel Sambuc } else { 105*0a6a1f1dSLionel Sambuc print "skel2c: no sections defined in skeleton file" > "/dev/stderr"; 106*0a6a1f1dSLionel Sambuc exit 2; 107*0a6a1f1dSLionel Sambuc } 108*0a6a1f1dSLionel Sambuc if (ifdef != "") { 109*0a6a1f1dSLionel Sambuc printf "skel2c: unmatched %%ifdef %s at end of skeleton file\n", $ifdef > "/dev/stderr"; 110*0a6a1f1dSLionel Sambuc exit 2; 111*0a6a1f1dSLionel Sambuc } 112*0a6a1f1dSLionel Sambuc } 113